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

[GH-15] manage webhooks with slash commands #137

Merged
merged 14 commits into from
Jun 11, 2020
Merged

[GH-15] manage webhooks with slash commands #137

merged 14 commits into from
Jun 11, 2020

Conversation

ericjaystevens
Copy link
Contributor

@ericjaystevens ericjaystevens commented Feb 28, 2020

Summary

Add features to manage projects and group scoped GitLab webhooks.

/gitlab webhook list group[/project]
/gitlab webhook add group[/project] [url] [triggers] [token]-- if url is omitted points to plugin URL, if triggers is blank sets all, if token is omitted uses plugin's token

When a user subscribes to a new project or group, it will tell them the command to run to create a webhook, if one isn't already created for the group/project.

TODO

  • list project hooks
  • list group hooks
  • create project hooks
  • create group hooks

Ticket Link

fixes #15
fixes #156

@hanzei hanzei requested a review from manland February 28, 2020 12:05
@hanzei hanzei added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels Feb 28, 2020
@codecov
Copy link

codecov bot commented Mar 10, 2020

Codecov Report

Merging #137 into master will increase coverage by 2.51%.
The diff coverage is 70.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #137      +/-   ##
==========================================
+ Coverage   39.79%   42.31%   +2.51%     
==========================================
  Files          16       16              
  Lines        1495     1633     +138     
