diff --git a/internal/pkg/githubactions/githubactions.go b/internal/pkg/githubactions/githubactions.go index 8d8fe2341..f6ca5abc7 100644 --- a/internal/pkg/githubactions/githubactions.go +++ b/internal/pkg/githubactions/githubactions.go @@ -12,20 +12,6 @@ import ( "golang.org/x/oauth2" ) -// Options is the struct for configurations of the githubactions plugin. -type Options struct { - Owner string - Repo string - Language *Language - Branch string -} - -// Language is the struct containing details of a programming language specified in the GitHub Actions workflow. -type Language struct { - Name string - Version string -} - type GithubActions struct { ctx context.Context client *github.Client @@ -53,13 +39,17 @@ func NewGithubActions(options *map[string]interface{}) (*GithubActions, error) { }, nil } -func (ga *GithubActions) AddWorkflow(workflow Workflow) error { +func (ga *GithubActions) GetLanguage() *Language { + return ga.options.Language +} + +func (ga *GithubActions) AddWorkflow(workflow *Workflow) error { exists, err := ga.fileExists(workflow.workflowFileName) if err != nil { return err } if exists { - log.Printf("github actions workflow %s already exists\n", workflow.workflowFileName) + log.Printf("github actions Workflow %s already exists\n", workflow.workflowFileName) return nil } @@ -71,7 +61,7 @@ func (ga *GithubActions) AddWorkflow(workflow Workflow) error { Branch: github.String("master"), } - log.Printf("creating github actions workflow %s...\n", workflow.workflowFileName) + log.Printf("creating github actions Workflow %s...\n", workflow.workflowFileName) _, _, err = ga.client.Repositories.CreateFile( ga.ctx, ga.options.Owner, @@ -83,17 +73,17 @@ func (ga *GithubActions) AddWorkflow(workflow Workflow) error { log.Println(err) return err } - log.Printf("github actions workflow %s created\n", workflow.workflowFileName) + log.Printf("github actions Workflow %s created\n", workflow.workflowFileName) return nil } -func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error { +func (ga *GithubActions) DeleteWorkflow(workflow *Workflow) error { exists, err := ga.fileExists(workflow.workflowFileName) if err != nil { return err } if !exists { - log.Printf("github actions workflow %s already removed\n", workflow.workflowFileName) + log.Printf("github actions Workflow %s already removed\n", workflow.workflowFileName) return nil } @@ -105,7 +95,7 @@ func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error { Branch: github.String("master"), } - log.Printf("deleting github actions workflow %s...\n", workflow.workflowFileName) + log.Printf("deleting github actions Workflow %s...\n", workflow.workflowFileName) _, _, err = ga.client.Repositories.DeleteFile( ga.ctx, ga.options.Owner, @@ -117,14 +107,10 @@ func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error { log.Println(err) return err } - log.Printf("github actions workflow %s removed\n", workflow.workflowFileName) + log.Printf("github actions Workflow %s removed\n", workflow.workflowFileName) return nil } -func generateGitHubWorkflowFileByName(f string) string { - return fmt.Sprintf(".github/workflows/%s", f) -} - func (ga *GithubActions) fileExists(filename string) (bool, error) { _, _, resp, err := ga.client.Repositories.GetContents( ga.ctx, @@ -148,6 +134,10 @@ func (ga *GithubActions) fileExists(filename string) (bool, error) { return false, fmt.Errorf("got some error is not expected") } +func generateGitHubWorkflowFileByName(f string) string { + return fmt.Sprintf(".github/workflows/%s", f) +} + func getGitHubToken() string { err := viper.BindEnv("github_token") if err != nil { diff --git a/internal/pkg/githubactions/templates.go b/internal/pkg/githubactions/golang/templates.go similarity index 96% rename from internal/pkg/githubactions/templates.go rename to internal/pkg/githubactions/golang/templates.go index 02d64ed1b..973e134e7 100644 --- a/internal/pkg/githubactions/templates.go +++ b/internal/pkg/githubactions/golang/templates.go @@ -1,6 +1,6 @@ -package githubactions +package golang -var prBuilder = ` +var PrBuilder = ` name: PR Builder on: pull_request: @@ -20,7 +20,7 @@ jobs: run: go build -v ./... ` -var masterBuilder = ` +var MasterBuilder = ` name: Master Builder on: push: diff --git a/internal/pkg/githubactions/install.go b/internal/pkg/githubactions/install.go index b4e4add41..6679566a0 100644 --- a/internal/pkg/githubactions/install.go +++ b/internal/pkg/githubactions/install.go @@ -1,10 +1,5 @@ package githubactions -var workflows = []Workflow{ - {"pr builder by DevStream", "pr-builder.yml", prBuilder}, - {"master builder by DevStream", "master-builder.yml", masterBuilder}, -} - // Install sets up GitHub Actions workflows. func Install(options *map[string]interface{}) (bool, error) { githubActions, err := NewGithubActions(options) @@ -12,7 +7,10 @@ func Install(options *map[string]interface{}) (bool, error) { return false, err } - for _, pipeline := range workflows { + language := githubActions.GetLanguage() + ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String()) + + for _, pipeline := range ws { err := githubActions.AddWorkflow(pipeline) if err != nil { return false, err diff --git a/internal/pkg/githubactions/nodejs/templates.go b/internal/pkg/githubactions/nodejs/templates.go new file mode 100644 index 000000000..944b70330 --- /dev/null +++ b/internal/pkg/githubactions/nodejs/templates.go @@ -0,0 +1,5 @@ +package nodejs + +var PrBuilder = `` + +var MasterBuilder = `` diff --git a/internal/pkg/githubactions/param.go b/internal/pkg/githubactions/param.go deleted file mode 100644 index f313a9061..000000000 --- a/internal/pkg/githubactions/param.go +++ /dev/null @@ -1,8 +0,0 @@ -package githubactions - -// Workflow is the struct for a GitHub Actions workflow. -type Workflow struct { - commitMessage string - workflowFileName string - workflowContent string -} diff --git a/internal/pkg/githubactions/python/templates.go b/internal/pkg/githubactions/python/templates.go new file mode 100644 index 000000000..69b954ea6 --- /dev/null +++ b/internal/pkg/githubactions/python/templates.go @@ -0,0 +1,5 @@ +package python + +var PrBuilder = `` + +var MasterBuilder = `` diff --git a/internal/pkg/githubactions/reinstall.go b/internal/pkg/githubactions/reinstall.go index fc721b0fb..d1157d619 100644 --- a/internal/pkg/githubactions/reinstall.go +++ b/internal/pkg/githubactions/reinstall.go @@ -7,7 +7,10 @@ func Reinstall(options *map[string]interface{}) (bool, error) { return false, err } - for _, pipeline := range workflows { + language := githubActions.GetLanguage() + ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String()) + + for _, pipeline := range ws { err := githubActions.DeleteWorkflow(pipeline) if err != nil { return false, err diff --git a/internal/pkg/githubactions/uninstall.go b/internal/pkg/githubactions/uninstall.go index 2853d212a..630470310 100644 --- a/internal/pkg/githubactions/uninstall.go +++ b/internal/pkg/githubactions/uninstall.go @@ -7,7 +7,10 @@ func Uninstall(options *map[string]interface{}) (bool, error) { return false, err } - for _, pipeline := range workflows { + language := githubActions.GetLanguage() + ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String()) + + for _, pipeline := range ws { err := githubActions.DeleteWorkflow(pipeline) if err != nil { return false, err diff --git a/internal/pkg/githubactions/workflow.go b/internal/pkg/githubactions/workflow.go new file mode 100644 index 000000000..fd0085239 --- /dev/null +++ b/internal/pkg/githubactions/workflow.go @@ -0,0 +1,82 @@ +package githubactions + +import ( + "fmt" + + "github.com/merico-dev/stream/internal/pkg/githubactions/golang" + "github.com/merico-dev/stream/internal/pkg/githubactions/nodejs" + "github.com/merico-dev/stream/internal/pkg/githubactions/python" +) + +const ( + defaultCommitMessage = "builder by DevStream" + BuilderYmlPr = "pr-builder.yml" + BuilderYmlMaster = "master-builder.yml" +) + +var go117 = &Language{ + Name: "go", + Version: "1.17", +} + +var python3 = &Language{ + Name: "python", + Version: "3", +} + +var nodejs9 = &Language{ + Name: "nodejs", + Version: "9", +} + +var defaultWorkflows = workflows{ + go117.String(): { + {defaultCommitMessage, BuilderYmlPr, golang.PrBuilder}, + {defaultCommitMessage, BuilderYmlMaster, golang.MasterBuilder}, + }, + python3.String(): { + {defaultCommitMessage, BuilderYmlPr, python.PrBuilder}, + {defaultCommitMessage, BuilderYmlMaster, python.MasterBuilder}, + }, + nodejs9.String(): { + {defaultCommitMessage, BuilderYmlPr, nodejs.PrBuilder}, + {defaultCommitMessage, BuilderYmlMaster, nodejs.MasterBuilder}, + }, +} + +// Options is the struct for configurations of the githubactions plugin. +type Options struct { + Owner string + Repo string + Branch string + Language *Language +} + +// Language is the struct containing details of a programming language specified in the GitHub Actions Workflow. +type Language struct { + Name string + Version string +} + +// Workflow is the struct for a GitHub Actions Workflow. +type Workflow struct { + commitMessage string + workflowFileName string + workflowContent string +} + +type LanguageString string + +type workflows map[LanguageString][]*Workflow + +func (l *Language) String() LanguageString { + return LanguageString(fmt.Sprintf("%s-%s", l.Name, l.Version)) +} + +func (ws *workflows) GetWorkflowByNameVersionTypeString(nvtStr LanguageString) []*Workflow { + workflowList, exist := (*ws)[nvtStr] + if exist { + return workflowList + } + return nil +}