diff --git a/githubissues/main.go b/githubissues/main.go index 18752940..9e760790 100644 --- a/githubissues/main.go +++ b/githubissues/main.go @@ -98,7 +98,12 @@ func (g *githubissuesNotifier) SendNotification(ctx context.Context, build *cbpb return nil } - webhookURL := fmt.Sprintf("%s/%s/issues", githubApiEndpoint, g.githubRepo) + repo := GetGithubRepo(build) + if repo == "" { + log.Warningf("could not determine GitHub repository from build, skipping notification") + return nil + } + webhookURL := fmt.Sprintf("%s/%s/issues", githubApiEndpoint, repo) log.Infof("sending GitHub Issue webhook for Build %q (status: %q) to url %q", build.Id, build.Status, webhookURL) @@ -148,3 +153,12 @@ func (g *githubissuesNotifier) SendNotification(ctx context.Context, build *cbpb log.V(2).Infoln("send HTTP request successfully") return nil } + +func GetGithubRepo(build *cbpb.Build) string { + if build.Substitutions != nil && build.Substitutions["REPO_FULL_NAME"] != "" { + // return repo full name if it's available + // e.g. "GoogleCloudPlatform/cloud-build-notifiers" + return build.Substitutions["REPO_FULL_NAME"] + } + return "" +} diff --git a/githubissues/main_test.go b/githubissues/main_test.go index 28927541..8d3577c9 100644 --- a/githubissues/main_test.go +++ b/githubissues/main_test.go @@ -161,3 +161,33 @@ func TestConfigs(t *testing.T) { }) } } + +func TestGetGithubRepo(t *testing.T) { + for _, tc := range []struct { + name string + build *cbpb.Build + expected string + }{{ + name: "REPO_FULL_NAME is set", + // test GetGithubRepo method + build: &cbpb.Build{ + Substitutions: map[string]string{"REPO_FULL_NAME": "somename/somerepo"}, + }, + expected: "somename/somerepo", + }, { + name: "REPO_FULL_NAME is not set", + // test GetGithubRepo method + build: &cbpb.Build{ + Substitutions: map[string]string{}, + }, + expected: "", + }, + } { + t.Run(tc.name, func(t *testing.T) { + actual := GetGithubRepo(tc.build) + if actual != tc.expected { + t.Errorf("expected %q, got %q", tc.expected, actual) + } + }) + } +}