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

webhooks: Support bitbucket server push events#45909

Merged
ryanslade merged 14 commits into
mainfrom
rs/bitbucket-server-push-event
Dec 22, 2022
Merged

webhooks: Support bitbucket server push events#45909
ryanslade merged 14 commits into
mainfrom
rs/bitbucket-server-push-event

Conversation

@ryanslade

@ryanslade ryanslade commented Dec 21, 2022

Copy link
Copy Markdown
Contributor

Handle Bitbucket Server webhook push events.

Closes https://github.com/sourcegraph/sourcegraph/issues/45764

Test plan

Unit tests added and did full end to end testing locally using ngrok.

@ryanslade ryanslade changed the title rs/bitbucket server push event webhooks: Support bitbucket server push events Dec 21, 2022
@ryanslade ryanslade requested a review from a team December 21, 2022 16:51
@ryanslade ryanslade marked this pull request as ready for review December 21, 2022 16:51
@sourcegraph-bot

sourcegraph-bot commented Dec 21, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 9239dcd...f12ac94.

Notify File(s)
@eseliger internal/extsvc/bitbucketserver/events.go
@sourcegraph/delivery doc/admin/config/webhooks.md
doc/admin/repo/webhooks.md

@indradhanush indradhanush left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Left some suggestions, feel free to ignore if you'd prefer otherwise.

Only one out of curiosity question and one point where I think we might be able to avoid bugs in the future.

Overall nothing that blocks this. LGTM!

Comment thread cmd/frontend/internal/httpapi/httpapi.go
Comment thread doc/admin/config/webhooks.md
Comment thread enterprise/cmd/frontend/internal/repos/webhooks/bitbucket_server_handler.go Outdated

repoName, err := bitbucketServerNameFromEvent(event)
if err != nil {
return errors.Wrap(err, "handleGitLabWebhook: get name failed")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also maybe?

Suggested change
return errors.Wrap(err, "handleGitLabWebhook: get name failed")
return errors.Wrap(err, "handleGitLabWebhook: failed to get event name")

if err != nil {
// Repo not existing on Sourcegraph is fine
if errcode.IsNotFound(err) {
g.logger.Warn("BitbucketServer push webhook received for unknown repo", log.String("repo", string(repoName)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want to add a hint like this for site admins looking at the logs and getting worried?

Suggested change
g.logger.Warn("BitbucketServer push webhook received for unknown repo", log.String("repo", string(repoName)))
g.logger.Warn("BitbucketServer push webhook received for unknown repo. Was the repo deleted?", log.String("repo", string(repoName)))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think putting this in docs might be better. IMHO this feels like it makes the logs a bit noisier.


func bitbucketServerNameFromEvent(event *bitbucketserver.PushEvent) (api.RepoName, error) {
if event == nil {
return "", errors.New("URL for repository not found")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why URL for repository? Should this be something like "no event found"?

Suggestion in case you want to accept it:

Suggested change
return "", errors.New("URL for repository not found")
return "", errors.New("no event found")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I made it more expicit: nil PushEvent received

return "", errors.New("URL for repository not found")
}
for _, link := range event.Repository.Links.Clone {
// The ssh link is the closest to our repo name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Out of curiosity: What does being close to our repo name mean here? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The url's in the other fields have different paths that seem to include other metadata. For example, https://bitbucket.sgdev.org/scm/private/test-2020-06-01.git which has the extra scm path section.

if err != nil {
return "", errors.Wrap(err, "unable to parse repository URL")
}
return api.RepoName(parsed.Hostname() + strings.TrimSuffix(parsed.Path, ".git")), nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is good timing. I only happened to learn yesterday that parsed.Hostname() may be an empty string if protocol is missing form the string being passed to url.Parse.

See example here: https://go.dev/play/p/nVMuAvo2v4D

Maybe we should check if parsed.Hostname() is an empty string an in that case return an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, nice catch. I'll add some extra test cases and make sure we handle that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

While looking into this I realised that we already have a function to do what we want here which will be much more robust so I'm going to submit another PR that uses it for all 3 code hosts:
https://github.com/sourcegraph/sourcegraph/blob/9239dcd9702c73e5b097a6c7962e2bd6152e1934/internal/cloneurls/clone_urls.go#L25

}

func TestBitbucketServerNameFromEvent(t *testing.T) {
mkEvent := func(name string, href string) *bitbucketserver.PushEvent {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

makeEvent? 😛

return "", errors.Wrap(err, "unable to parse repository URL")
}
return api.RepoName(parsed.Host + parsed.Path), nil
return api.RepoName(parsed.Hostname() + parsed.Path), nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same comment as above for parsed.Hostname() being empty and other usages in this PR.

@ryanslade ryanslade merged commit 3666bac into main Dec 22, 2022
@ryanslade ryanslade deleted the rs/bitbucket-server-push-event branch December 22, 2022 14:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webhooks: Handle push events for Bitbucket Server

3 participants