diff --git a/README.md b/README.md index 699a776..da3bfdf 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ metadata: type: Opaque stringData: github.com: ":" + github.com_foo: ":" + github.com_foo_bar: ":" EOF ``` diff --git a/main.go b/main.go index 88131ab..4a6bd78 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,14 @@ func getNS() (string, error) { return "default", nil } +func makeKeys(host, owner, repo string) []string { + return []string{ + host + secretDelim + owner + secretDelim + repo, + host + secretDelim + owner, + host, + } +} + func mapGitAPI(s string) string { switch s { case "ghe", "github": @@ -62,7 +70,7 @@ func mapGitAPI(s string) string { func mapState(s string) (state, description string) { switch s { case "Running": - return "pending", "Pending - Job triggered." + return "pending", "Job triggered." case "PipelineRunCancelled": return "error", "Job cancelled." case "Failed": @@ -221,6 +229,8 @@ func statusUpdate(obj interface{}, s *StatusCache, sec typedcorev1.SecretInterfa const ( contentType = "Content-Type" jsonType = "application/json" + + secretDelim = "_" ) type GitStatus struct { @@ -230,13 +240,22 @@ type GitStatus struct { TargetURL string `json:"target_url"` } -func getSecret(sec *corev1.Secret, host string) (username, password string, err error) { - s, ok := sec.Data[host] +func getSecret(sec *corev1.Secret, host, owner, repo string) (username, password string, err error) { + var ( + secret []byte + ok bool + ) + for _, v := range makeKeys(host, owner, repo) { + secret, ok = sec.Data[v] + if ok { + break + } + } if !ok { err = fmt.Errorf("no key found for host %s in secret %s/%s", host, sec.Namespace, sec.Name) return } - a := strings.SplitN(string(s), ":", 2) + a := strings.SplitN(string(secret), ":", 2) if len(a) < 2 { err = fmt.Errorf("missing credentials for host %s in secret %s/%s", host, sec.Namespace, sec.Name) return @@ -246,6 +265,16 @@ func getSecret(sec *corev1.Secret, host string) (username, password string, err return } +func getOwnerRepo(uri string) (owner, repo string) { + a := strings.SplitN(uri, "/", 2) + if len(a) < 2 { + return + } + owner = a[0] + repo = a[1] + return +} + func postStatus(pr *PipelineRun, gitUrl, sha, status string, sec *corev1.Secret) error { u, err := url.Parse(gitUrl) if err != nil { @@ -284,7 +313,8 @@ func postStatus(pr *PipelineRun, gitUrl, sha, status string, sec *corev1.Secret) return err } req.Header.Set(contentType, jsonType) - user, pass, err := getSecret(sec, u.Host) + owner, repo := getOwnerRepo(uri) + user, pass, err := getSecret(sec, u.Host, owner, repo) if err != nil { return err } @@ -321,7 +351,8 @@ func postComment(pr *PipelineRun, gitUrl, issue string, sec *corev1.Secret) erro } scheme := getParam(pr, "scheme") preview := fmt.Sprintf("Preview Environment: [Link](%s%s)", scheme, previewurl) - user, pass, err := getSecret(sec, u.Host) + owner, repo := getOwnerRepo(uri) + user, pass, err := getSecret(sec, u.Host, owner, repo) if err != nil { return err }