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

feat: adding multi-language entrances support #68

Merged
merged 1 commit into from
Dec 24, 2021
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
42 changes: 16 additions & 26 deletions internal/pkg/githubactions/githubactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package githubactions
package golang

var prBuilder = `
var PrBuilder = `
name: PR Builder
on:
pull_request:
Expand All @@ -20,7 +20,7 @@ jobs:
run: go build -v ./...
`

var masterBuilder = `
var MasterBuilder = `
name: Master Builder
on:
push:
Expand Down
10 changes: 4 additions & 6 deletions internal/pkg/githubactions/install.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
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)
if err != nil {
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
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/githubactions/nodejs/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nodejs

var PrBuilder = ``

var MasterBuilder = ``
8 changes: 0 additions & 8 deletions internal/pkg/githubactions/param.go

This file was deleted.

5 changes: 5 additions & 0 deletions internal/pkg/githubactions/python/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package python

var PrBuilder = ``

var MasterBuilder = ``
5 changes: 4 additions & 1 deletion internal/pkg/githubactions/reinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/githubactions/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions internal/pkg/githubactions/workflow.go
Original file line number Diff line number Diff line change
@@ -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
}