Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Conversation

@MorganEPatch
Copy link
Contributor

@MorganEPatch MorganEPatch commented Jul 11, 2017

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.

@MorganEPatch MorganEPatch self-assigned this Jul 11, 2017
@MorganEPatch MorganEPatch mentioned this pull request Jul 17, 2017
Copy link
Contributor

@squat squat left a 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"), "/")
Copy link
Contributor

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
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor

@squat squat left a 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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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() != "" {
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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"

Copy link
Contributor

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"

Copy link
Contributor

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
Copy link
Contributor

@squat squat Jul 20, 2017

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

Copy link
Contributor Author

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.

@squat
Copy link
Contributor

squat commented Jul 20, 2017

Thanks for making those changes. Thoughts on: #6 (comment)?

@squat
Copy link
Contributor

squat commented Jul 20, 2017

@LyonesGamer one last thing: #6 (comment)

@MorganEPatch
Copy link
Contributor Author

Oops. Can't believe I missed that one.

@MorganEPatch
Copy link
Contributor Author

@squat 👍

Copy link
Contributor

@squat squat left a 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)
Copy link
Contributor

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

Copy link
Contributor Author

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)
Copy link
Contributor

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.
@MorganEPatch
Copy link
Contributor Author

@squat Fixed, squashed, and ready to merge!

@squat
Copy link
Contributor

squat commented Jul 20, 2017

:shipit:

@MorganEPatch MorganEPatch merged commit 99678ca into coreos:master Jul 20, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants