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

chore: Extend the list of possible connections to GitHub #640

Merged
merged 1 commit into from
Oct 21, 2024
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
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");
}
}