Skip to content

Commit

Permalink
Merge pull request #23 from isaoshimizu/add_support_codebuild
Browse files Browse the repository at this point in the history
Add support AWS CodeBuild
  • Loading branch information
b4b4r07 authored Oct 30, 2018
2 parents 7ae817e + 38ffc75 commit 57d648a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Currently, supported CI are here:
- Circle CI
- Travis CI
- AWS CodeBuild
### Private Repository Considerations
GitHub private repositories require the `repo` and `write:discussion` permissions.
Expand Down
17 changes: 17 additions & 0 deletions ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"regexp"
"strconv"
"strings"
)

// CI represents a common information obtained from all CI platforms
Expand Down Expand Up @@ -46,3 +47,19 @@ func travisci() (ci CI, err error) {
ci.PR.Number, err = strconv.Atoi(os.Getenv("TRAVIS_PULL_REQUEST"))
return ci, err
}

func codebuild() (ci CI, err error) {
ci.PR.Number = 0
ci.PR.Revision = os.Getenv("CODEBUILD_RESOLVED_SOURCE_VERSION")
ci.URL = os.Getenv("CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL")
sourceVersion := os.Getenv("CODEBUILD_SOURCE_VERSION")
if sourceVersion == "" {
return ci, nil
}
pr := strings.Replace(sourceVersion, "pr/", "", 1)
if pr == "" {
return ci, nil
}
ci.PR.Number, err = strconv.Atoi(pr)
return ci, err
}
98 changes: 98 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,101 @@ func TestTravisCI(t *testing.T) {
}
}
}

func TestCodeBuild(t *testing.T) {
envs := []string{
"CODEBUILD_RESOLVED_SOURCE_VERSION",
"CODEBUILD_SOURCE_VERSION",
"CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL",
}
saveEnvs := make(map[string]string)
for _, key := range envs {
saveEnvs[key] = os.Getenv(key)
os.Unsetenv(key)
}
defer func() {
for key, value := range saveEnvs {
os.Setenv(key, value)
}
}()

// https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html
testCases := []struct {
fn func()
ci CI
ok bool
}{
{
fn: func() {
os.Setenv("CODEBUILD_RESOLVED_SOURCE_VERSION", "abcdefg")
os.Setenv("CODEBUILD_SOURCE_VERSION", "pr/123")
os.Setenv("CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL", "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 123,
},
URL: "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new",
},
ok: true,
},
{
fn: func() {
os.Setenv("CODEBUILD_RESOLVED_SOURCE_VERSION", "abcdefg")
os.Setenv("CODEBUILD_SOURCE_VERSION", "pr/1")
os.Setenv("CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL", "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 1,
},
URL: "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new",
},
ok: true,
},
{
fn: func() {
os.Setenv("CODEBUILD_RESOLVED_SOURCE_VERSION", "")
os.Setenv("CODEBUILD_SOURCE_VERSION", "")
os.Setenv("CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL", "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new")
},
ci: CI{
PR: PullRequest{
Revision: "",
Number: 0,
},
URL: "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new",
},
ok: true,
},
{
fn: func() {
os.Setenv("CODEBUILD_RESOLVED_SOURCE_VERSION", "")
os.Setenv("CODEBUILD_SOURCE_VERSION", "pr/abc")
os.Setenv("CODEBUILD_AGENT_ENV_CODEBUILD_BUILD_URL", "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new")
},
ci: CI{
PR: PullRequest{
Revision: "",
Number: 0,
},
URL: "https://ap-northeast-1.console.aws.amazon.com/codebuild/home?region=ap-northeast-1#/builds/test:f2ae4314-c2d6-4db6-83c2-eacbab1517b7/view/new",
},
ok: false,
},
}

for _, testCase := range testCases {
testCase.fn()
ci, err := codebuild()
if !reflect.DeepEqual(ci, testCase.ci) {
t.Errorf("got %q but want %q", ci, testCase.ci)
}
if (err == nil) != testCase.ok {
t.Errorf("got error %q", err)
}
}
}

2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (cfg *Config) Validation() error {
// ok pattern
case "travis", "travisci", "travis-ci":
// ok pattern
case "codebuild":
// ok pattern
default:
return fmt.Errorf("%s: not supported yet", cfg.CI)
}
Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func TestValidation(t *testing.T) {
contents: []byte("ci: travisci\n"),
expected: "notifier is missing",
},
{
contents: []byte("ci: codebuild\n"),
expected: "notifier is missing",
},
{
contents: []byte("ci: circleci\nnotifier:\n github:\n"),
expected: "notifier is missing",
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func (t *tfnotify) Run() error {
if err != nil {
return err
}
case "codebuild":
ci, err = codebuild()
if err != nil {
return err
}
case "":
return fmt.Errorf("CI service: required (e.g. circleci)")
default:
Expand Down

0 comments on commit 57d648a

Please sign in to comment.