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

Implement parsing/handling for missing pull request webhook events for BitBucket Server (Stash) driver #130

Merged
merged 1 commit into from
Jan 12, 2022
Merged

Conversation

raphendyr
Copy link
Contributor

@raphendyr raphendyr commented Nov 23, 2021

I looked into getting PR events working with the Stash driver to support my teams current workflow. My goal was to address issues mentioned in #116, but I did find few other missing events too.

I open this PR for comments. I have tested these changes with a single user and a single repo in my local BitBucket server, so I'm not yet sure if a PR from a fork work as expected. In addition, I have not written any tests yet.

I'm particularly looking feedback on the parts of the code, which I have added GitHub comments for.

Fixes #116

@CLAassistant
Copy link

CLAassistant commented Nov 23, 2021

CLA assistant check
All committers have signed the CLA.

Base: scm.Reference{
Name: from.ToRef.DisplayID,
Path: from.ToRef.ID,
Sha: from.ToRef.LatestCommit,
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 these should be correct. However, I didn't find use for anything else except .Base.Sha in Drone server

https://github.com/harness/drone/blob/master/service/hook/parser/parse.go#L275

So, should this addition be omitted from the PR?

case "pr:from_ref_updated":
// The request includes field "previousFromHash", which could be compared
// to the FromRef.Latestcommit to ensure there is actually a change,
// but there is unlikely need for that.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should these comments be added to the git commit instead? They would be available via e.g. blame feature in Github.

ID string `json:"id"` // "refs\/heads\/master"
DisplayID string `json:"displayId"` // "master"
Type string `json:"type"` // "BRANCH"
LatestCommit string `json:"latestCommit"` // "860c4eb4ed0f969b47144234ba13c31c498cca69"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only fields . PreviousTarget and .PreviousTarget.LatestCommit are currently used. Should I remove everything else from here?

Additionally, if not, should I keep the examples of the data in comments or should I drop those?

@bradrydzewski
Copy link
Member

👋 hey there, I think in order to provide a review we would need unit tests. This is largely a data mapping exercise and the unit tests include sample webhooks (with real world data) before and after parsing, which are critical to the review process.

// action only when the target reference has changed (name or hash).
if src.PullRequest.ToRef.DisplayID == src.PreviousTarget.DisplayID &&
src.PullRequest.ToRef.LatestCommit == src.PreviousTarget.LatestCommit {
return nil, nil
Copy link
Member

Choose a reason for hiding this comment

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

I presume this is checking to differentiate between title / description change vs a code change? If this is just a title or description change we can set the action to Updated as opposed to returning a nil.

		if src.PullRequest.ToRef.DisplayID == src.PreviousTarget.DisplayID &&
		src.PullRequest.ToRef.LatestCommit == src.PreviousTarget.LatestCommit {
+			dst.Action = scm.ActionUpdate
		} else {
			dst.Action = scm.ActionSync
		}

Copy link
Contributor Author

@raphendyr raphendyr Nov 23, 2021

Choose a reason for hiding this comment

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

It is. I'll make that change. I wasn't sure what the ActionUpdate was used for.

Although, the same event can include updates to the title/description and changes to the target branch. Should that be mentioned here?

@raphendyr
Copy link
Contributor Author

@bradrydzewski Sure, I was planning to do that next, likely tomorrow. I just wanted to publish what I have done so far for others to read or comment on the things which are not specific to the implementation (i.e., what to include or where to put comments).

@raphendyr
Copy link
Contributor Author

raphendyr commented Nov 24, 2021

I added some test data for the events. I copied the json data from bitbucket server, but I replaced author info to match what was used before.

I noticed that the newer data includes links to different things. So, I think these TODOs might be fixed?

webhook.go:https:// TODO(bradrydzewski) pr hook does not include repository git+http link
webhook.go:https:// TODO(bradrydzewski) pr hook does not include repository git+ssh link
webhook.go:https:// TODO(bradrydzewski) pr hook does not include repository html link

I can add updated pr_open.json, which includes links. I saved a json data from creating a PR from a fork, so I can use that or one without the fork info. Or I can add that as another test, thus yielding one pr:open test for in repo PR and test for a PR from a fork. The pr_modified_meta.json was created from the PR from the fork, so it currently includes the different kind of data structure.

@raphendyr
Copy link
Contributor Author

raphendyr commented Nov 24, 2021

Also test coverage seems to be a bit lower for Stash than others 🤔

ok github.com/drone/go-scm/scm/driver/stash 0.019s coverage: 89.3% of statements

EDIT: untested lines are errors and unimplemented functions, so all good 🙂

@d1wilko
Copy link
Contributor

d1wilko commented Nov 30, 2021

hey @raphendyr - I see this is still marked as work in progress, please let us know when you would like us to review :)

@raphendyr raphendyr marked this pull request as ready for review November 30, 2021 15:32
@raphendyr
Copy link
Contributor Author

hey @raphendyr - I see this is still marked as work in progress, please let us know when you would like us to review :)

Sorry, my head was stuck waiting possible answers to my questions and without answers, I got stuck there.

@tphoney
Copy link
Contributor

tphoney commented Jan 11, 2022

Hey @raphendyr i have spent some time getting familiar with this PR and the change. Code wise it looks solid and all of the testing is great to have, and made reading through the code much easier.
Going on to your questions
`Only fields . PreviousTarget and .PreviousTarget.LatestCommit are currently used. Should I remove everything else from here?

Additionally, if not, should I keep the examples of the data in comments or should I drop those?`
please keep the fields.

I think these should be correct. However, I didn't find use for anything else except .Base.Sha in Drone server

https://github.com/harness/drone/blob/master/service/hook/parser/parse.go#L275

So, should this addition be omitted from the PR?

please add these. it brings it in line with the other transports.

Should these comments be added to the git commit instead? They would be available via e.g. blame feature in Github.

please leave these in comments in the file, as it aides in reading.

So leave everything as it is, but could you squash the commits.

Many thanks for your patience, we have been busy else where.

TP

@raphendyr
Copy link
Contributor Author

@tphoney Thanks for the feedback. As per your request, I'll squash the commits to a single commit.

* handle rebases to a pull request
* handle target branch changes of a pull request
* handle deletion of a pull request
@tphoney tphoney added the bug label Jan 12, 2022
@tphoney tphoney merged commit b926ba7 into drone:master Jan 12, 2022
@tphoney
Copy link
Contributor

tphoney commented Jan 12, 2022

Many thanks @raphendyr for your patience and your excellent code. 🍻

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.

Bitbucket Stash driver doesn't handle event pr:from_ref_updated (new commits / force push)
5 participants