Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor names - RK-18987 #51

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions pkg/git/github_utils.go → pkg/git_provider/github_utils.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,7 +14,7 @@ import (

func isOrgWebhookEnabled(ctx context.Context, c *GithubClientImpl) (*github.Hook, bool) {
emptyHook := github.Hook{}
hooks, resp, err := c.client.Organizations.ListHooks(ctx, c.cfg.GitConfig.OrgName, &github.ListOptions{})
hooks, resp, err := c.client.Organizations.ListHooks(ctx, c.cfg.GitProviderConfig.OrgName, &github.ListOptions{})
if err != nil {
return &emptyHook, false
}
Expand All @@ -25,7 +25,7 @@ func isOrgWebhookEnabled(ctx context.Context, c *GithubClientImpl) (*github.Hook
return &emptyHook, false
}
for _, hook := range hooks {
if hook.GetActive() && hook.GetName() == "web" && hook.Config["url"] == c.cfg.GitConfig.WebhookURL {
if hook.GetActive() && hook.GetName() == "web" && hook.Config["url"] == c.cfg.GitProviderConfig.WebhookURL {
return hook, true
}
}
Expand All @@ -34,7 +34,7 @@ func isOrgWebhookEnabled(ctx context.Context, c *GithubClientImpl) (*github.Hook

func isRepoWebhookEnabled(ctx context.Context, c *GithubClientImpl, repo string) (*github.Hook, bool) {
emptyHook := github.Hook{}
hooks, resp, err := c.client.Repositories.ListHooks(ctx, c.cfg.GitConfig.OrgName, repo, &github.ListOptions{})
hooks, resp, err := c.client.Repositories.ListHooks(ctx, c.cfg.GitProviderConfig.OrgName, repo, &github.ListOptions{})
if err != nil {
return &emptyHook, false
}
Expand All @@ -46,7 +46,7 @@ func isRepoWebhookEnabled(ctx context.Context, c *GithubClientImpl, repo string)
}

for _, hook := range hooks {
if hook.GetActive() && hook.GetName() == "web" && hook.Config["url"] == c.cfg.GitConfig.WebhookURL {
if hook.GetActive() && hook.GetName() == "web" && hook.Config["url"] == c.cfg.GitProviderConfig.WebhookURL {
return hook, true
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func GetScopes(ctx context.Context, client *github.Client) ([]string, error) {

}

func ValidatePermissions(ctx context.Context, client *github.Client, cfg *conf.Config) error {
func ValidatePermissions(ctx context.Context, client *github.Client, cfg *conf.GlobalConfig) error {

orgScopes := []string{"admin:org_hook"}
repoAdminScopes := []string{"admin:repo_hook"}
Expand All @@ -92,7 +92,7 @@ func ValidatePermissions(ctx context.Context, client *github.Client, cfg *conf.C
return fmt.Errorf("permissions error: no scopes found for the github client")
}

if cfg.GitConfig.OrgLevelWebhook {
if cfg.GitProviderConfig.OrgLevelWebhook {
if utils.ListContains(orgScopes, scopes) {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git
package git_provider

import (
"context"
Expand Down Expand Up @@ -37,8 +37,8 @@ func TestIsOrgWebhookEnabled(t *testing.T) {

c := GithubClientImpl{
client: client,
cfg: &conf.Config{
GitConfig: conf.GitConfig{
cfg: &conf.GlobalConfig{
GitProviderConfig: conf.GitProviderConfig{
OrgName: "test",
WebhookURL: "https://bla.com",
},
Expand Down
Loading