-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
Can one of the admins verify this patch? |
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.
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. |
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.
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.
docs/fragments/work-in-progress.adoc
Outdated
* 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. |
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 was thinking if it would make sense to write something similar to what you wrote in the issue: #27 (comment)
pkg/github/client/client.go
Outdated
@@ -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 { |
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.
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": |
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.
make it a constant in the types.go file
return "", false | ||
} | ||
|
||
func (gh *GitHubWIPPRHandler) getRepositoryChange(pullRequest *gogh.PullRequest) scm.RepositoryChange { |
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 should be placed next to the scm.RepositoryChange
struct as its constructor. And by convention, the name should start with "New"
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 is already such a function: https://github.com/arquillian/ike-prow-plugins/blob/master/pkg/github/service/repository_change.go so please use this one
log.Warnf("failed to update PR title [%q]. cause: %s", *pullRequest, err) | ||
} | ||
} | ||
return statusService.Success(ReadyForReviewMessage, ReadyForReviewDetailsPageName) |
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 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) |
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.
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) { |
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.
with the ability to use labels, we should rename the method for example to: HasWorkInProgressPrefix
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 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 |
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.
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 { |
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.
name it newStatusService
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.
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). |
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, make it same as for the failure page - the redirection to the configuration options
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 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) |
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.
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.
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.
err.Error()
is not required since the fmt
package formats an error value by calling its Error() string method.
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.
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 { |
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.
why strings.Fields
? the label should be only one and can also contain a white space - eg: work in progress
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.
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) {
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.
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}) |
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 please also verify (in all cases) the retrieved prefix - this way you verify that the find works correctly
pkg/github/client/client.go
Outdated
@@ -28,6 +29,7 @@ type Client interface { | |||
ListPullRequestLabels(change scm.RepositoryChange, prNumber int) ([]*gogh.Label, error) |
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.
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}]`) |
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 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). |
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 also a matcher checking that the title was correctly updated - removed the prefix including the flavours
… into status-update
…lugins into status-update
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.
Except for the failure and success page it looks good.
Good job @hemanik
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.
LGTM 👍 🚀 🥇
[test] |
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:
wip
label (irrespective to the title prefix) or it has awip
prefix in the title (in which case the label is automatically added).wip
prefix from the title (in which case the label is also removed automatically) or by removing thewip
label (in which case, if present, thewip
title prefix is also remove automatically).Fixes: #27