Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pullrequest-init: Add getters to make PullRequest nil-safe. #1017

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd/pullrequest-init/fake_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func TestFakeGitHubComments(t *testing.T) {
t.Fatalf("List Issues: wanted [], got %+v, %+v, %v", got, resp, err)
}

if _, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, comment); err != nil {
comment, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, &github.IssueComment{
Copy link
Collaborator

Choose a reason for hiding this comment

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

i might just not be seeing it, but I can't find where the comment var was declared before this change :O Was this a bug in the tests? If so that would mean they aren't running in CI and there's no linting happening... 😨

Copy link
Member

Choose a reason for hiding this comment

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

😨 😨 😨 😨 😨 😨 😨 😨

Body: github.String("tacocat"),
})
if err != nil {
t.Fatalf("CreateComment: %v", err)
}

Expand Down
40 changes: 27 additions & 13 deletions cmd/pullrequest-init/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,45 @@ func (h *GitHubHandler) Upload(ctx context.Context, path string) error {
}

var merr error
if err := h.uploadLabels(ctx, pr.Labels); err != nil {
if err := h.uploadLabels(ctx, pr.GetLabels()); err != nil {
merr = multierror.Append(merr, err)
}

if err := h.uploadComments(ctx, pr.Comments); err != nil {
if err := h.uploadComments(ctx, pr.GetComments()); err != nil {
merr = multierror.Append(merr, err)
}

return merr
}

func (h *GitHubHandler) uploadLabels(ctx context.Context, labels []*Label) error {
if len(labels) == 0 {
return nil
}

labelNames := make([]string, 0, len(labels))
for _, l := range labels {
labelNames = append(labelNames, l.Text)
labelNames = append(labelNames, l.GetText())
}
h.Logger.Infof("Setting labels for PR %d to %v", h.prNum, labelNames)
_, _, err := h.Issues.ReplaceLabelsForIssue(ctx, h.owner, h.repo, h.prNum, labelNames)
return err
}

func (h *GitHubHandler) uploadComments(ctx context.Context, comments []*Comment) error {
if comments == nil {
return nil
}

h.Logger.Infof("Setting comments for PR %d to: %v", h.prNum, comments)

// Sort comments into whether they are new or existing comments (based on
// whether there is an ID defined).
existingComments := map[int64]*Comment{}
newComments := []*Comment{}
for _, c := range comments {
if c.ID != 0 {
existingComments[c.ID] = c
if c.GetID() != 0 {
existingComments[c.GetID()] = c
} else {
newComments = append(newComments, c)
}
Expand All @@ -251,9 +259,6 @@ func (h *GitHubHandler) updateExistingComments(ctx context.Context, comments map
return err
}

h.Logger.Info(existingComments)
h.Logger.Info(comments)

var merr error
for _, ec := range existingComments {
dc, ok := comments[ec.GetID()]
Expand All @@ -265,14 +270,14 @@ func (h *GitHubHandler) updateExistingComments(ctx context.Context, comments map
merr = multierror.Append(merr, err)
continue
}
} else if dc.Text != ec.GetBody() {
} else if dc.GetText() != ec.GetBody() {
Copy link
Member

Choose a reason for hiding this comment

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

If L264 dc, ok := comments[ec.GetID()] returns nil, true the comment ID is in comments but the value is nil, so we're probably in an error situation, since it shouldn't be possible in normal conditions to for the desired comment to be nil. With the nil-safe getter this condition here will be met and we will update the comment to "". I think this is not the desired behaviour.

// Update
c := &github.IssueComment{
ID: ec.ID,
Body: github.String(dc.Text),
Body: github.String(dc.GetText()),
User: ec.User,
}
h.Logger.Infof("Updating comment %d for PR %d to %s", ec.GetID(), h.prNum, dc.Text)
h.Logger.Infof("Updating comment %d for PR %d to %s", ec.GetID(), h.prNum, dc.GetText())
if _, _, err := h.Issues.EditComment(ctx, h.owner, h.repo, ec.GetID(), c); err != nil {
h.Logger.Warnf("Error editing comment: %v", err)
merr = multierror.Append(merr, err)
Expand All @@ -287,13 +292,22 @@ func (h *GitHubHandler) createNewComments(ctx context.Context, comments []*Comme
var merr error
for _, dc := range comments {
c := &github.IssueComment{
Body: github.String(dc.Text),
Body: github.String(dc.GetText()),
}
h.Logger.Infof("Creating comment %s for PR %d", dc.Text, h.prNum)
h.Logger.Infof("Creating comment %s for PR %d", dc.GetText(), h.prNum)
if _, _, err := h.Issues.CreateComment(ctx, h.owner, h.repo, h.prNum, c); err != nil {
h.Logger.Warnf("Error creating comment: %v", err)
merr = multierror.Append(merr, err)
}
}
return merr
}

func githubCommentToTekton(c *github.IssueComment, pathPrefix string) *Comment {
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, why did you place this when it's only used to build Comment for testing?

Copy link
Member

Choose a reason for hiding this comment

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

right, if it's only used for the test, let's add it in test files.

return &Comment{
ID: c.GetID(),
Author: c.GetUser().GetLogin(),
Text: c.GetBody(),
Raw: filepath.Join(pathPrefix, fmt.Sprintf("github/comments/%d.json", c.GetID())),
}
}
Loading