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

Support prow #174

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,38 @@ If you are using Coveralls Enterprise and have a self-signed certificate, you ne
$ goveralls -insecure
```

## Prow

Goveralls can be used in [prow](https://github.com/kubernetes/test-infra/tree/master/prow) presubmit jobs, picking up the prow [environment variables](https://github.com/kubernetes/test-infra/blob/master/prow/jobs.md#job-environment-variables) `PULL_PULL_SHA`, `PULL_BASE_REF` and `PULL_NUMBER` to determine the original git commit id, the branch and the PR number automatically.

To avoid leaking your coveralls repo token you can use the `COVERALLS_TOKEN_FILE` env variable, so that a token gets read and used in the API call.

When configuring your prow job you can mount a defined secret containing your token like this:

```yaml
...
- name: coveralls
always_run: true
optional: false
spec:
containers:
- image: gcr.io/k8s-testimages/...
env:
- name: COVERALLS_TOKEN_FILE
value: /etc/secrets/coveralls/token
command:
- "goveralls -service=prow"
volumeMounts:
- name: coveralls
mountPath: /etc/secrets/coveralls
readOnly: true
volumes:
- name: coveralls
secret:
secretName: coveralls-token
...
```

# Authors

* Yasuhiro Matsumoto (a.k.a. mattn)
Expand Down
27 changes: 26 additions & 1 deletion gitinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func collectGitInfo(ref string) (*Git, error) {
continue
}
}
if key == "GIT_ID" {
if envGitId := loadGitIdFromEnv(); envGitId != "" {
err := os.Setenv(key, envGitId)
if err != nil {
return nil, err
}
continue
}
}
if os.Getenv(key) != "" {
// metadata already available via environment variable
continue
Expand Down Expand Up @@ -129,7 +138,7 @@ var varNames = [...]string{
"CI_BRANCH", "APPVEYOR_REPO_BRANCH",
"WERCKER_GIT_BRANCH", "DRONE_BRANCH",
"BUILDKITE_BRANCH", "BRANCH_NAME",
"CI_COMMIT_REF_NAME",
"CI_COMMIT_REF_NAME", "PULL_BASE_REF",
}

func loadBranchFromEnv() string {
Expand All @@ -144,3 +153,19 @@ func loadBranchFromEnv() string {

return ""
}

var gitIdVarNames = [...]string{
"GIT_ID",

"PULL_PULL_SHA",
}

func loadGitIdFromEnv() string {
for _, gitIdVarName := range gitIdVarNames {
if gitId := os.Getenv(gitIdVarName); gitId != "" {
return gitId
}
}

return ""
}
54 changes: 54 additions & 0 deletions gitinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func TestLoadBranchFromEnv(t *testing.T) {
},
"github-master",
},
{
"Prow PULL_BASE_REF defined",
map[string]string{
"PULL_BASE_REF": "prow-master",
},
"prow-master",
},
{
"no branch var defined",
map[string]string{},
Expand All @@ -137,3 +144,50 @@ func resetBranchEnvs(values map[string]string) {
os.Setenv(k, v)
}
}

func TestLoadGitIdFromEnv(t *testing.T) {
t.Parallel()

var tests = []struct {
testCase string
envs map[string]string
expectedGitId string
}{
{
"all vars defined",
map[string]string{
"GIT_ID": "git-id",
"PULL_PULL_SHA": "prow-git-id",
},
"git-id",
},
{
"all except GIT_ID",
map[string]string{
"PULL_PULL_SHA": "prow-git-id",
},
"prow-git-id",
},
{
"no git id defined",
map[string]string{},
"",
},
}
for _, test := range tests {
resetGitIdEnvs(test.envs)
envGitId := loadGitIdFromEnv()
if envGitId != test.expectedGitId {
t.Errorf("%s: wrong git id returned. Expected %q, but got %q", test.testCase, test.expectedGitId, envGitId)
}
}
}

func resetGitIdEnvs(values map[string]string) {
for _, envVar := range gitIdVarNames {
os.Unsetenv(envVar)
}
for k, v := range values {
os.Setenv(k, v)
}
}
2 changes: 2 additions & 0 deletions goveralls.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ func process() error {
} else if prURL := os.Getenv("CI_PULL_REQUEST"); prURL != "" {
// for Circle CI
pullRequest = regexp.MustCompile(`[0-9]+$`).FindString(prURL)
} else if prNumber := os.Getenv("PULL_NUMBER"); prNumber != "" {
pullRequest = prNumber
} else if os.Getenv("GITHUB_EVENT_NAME") == "pull_request" {
number := githubEvent["number"].(float64)
pullRequest = strconv.Itoa(int(number))
Expand Down