==========================================
+ Hits          595      691      +96     
- Misses        840      871      +31     
- Partials       60       71      +11     
Impacted Files Coverage Δ
server/api.go 0.00% <0.00%> (ø)
server/plugin.go 14.97% <56.25%> (+3.45%) ⬆️
server/command.go 40.20% <74.10%> (+23.13%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0c1eab3...3314c7b. Read the comment docs.

@hanzei hanzei added Work In Progress Not yet ready for review and removed 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels Mar 24, 2020
@hanzei hanzei removed the request for review from manland March 24, 2020 11:17
@ericjaystevens
Copy link
Contributor Author

I've made all the required upstream changes, and it was just released today https://github.com/xanzy/go-gitlab/releases/tag/v0.30.0

a little more work on my end and this should be ready to move forward.

hanzei and others added 4 commits April 10, 2020 15:26
Added user message based on Web hook status when /gitlab subscribe is used. (#130)

Message alerting the user to the need to add a webhook is functioning. Additional refactoring and tests needed.

single webhook test with mocking

refactored project hook tests

added no webhooks test

moved mock function to gitlabtest package

replaced manual mocks with auto generated

refactored plugin command handling and clarified messages

added api error test

removed mocks that are no longer needed

removed test for GetProjectHooks as it seems to implementation-specific

go format

removed shadow declaration based on vet output

broke out subscribeComand method to improve testability

tested no subscripions

paramaterized testSubscribeCommand tests

mocked KVSet

tests webhook found

moved tests out of plugin_test and into command_test

fixed linting issues

fixed Error identifier

Co-Authored-By: Michael Kochell <mjkochell@gmail.com>

fixed Error identifier

Co-Authored-By: Michael Kochell <mjkochell@gmail.com>

spelling

Co-Authored-By: Michael Kochell <mjkochell@gmail.com>

generated mock with gomock

works for all test except no Substitutions

all tests pass

changed mock boolean flag name

WIP
new webhook test hard coded

wip

added trigger default logic

creates webhook with nice output

formated project hooks properly

explicit trigers

works with explicit URL

wip

improved hook formating
tested add group hook

abstracted group and project hook options

updated subscribe messages

fixed spelling

rebased with upsream master
@ericjaystevens
Copy link
Contributor Author

feature complete, @hanzei Work in Progress can be removed.

go mod tidy

merged go.sum conlict

go tidy
@hanzei hanzei requested a review from iomodo April 10, 2020 19:08
@hanzei hanzei added 2: Dev Review Requires review by a core committer and removed Work In Progress Not yet ready for review labels Apr 10, 2020
@hanzei
Copy link
Collaborator

hanzei commented Apr 10, 2020

@ericjaystevens Heads up that there is a merge conflict to resolve

@hanzei hanzei changed the title WIP: [GH-15] manage webhooks with slash commands [GH-15] manage webhooks with slash commands Apr 13, 2020
Copy link
Contributor

@iomodo iomodo left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks for the contribution @ericjaystevens, great work!
One question below.

fullPath := strings.Split(namespace, "/")

siteURL := *p.API.GetConfig().ServiceSettings.SiteURL
urlPath := fmt.Sprintf("%v/%s", siteURL, inboundWebhookURL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we add the port as well in case it is not :80?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good Thinking, I use port 8065 in my test environment. But I have the port number included in my site URL.

Looking at the System Console under site URL I see,

The URL that users will use to access Mattermost. Standard ports, such as 80 and 443, can be omitted, but non-standard ports are required. For example: http://example.com:8065. This setting is required.

So I think it might be safe to assume the port will be included in the Site URL. But if that's not a safe assumption, it should be an easy check.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are absolutely right. One case worth checking might be siteURL being empty, because it's a default. I'd suggest using

"localhost" + *p.API.GetConfig().ServiceSettings.ListenAddress

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 think it's a good idea to check siteURL for empty, I'll add that.

I'm pretty sure other things won't work (like subscribe and activate) if siteURL is not set. But if gets unset webhook creation would set webhooks to point to just the inboundWebhookURL, which would be unexpected.

I'm not sure computing the URL is practical or desired, they could be behind a load balancer or proxy and we wouldn't know their external IP/FQDN.

How about I just add a check, and if it is empty I fail the webhook creation with a message like

Unable to create webhook. The Mattermot Site URL is not set. Set it in the admin console or rerun /gitlab webhook add group/project URL including the desired URL.

Copy link
Contributor

@iomodo iomodo left a comment

Choose a reason for hiding this comment

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

Thanks @ericjaystevens, looks good to me!

@hanzei
Copy link
Collaborator

hanzei commented May 7, 2020

/update-branch

Copy link
Collaborator

@hanzei hanzei left a comment

Choose a reason for hiding this comment

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

Awesome work 🎉

I leaved a few suggestions

* PipelineEvents
* WikiPageEvents
* SSLverification
* |url| is the URL that will be called when triggered. Defaults to this plugins URL
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is a use case for setting this not to the plugin URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

creating a webhook for your CI/CD tooling or another chat or notification service

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate a bit? Is the exact use case that a user wan't to create a webhook to an different service inside of Mattermost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, An example user story would be a developer creates a new GitLab repo, and wants to get it added to the team's Jenkins server. So the developer messages the Jenkins administrator to get the token and Jenkins URL for the webhook. Then the developer runs the Mattermost Gitlab Plugin command and creates the webhook to Jenkins.

server/command.go Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/command.go Show resolved Hide resolved
server/gitlab/api.go Outdated Show resolved Hide resolved
server/gitlab/gitlab.go Outdated Show resolved Hide resolved
server/plugin.go Outdated Show resolved Hide resolved
server/plugin.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@hanzei hanzei left a comment

Choose a reason for hiding this comment

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

Great work 🎉

server/command.go Outdated Show resolved Hide resolved
@hanzei hanzei added 3: QA Review Requires review by a QA tester and removed 2: Dev Review Requires review by a core committer labels May 9, 2020
@hanzei hanzei requested a review from DHaussermann May 9, 2020 00:27
@hanzei hanzei added this to the v1.2.0 milestone May 9, 2020
@hanzei hanzei mentioned this pull request May 9, 2020
use ! instead of == false

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
@hanzei
Copy link
Collaborator

hanzei commented Jun 3, 2020

@iomodo Do you have a local GitLab instance setup to test these changes?

@iomodo
Copy link
Contributor

iomodo commented Jun 3, 2020

@hanzei it's on my todo list.

Copy link
Contributor

@iomodo iomodo left a comment

Choose a reason for hiding this comment

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

Started testing this PR on a local instance. Found a bug I missed in the review.

client := internGitlab.NewOAuthClient(tc, token.AccessToken)
if err := client.SetBaseURL(g.enterpriseBaseURL); err != nil {
return nil, fmt.Errorf("can't set base url to GitLab client lib: %w", err)
client, err := internGitlab.NewOAuthClient(token.AccessToken)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not setting base url will make requests go to the default gitlab.com URL.
What was the reason for changing gitlabConnect method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are absolutely right. The reason for the modifications to gitlabConnect was because of changes in go-gitlab. The types of arguments for NewOAuthClient changed, and SetBaseURL became a private function. I must have changed it to get this feature to compile and the forgot that the gitlabConnect method needs a re-write.

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 just pushed a change and tested it with Gitlab.com. Here is the link to the breaking change commit that I updated gitlabConnect to address. xanzy/go-gitlab#826

Copy link
Contributor

@iomodo iomodo left a comment

Choose a reason for hiding this comment

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

Tested locally, works as expected. Could not test groups though.
Great job @ericjaystevens !

@hanzei
Copy link
Collaborator

hanzei commented Jun 11, 2020

@DHaussermann ☝️

Copy link

@DHaussermann DHaussermann 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 testing @iomodo. I have added this to release testing so it is included in end to end testing in the next release.
LGTM!
Huge thanks @ericjaystevens for this contribution.

@DHaussermann DHaussermann added 4: Reviews Complete All reviewers have approved the pull request and removed 3: QA Review Requires review by a QA tester labels Jun 11, 2020
@hanzei hanzei merged commit e5d1e89 into mattermost:master Jun 11, 2020
@iomodo iomodo mentioned this pull request Jul 29, 2020
@bbodenmiller
Copy link
Contributor

Looks like https://mattermost.gitbook.io/plugin-gitlab/setup/configuration needs to be updated following this change.

@hanzei hanzei added the Docs/Needed Requires documentation label Feb 19, 2021
@hanzei
Copy link
Collaborator

hanzei commented Feb 19, 2021

@ericjaystevens Would you be open on updating the documentation?

@hanzei
Copy link
Collaborator

hanzei commented Feb 19, 2021

@bbodenmiller Thanks for the hint 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4: Reviews Complete All reviewers have approved the pull request Docs/Needed Requires documentation
Projects
None yet
6 participants