about summary refs log tree commit diff
path: root/ops/besadii/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'ops/besadii/main.go')
-rw-r--r--ops/besadii/main.go92
1 files changed, 50 insertions, 42 deletions
diff --git a/ops/besadii/main.go b/ops/besadii/main.go
index 77c04099ebed..a04595c6c539 100644
--- a/ops/besadii/main.go
+++ b/ops/besadii/main.go
@@ -60,6 +60,47 @@ type Build struct {
 	Env    map[string]string `json:"env"`
 }
 
+// reviewInput is a struct representing the data submitted to Gerrit
+// to post a review on a CL.
+//
+// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#review-input
+type reviewInput struct {
+	Message                        string         `json:"message"`
+	Labels                         map[string]int `json:"labels,omitempty"`
+	OmitDuplicateComments          bool           `json:"omit_duplicate_comments"`
+	IgnoreDefaultAttentionSetRules bool           `json:"ignore_default_attention_set_rules"`
+	Tag                            string         `json:"tag"`
+}
+
+// updateGerrit posts a comment on a Gerrit CL to indicate the current build status.
+func updateGerrit(review reviewInput, changeId, patchset string) {
+	body, _ := json.Marshal(review)
+	reader := ioutil.NopCloser(bytes.NewReader(body))
+
+	url := fmt.Sprintf("https://cl.tvl.fyi/a/changes/%s/revisions/%s/review", changeId, patchset)
+	req, err := http.NewRequest("POST", url, reader)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "failed to create an HTTP request: %w", err)
+		os.Exit(1)
+	}
+
+	req.SetBasicAuth("buildkite", gerritPassword())
+	req.Header.Add("Content-Type", "application/json")
+
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		fmt.Errorf("failed to update CL on Gerrit: %w", err)
+	}
+	defer resp.Body.Close()
+
+	if resp.StatusCode != http.StatusOK {
+		respBody, _ := ioutil.ReadAll(resp.Body)
+		fmt.Fprintf(os.Stderr, "received non-success response from Gerrit: %s (%v)", respBody, resp.Status)
+	} else {
+		fmt.Printf("Added CI status comment on https://cl.tvl.fyi/c/depot/+/%s/%s", changeId, patchset)
+	}
+}
+
 // Trigger a build of a given branch & commit on Buildkite
 func triggerBuild(log *syslog.Writer, token string, update *refUpdated) error {
 	env := make(map[string]string)
@@ -212,16 +253,14 @@ func refUpdatedMain() {
 	log.Info("triggered sourcegraph index update")
 }
 
-// reviewInput is a struct representing the data submitted to Gerrit
-// to post a review on a CL.
-//
-// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#review-input
-type reviewInput struct {
-	Message                        string         `json:"message"`
-	Labels                         map[string]int `json:"labels"`
-	OmitDuplicateComments          bool           `json:"omit_duplicate_comments"`
-	IgnoreDefaultAttentionSetRules bool           `json:"ignore_default_attention_set_rules"`
-	Tag                            string         `json:"tag"`
+func gerritPassword() string {
+	gerritPassword, err := ioutil.ReadFile("/etc/secrets/buildkite-gerrit")
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "Gerrit password could not be read: %s", err)
+		os.Exit(1)
+	}
+
+	return string(gerritPassword)
 }
 
 func postCommandMain() {
@@ -241,12 +280,6 @@ func postCommandMain() {
 		return
 	}
 
-	gerritPassword, err := ioutil.ReadFile("/etc/secrets/buildkite-gerrit")
-	if err != nil {
-		fmt.Fprintf(os.Stderr, "Gerrit password could not be read: %s", err)
-		os.Exit(1)
-	}
-
 	var verified int
 	var verb string
 
@@ -271,32 +304,7 @@ func postCommandMain() {
 
 		Tag: "autogenerated:buildkite~result",
 	}
-
-	body, _ := json.Marshal(review)
-	reader := ioutil.NopCloser(bytes.NewReader(body))
-
-	url := fmt.Sprintf("https://cl.tvl.fyi/a/changes/%s/revisions/%s/review", changeId, patchset)
-	req, err := http.NewRequest("POST", url, reader)
-	if err != nil {
-		fmt.Fprintf(os.Stderr, "failed to create an HTTP request: %w", err)
-		os.Exit(1)
-	}
-
-	req.SetBasicAuth("buildkite", string(gerritPassword))
-	req.Header.Add("Content-Type", "application/json")
-
-	resp, err := http.DefaultClient.Do(req)
-	if err != nil {
-		fmt.Errorf("failed to update CL on Gerrit: %w", err)
-	}
-	defer resp.Body.Close()
-
-	if resp.StatusCode != 200 {
-		respBody, _ := ioutil.ReadAll(resp.Body)
-		fmt.Fprintf(os.Stderr, "received non-success response from Gerrit: %s (%v)", respBody, resp.Status)
-	} else {
-		fmt.Printf("Updated CI status on https://cl.tvl.fyi/c/depot/+/%s/%s", changeId, patchset)
-	}
+	updateGerrit(review, changeId, patchset)
 }
 
 func main() {