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

null check in GithubUtils #908

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
22 changes: 21 additions & 1 deletion libs/utils/src/main/java/com/akto/github/GithubUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.lang.RuntimeEnvironment;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.github.*;

import java.security.Key;
Expand Down Expand Up @@ -74,11 +75,20 @@ public static void publishGithubStatus(TestingRunResultSummary testingRunResultS
AccountSettings accountSettings = AccountSettingsDao.instance.findOne(generateFilter());
String privateKey = accountSettings.getGithubAppSecretKey();
String githubAppId = accountSettings.getGithubAppId();
if (StringUtils.isEmpty(privateKey) || StringUtils.isEmpty(githubAppId)) {//If github app is not integrated
return;
}
String jwtToken;
try {
Map<String, String> metaData = testingRunResultSummary.getMetadata();
if (metaData == null) {//No metaData is present, i.e. not a cicd test
return;
}
String repository = metaData.get("repository");
String commitSHA = metaData.get("commit_sha_head");
if (StringUtils.isEmpty(repository) || StringUtils.isEmpty(commitSHA)) {
return;
}
jwtToken = createJWT(githubAppId,privateKey, 10 * 60 * 1000);
GitHub gitHub = new GitHubBuilder().withJwtToken(jwtToken).build();
GHApp ghApp = gitHub.getApp();
Expand Down Expand Up @@ -115,11 +125,21 @@ public static void publishGithubComments(TestingRunResultSummary testingRunResul
AccountSettings accountSettings = AccountSettingsDao.instance.findOne(generateFilter());
String privateKey = accountSettings.getGithubAppSecretKey();
String githubAppId = accountSettings.getGithubAppId();
if (StringUtils.isEmpty(privateKey) || StringUtils.isEmpty(githubAppId)) {//If github app is not integrated
return;
}
try {
Map<String, String> metaData = testingRunResultSummary.getMetadata();
if (metaData == null) {//No metaData is present, i.e. not a cicd test
return;
}
String repository = metaData.get("repository");
String pullRequestId = metaData.get("pull_request_id");
String commitSHA = metaData.get("commit_sha_head");
if (StringUtils.isEmpty(repository) || StringUtils.isEmpty(pullRequestId) || StringUtils.isEmpty(commitSHA)) {
return;
}

boolean isCompleted = testingRunResultSummary.getState() == TestingRun.State.COMPLETED;
StringBuilder messageStringBuilder = new StringBuilder();
if (isCompleted) {
Expand Down Expand Up @@ -152,7 +172,7 @@ public static void publishGithubComments(TestingRunResultSummary testingRunResul
loggerMaker.infoAndAddToDb("Github app doesn't have access to repository", LoggerMaker.LogDb.TESTING);
return;
}
if (pullRequestId == null || !pullRequestId.startsWith("refs/pull/")) {
if (!pullRequestId.startsWith("refs/pull/")) {
loggerMaker.infoAndAddToDb("Pull request id not available", LoggerMaker.LogDb.TESTING);
return;
}
Expand Down
Loading