Skip to content

Commit

Permalink
Refactor hardcoded values and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hanelliotphan committed Aug 19, 2024
1 parent b3261ce commit 463a753
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public GithubAppCheck(final Namespace ns){
* Method to verify whether the github app is installed on a repository or not.
* @param fullRepoName = The repository full name, i.e, of the format "owner/repoName". Eg: "Salesforce/dockerfile-image-update"
* @return True if github app is installed, false otherwise.
* Reference: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app
*/
protected boolean isGithubAppEnabledOnRepository(String fullRepoName) {
refreshJwtIfNeeded(appId, privateKeyPath);
try {
// Return true if the app is found on the repository via JWT token and API call
// Reference: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app
gitHub.getApp().getInstallationByRepository(fullRepoName.split("/")[0], fullRepoName.split("/")[1]);
String org = fullRepoName.split("/")[0];
String repo = fullRepoName.split("/")[1];
gitHub.getApp().getInstallationByRepository(org, repo);
return true;
} catch (HttpException exception) {
if (exception.getResponseCode() != 404) {
Expand All @@ -93,7 +94,6 @@ protected boolean isGithubAppEnabledOnRepository(String fullRepoName) {
private void refreshJwtIfNeeded(String appId, String privateKeyPath) {
if (jwt == null || jwtExpiry.isBefore(Instant.now().minusSeconds(jwtRefreshBuffer))) { // Adding a buffer to ensure token validity
try {
// Generate JWT token 60 seconds before the expiry to continue Github app check
generateJWT(appId, privateKeyPath);
} catch (IOException | GeneralSecurityException exception) {
log.warn("Could not refresh the JWT due to exception: {}", exception.getMessage());
Expand Down

0 comments on commit 463a753

Please sign in to comment.