in cmd/publishing-bot/github.go [40:100]
func ReportOnIssue(e error, logs, token, org, repo string, issue int) error {
ctx := context.Background()
client := githubClient(token)
// filter out token, if it happens to be in the log (it shouldn't!)
// TODO: Consider using log sanitizer from sigs.k8s.io/release-utils
logs = strings.ReplaceAll(logs, token, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
// who am I?
myself, resp, err := client.Users.Get(ctx, "")
if err != nil {
return fmt.Errorf("failed to get own user: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get own user: HTTP code %d", resp.StatusCode)
}
// create new newComment
body := transfromLogToGithubFormat(logs, 50, fmt.Sprintf("/reopen\n\nThe last publishing run failed: %v", e))
newComment, resp, err := client.Issues.CreateComment(ctx, org, repo, issue, &github.IssueComment{
Body: &body,
})
if err != nil {
return fmt.Errorf("failed to comment on issue #%d: %v", issue, err)
}
if resp.StatusCode >= 300 {
return fmt.Errorf("failed to comment on issue #%d: HTTP code %d", issue, resp.StatusCode)
}
// delete all other comments from this user
comments, resp, err := client.Issues.ListComments(ctx, org, repo, issue, &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{PerPage: 100},
})
if err != nil {
return fmt.Errorf("failed to get github comments of issue #%d: %v", issue, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get github comments of issue #%d: HTTP code %d", issue, resp.StatusCode)
}
for _, c := range comments {
if *c.User.ID != *myself.ID {
glog.Infof("Skipping comment %d not by me, but %v", *c.ID, c.User.Name)
continue
}
if *c.ID == *newComment.ID {
continue
}
glog.Infof("Deleting comment %d", *c.ID)
resp, err = client.Issues.DeleteComment(ctx, org, repo, *c.ID)
if err != nil {
return fmt.Errorf("failed to delete github comment %d of issue #%d: %v", *c.ID, issue, err)
}
if resp.StatusCode >= 300 {
return fmt.Errorf("failed to delete github comment %d of issue #%d: HTTP code %d", *c.ID, issue, resp.StatusCode)
}
}
return nil
}