Skip to content

Commit 5a78598

Browse files
authored
[JENKINS-73388] Allow alternative implementation for GitHub App credentials (#796)
Encapsulate fields
1 parent 5b0c0ce commit 5a78598

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private static class CredentialsTokenProvider extends TokenProvider {
191191
private final GitHubAppCredentials credentials;
192192

193193
CredentialsTokenProvider(GitHubAppCredentials credentials) {
194-
super(credentials.appID, credentials.privateKey.getPlainText());
194+
super(credentials.getAppID(), credentials.getPrivateKey().getPlainText());
195195
this.credentials = credentials;
196196
}
197197

@@ -284,17 +284,17 @@ private static long getExpirationSeconds(GHAppInstallationToken appInstallationT
284284

285285
@NonNull
286286
String actualApiUri() {
287-
return Util.fixEmpty(apiUri) == null ? "https://api.github.com" : apiUri;
287+
return Util.fixEmpty(getApiUri()) == null ? "https://api.github.com" : getApiUri();
288288
}
289289

290290
private AppInstallationToken getToken(GitHub gitHub) {
291291
synchronized (this) {
292292
try {
293293
if (cachedToken == null || cachedToken.isStale()) {
294-
LOGGER.log(Level.FINE, "Generating App Installation Token for app ID {0}", appID);
294+
LOGGER.log(Level.FINE, "Generating App Installation Token for app ID {0}", getAppID());
295295
cachedToken = generateAppInstallationToken(
296-
gitHub, appID, privateKey.getPlainText(), actualApiUri(), owner);
297-
LOGGER.log(Level.FINER, "Retrieved GitHub App Installation Token for app ID {0}", appID);
296+
gitHub, getAppID(), getPrivateKey().getPlainText(), actualApiUri(), getOwner());
297+
LOGGER.log(Level.FINER, "Retrieved GitHub App Installation Token for app ID {0}", getAppID());
298298
}
299299
} catch (Exception e) {
300300
if (cachedToken != null && !cachedToken.isExpired()) {
@@ -304,14 +304,14 @@ private AppInstallationToken getToken(GitHub gitHub) {
304304
LOGGER.log(
305305
Level.WARNING,
306306
"Failed to generate new GitHub App Installation Token for app ID "
307-
+ appID
307+
+ getAppID()
308308
+ ": cached token is stale but has not expired",
309309
e);
310310
} else {
311311
throw e;
312312
}
313313
}
314-
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0}", appID);
314+
LOGGER.log(Level.FINEST, "Returned GitHub App Installation Token for app ID {0}", getAppID());
315315

316316
return cachedToken;
317317
}
@@ -328,7 +328,7 @@ public Secret getPassword() {
328328
@NonNull
329329
@Override
330330
public String getUsername() {
331-
return appID;
331+
return getAppID();
332332
}
333333

334334
@Override
@@ -338,9 +338,9 @@ public boolean isUsernameSecret() {
338338

339339
@NonNull
340340
public synchronized GitHubAppCredentials withOwner(@NonNull String owner) {
341-
if (this.owner != null) {
342-
if (!owner.equals(this.owner)) {
343-
throw new IllegalArgumentException("Owner mismatch: " + this.owner + " vs. " + owner);
341+
if (this.getOwner() != null) {
342+
if (!owner.equals(this.getOwner())) {
343+
throw new IllegalArgumentException("Owner mismatch: " + this.getOwner() + " vs. " + owner);
344344
}
345345
return this;
346346
}
@@ -349,17 +349,17 @@ public synchronized GitHubAppCredentials withOwner(@NonNull String owner) {
349349
}
350350
return byOwner.computeIfAbsent(owner, k -> {
351351
GitHubAppCredentials clone =
352-
new GitHubAppCredentials(getScope(), getId(), getDescription(), appID, privateKey);
353-
clone.apiUri = apiUri;
354-
clone.owner = owner;
352+
new GitHubAppCredentials(getScope(), getId(), getDescription(), getAppID(), getPrivateKey());
353+
clone.apiUri = getApiUri();
354+
clone.owner = getOwner();
355355
return clone;
356356
});
357357
}
358358

359359
@NonNull
360360
@Override
361361
public Credentials forRun(Run<?, ?> context) {
362-
if (owner != null) {
362+
if (getOwner() != null) {
363363
return this;
364364
}
365365
Job<?, ?> job = context.getParent();
@@ -523,12 +523,12 @@ private static final class DelegatingGitHubAppCredentials extends BaseStandardCr
523523
DelegatingGitHubAppCredentials(GitHubAppCredentials onMaster) {
524524
super(onMaster.getScope(), onMaster.getId(), onMaster.getDescription());
525525
JenkinsJVM.checkJenkinsJVM();
526-
appID = onMaster.appID;
526+
appID = onMaster.getAppID();
527527
JSONObject j = new JSONObject();
528528
j.put("appID", appID);
529-
j.put("privateKey", onMaster.privateKey.getPlainText());
529+
j.put("privateKey", onMaster.getPrivateKey().getPlainText());
530530
j.put("apiUri", onMaster.actualApiUri());
531-
j.put("owner", onMaster.owner);
531+
j.put("owner", onMaster.getOwner());
532532
tokenRefreshData = Secret.fromString(j.toString()).getEncryptedValue();
533533

534534
// Check token is valid before sending it to the agent.
@@ -541,7 +541,7 @@ private static final class DelegatingGitHubAppCredentials extends BaseStandardCr
541541
LOGGER.log(
542542
Level.FINEST,
543543
"Checking App Installation Token for app ID {0} before sending to agent",
544-
onMaster.appID);
544+
onMaster.getAppID());
545545
onMaster.getPassword();
546546
} catch (Exception e) {
547547
LOGGER.log(

0 commit comments

Comments
 (0)