create() {
"Connecting to %s to obtain list of collaborators for %s/%s%n",
apiUri, repoOwner, repository);
StandardCredentials credentials =
- Connector.lookupScanCredentials((Item) getOwner(), apiUri, credentialsId);
+ Connector.lookupScanCredentials((Item) getOwner(), apiUri, credentialsId, repoOwner);
// Github client and validation
try {
GitHub github = Connector.connect(apiUri, credentials);
@@ -2986,7 +2990,7 @@ public GHPermissionType fetch(String username) throws IOException, InterruptedEx
"Connecting to %s to check permissions of obtain list of %s for %s/%s%n",
apiUri, username, repoOwner, repository);
StandardCredentials credentials =
- Connector.lookupScanCredentials((Item) getOwner(), apiUri, credentialsId);
+ Connector.lookupScanCredentials((Item) getOwner(), apiUri, credentialsId, repoOwner);
github = Connector.connect(apiUri, credentials);
String fullName = repoOwner + "/" + repository;
repo = github.getRepository(fullName);
diff --git a/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials/help-owner.html b/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials/help-owner.html
index 2097e42f0..86a279f3f 100644
--- a/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials/help-owner.html
+++ b/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials/help-owner.html
@@ -1,3 +1,6 @@
- The organisation or user that this app is to be used for. Only required if this app is installed to multiple organisations.
+ The organisation or user that this app is to be used for.
+ Only required if this app is installed to multiple organisations.
+ May be omitted in case credentials are used from GitHub multibranch projects
+ (in that case the account is determined from the branch source where the credentials are used).
diff --git a/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource/config-detail.jelly b/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource/config-detail.jelly
index ad84a0a01..5af7baa48 100644
--- a/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource/config-detail.jelly
+++ b/src/main/resources/org/jenkinsci/plugins/github_branch_source/GitHubSCMSource/config-detail.jelly
@@ -17,7 +17,7 @@
-
+
diff --git a/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java b/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java
index 814b738dc..a58cb6890 100644
--- a/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java
+++ b/src/test/java/org/jenkinsci/plugins/github_branch_source/GithubAppCredentialsAppInstallationTokenTest.java
@@ -1,11 +1,16 @@
package org.jenkinsci.plugins.github_branch_source;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
+import static org.hamcrest.Matchers.is;
import hudson.util.Secret;
+import java.math.BigDecimal;
import java.time.Duration;
import java.time.Instant;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.hamcrest.number.BigDecimalCloseTo;
import org.junit.Test;
public class GithubAppCredentialsAppInstallationTokenTest {
@@ -22,7 +27,7 @@ public void testAppInstallationTokenStale() throws Exception {
assertThat(token.isStale(), is(false));
assertThat(
token.getTokenStaleEpochSeconds(),
- equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
+ closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
now = Instant.now().getEpochSecond();
token =
@@ -31,7 +36,7 @@ public void testAppInstallationTokenStale() throws Exception {
assertThat(token.isStale(), is(false));
assertThat(
token.getTokenStaleEpochSeconds(),
- equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
+ closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
now = Instant.now().getEpochSecond();
token =
@@ -41,7 +46,7 @@ public void testAppInstallationTokenStale() throws Exception {
assertThat(token.isStale(), is(false));
assertThat(
token.getTokenStaleEpochSeconds(),
- equalTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS));
+ closeTo(now + GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS, 3));
now = Instant.now().getEpochSecond();
token =
@@ -52,7 +57,7 @@ public void testAppInstallationTokenStale() throws Exception {
+ Duration.ofMinutes(7).getSeconds());
assertThat(token.isStale(), is(false));
assertThat(
- token.getTokenStaleEpochSeconds(), equalTo(now + Duration.ofMinutes(7).getSeconds()));
+ token.getTokenStaleEpochSeconds(), closeTo(now + Duration.ofMinutes(7).getSeconds(), 3));
now = Instant.now().getEpochSecond();
token =
@@ -61,8 +66,9 @@ public void testAppInstallationTokenStale() throws Exception {
assertThat(token.isStale(), is(false));
assertThat(
token.getTokenStaleEpochSeconds(),
- equalTo(now + GitHubAppCredentials.AppInstallationToken.STALE_AFTER_SECONDS + 1));
+ closeTo(now + GitHubAppCredentials.AppInstallationToken.STALE_AFTER_SECONDS + 1, 3));
+ // TODO use FlagRule
long notStaleSeconds = GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS;
try {
// Should revert to 1 second minimum
@@ -71,7 +77,7 @@ public void testAppInstallationTokenStale() throws Exception {
now = Instant.now().getEpochSecond();
token = new GitHubAppCredentials.AppInstallationToken(secret, now);
assertThat(token.isStale(), is(false));
- assertThat(token.getTokenStaleEpochSeconds(), equalTo(now + 1));
+ assertThat(token.getTokenStaleEpochSeconds(), closeTo(now + 1, 3));
// Verify goes stale
Thread.sleep(1000);
@@ -80,4 +86,25 @@ public void testAppInstallationTokenStale() throws Exception {
GitHubAppCredentials.AppInstallationToken.NOT_STALE_MINIMUM_SECONDS = notStaleSeconds;
}
}
+
+ private static Matcher closeTo(long operand, long error) {
+ BigDecimalCloseTo delegate =
+ new BigDecimalCloseTo(new BigDecimal(operand), new BigDecimal(error));
+ return new TypeSafeMatcher(Long.class) {
+ @Override
+ protected boolean matchesSafely(Long item) {
+ return delegate.matches(new BigDecimal(item));
+ }
+
+ @Override
+ protected void describeMismatchSafely(Long item, Description mismatchDescription) {
+ delegate.describeMismatchSafely(new BigDecimal(item), mismatchDescription);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ delegate.describeTo(description);
+ }
+ };
+ }
}