Skip to content

Commit e07de61

Browse files
authored
remove 'job' item, support default config (#186)
* remove 'job' item, supports default config * remove 'job' item, supports default config * remove 'job' item, supports default config * remove 'job' item, supports default config * remove 'job' item, supports default config * remove 'jobs' struct
1 parent 0f2a93a commit e07de61

File tree

9 files changed

+67
-38
lines changed

9 files changed

+67
-38
lines changed

examples/config.yaml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,14 @@ tools:
2121
name: go
2222
version: "1.17"
2323
branch: main
24-
jobs:
25-
build:
24+
build:
25+
enable: True
26+
test:
27+
enable: True
28+
coverage:
2629
enable: True
27-
command: "go build ./..."
28-
test:
29-
enable: True
30-
command: "go test ./..."
31-
coverage:
32-
enable: True
33-
profile: "-race -covermode=atomic"
34-
output: "coverage.out"
35-
docker:
36-
enable: True
37-
repo: golang-demo
30+
docker:
31+
enable: True
3832
- name: trello-github-integ-golang-demo
3933
plugin:
4034
kind: trello-github-integ

internal/pkg/plugin/githubactions/golang/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Install(options *map[string]interface{}) (bool, error) {
2828
log.Infof("Language is: %s.", ga.GetLanguage(opt.Language))
2929

3030
// if docker is enabled, create repo secrets for DOCKERHUB_USERNAME and DOCKERHUB_TOKEN
31-
if opt.Jobs.Docker.Enable {
31+
if opt.Docker.Enable {
3232
if err := gitHubClient.AddRepoSecret("DOCKERHUB_USERNAME", viper.GetString("dockerhub_username")); err != nil {
3333
return false, err
3434
}

internal/pkg/plugin/githubactions/golang/ishealthy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func IsHealthy(options *map[string]interface{}) (bool, error) {
4040
}
4141

4242
// if docker is enabled, verify if secrets DOCKERHUB_USERNAME and DOCKERHUB_TOKEN are deleted
43-
if opt.Jobs.Docker.Enable {
43+
if opt.Docker.Enable {
4444
for _, secret := range []string{"DOCKERHUB_USERNAME", "DOCKERHUB_TOKEN"} {
4545
exists, err := gitHubClient.RepoSecretExists(secret)
4646
if err != nil {

internal/pkg/plugin/githubactions/golang/jobs.go

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

3-
// Jobs is the struct for github actions custom config.
4-
type Jobs struct {
5-
Build Build
6-
Test Test
7-
Docker Docker
3+
func (b *Build) Validate() []error {
4+
retErrors := make([]error, 0)
5+
6+
// TODO(daniel-hutao): what should we validate here?
7+
8+
return retErrors
9+
}
10+
11+
func (t *Test) Validate() []error {
12+
retErrors := make([]error, 0)
13+
14+
// TODO(daniel-hutao): what should we validate here?
15+
16+
return retErrors
817
}
918

10-
func (j *Jobs) Validate() []error {
19+
func (d *Docker) Validate() []error {
1120
retErrors := make([]error, 0)
1221

1322
// TODO(daniel-hutao): what should we validate here?

internal/pkg/plugin/githubactions/golang/options.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ type Options struct {
1313
Repo string
1414
Branch string
1515
Language *ga.Language
16-
Jobs *Jobs
16+
Build *Build
17+
Test *Test
18+
Docker *Docker
1719
}
1820

1921
// validate validates the options provided by the core.
@@ -42,12 +44,21 @@ func validate(param *Options) []error {
4244
}
4345

4446
// jobs
45-
if param.Jobs == nil {
46-
retErrors = append(retErrors, fmt.Errorf("job is empty"))
47+
if param.Test == nil {
48+
retErrors = append(retErrors, fmt.Errorf("test is empty"))
4749
}
48-
if errs := param.Jobs.Validate(); len(errs) != 0 {
50+
if errs := param.Test.Validate(); len(errs) != 0 {
4951
for _, e := range errs {
50-
retErrors = append(retErrors, fmt.Errorf("job is invalid: %s", e))
52+
retErrors = append(retErrors, fmt.Errorf("test is invalid: %s", e))
53+
}
54+
}
55+
56+
if param.Docker == nil {
57+
retErrors = append(retErrors, fmt.Errorf("docker is empty"))
58+
}
59+
if errs := param.Docker.Validate(); len(errs) != 0 {
60+
for _, e := range errs {
61+
retErrors = append(retErrors, fmt.Errorf("docker is invalid: %s", e))
5162
}
5263
}
5364

internal/pkg/plugin/githubactions/golang/reinstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Reinstall(options *map[string]interface{}) (bool, error) {
2727

2828
log.Infof("language is %s", ga.GetLanguage(opt.Language))
2929

30-
if opt.Jobs.Docker.Enable {
30+
if opt.Docker.Enable {
3131
for _, secret := range []string{"DOCKERHUB_USERNAME", "DOCKERHUB_TOKEN"} {
3232
if err := gitHubClient.DeleteRepoSecret(secret); err != nil {
3333
return false, err

internal/pkg/plugin/githubactions/golang/render.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import (
1010
)
1111

1212
func renderTemplate(workflow *github.Workflow, options *Options) (string, error) {
13-
var jobs Jobs
14-
err := mapstructure.Decode(options.Jobs, &jobs)
13+
var opts Options
14+
err := mapstructure.Decode(options, &opts)
1515
if err != nil {
1616
return "", err
1717
}
18+
if opts.Build == nil {
19+
opts.Build = &Build{false, ""}
20+
}
1821

1922
//if use default {{.}}, it will confict (github actions vars also use them)
2023
t, err := template.New("githubactions").Delims("[[", "]]").Parse(workflow.WorkflowContent)
@@ -23,7 +26,7 @@ func renderTemplate(workflow *github.Workflow, options *Options) (string, error)
2326
}
2427

2528
var buff bytes.Buffer
26-
err = t.Execute(&buff, jobs)
29+
err = t.Execute(&buff, opts)
2730
if err != nil {
2831
return "", err
2932
}

internal/pkg/plugin/githubactions/golang/templates.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Build
13-
run: [[.Build.Command]]
13+
run: [[- if not .Build.Command]] go build ./...[[- else]][[.Build.Command]][[- end]]
14+
[[- else]]
1415
[[- end]]
1516
[[- if .Test.Enable]]
1617
test:
@@ -22,15 +23,16 @@ jobs:
2223
with:
2324
go-version: 1.17
2425
- name: Test
25-
run: [[.Test.Command]] [[- if .Test.Coverage.Enable]] -race -covermode=atomic -coverprofile=[[.Test.Coverage.Output]] [[- end]]
26+
run: [[- if not .Test.Command]] go test ./...[[- else]][[.Test.Command]][[- end]] [[- if .Test.Coverage.Enable]] -race -covermode=atomic -coverprofile=[[- if not .Test.Coverage.Output]]coverage.out[[- else]][[.Test.Coverage.Output]][[- end]] [[- end]]
2627
[[- if .Test.Coverage.Enable]]
2728
- name: comment PR
2829
uses: machine-learning-apps/pr-comment@master
2930
env:
3031
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3132
with:
32-
path: [[.Test.Coverage.Output]]
33+
path: [[- if not .Test.Coverage.Output]]coverage.out[[- else]][[.Test.Coverage.Output]][[- end]]
3334
[[- end]]
35+
[[- else]]
3436
[[- end]]
3537
tag:
3638
name: Tag
@@ -65,7 +67,7 @@ jobs:
6567
uses: docker/build-push-action@v2
6668
with:
6769
push: true
68-
tags: ${{ secrets.DOCKERHUB_USERNAME }}/[[.Docker.Repo]]:${{needs.tag.outputs.new_tag}}
70+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/[[- if not .Docker.Repo]][[.Repo]][[- else]][[.Docker.Repo]][[- end]]:${{needs.tag.outputs.new_tag}}
6971
[[- end]]
7072
`
7173

@@ -79,7 +81,8 @@ jobs:
7981
runs-on: ubuntu-latest
8082
steps:
8183
- name: Build
82-
run: [[.Build.Command]]
84+
run: [[- if not .Build.Command]] go build ./...[[- else]][[.Build.Command]][[- end]]
85+
[[- else]]
8386
[[- end]]
8487
[[- if .Test.Enable]]
8588
test:
@@ -91,7 +94,16 @@ jobs:
9194
with:
9295
go-version: 1.17
9396
- name: Test
94-
run: [[.Test.Command]] [[- if .Test.Coverage.Enable]] -race -covermode=atomic -coverprofile=[[.Test.Coverage.Output]] [[- end]]
97+
run: [[- if not .Test.Command]] go test ./...[[- else]][[.Test.Command]][[- end]] [[- if .Test.Coverage.Enable]] -race -covermode=atomic -coverprofile=[[- if not .Test.Coverage.Output]]coverage.out[[- else]][[.Test.Coverage.Output]][[- end]] [[- end]]
98+
[[- if .Test.Coverage.Enable]]
99+
- name: comment PR
100+
uses: machine-learning-apps/pr-comment@master
101+
env:
102+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
103+
with:
104+
path: [[- if not .Test.Coverage.Output]]coverage.out[[- else]][[.Test.Coverage.Output]][[- end]]
105+
[[- end]]
106+
[[- else]]
95107
[[- end]]
96108
tag:
97109
name: Tag
@@ -126,6 +138,6 @@ jobs:
126138
uses: docker/build-push-action@v2
127139
with:
128140
push: true
129-
tags: ${{ secrets.DOCKERHUB_USERNAME }}/[[.Docker.Repo]]:${{needs.tag.outputs.new_tag}}
141+
tags: ${{ secrets.DOCKERHUB_USERNAME }}/[[- if not .Docker.Repo]][[.Repo]][[- else]][[.Docker.Repo]][[- end]]:${{needs.tag.outputs.new_tag}}
130142
[[- end]]
131143
`

internal/pkg/plugin/githubactions/golang/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Uninstall(options *map[string]interface{}) (bool, error) {
2626
log.Infof("language is %s", ga.GetLanguage(opt.Language))
2727

2828
// if docker is enabled, delete repo secrets DOCKERHUB_USERNAME and DOCKERHUB_TOKEN
29-
if opt.Jobs.Docker.Enable {
29+
if opt.Docker.Enable {
3030
for _, secret := range []string{"DOCKERHUB_USERNAME", "DOCKERHUB_TOKEN"} {
3131
if err := gitHubClient.DeleteRepoSecret(secret); err != nil {
3232
return false, err

0 commit comments

Comments
 (0)