Skip to content

Commit

Permalink
Merge pull request #14 from haya14busa/support-common-ci
Browse files Browse the repository at this point in the history
Support "common" as CI flag
  • Loading branch information
haya14busa committed Oct 21, 2016
2 parents db0fe2c + e7ffd83 commit b76f0c3
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 6 deletions.
67 changes: 61 additions & 6 deletions cmd/watchdogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,44 @@ import (
"github.com/mattn/go-shellwords"
)

// TODO(#14): remove
func F() {
}

const usageMessage = "" +
`Usage: watchdogs [flags]
`Usage: watchdogs [flags]
watchdogs accepts any compiler or linter results from stdin and filters
them by diff for review. watchdogs also can posts the results as a comment to
GitHub if you use watchdogs in CI service.
`

// flags
var (
diffCmd string
diffCmd string
diffCmdDoc = `diff command (e.g. "git diff"). diff flag is ignored if you pass "ci" flag`

diffStrip int
efms strslice
ci string

ci string
ciDoc = `CI service (supported travis, circle-ci, droneio(OSS 0.4), common)
If you use "ci" flag, you need to set WATCHDOGS_GITHUB_API_TOKEN environment
variable. Go to https://github.com/settings/tokens and create new Personal
access token with repo scope.
"common" requires following environment variables
CI_PULL_REQUEST Pull Request number (e.g. 14)
CI_COMMIT SHA1 for the current build
CI_REPO_OWNER repository owner (e.g. "haya14busa" for https://github.com/haya14busa/watchdogs)
CI_REPO_NAME repository name (e.g. "watchdogs" for https://github.com/haya14busa/watchdogs)
`
)

func init() {
flag.StringVar(&diffCmd, "diff", "", "diff command for filitering checker results")
flag.StringVar(&diffCmd, "diff", "", diffCmdDoc)
flag.IntVar(&diffStrip, "strip", 1, "strip NUM leading components from diff file names (equivalent to `patch -p`) (default is 1 for git diff)")
flag.Var(&efms, "efm", "list of errorformat")
flag.StringVar(&ci, "ci", "", "CI service (supported travis, circle-ci, droneio(OSS 0.4))")
flag.Var(&efms, "efm", "list of errorformat (https://github.com/haya14busa/errorformat)")
flag.StringVar(&ci, "ci", "", ciDoc)
}

func usage() {
Expand Down Expand Up @@ -144,6 +165,8 @@ func githubService(ci string) (githubservice *watchdogs.GitHubPullRequest, isPR
g, isPR, err = circleci()
case "droneio":
g, isPR, err = droneio()
case "common":
g, isPR, err = commonci()
default:
return nil, false, fmt.Errorf("unsupported CI: %v", ci)
}
Expand Down Expand Up @@ -275,6 +298,38 @@ func droneio() (g *GitHubPR, isPR bool, err error) {
return g, true, nil
}

func commonci() (g *GitHubPR, isPR bool, err error) {
var prs string // pull request number in string
prs = os.Getenv("CI_PULL_REQUEST")
if prs == "" {
// not a pull-request build
return nil, false, nil
}
pr, err := strconv.Atoi(prs)
if err != nil {
return nil, true, fmt.Errorf("unexpected env variable (CI_PULL_REQUEST): %v", prs)
}
owner, err := nonEmptyEnv("CI_REPO_OWNER")
if err != nil {
return nil, true, err
}
repo, err := nonEmptyEnv("CI_REPO_NAME")
if err != nil {
return nil, true, err
}
sha, err := nonEmptyEnv("CI_COMMIT")
if err != nil {
return nil, true, err
}
g = &GitHubPR{
owner: owner,
repo: repo,
pr: pr,
sha: sha,
}
return g, true, nil
}

// GitHubPR represents required information about GitHub PullRequest.
type GitHubPR struct {
owner string
Expand Down
73 changes: 73 additions & 0 deletions cmd/watchdogs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,76 @@ func TestDroneio(t *testing.T) {
t.Log(err)
}
}

func TestCommonci(t *testing.T) {
envs := []string{
"CI_PULL_REQUEST",
"CI_COMMIT",
"CI_REPO_OWNER",
"CI_REPO_NAME",
"WATCHDOGS_GITHUB_API_TOKEN",
}
// save and clean
saveEnvs := make(map[string]string)
for _, key := range envs {
saveEnvs[key] = os.Getenv(key)
os.Setenv(key, "")
}
// restore
defer func() {
for key, value := range saveEnvs {
os.Setenv(key, value)
}
}()

if _, isPR, err := commonci(); isPR {
t.Errorf("should be non pull-request build. error: %v", err)
}

os.Setenv("CI_PULL_REQUEST", "invalid")
if _, _, err := commonci(); err == nil {
t.Error("error expected but got nil")
} else {
t.Log(err)
}

os.Setenv("CI_PULL_REQUEST", "1")
if _, _, err := commonci(); err == nil {
t.Error("error expected but got nil")
} else {
t.Log(err)
}

os.Setenv("CI_REPO_OWNER", "haya14busa")
if _, _, err := commonci(); err == nil {
t.Error("error expected but got nil")
} else {
t.Log(err)
}

os.Setenv("CI_REPO_NAME", "watchdogs")
if _, _, err := commonci(); err == nil {
t.Error("error expected but got nil")
} else {
t.Log(err)
}

os.Setenv("CI_COMMIT", "sha1")
g, isPR, err := commonci()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !isPR {
t.Error("should be pull request build")
}
want := &GitHubPR{
owner: "haya14busa",
repo: "watchdogs",
pr: 1,
sha: "sha1",
}
if !reflect.DeepEqual(g, want) {
t.Errorf("got: %#v, want: %#v", g, want)
}

}

0 comments on commit b76f0c3

Please sign in to comment.