Skip to content

Commit d1bce2c

Browse files
authored
Merge pull request #69 from merico-dev/feat-actions
feat: add support for Python and Nodejs with GitHub Actions plugin
2 parents 8d31461 + 3fd5e2d commit d1bce2c

File tree

10 files changed

+224
-46
lines changed

10 files changed

+224
-46
lines changed

examples/config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ tools:
99
name: go
1010
version: "1.17"
1111
branch: master
12+
##for python config
13+
#- name: githubactions
14+
# version: 0.0.1
15+
# options:
16+
# owner: lfbdev
17+
# repo: spotipy
18+
# language:
19+
# name: python
20+
# version: "3"
21+
# branch: master
22+
##for nodejs config
23+
#- name: githubactions
24+
# version: 0.0.1
25+
# options:
26+
# owner: lfbdev
27+
# repo: lowdb
28+
# language:
29+
# name: nodejs
30+
# version: "9"
31+
# branch: main
1232
- name: argocd
1333
version: 0.0.1
1434
options:

internal/pkg/githubactions/githubactions.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@ import (
1212
"golang.org/x/oauth2"
1313
)
1414

15-
// Options is the struct for configurations of the githubactions plugin.
16-
type Options struct {
17-
Owner string
18-
Repo string
19-
Language *Language
20-
Branch string
21-
}
22-
23-
// Language is the struct containing details of a programming language specified in the GitHub Actions workflow.
24-
type Language struct {
25-
Name string
26-
Version string
27-
}
28-
2915
type GithubActions struct {
3016
ctx context.Context
3117
client *github.Client
@@ -53,13 +39,17 @@ func NewGithubActions(options *map[string]interface{}) (*GithubActions, error) {
5339
}, nil
5440
}
5541

