func (syncer *Syncer) ensureCommentWithStories( repo *github.Repository, issue *github.Issue, allStories []tracker.Story, ) error { comments, err := syncer.allCommentsForIssue(repo, issue) if err != nil { return fmt.Errorf("failed to fetch issue comments: %s", err) } currentUser, err := syncer.currentUser() if err != nil { return fmt.Errorf("failed to get current user: %s", err) } var existingComment *github.IssueComment for _, comment := range comments { if *comment.User.ID == *currentUser.ID { existingComment = comment break } } buf := new(bytes.Buffer) if err := storyStateCommentTemplate.Execute(buf, allStories); err != nil { return fmt.Errorf("error building comment body: %s", err) } commentBody := buf.String() if existingComment == nil { createdComment, _, err := syncer.GithubClient.Issues.CreateComment( *repo.Owner.Login, *repo.Name, *issue.Number, &github.IssueComment{Body: &commentBody}, ) if err != nil { return fmt.Errorf("failed to create comment: %s", err) } log.Println("created comment:", *createdComment.HTMLURL) } else if *existingComment.Body != commentBody { existingComment.Body = &commentBody updatedComment, _, err := syncer.GithubClient.Issues.EditComment( *repo.Owner.Login, *repo.Name, *existingComment.ID, &github.IssueComment{Body: &commentBody}, ) if err != nil { return fmt.Errorf("failed to update comment: %s", err) } log.Println("updated comment:", *updatedComment.HTMLURL) } return nil }
// Edit a comment already on github. func EditComment(repo, body string, commentId int) (github.IssueComment, error) { s := strings.Split(repo, "/") comment := new(github.IssueComment) comment.Body = &body temp, _, err := client.Issues.EditComment(s[0], s[1], commentId, comment) newComment := *temp return newComment, err }
// Comment on a issue on github. func Comment(repo, body string, issueNum int) (github.IssueComment, error) { s := strings.Split(repo, "/") comment := new(github.IssueComment) comment.Body = &body temp, _, err := client.Issues.CreateComment(s[0], s[1], issueNum, comment) newComment := *temp return newComment, err }