webhooks: Support bitbucket server push events#45909
Conversation
push events
|
Codenotify: Notifying subscribers in CODENOTIFY files for diff 9239dcd...f12ac94.
|
indradhanush
left a comment
There was a problem hiding this comment.
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!
|
|
||
| repoName, err := bitbucketServerNameFromEvent(event) | ||
| if err != nil { | ||
| return errors.Wrap(err, "handleGitLabWebhook: get name failed") |
There was a problem hiding this comment.
Also maybe?
| 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))) |
There was a problem hiding this comment.
Do we want to add a hint like this for site admins looking at the logs and getting worried?
| 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))) |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
Why URL for repository? Should this be something like "no event found"?
Suggestion in case you want to accept it:
| return "", errors.New("URL for repository not found") | |
| return "", errors.New("no event found") |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Out of curiosity: What does being close to our repo name mean here? 🤔
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Oh, nice catch. I'll add some extra test cases and make sure we handle that.
There was a problem hiding this comment.
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 { |
| 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 |
There was a problem hiding this comment.
Same comment as above for parsed.Hostname() being empty and other usages in this PR.
…er_handler.go Co-authored-by: Indradhanush Gupta <indradhanush.gupta@gmail.com>
…er_handler.go Co-authored-by: Indradhanush Gupta <indradhanush.gupta@gmail.com>
Handle Bitbucket Server webhook
pushevents.Closes https://github.com/sourcegraph/sourcegraph/issues/45764
Test plan
Unit tests added and did full end to end testing locally using
ngrok.