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

add pr:reviewer:approved as a valid event for Bitbucket Server #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/hook/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func transformPullRequestEvent(pullRequest PullRequestEventModel) hookCommon.Tra
}

func isAcceptEventType(eventKey string) bool {
return slices.Contains([]string{"repo:refs_changed", "pr:opened", "pr:modified", "pr:merged", "diagnostics:ping", "pr:from_ref_updated"}, eventKey)
return slices.Contains([]string{"repo:refs_changed", "pr:opened", "pr:modified", "pr:merged", "diagnostics:ping", "pr:from_ref_updated", "pr:reviewer:approved"}, eventKey)
}

// TransformRequest ...
Expand Down Expand Up @@ -301,7 +301,7 @@ func (hp HookProvider) TransformRequest(r *http.Request) hookCommon.TransformRes
return transformPushEvent(pushEvent)
}

if eventKey == "pr:opened" || eventKey == "pr:modified" || eventKey == "pr:merged" || eventKey == "pr:from_ref_updated" {
if eventKey == "pr:opened" || eventKey == "pr:modified" || eventKey == "pr:merged" || eventKey == "pr:from_ref_updated" || eventKey == "pr:reviewer:approved" {
var pullRequestEvent PullRequestEventModel
if err := json.NewDecoder(r.Body).Decode(&pullRequestEvent); err != nil {
return hookCommon.TransformResultModel{
Expand Down
186 changes: 185 additions & 1 deletion service/hook/bitbucketserver/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,163 @@ const (
}
}`

samplePullRequestApprovedData = `{
"date": "2024-08-08T14:58:09+0200",
"actor": {
"emailAddress": "[email protected]",
"displayName": "User One",
"name": "user1",
"active": true,
"links": {"self": [{"href": "https://stash-ui.example.com/users/user1"}]},
"id": 1,
"type": "NORMAL",
"slug": "user1"
},
"eventKey": "pr:reviewer:approved",
"pullRequest": {
"author": {
"approved": false,
"role": "AUTHOR",
"user": {
"emailAddress": "[email protected]",
"displayName": "User Two",
"name": "user2",
"active": true,
"links": {"self": [{"href": "https://stash-ui.example.com/users/user2"}]},
"id": 2,
"type": "NORMAL",
"slug": "user2"
},
"status": "UNAPPROVED"
},
"description": "Approved!",
"updatedDate": 1723045050014,
"title": "fix error",
"version": 4,
"reviewers": [{
"approved": true,
"role": "REVIEWER",
"user": {
"emailAddress": "[email protected]",
"displayName": "User One",
"name": "user1",
"active": true,
"links": {"self": [{"href": "https://stash-ui.example.com/users/user1"}]},
"id": 1,
"type": "NORMAL",
"slug": "user1"
},
"lastReviewedCommit": "836204e0cbf06ce4ebfb878be21fb383d8c12e62",
"status": "APPROVED"
}],
"toRef": {
"latestCommit": "4755079fa762299a4d99512ca18703db80d42a35",
"id": "refs/heads/main-branch",
"displayId": "main-branch",
"type": "BRANCH",
"repository": {
"archived": false,
"public": false,
"hierarchyId": "7087f8beac6eb0d81cd8",
"name": "app-repo",
"forkable": true,
"project": {
"public": false,
"name": "Project - Mobile",
"description": "Internal mobile repository",
"links": {"self": [{"href": "https://stash-ui.example.com/projects/PROJ"}]},
"id": 1,
"type": "NORMAL",
"key": "PROJ"
},
"links": {
"clone": [
{
"name": "http",
"href": "https://stash-ui.example.com/scm/proj/app-repo.git"
},
{
"name": "ssh",
"href": "ssh://[email protected]:7999/proj/app-repo.git"
}
],
"self": [{"href": "https://stash-ui.example.com/projects/PROJ/repos/app-repo/browse"}]
},
"id": 84,
"scmId": "git",
"state": "AVAILABLE",
"slug": "app-repo",
"statusMessage": "Available"
}
},
"createdDate": 1723023559286,
"closed": false,
"fromRef": {
"latestCommit": "836204e0cbf06ce4ebfb878be21fb383d8c12e62",
"id": "refs/heads/test-fix-error",
"displayId": "test-fix-error",
"type": "BRANCH",
"repository": {
"archived": false,
"public": false,
"hierarchyId": "7087f8beac6eb0d81cd8",
"name": "app-repo",
"forkable": true,
"project": {
"public": false,
"name": "Project - Mobile",
"description": "Internal mobile repository",
"links": {"self": [{"href": "https://stash-ui.example.com/projects/PROJ"}]},
"id": 1,
"type": "NORMAL",
"key": "PROJ"
},
"links": {
"clone": [
{
"name": "http",
"href": "https://stash-ui.example.com/scm/proj/app-repo.git"
},
{
"name": "ssh",
"href": "ssh://[email protected]:7999/proj/app-repo.git"
}
],
"self": [{"href": "https://stash-ui.example.com/projects/PROJ/repos/app-repo/browse"}]
},
"id": 84,
"scmId": "git",
"state": "AVAILABLE",
"slug": "app-repo",
"statusMessage": "Available"
}
},
"links": {"self": [{"href": "https://stash-ui.example.com/projects/PROJ/repos/app-repo/pull-requests/1"}]},
"id": 1,
"state": "OPEN",
"locked": false,
"open": true,
"participants": []
},
"participant": {
"approved": true,
"role": "REVIEWER",
"user": {
"emailAddress": "[email protected]",
"displayName": "User One",
"name": "user1",
"active": true,
"links": {"self": [{"href": "https://stash-ui.example.com/users/user1"}]},
"id": 1,
"type": "NORMAL",
"slug": "user1"
},
"lastReviewedCommit": "836204e0cbf06ce4ebfb878be21fb383d8c12e62",
"status": "APPROVED"
},
"previousStatus": "NEEDS_WORK"
}`

samplePingData = `{
"test": true
}`
Expand Down Expand Up @@ -1193,7 +1350,7 @@ func Test_isAcceptEventType(t *testing.T) {
{
for _, anAction := range []string{"",
"a", "not-an-action",
"repo:forked", "repo:modified", "repo:comment:added", "repo:comment:edited", "repo:comment:deleted", "pr:reviewer:approved",
"repo:forked", "repo:modified", "repo:comment:added", "repo:comment:edited", "repo:comment:deleted",
"pr:reviewer:unapproved", "pr:reviewer:needs_work", "pr:declined", "pr:deleted",
"pr:comment:added", "pr:comment:updated", "pr:comment:deleted",
} {
Expand Down Expand Up @@ -1401,6 +1558,33 @@ func Test_HookProvider_TransformRequest(t *testing.T) {
require.Equal(t, false, hookTransformResult.DontWaitForTriggerResponse)
}

t.Log("Test with Sample Pull Request approved Data")
{
request := http.Request{
Header: http.Header{
"X-Event-Key": {"pr:reviewer:approved"},
"Content-Type": {"application/json; charset=utf-8"},
},
Body: ioutil.NopCloser(strings.NewReader(samplePullRequestApprovedData)),
}
hookTransformResult := provider.TransformRequest(&request)
require.NoError(t, hookTransformResult.Error)
require.False(t, hookTransformResult.ShouldSkip)
require.Equal(t, []bitriseapi.TriggerAPIParamsModel{
{
BuildParams: bitriseapi.BuildParamsModel{
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: with the current implementation the approved event will call the Bitrise Build Trigger API with the same parameters as the regular push/pull-request events.

This means that the Trigger API will have no information (no parameter passed) indicating that this was an approve event, and so no filtering can be applied on it.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @viktorbenei,

I am aware that this will indeed be the case. From my perspective, the pr:approved event is also a pull-request event coming from Bitbucket Server so I presume we can consider this as equal in Bitrise.

If this is blocking for this PR, I can try to have another look at it, but some directions would come in handy as I'm not too familiar with both Go as this codebase.

Choose a reason for hiding this comment

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

Any updates on this?

CommitHash: "836204e0cbf06ce4ebfb878be21fb383d8c12e62",
CommitMessage: "fix error",
Branch: "test-fix-error",
BranchDest: "main-branch",
PullRequestID: &intOne,
},
TriggeredBy: "webhook-bitbucket-server/user1",
},
}, hookTransformResult.TriggerAPIParams)
require.Equal(t, false, hookTransformResult.DontWaitForTriggerResponse)
}

t.Log("Test with Sample Pull Request merged data")
{
request := http.Request{
Expand Down