Skip to content

Commit 30bd6fd

Browse files
committed
internal/telemetry/cmd/stacks: move dry run checks down
Check for dry run at the point where we actually request modifications to GitHub. Change-Id: Id92d078d2db82ff6487b42f5b0f10df0e9b90791 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643815 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent e4adc38 commit 30bd6fd

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

gopls/internal/telemetry/cmd/stacks/stacks.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,6 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL
564564
writeStackComment(comment, stack, id, stackToURL[stack], stacks[stack])
565565
}
566566

567-
if *dryRun {
568-
log.Printf("DRY RUN: would add stacks %s to issue #%d", newStackIDs, issue.Number)
569-
continue
570-
}
571-
572567
if err := addIssueComment(issue.Number, comment.String()); err != nil {
573568
log.Println(err)
574569
continue
@@ -870,19 +865,8 @@ func updateIssueBody(number int, body string) error {
870865
}
871866

872867
url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d", number)
873-
req, err := http.NewRequest("PATCH", url, bytes.NewReader(data))
874-
if err != nil {
875-
return err
876-
}
877-
req.Header.Add("Authorization", "Bearer "+authToken)
878-
resp, err := http.DefaultClient.Do(req)
879-
if err != nil {
880-
return err
881-
}
882-
defer resp.Body.Close()
883-
if resp.StatusCode != http.StatusOK {
884-
body, _ := io.ReadAll(resp.Body)
885-
return fmt.Errorf("issue update failed: %s (body: %s)", resp.Status, body)
868+
if err := requestChange("PATCH", url, data, http.StatusOK); err != nil {
869+
return fmt.Errorf("updating issue: %v", err)
886870
}
887871
return nil
888872
}
@@ -900,7 +884,20 @@ func addIssueComment(number int, comment string) error {
900884
}
901885

902886
url := fmt.Sprintf("https://api.github.com/repos/golang/go/issues/%d/comments", number)
903-
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
887+
if err := requestChange("POST", url, data, http.StatusCreated); err != nil {
888+
return fmt.Errorf("creating issue comment: %v", err)
889+
}
890+
return nil
891+
}
892+
893+
// requestChange sends a request to url using method, which may change the state at the server.
894+
// The data is sent as the request body, and wantStatus is the expected response status code.
895+
func requestChange(method, url string, data []byte, wantStatus int) error {
896+
if *dryRun {
897+
log.Printf("DRY RUN: %s %s", method, url)
898+
return nil
899+
}
900+
req, err := http.NewRequest(method, url, bytes.NewReader(data))
904901
if err != nil {
905902
return err
906903
}
@@ -910,9 +907,9 @@ func addIssueComment(number int, comment string) error {
910907
return err
911908
}
912909
defer resp.Body.Close()
913-
if resp.StatusCode != http.StatusCreated {
910+
if resp.StatusCode != wantStatus {
914911
body, _ := io.ReadAll(resp.Body)
915-
return fmt.Errorf("failed to create issue comment: %s (body: %s)", resp.Status, body)
912+
return fmt.Errorf("request failed: %s (body: %s)", resp.Status, body)
916913
}
917914
return nil
918915
}

0 commit comments

Comments
 (0)