Skip to content

Commit

Permalink
Merge pull request #323 from checkmarx-ltd/develop
Browse files Browse the repository at this point in the history
Handle race condition when creating a project
  • Loading branch information
satyamchaurasiapersistent authored May 25, 2023
2 parents ba01163 + 058d885 commit 693dac9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.github.checkmarx-ltd</groupId>
<artifactId>cx-spring-boot-sdk</artifactId>
<version>0.5.49</version>
<version>0.5.50</version>
<name>cx-spring-boot-sdk</name>
<description>Checkmarx Java Spring Boot SDK</description>
<properties>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/checkmarx/sdk/service/CxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class CxService implements CxClient {
private static final Integer SCAN_STATUS_NONE = 1001;

private static final Integer MESSAGE_CODE_BRANCH_NOT_FOUND = 49805;
private static final Integer MESSAGE_CODE_PROJECT_NAME_ALREADY_EXISTS = 12108;
private static final Integer BRANCHING_STATUS_COMPLETED = 2;
private static final Integer BRANCHING_STATUS_FAILED = 4;

Expand Down Expand Up @@ -1152,6 +1153,28 @@ public Integer createProject(String ownerId, String name) {
String id = obj.get("id").toString();
return Integer.parseInt(id);
} catch (HttpStatusCodeException e) {
if (e.getStatusCode() == HttpStatus.BAD_REQUEST) {
String responseBody = e.getResponseBodyAsString();
try {
JSONObject obj = new JSONObject(responseBody);
int messageCode = obj.optInt("messageCode");
if (messageCode == MESSAGE_CODE_PROJECT_NAME_ALREADY_EXISTS) {
log.debug("Project with name \"{}\" already exists", name);
Integer projectId = getProjectId(ownerId, name);
if (!projectId.equals(UNKNOWN_INT)) {
log.debug("Existing project has projectId {}", projectId);
return projectId;
} else {
log.error("Cannot determine projectId of existing project");
}
} else {
String messageDetails = obj.optString("messageDetails");
log.debug("{}: failure not due to existing project with same name (messageDetails: {})", messageCode, messageDetails);
}
} catch (JSONException je) {
log.error("Response payload is not JSON.");
}
}
log.error("HTTP error code {} while creating project with name {} under owner id {}", e.getStatusCode(), name, ownerId);
log.error(ExceptionUtils.getStackTrace(e));
} catch (JSONException e) {
Expand Down

0 comments on commit 693dac9

Please sign in to comment.