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

fix(bitbucket): Add project key as identifier #1250

Merged
merged 1 commit into from
Feb 8, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ private void handleBitbucketCloudEvent(Event event, Map postedEvent) {
if (bitbucketCloudEvent.repository.owner != null) {
repoProject = StringUtils.defaultIfEmpty(bitbucketCloudEvent.repository.owner.username, "");
}
if (StringUtils.isEmpty(repoProject) && bitbucketCloudEvent.repository.project != null) {
repoProject = StringUtils.defaultIfEmpty(bitbucketCloudEvent.repository.project.key, "");
}
}
if (bitbucketCloudEvent.pullRequest != null) {
BitbucketCloudEvent.PullRequest pullRequest = bitbucketCloudEvent.pullRequest;
Expand Down Expand Up @@ -207,13 +210,20 @@ private static class Repository {
String fullName;

Owner owner;

Project project;
}

@Data
private static class Owner {
String username;
}

@Data
public static class Project {
String key;
}

@Data
private static class PullRequest {
@JsonProperty("merge_commit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,54 @@ class WebhooksControllerSpec extends Specification {
event.content.action == "pullrequest:fulfilled"
}

void "handles Bitbucket Cloud Webhook PR Event with Project key"() {
def event

given:
WebhooksController controller = new WebhooksController(mapper: EchoObjectMapper.getInstance(), scmWebhookHandler: scmWebhookHandler)
controller.propagator = Mock(EventPropagator)
controller.artifactExtractor = Mock(ArtifactExtractor)
controller.artifactExtractor.extractArtifacts(_, _, _) >> []
HttpHeaders headers = new HttpHeaders();
headers.add("X-Event-Key", "pullrequest:fulfilled")

when:
def response = controller.forwardEvent(
"git",
"bitbucket",
"""{
"repository": {
"full_name": "echo",
"owner": {
"display_name": "spinnaker"
},
"project": {
"key": "ECH"
}
},
"pullrequest": {
"merge_commit": {
"hash": "firstHash"
},
"destination": {
"branch": {"name": "master"}
}
}
}
""",headers)

then:
1 * controller.propagator.processEvent(_) >> {
event = it[0]
}

event.content.hash == "firstHash"
event.content.repoProject == "ECH"
event.content.slug == "echo"
event.content.branch == "master"
event.content.action == "pullrequest:fulfilled"
}

void "handles Bitbucket Cloud Webhook Push Event"() {
def event

Expand Down Expand Up @@ -664,4 +712,57 @@ class WebhooksControllerSpec extends Specification {
event.content.branch == "master"
event.content.action == "repo:push"
}

void "handles Bitbucket Cloud Webhook Push Event with Project key"() {
def event

given:
WebhooksController controller = new WebhooksController(mapper: EchoObjectMapper.getInstance(), scmWebhookHandler: scmWebhookHandler)
controller.propagator = Mock(EventPropagator)
controller.artifactExtractor = Mock(ArtifactExtractor)
controller.artifactExtractor.extractArtifacts(_, _, _) >> []
HttpHeaders headers = new HttpHeaders();
headers.add("X-Event-Key", "repo:push")

when:
def response = controller.forwardEvent(
"git",
"bitbucket",
"""{
"repository": {
"full_name": "echo",
"owner": {"display_name": "spinnaker"},
"project": {
"key": "ECH"
}
},
"push": {
"changes": [
{
"new": {
"type": "branch",
"name": "master"
},
"commits": [
{
"hash": "firstHash"
}
]
}
]
}
}
""",headers)

then:
1 * controller.propagator.processEvent(_) >> {
event = it[0]
}

event.content.hash == "firstHash"
event.content.repoProject == "ECH"
event.content.slug == "echo"
event.content.branch == "master"
event.content.action == "repo:push"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void canHandlePayload() throws IOException {

assertThat(event.content)
.contains(
entry("repoProject", ""),
entry("repoProject", "TES"),
entry("slug", "bitbucketfan/test_webhooks"),
entry("hash", "9384d1bf6db69ea35cda200648ade30f8ea7b1a4"),
entry("branch", "master"),
Expand Down