Skip to content

Commit

Permalink
fix: refactor names (#51)
Browse files Browse the repository at this point in the history
Signed-off-by: Eli Hatamov <[email protected]>
Signed-off-by: Gosha <[email protected]>
  • Loading branch information
EliRookout authored and gosharo committed Jun 22, 2023
1 parent 3239a15 commit d13c9d9
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 119 deletions.
26 changes: 13 additions & 13 deletions cmd/piper/piper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (

"github.com/rookout/piper/pkg/clients"
"github.com/rookout/piper/pkg/conf"
"github.com/rookout/piper/pkg/git"
"github.com/rookout/piper/pkg/git_provider"
"github.com/rookout/piper/pkg/server"
"github.com/rookout/piper/pkg/utils"
workflowHandler "github.com/rookout/piper/pkg/workflow-handler"
workflowHandler "github.com/rookout/piper/pkg/workflow_handler"

rookout "github.com/Rookout/GoSDK"
)

func main() {
cfg, err := conf.LoadConfig()
if err != nil {
log.Fatalf("failed to load the configuration for Piper, error: %v", err)
log.Panicf("failed to load the configuration for Piper, error: %v", err)
}

if cfg.RookoutConfig.Token != "" {
Expand All @@ -27,26 +27,26 @@ func main() {
}
}

err = cfg.WorkflowConfig.WorkflowsSpecLoad("/piper-config/..data")
err = cfg.WorkflowsConfig.WorkflowsSpecLoad("/piper-config/..data")
if err != nil {
log.Fatalf("Failed to load workflow spec configuration, error: %v", err)
log.Panicf("Failed to load workflow spec configuration, error: %v", err)
}

git, err := git.NewGitProviderClient(cfg)
gitProvider, err := git_provider.NewGitProviderClient(cfg)
if err != nil {
log.Fatalf("failed to load the Git client for Piper, error: %v", err)
log.Panicf("failed to load the Git client for Piper, error: %v", err)
}
workflows, err := workflowHandler.NewWorkflowsClient(cfg)
if err != nil {
log.Fatalf("failed to load the Argo Workflows client for Piper, error: %v", err)
log.Panicf("failed to load the Argo Workflows client for Piper, error: %v", err)
}

clients := &clients.Clients{
Git: git,
Workflows: workflows,
globalClients := &clients.Clients{
GitProvider: gitProvider,
Workflows: workflows,
}

err = clients.Git.SetWebhook()
err = globalClients.GitProvider.SetWebhook()
if err != nil {
panic(err)
}
Expand All @@ -56,5 +56,5 @@ func main() {
// panic(err)
//}

server.Start(cfg, clients)
server.Start(cfg, globalClients)
}
8 changes: 4 additions & 4 deletions pkg/clients/types.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package clients

import (
"github.com/rookout/piper/pkg/git"
workflowHandler "github.com/rookout/piper/pkg/workflow-handler"
"github.com/rookout/piper/pkg/git_provider"
"github.com/rookout/piper/pkg/workflow_handler"
)

type Clients struct {
Git git.Client
Workflows workflowHandler.WorkflowsClient
GitProvider git_provider.Client
Workflows workflow_handler.WorkflowsClient
}
12 changes: 6 additions & 6 deletions pkg/common/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package common

import (
"github.com/rookout/piper/pkg/git"
"github.com/rookout/piper/pkg/git_provider"
)

type WorkflowsBatch struct {
OnStart []*git.CommitFile
OnExit []*git.CommitFile
Templates []*git.CommitFile
Parameters *git.CommitFile
OnStart []*git_provider.CommitFile
OnExit []*git_provider.CommitFile
Templates []*git_provider.CommitFile
Parameters *git_provider.CommitFile
Config *string
Payload *git.WebhookPayload
Payload *git_provider.WebhookPayload
}
14 changes: 7 additions & 7 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/kelseyhightower/envconfig"
)

type Config struct {
GitConfig
ArgoConfig
type GlobalConfig struct {
GitProviderConfig
WorkflowServerConfig
RookoutConfig
WorkflowConfig
WorkflowsConfig
}

func (cfg *Config) Load() error {
func (cfg *GlobalConfig) Load() error {
err := envconfig.Process("", cfg)
if err != nil {
return fmt.Errorf("failed to load the configuration, error: %v", err)
Expand All @@ -21,8 +21,8 @@ func (cfg *Config) Load() error {
return nil
}

func LoadConfig() (*Config, error) {
cfg := new(Config)
func LoadConfig() (*GlobalConfig, error) {
cfg := new(GlobalConfig)

err := cfg.Load()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/conf/git.go → pkg/conf/git_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/kelseyhightower/envconfig"
)

type GitConfig struct {
type GitProviderConfig struct {
Provider string `envconfig:"GIT_PROVIDER" required:"true"`
Token string `envconfig:"GIT_TOKEN" required:"true"`
OrgName string `envconfig:"GIT_ORG_NAME" required:"true"`
Expand All @@ -16,7 +16,7 @@ type GitConfig struct {
WebhookSecret string `envconfig:"GIT_WEBHOOK_SECRET" required:"false"`
}

func (cfg *GitConfig) GitConfLoad() error {
func (cfg *GitProviderConfig) GitConfLoad() error {
err := envconfig.Process("", cfg)
if err != nil {
return fmt.Errorf("failed to load the Git provider configuration, error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/conf/argo.go → pkg/conf/workflow_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"github.com/kelseyhightower/envconfig"
)

type ArgoConfig struct {
type WorkflowServerConfig struct {
ArgoToken string `envconfig:"ARGO_WORKFLOWS_TOKEN" required:"true"`
ArgoAddress string `envconfig:"ARGO_WORKFLOWS_ADDRESS" required:"true"`
CreateCRD bool `envconfig:"ARGO_WORKFLOWS_CREATE_CRD" default:"true"`
Namespace string `envconfig:"ARGO_WORKFLOWS_NAMESPACE" default:"default"`
KubeConfig string `envconfig:"KUBE_CONFIG" default:""`
}

func (cfg *ArgoConfig) ArgoConfLoad() error {
func (cfg *WorkflowServerConfig) ArgoConfLoad() error {
err := envconfig.Process("", cfg)
if err != nil {
return fmt.Errorf("failed to load the Argo configuration, error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/conf/workflows_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/rookout/piper/pkg/utils"
)

type WorkflowConfig struct {
type WorkflowsConfig struct {
Configs map[string]*ConfigInstance
}

Expand All @@ -16,7 +16,7 @@ type ConfigInstance struct {
OnExit []v1alpha1.DAGTask `yaml:"onExit"`
}

func (wfc *WorkflowConfig) WorkflowsSpecLoad(configPath string) error {
func (wfc *WorkflowsConfig) WorkflowsSpecLoad(configPath string) error {
var jsonBytes []byte
wfc.Configs = make(map[string]*ConfigInstance)

Expand Down
38 changes: 19 additions & 19 deletions pkg/git/github.go → pkg/git_provider/github.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git
package git_provider

import (
"context"
Expand All @@ -14,14 +14,14 @@ import (

type GithubClientImpl struct {
client *github.Client
cfg *conf.Config
cfg *conf.GlobalConfig
hooks []*github.Hook
}

func NewGithubClient(cfg *conf.Config) (Client, error) {
func NewGithubClient(cfg *conf.GlobalConfig) (Client, error) {
ctx := context.Background()

client := github.NewTokenClient(ctx, cfg.GitConfig.Token)
client := github.NewTokenClient(ctx, cfg.GitProviderConfig.Token)
err := ValidatePermissions(ctx, client, cfg)
if err != nil {
return nil, fmt.Errorf("failed to validate permissions: %v", err)
Expand All @@ -38,7 +38,7 @@ func (c *GithubClientImpl) ListFiles(ctx *context.Context, repo string, branch s
var files []string

opt := &github.RepositoryContentGetOptions{Ref: branch}
_, directoryContent, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitConfig.OrgName, repo, path, opt)
_, directoryContent, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
if err != nil {
return nil, err
}
Expand All @@ -58,7 +58,7 @@ func (c *GithubClientImpl) GetFile(ctx *context.Context, repo string, branch str
var commitFile CommitFile

opt := &github.RepositoryContentGetOptions{Ref: branch}
fileContent, _, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitConfig.OrgName, repo, path, opt)
fileContent, _, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
if err != nil {
return &commitFile, err
}
Expand Down Expand Up @@ -104,20 +104,20 @@ func (c *GithubClientImpl) SetWebhook() error {
ctx := context.Background()
hook := &github.Hook{
Config: map[string]interface{}{
"url": c.cfg.GitConfig.WebhookURL,
"url": c.cfg.GitProviderConfig.WebhookURL,
"content_type": "json",
"secret": c.cfg.GitConfig.WebhookSecret, // TODO webhook from k8s secret
"secret": c.cfg.GitProviderConfig.WebhookSecret, // TODO webhook from k8s secret

},
Events: []string{"push", "pull_request"},
Active: github.Bool(true),
}
if c.cfg.GitConfig.OrgLevelWebhook {
if c.cfg.GitProviderConfig.OrgLevelWebhook {
respHook, ok := isOrgWebhookEnabled(ctx, c)
if !ok {
retHook, resp, err := c.client.Organizations.CreateHook(
ctx,
c.cfg.GitConfig.OrgName,
c.cfg.GitProviderConfig.OrgName,
hook,
)
if err != nil {
Expand All @@ -130,7 +130,7 @@ func (c *GithubClientImpl) SetWebhook() error {
} else {
updatedHook, resp, err := c.client.Organizations.EditHook(
ctx,
c.cfg.GitConfig.OrgName,
c.cfg.GitProviderConfig.OrgName,
respHook.GetID(),
hook,
)
Expand All @@ -140,7 +140,7 @@ func (c *GithubClientImpl) SetWebhook() error {
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(
"failed to update org level webhhok for %s, API returned %d",
c.cfg.GitConfig.OrgName,
c.cfg.GitProviderConfig.OrgName,
resp.StatusCode,
)
}
Expand All @@ -149,10 +149,10 @@ func (c *GithubClientImpl) SetWebhook() error {

return nil
} else {
for _, repo := range strings.Split(c.cfg.GitConfig.RepoList, ",") {
for _, repo := range strings.Split(c.cfg.GitProviderConfig.RepoList, ",") {
respHook, ok := isRepoWebhookEnabled(ctx, c, repo)
if !ok {
_, resp, err := c.client.Repositories.CreateHook(ctx, c.cfg.GitConfig.OrgName, repo, hook)
_, resp, err := c.client.Repositories.CreateHook(ctx, c.cfg.GitProviderConfig.OrgName, repo, hook)
if err != nil {
return err
}
Expand All @@ -162,7 +162,7 @@ func (c *GithubClientImpl) SetWebhook() error {
}
c.hooks = append(c.hooks, hook)
} else {
updatedHook, resp, err := c.client.Repositories.EditHook(ctx, c.cfg.GitConfig.OrgName, repo, respHook.GetID(), hook)
updatedHook, resp, err := c.client.Repositories.EditHook(ctx, c.cfg.GitProviderConfig.OrgName, repo, respHook.GetID(), hook)
if err != nil {
return err
}
Expand All @@ -182,9 +182,9 @@ func (c *GithubClientImpl) UnsetWebhook() error {
ctx := context.Background()

for _, hook := range c.hooks {
if c.cfg.GitConfig.OrgLevelWebhook {
if c.cfg.GitProviderConfig.OrgLevelWebhook {

resp, err := c.client.Organizations.DeleteHook(ctx, c.cfg.GitConfig.OrgName, *hook.ID)
resp, err := c.client.Organizations.DeleteHook(ctx, c.cfg.GitProviderConfig.OrgName, *hook.ID)

if err != nil {
return err
Expand All @@ -195,8 +195,8 @@ func (c *GithubClientImpl) UnsetWebhook() error {
}

} else {
for _, repo := range strings.Split(c.cfg.GitConfig.RepoList, ",") {
resp, err := c.client.Repositories.DeleteHook(ctx, c.cfg.GitConfig.OrgName, repo, *hook.ID)
for _, repo := range strings.Split(c.cfg.GitProviderConfig.RepoList, ",") {
resp, err := c.client.Repositories.DeleteHook(ctx, c.cfg.GitProviderConfig.OrgName, repo, *hook.ID)

if err != nil {
return fmt.Errorf("failed to delete repo level webhhok for %s, API call returned %d. %s", repo, resp.StatusCode, err)
Expand Down
11 changes: 6 additions & 5 deletions pkg/git/github_test.go → pkg/git_provider/github_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package git
package git_provider

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/google/go-github/v52/github"
"github.com/rookout/piper/pkg/conf"
assertion "github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestListFiles(t *testing.T) {
Expand All @@ -35,8 +36,8 @@ func TestListFiles(t *testing.T) {

c := GithubClientImpl{
client: client,
cfg: &conf.Config{
GitConfig: conf.GitConfig{
cfg: &conf.GlobalConfig{
GitProviderConfig: conf.GitProviderConfig{
OrgLevelWebhook: false,
OrgName: "test",
RepoList: "test-repo1",
Expand Down
Loading

0 comments on commit d13c9d9

Please sign in to comment.