56-
func (ga *GithubActions) AddWorkflow(workflow Workflow) error {
42+
func (ga *GithubActions) GetLanguage() *Language {
43+
return ga.options.Language
44+
}
45+
46+
func (ga *GithubActions) AddWorkflow(workflow *Workflow) error {
5747
exists, err := ga.fileExists(workflow.workflowFileName)
5848
if err != nil {
5949
return err
6050
}
6151
if exists {
62-
log.Printf("github actions workflow %s already exists\n", workflow.workflowFileName)
52+
log.Printf("github actions Workflow %s already exists\n", workflow.workflowFileName)
6353
return nil
6454
}
6555

@@ -68,10 +58,10 @@ func (ga *GithubActions) AddWorkflow(workflow Workflow) error {
6858
opts := &github.RepositoryContentFileOptions{
6959
Message: github.String(workflow.commitMessage),
7060
Content: []byte(workflow.workflowContent),
71-
Branch: github.String("master"),
61+
Branch: github.String(ga.options.Branch),
7262
}
7363

74-
log.Printf("creating github actions workflow %s...\n", workflow.workflowFileName)
64+
log.Printf("creating github actions Workflow %s...\n", workflow.workflowFileName)
7565
_, _, err = ga.client.Repositories.CreateFile(
7666
ga.ctx,
7767
ga.options.Owner,
@@ -83,17 +73,17 @@ func (ga *GithubActions) AddWorkflow(workflow Workflow) error {
8373
log.Println(err)
8474
return err
8575
}
86-
log.Printf("github actions workflow %s created\n", workflow.workflowFileName)
76+
log.Printf("github actions Workflow %s created\n", workflow.workflowFileName)
8777
return nil
8878
}
8979

90-
func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error {
80+
func (ga *GithubActions) DeleteWorkflow(workflow *Workflow) error {
9181
exists, err := ga.fileExists(workflow.workflowFileName)
9282
if err != nil {
9383
return err
9484
}
9585
if !exists {
96-
log.Printf("github actions workflow %s already removed\n", workflow.workflowFileName)
86+
log.Printf("github actions Workflow %s already removed\n", workflow.workflowFileName)
9787
return nil
9888
}
9989

@@ -102,10 +92,10 @@ func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error {
10292
opts := &github.RepositoryContentFileOptions{
10393
Message: github.String(workflow.commitMessage),
10494
Content: []byte(workflow.workflowContent),
105-
Branch: github.String("master"),
95+
Branch: github.String(ga.options.Branch),
10696
}
10797

108-
log.Printf("deleting github actions workflow %s...\n", workflow.workflowFileName)
98+
log.Printf("deleting github actions Workflow %s...\n", workflow.workflowFileName)
10999
_, _, err = ga.client.Repositories.DeleteFile(
110100
ga.ctx,
111101
ga.options.Owner,
@@ -117,14 +107,10 @@ func (ga *GithubActions) DeleteWorkflow(workflow Workflow) error {
117107
log.Println(err)
118108
return err
119109
}
120-
log.Printf("github actions workflow %s removed\n", workflow.workflowFileName)
110+
log.Printf("github actions Workflow %s removed\n", workflow.workflowFileName)
121111
return nil
122112
}
123113

124-
func generateGitHubWorkflowFileByName(f string) string {
125-
return fmt.Sprintf(".github/workflows/%s", f)
126-
}
127-
128114
func (ga *GithubActions) fileExists(filename string) (bool, error) {
129115
_, _, resp, err := ga.client.Repositories.GetContents(
130116
ga.ctx,
@@ -148,6 +134,10 @@ func (ga *GithubActions) fileExists(filename string) (bool, error) {
148134
return false, fmt.Errorf("got some error is not expected")
149135
}
150136

137+
func generateGitHubWorkflowFileByName(f string) string {
138+
return fmt.Sprintf(".github/workflows/%s", f)
139+
}
140+
151141
func getGitHubToken() string {
152142
err := viper.BindEnv("github_token")
153143
if err != nil {

internal/pkg/githubactions/templates.go renamed to internal/pkg/githubactions/golang/templates.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package githubactions
1+
package golang
22

3-
var prBuilder = `
3+
var PrBuilder = `
44
name: PR Builder
55
on:
66
pull_request:
@@ -20,7 +20,7 @@ jobs:
2020
run: go build -v ./...
2121
`
2222

23-
var masterBuilder = `
23+
var MasterBuilder = `
2424
name: Master Builder
2525
on:
2626
push:

internal/pkg/githubactions/install.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package githubactions
22

3-
var workflows = []Workflow{
4-
{"pr builder by DevStream", "pr-builder.yml", prBuilder},
5-
{"master builder by DevStream", "master-builder.yml", masterBuilder},
6-
}
3+
import "fmt"
74

85
// Install sets up GitHub Actions workflows.
96
func Install(options *map[string]interface{}) (bool, error) {
@@ -12,7 +9,13 @@ func Install(options *map[string]interface{}) (bool, error) {
129
return false, err
1310
}
1411

15-
for _, pipeline := range workflows {
12+
language := githubActions.GetLanguage()
13+
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String())
14+
15+
fmt.Println("lang is ", language.Name, language.Version, language.String())
16+
fmt.Println("ws is ", ws)
17+
18+
for _, pipeline := range ws {
1619
err := githubActions.AddWorkflow(pipeline)
1720
if err != nil {
1821
return false, err
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package nodejs
2+
3+
var PrBuilder = `
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [12.x, 14.x, 16.x]
20+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v2
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
- run: npm ci
29+
- run: npm run build --if-present
30+
- run: npm test
31+
`
32+
33+
var MasterBuilder = ``

internal/pkg/githubactions/param.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package python
2+
3+
var PrBuilder = `
4+
name: "Pull Request Workflow"
5+
on:
6+
pull_request:
7+
types: [ready_for_review]
8+
9+
jobs:
10+
# Enforces the update of a changelog file on every pull request
11+
changelog:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v1
15+
- uses: dangoslen/[email protected]
16+
with:
17+
changeLogPath: 'CHANGELOG.md'
18+
skipLabel: 'skip-changelog'
19+
`
20+
21+
var MasterBuilder = `
22+
name: Tests
23+
24+
on: [push, pull_request]
25+
26+
jobs:
27+
build:
28+
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
python-version: [2.7, 3.5, 3.6, 3.7]
33+
34+
steps:
35+
- uses: actions/checkout@v2
36+
- name: Set up Python ${{ matrix.python-version }}
37+
uses: actions/setup-python@v1
38+
with:
39+
python-version: ${{ matrix.python-version }}
40+
- name: Install dependencies
41+
run: |
42+
python -m pip install --upgrade pip
43+
pip install .[test]
44+
- name: Lint with flake8
45+
run: |
46+
pip install -Iv enum34==1.1.6 # https://bitbucket.org/stoneleaf/enum34/issues/27/enum34-118-broken
47+
pip install flake8
48+
flake8 . --count --show-source --statistics
49+
- name: Run unit tests
50+
run: |
51+
python -m unittest discover -v tests/unit
52+
`

internal/pkg/githubactions/reinstall.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ func Reinstall(options *map[string]interface{}) (bool, error) {
77
return false, err
88
}
99

10-
for _, pipeline := range workflows {
10+
language := githubActions.GetLanguage()
11+
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String())
12+
13+
for _, pipeline := range ws {
1114
err := githubActions.DeleteWorkflow(pipeline)
1215
if err != nil {
1316
return false, err

internal/pkg/githubactions/uninstall.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ func Uninstall(options *map[string]interface{}) (bool, error) {
77
return false, err
88
}
99

10-
for _, pipeline := range workflows {
10+
language := githubActions.GetLanguage()
11+
ws := defaultWorkflows.GetWorkflowByNameVersionTypeString(language.String())
12+
13+
for _, pipeline := range ws {
1114
err := githubActions.DeleteWorkflow(pipeline)
1215
if err != nil {
1316
return false, err
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package githubactions
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/merico-dev/stream/internal/pkg/githubactions/golang"
7+
"github.com/merico-dev/stream/internal/pkg/githubactions/nodejs"
8+
"github.com/merico-dev/stream/internal/pkg/githubactions/python"
9+
)
10+
11+
const (
12+
defaultCommitMessage = "builder by DevStream"
13+
BuilderYmlPr = "pr-builder.yml"
14+
BuilderYmlMaster = "master-builder.yml"
15+
)
16+
17+
var go117 = &Language{
18+
Name: "go",
19+
Version: "1.17",
20+
}
21+
22+
var python3 = &Language{
23+
Name: "python",
24+
Version: "3",
25+
}
26+
27+
var nodejs9 = &Language{
28+
Name: "nodejs",
29+
Version: "9",
30+
}
31+
32+
var defaultWorkflows = workflows{
33+
go117.String(): {
34+
{defaultCommitMessage, BuilderYmlPr, golang.PrBuilder},
35+
{defaultCommitMessage, BuilderYmlMaster, golang.MasterBuilder},
36+
},
37+
python3.String(): {
38+
{defaultCommitMessage, BuilderYmlPr, python.PrBuilder},
39+
{defaultCommitMessage, BuilderYmlMaster, python.MasterBuilder},
40+
},
41+
nodejs9.String(): {
42+
{defaultCommitMessage, BuilderYmlPr, nodejs.PrBuilder},
43+
{defaultCommitMessage, BuilderYmlMaster, nodejs.MasterBuilder},
44+
},
45+
}
46+
47+
// Options is the struct for configurations of the githubactions plugin.
48+
type Options struct {
49+
Owner string
50+
Repo string
51+
Branch string
52+
Language *Language
53+
}
54+
55+
// Language is the struct containing details of a programming language specified in the GitHub Actions Workflow.
56+
type Language struct {
57+
Name string
58+
Version string
59+
}
60+
61+
// Workflow is the struct for a GitHub Actions Workflow.
62+
type Workflow struct {
63+
commitMessage string
64+
workflowFileName string
65+
workflowContent string
66+
}
67+
68+
type LanguageString string
69+
70+
type workflows map[LanguageString][]*Workflow
71+
72+
func (l *Language) String() LanguageString {
73+
return LanguageString(fmt.Sprintf("%s-%s", l.Name, l.Version))
74+
}
75+
76+
func (ws *workflows) GetWorkflowByNameVersionTypeString(nvtStr LanguageString) []*Workflow {
77+
workflowList, exist := (*ws)[nvtStr]
78+
if exist {
79+
return workflowList
80+
}
81+
return nil
82+
}

0 commit comments

Comments
 (0)