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

feat(#27): change status based on existing label #189

Merged
merged 18 commits into from
Jun 15, 2018

Conversation

hemanik
Copy link
Member

@hemanik hemanik commented Jun 12, 2018

The current WIP plugin looks for the PR title prefix to set the status. The scope of this enhanced to change PR status based on the existing label.

The new behavior handles the following cases as described below:

  • set the status to failure if PR either contains a wip label (irrespective to the title prefix) or it has a wip prefix in the title (in which case the label is automatically added).
  • set the status to success if the PR is updated by either removing the wip prefix from the title (in which case the label is also removed automatically) or by removing the wip label (in which case, if present, the wip title prefix is also remove automatically).

Fixes: #27

@centos-ci
Copy link
Collaborator

Can one of the admins verify this patch?

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

Thanks @hemanik for this PR! I'm looking forward to having it running.
My comments are inline.

@@ -1,8 +1,8 @@
==== Failed [[wip-failed]]

Your Pull Request has been rejected because the plugin detected that the PR title starts with one of the <<index#work-in-progress-config,"work in progress prefixes">>, so it seems that there is still an ongoing work.
Your Pull Request has been rejected because the plugin detected that either the PR title starts with one of the <<index#work-in-progress-config,"work in progress prefixes">> or has the "work-in-progress" label, so it seems that there is still an ongoing work.
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about this:
...starts with one of the "work in progress prefixes" or has the "work-in-progress" label (both of them are <<index#work-in-progress-config, configurable>>), so it seems that there is still an ongoing work.

* If the PR title starts with any of the default prefixes such as `WIP`, `DO NOT MERGE`, `DON'T MERGE`, `WORK-IN-PROGRESS` with combinations of `( )`, `[ ]`, `:` or any of the user configured prefixes as described below (non-case sensitive) then the plugin marks its check as **Failure**, or as **Success** otherwise.
* Or if the PR contains the default (`const:pkg/plugin/work-in-progress/configuration.go[name="DefaultLabel"]`) or any other user configured label then the plugin marks its check as **Failure**, or as **Success** otherwise.

In addition to managing the status for the PR in progress, it also manages adding/removing the GitHub label (`const:pkg/plugin/work-in-progress/configuration.go[name="DefaultLabel"]` by default or configured otherwise) and updating the PR title in the event of updating the given PR by either removing the prefix title or removing the label.
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking if it would make sense to write something similar to what you wrote in the issue: #27 (comment)

@@ -170,7 +172,17 @@ func (c *client) CreateStatus(change scm.RepositoryChange, repoStatus *gogh.Repo
return err
}

func (c client) ListPullRequestLabels(change scm.RepositoryChange, prNumber int) ([]*gogh.Label, error) {
func (c *client) EditPullRequest(change scm.RepositoryChange, prNumber int, pullRequest *gogh.PullRequest) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

both the prNumber and all information in change are available in the pullRequest instance as well - no need to provide it separately

return gh.checkTitleAndSetStatus(log, event.PullRequest)
}
switch *event.Action {
case "labeled":
Copy link
Contributor

Choose a reason for hiding this comment

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

make it a constant in the types.go file

return "", false
}

func (gh *GitHubWIPPRHandler) getRepositoryChange(pullRequest *gogh.PullRequest) scm.RepositoryChange {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be placed next to the scm.RepositoryChange struct as its constructor. And by convention, the name should start with "New"

Copy link
Contributor

Choose a reason for hiding this comment

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

log.Warnf("failed to update PR title [%q]. cause: %s", *pullRequest, err)
}
}
return statusService.Success(ReadyForReviewMessage, ReadyForReviewDetailsPageName)
Copy link
Contributor

Choose a reason for hiding this comment

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

if the label was removed, but the edit of the PR failed should we change the status to success as well? then it looks a bit inconsistent - the PR contains the prefix, but the status is green...
maybe, instead of logging the error https://github.com/arquillian/ike-prow-plugins/pull/189/files#diff-11865c15f9f479c8ca04795d06b465acR162 we should return it

func (gh *GitHubWIPPRHandler) getPullRequestLabels(log log.Logger, change scm.RepositoryChange, pullRequest *gogh.PullRequest) []*gogh.Label {
labels, err := gh.Client.ListPullRequestLabels(change, *pullRequest.Number)
if err != nil {
log.Warnf("failed to list labels on PR [%q]. cause: %s", *pullRequest, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

log it as an error.
btw, I think that you should call err.Error() to get the message

}

// IsWorkInProgress checks if title is marked as Work In Progress
func (gh *GitHubWIPPRHandler) IsWorkInProgress(title string, config PluginConfiguration) (string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

with the ability to use labels, we should rename the method for example to: HasWorkInProgressPrefix

Copy link
Contributor

Choose a reason for hiding this comment

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

And I would probably swap the return parameters - the first would be bool, then the prefix. The reason is that the main purpose of this method is checking if the title contains the prefix, the retrieved prefix is just a side effect.

for _, prefix := range prefixes {
pattern := `(?mi)^(\[|\()?` + prefix + `(\]|\))?(:| )+`
if match, _ := regexp.MatchString(pattern, title); match {
return prefix, true
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the prefix contains also brackets? are they matched and then removed as well? the same applies also to the colon.

}
}

func (gh *GitHubWIPPRHandler) getStatusService(log log.Logger, change scm.RepositoryChange) scm.StatusService {
Copy link
Contributor

Choose a reason for hiding this comment

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

name it newStatusService

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. I have a few more comments, but nothing significant...

@@ -1,6 +1,6 @@
==== Success [[wip-success]]

Your Pull Request has been approved as the plugin didn't detect any sign of work in progress (the PR title doesn't start with any of the <<index#work-in-progress-config,"work in progress prefixes">>).
Your Pull Request has been approved as the plugin didn't detect any sign of work in progress (the PR title doesn't start with any of the <<index#work-in-progress-config,"work in progress prefixes">> or contain any "work-in-progress" label).
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, make it same as for the failure page - the redirection to the configuration options

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that you have updated the failure page. 39dc7fa
I wanted to say that you should update the success page to be same as the failure page was, not the other way around. I see that my previous comment is very confusing and also grammatically wrong, so I do not wonder that you misunderstood. Sorry for that :-)
Could you please change both of them one more time? Thanks :-)

if labelUpdated {
*pullRequest.Title = strings.TrimPrefix(*pullRequest.Title, prefix)
if err := gh.Client.EditPullRequest(pullRequest); err != nil {
return fmt.Errorf("failed to update PR title [%q]. cause: %s", *pullRequest, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct me if I'm wrong - to get the nice format of the error (that contains a message of the original error) it is necessary to call err.Error() on the original error - that retrieves the message of the root cause.

Copy link
Member Author

@hemanik hemanik Jun 14, 2018

Choose a reason for hiding this comment

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

err.Error() is not required since the fmt package formats an error value by calling its Error() string method.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I wasn't sure about that - thanks for the info :-)

}
return statusService.Success(ReadyForReviewMessage, ReadyForReviewDetailsPageName)
}
if err := gh.Client.AddPullRequestLabel(change, *pullRequest.Number, strings.Fields(configuration.Label)); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

why strings.Fields? the label should be only one and can also contain a white space - eg: work in progress

Copy link
Member Author

Choose a reason for hiding this comment

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

The following function used to add label expects a string array of labels. We only have one string label and that needs to be converted to a string array and hence the choice of string.Field().

The choice of converting the string to string array is done at this location instead inside the gh.Client.AddPullRequestLabel because I wanted to keep this method unmodified considering this could be used elsewhere where we could have to send multiple label values. (Example: #57)

// AddLabelsToIssue adds labels to an issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
func (s *IssuesService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the explanation @MatousJobanek, updated it.

@@ -20,7 +20,8 @@ var _ = Describe("Work-in-progress Plugin features", func() {

DescribeTable("should recognize PR as work-in-progress if title starts with configured or default prefix",
func(title string) {
Expect(handler.IsWorkInProgress(title, wip.PluginConfiguration{Prefix: []string{`On Hold`}, Combine: true})).To(BeTrue())
state, _ := handler.HasWorkInProgressPrefix(title, wip.PluginConfiguration{Prefix: []string{`On Hold`}, Combine: true})
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 please also verify (in all cases) the retrieved prefix - this way you verify that the find works correctly

@@ -28,6 +29,7 @@ type Client interface {
ListPullRequestLabels(change scm.RepositoryChange, prNumber int) ([]*gogh.Label, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this used anywhere?

Reply(200).
BodyString(`[{"id": 934813958,` +
`"url": "https://api.github.com/repos/bartoszmajsak/wfswarm-booster-pipeline-test/labels/work-in-progress",` +
`"name": "work-in-progress", "color": "ededed", "default": false}]`)
Copy link
Contributor

Choose a reason for hiding this comment

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

this mock (and all other mocks for the labels listing) is not necessary - the information is taken from the PR


gock.New("https://api.github.com").
Patch("repos/bartoszmajsak/wfswarm-booster-pipeline-test/pulls/4").
Reply(200).
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 also a matcher checking that the title was correctly updated - removed the prefix including the flavours

MatousJobanek
MatousJobanek previously approved these changes Jun 14, 2018
Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

Except for the failure and success page it looks good.
Good job @hemanik

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

LGTM 👍 🚀 🥇

@MatousJobanek
Copy link
Contributor

[test]

@MatousJobanek MatousJobanek merged commit 43a7ffa into arquillian:master Jun 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change "WIP" status based on existing label
4 participants