Skip to content

Commit

Permalink
Fix GitHub app information getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Aug 11, 2024
1 parent 5f7625d commit 1509014
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void run(GitHub gh, GHPullRequest pr, Configuration.PRActions acti
}
git.add().addFilepattern(".").call();

var botName = gitHub.getApp().getSlug() + "[bot]";
var botName = GitHubAccessor.getApp(gitHub).getSlug() + "[bot]";
var user = gitHub.getUser(botName);
var creds = new UsernamePasswordCredentialsProvider(
botName,
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/net/neoforged/automation/util/AuthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ public static byte[] parsePKCS8(String input) throws IOException {
* @param owner the application installation owner
* @return the authorization provider
*/
public static AuthorizationProvider githubApp(String appId, byte[] key, ThrowingFunction<GHApp, GHAppInstallation> owner) throws NoSuchAlgorithmException, InvalidKeySpecException {
public static AppBasedAuthProvider githubApp(String appId, byte[] key, ThrowingFunction<GHApp, GHAppInstallation> owner) throws NoSuchAlgorithmException, InvalidKeySpecException {
final PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(key));
return new AuthorizationProvider() {
return new AppBasedAuthProvider() {
@Override
public String getEncodedAuthorization() throws IOException {
return "Bearer " + jwt();
}

@Override
public GitHub getApp() throws IOException {
return new GitHubBuilder()
.withJwtToken(refreshJWT(appId, privateKey))
.build();
}

private Jwt jwt = null;

public String jwt() throws IOException {
Expand All @@ -85,9 +92,7 @@ public String jwt() throws IOException {
}

private Jwt createJwt() throws IOException {
final GitHub gitHub = new GitHubBuilder()
.withJwtToken(refreshJWT(appId, privateKey))
.build();
final GitHub gitHub = getApp();

final GHAppInstallationToken token = owner.apply(gitHub.getApp()).createToken().create();
return new Jwt(token.getExpiresAt().toInstant(), token.getToken());
Expand All @@ -113,4 +118,8 @@ public record Jwt(Instant expirationDate, String jwt) {
public interface ThrowingFunction<T, R> {
R apply(T t) throws IOException;
}

public interface AppBasedAuthProvider extends AuthorizationProvider {
GitHub getApp() throws IOException;
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/kohsuke/github/GitHubAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,14 @@ public static Set<String> getExistingLabels(GHRepository repository) throws IOEx
EXISTING_LABELS.put(repository, ex);
return ex;
}

public static GHApp getApp(GitHub owner) throws IOException {
try {
var field = GitHubClient.class.getDeclaredField("authorizationProvider");
field.setAccessible(true);
return ((AuthUtil.AppBasedAuthProvider) field.get(owner.getClient())).getApp().getApp();
} catch (NoSuchFieldException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}

0 comments on commit 1509014

Please sign in to comment.