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

Pr bitbucket delete event support #932

Merged
merged 2 commits into from
Feb 16, 2022
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
11 changes: 9 additions & 2 deletions docs/Delete-Branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This will make CxFlow to always read configuration-as-code from repository defau
* [GitHub](#github)
* [Azure Devops](#ado)
* [GitLab](#gitlab)
* [BitBucket] (#bitbucket)
* [BitBucket](#bitbucket)

#### <a name="github">GitHub</a>

Expand Down Expand Up @@ -71,7 +71,14 @@ GitLab does not support webhook delete events therefore CxFlow does not support

#### <a name="bitbucket">Bitbucket Server</a>

Bitbucket Server will delete a SAST project **only when using the Post Webhooks plugin**. The current implementation is limited in that:
* Uses the webhook PUSH event
* When an unprotected branch is deleted BitBucket server sends a PUSH event of type DELETE.

Bitbucket Server will delete a SAST project either using the PUSH webhook event or using the Post Webhooks plugin. The current implementation is limited in that:

* Project delete not work if using Config-As-Code given the settings for team and/or project name have been deleted from the branch.
* Project delete will work if the project name is calculated or scripted and the team assigned to the project matches the default team in the CxFlow YAML configuration.

**Bitbucket Cloud**

Bitbucket cloud currently does not support deleting project in CxSAST when unprotected branch is deleted.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.checkmarx.flow.dto.bitbucketserver.PushEvent;
import com.checkmarx.flow.exception.InvalidTokenException;
import com.checkmarx.flow.exception.MachinaRuntimeException;
import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerDeleteHandler;
import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler;
import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerMergeHandler;
import com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerPushHandler;
Expand Down Expand Up @@ -201,6 +202,11 @@ public ResponseEntity<EventResponse> pushRequest(
} catch (IOException e) {
throw new MachinaRuntimeException(e);
}

if(event.getChanges().get(0).getType().equalsIgnoreCase("DELETE")){
log.info("Push event is associated with a Delete branch event...ignoring request");
return handleDeleteEvent(body,uid,event,signature,product,controllerRequest);
}

String application = event.getRepository().getName();

Expand Down Expand Up @@ -244,6 +250,38 @@ private void verifyHmacSignature(String message, String signature) {
log.info("Signature verified");
}

public ResponseEntity<EventResponse> handleDeleteEvent(String body, String uid, PushEvent event, String signature, String product, ControllerRequest controllerRequest){
log.info("Processing BitBucket DELETE branch request");
if(flowProperties == null){
log.error("Properties have null values");
throw new MachinaRuntimeException();
}

verifyHmacSignature(body, signature);

String application = event.getRepository().getName();
if(!ScanUtils.empty(controllerRequest.getApplication())){
application = controllerRequest.getApplication();
}

if(ScanUtils.empty(product)){
product = ScanRequest.Product.CX.getProduct();
}

BitbucketServerEventHandler handler = BitbucketServerDeleteHandler.builder()
.controllerRequest(controllerRequest)
.branchNameForDelete(event.getChanges().get(INDEX_FROM_CHANGES).getRefId())
.fromProjectKey(event.getRepository().getProject().getKey())
.repositoryName(event.getRepository().getName())
.product(product)
.application(application)
.webhookPayload(body)
.configProvider(this)
.build();

return handler.execute(uid);
}

public FlowProperties getFlowProperties() {
return flowProperties;
}
Expand Down