Skip to content

Commit

Permalink
chore: Extend the list of possible connections to GitHub (#640)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Oct 21, 2024
1 parent 84b9ba1 commit 26d4ae7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/lpvs/controller/GitHubController.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(
public ResponseEntity<LPVSResponseWrapper> gitHubSingleScan(
@PathVariable("gitHubOrg") @NotEmpty @Valid String gitHubOrg,
@PathVariable("gitHubRepo") @NotEmpty @Valid String gitHubRepo,
@PathVariable("prNumber") @Min(1) @Valid Integer prNumber)
throws InterruptedException, IOException {
@PathVariable("prNumber") @Min(1) @Valid Integer prNumber) {
log.debug("New GitHub single scan request received");

if (GITHUB_SECRET.trim().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public enum LPVSPullRequestAction {
/**
* Represents the action of triggering a scan of a pull request by automation bot.
*/
BOT_SCAN("bot-scan");
BOT_SCAN("bot-scan"),

/**
* Represents the action of triggering a scan of a repository branch.
*/
REPO_SCAN("repo-scan");

/**
* The string representation of the pull request action.
Expand Down Expand Up @@ -91,6 +96,8 @@ public static LPVSPullRequestAction convertFrom(String action) {
return SINGLE_SCAN;
} else if (action.equals(BOT_SCAN.getPullRequestAction())) {
return BOT_SCAN;
} else if (action.equals(REPO_SCAN.getPullRequestAction())) {
return REPO_SCAN;
} else {
return null;
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/lpvs/service/LPVSGitHubConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ public GitHub connectToGitHubApi() throws IOException {
return gH;
}

/**
* Connects to the GitHub API based on the configured authentication token and API URL with custom token.
*
* @param token GitHub custom authentication token.
* @return GitHub instance for interacting with the GitHub API.
* @throws IOException if an error occurs during the GitHub connection.
*/
public GitHub connectToEnterpriseApiWithCustomToken(String token) throws IOException {
GitHub gH;
if (StringUtils.isBlank(token)) {
gH = connectToGitHubApi();
} else {
gH = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, token);
}
return gH;
}

/**
* Sets the GitHub authentication token from the environment variable if available.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void testConvertFrom() {
LPVSPullRequestAction.convertFrom("single-scan"),
LPVSPullRequestAction.SINGLE_SCAN);
assertEquals(LPVSPullRequestAction.convertFrom("bot-scan"), LPVSPullRequestAction.BOT_SCAN);
assertEquals(
LPVSPullRequestAction.convertFrom("repo-scan"), LPVSPullRequestAction.REPO_SCAN);

assertNotEquals(
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.OPEN);
Expand All @@ -40,6 +42,8 @@ public void testConvertFrom() {
LPVSPullRequestAction.SINGLE_SCAN);
assertNotEquals(
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.BOT_SCAN);
assertNotEquals(
LPVSPullRequestAction.convertFrom("random_name"), LPVSPullRequestAction.REPO_SCAN);

assertNull(LPVSPullRequestAction.convertFrom("random_name"));
}
Expand All @@ -53,5 +57,6 @@ public void testGetPullRequestAction() {
assertEquals(LPVSPullRequestAction.RESCAN.getPullRequestAction(), "rescan");
assertEquals(LPVSPullRequestAction.SINGLE_SCAN.getPullRequestAction(), "single-scan");
assertEquals(LPVSPullRequestAction.BOT_SCAN.getPullRequestAction(), "bot-scan");
assertEquals(LPVSPullRequestAction.REPO_SCAN.getPullRequestAction(), "repo-scan");
}
}

0 comments on commit 26d4ae7

Please sign in to comment.