-
Notifications
You must be signed in to change notification settings - Fork 32
Create comments in JIRA after create and update #6
Conversation
squat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good overall, however, the updateComment, createComment etc functions must be broken out into a new file, preferably in a new package. this file in unwieldy and contains code that is not relevant to cmd handling
| } | ||
|
|
||
| ctx := context.Background() | ||
| repo := strings.Split(rootCmdCfg.GetString("repo-name"), "/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you do this same parsing in multiple places. repo should be a field on a config object so it can be reused.
cmd/root.go
Outdated
| return err | ||
| } | ||
|
|
||
| res, err := jClient.Do(req, nil) // Response should be 204 No Content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it should be 204, why not confirm it by comparing to http.StatusNoContent?
cmd/root.go
Outdated
| res, err := jClient.Do(req, nil) // Response should be 204 No Content | ||
| if err != nil { | ||
| log.Errorf("Error updating comment: %s", err) | ||
| body, _ := ioutil.ReadAll(res.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not ignore the error returned by ReadAll; handle it as necessary
cmd/root.go
Outdated
| return err | ||
| } else if err = jira.CheckResponse(res.Response); err != nil { | ||
| log.Errorf("Error updating comment: %s", err) | ||
| body, _ := ioutil.ReadAll(res.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
cmd/root.go
Outdated
| jComment, res, err := jClient.Issue.AddComment(jIssue.ID, jComment) | ||
| if err != nil { | ||
| log.Errorf("Error creating JIRA comment on issue %s. Error: %s", jIssue.Key, err) | ||
| body, _ := ioutil.ReadAll(res.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
squat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
almost there
cmd/root.go
Outdated
| if err := updateIssue(*ghIssue, jIssue, jiraClient); err != nil { | ||
| log.Errorf("Error updating issue %s. Error: %v", jIssue.Key, err) | ||
| if err := updateIssue(*ghIssue, jIssue, ghClient, jiraClient); err != nil { | ||
| log.Errorf("Error updating issue %s. Error: %s", jIssue.Key, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you format the error with %v
cmd/root.go
Outdated
| Direction: "asc", | ||
| }) | ||
| if err != nil { | ||
| log.Errorf("Error retrieving GitHub comments for issue #%d. Error: %s.", *ghIssue.Number, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
cmd/root.go
Outdated
|
|
||
| user, _, err := ghClient.Users.Get(context.Background(), *ghComment.User.Login) | ||
| if err != nil { | ||
| log.Errorf("Error retrieving GitHub user %s. Error: %s", *ghComment.User.Login, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same formatting change here.
| log.Errorf("Error retrieving GitHub user %s. Error: %s", *ghComment.User.Login, err) | ||
| } | ||
| body := fmt.Sprintf("Comment (ID %d) from GitHub user %s", *ghComment.ID, user.GetLogin()) | ||
| if user.GetName() != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To simplify this a little bit, how about making the only bit that is conditioned on GetName() != "" be the part that actually appends the username:
if user.GetName() != "" {
body - fmt.Sprintf("%s (%s)", body, user.GetName())
}
body = fmt.Sprintf(
"%s at %s:\n\n%s",
body,
ghComment.CreatedAt.Format(commentDateFormat),
*ghComment.Body,
)This cleans up the logic, de-dupes the code and shortens everything a little.
| Body: body, | ||
| } | ||
|
|
||
| req, err := jClient.NewRequest("PUT", fmt.Sprintf("rest/api/2/issue/%s/comment/%s", jIssue.Key, jComment.ID), request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a TODO
TODO(LyonesGamer): abstract this into its own method
cmd/root.go
Outdated
|
|
||
| jComment, res, err := jClient.Issue.AddComment(jIssue.ID, jComment) | ||
| if err != nil { | ||
| log.Errorf("Error creating JIRA comment on issue %s. Error: %s", jIssue.Key, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%v
| } | ||
| found = true | ||
|
|
||
| updateComment(*ghComment, jComment, jIssue, ghClient, jClient) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you have 20 Jira comments and you find the GitHub comment on the first 1, you will loop over the remaining 19 comments. Instead, you should break after updateComment.
cmd/root.go
Outdated
| var comments []jira.Comment | ||
| if issue.Fields.Comments == nil { | ||
| log.Debugf("JIRA issue %s has no comments.", jIssue.Key) | ||
| comments = make([]jira.Comment, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you do not need to call make on the slice; in fact, you are allocating an array that will never be used. if you need to append elements to the slice, the built-in append function will lazily allocate and re-allocate a backing array for the slice as needed.
cmd/root.go
Outdated
| "time" | ||
|
|
||
| "regexp" | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should not be any newlines between these stdlibs
cmd/root.go
Outdated
| "strconv" | ||
|
|
||
| "net/http" | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this newline separating stdlib and remote imports is good
cmd/root.go
Outdated
| var comments []jira.Comment | ||
| if issue.Fields.Comments == nil { | ||
| log.Debugf("JIRA issue %s has no comments.", jIssue.Key) | ||
| comments = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should delete this line entirely; it is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Yep, that's a holdover from C programming, and the assumption that var comments []jira.Comment will leave comments uninitialized and unusable.
|
Thanks for making those changes. Thoughts on: #6 (comment)? |
|
@LyonesGamer one last thing: #6 (comment) |
|
Oops. Can't believe I missed that one. |
|
@squat 👍 |
squat
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just gave it one last look and I found two last things that slipped through. Once these are done, lets squash and merge.
cmd/root.go
Outdated
| if err != nil { | ||
| log.Errorf("Error updating JIRA issue %s: %v", jIssue.Key, err) | ||
| defer res.Body.Close() | ||
| body, _ := ioutil.ReadAll(res.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized this one slipped through the cracks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate it when I accidentally revert a fix I've already made... 😞
cmd/root.go
Outdated
| log.Errorf("Error creating JIRA issue: %s", err) | ||
| log.Errorf("Error creating JIRA issue: %v", err) | ||
| defer res.Body.Close() | ||
| body, _ := ioutil.ReadAll(res.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this one too
This PR adds the createComments, createComment, and updateComment functions, which add any new comments from a GitHub issue to a JIRA issue, and updates existing ones if they've been edited. The comments are created by the user running the tool; a header will be created to track metadata.
|
@squat Fixed, squashed, and ready to merge! |
|
|
This PR builds on #4, and is thus blocked on it.
This PR adds the createComments() function, which adds any new comments on a given GitHub issue to the to given JIRA issue, and updates existing issues if they've been edited on GitHub. Currently, the comment is made by the user under which the tool is running.