Skip to content

Commit

Permalink
Merge pull request #190 from kramphub/master
Browse files Browse the repository at this point in the history
feat(#189): Infer github repo from build's information to allow using…
  • Loading branch information
Saaarah authored May 17, 2024
2 parents 20f8b6b + 4dbb911 commit 6569f16
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion githubissues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 ""
}
30 changes: 30 additions & 0 deletions githubissues/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 6569f16

Please sign in to comment.