@@ -564,11 +564,6 @@ func updateIssues(issues []*Issue, stacks map[string]map[Info]int64, stackToURL
564
564
writeStackComment (comment , stack , id , stackToURL [stack ], stacks [stack ])
565
565
}
566
566
567
- if * dryRun {
568
- log .Printf ("DRY RUN: would add stacks %s to issue #%d" , newStackIDs , issue .Number )
569
- continue
570
- }
571
-
572
567
if err := addIssueComment (issue .Number , comment .String ()); err != nil {
573
568
log .Println (err )
574
569
continue
@@ -870,19 +865,8 @@ func updateIssueBody(number int, body string) error {
870
865
}
871
866
872
867
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 )
886
870
}
887
871
return nil
888
872
}
@@ -900,7 +884,20 @@ func addIssueComment(number int, comment string) error {
900
884
}
901
885
902
886
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 ))
904
901
if err != nil {
905
902
return err
906
903
}
@@ -910,9 +907,9 @@ func addIssueComment(number int, comment string) error {
910
907
return err
911
908
}
912
909
defer resp .Body .Close ()
913
- if resp .StatusCode != http . StatusCreated {
910
+ if resp .StatusCode != wantStatus {
914
911
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 )
916
913
}
917
914
return nil
918
915
}
0 commit comments