Skip to content

Commit 3a76035

Browse files
rkivistoDohbedoh
andauthored
[JENKINS-67946] Support underscores in user names (GitHub Enterprise Managed User accounts) (#626)
* [JENKINS-67946] Support underscores in user names * [JENKINS-67946] Correct testcase spotbugs and remove throws * [JENKINS-67946] Make user regex more accurate, and add a few more tests Co-authored-by: Allan Burdajewicz <[email protected]>
1 parent 88de84e commit 3a76035

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public class GitHubSCMSource extends AbstractGitSCMSource {
151151

152152
public static final String VALID_GITHUB_REPO_NAME = "^[0-9A-Za-z._-]+$";
153153
public static final String VALID_GITHUB_USER_NAME =
154-
"^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$";
154+
"^(?=[A-Za-z0-9-_]{1,39}$)([A-Za-z0-9]((?:[A-Za-z0-9]+|-(?=[A-Za-z0-9]+))*)(_(?:[A-Za-z0-9]+))?)";
155155
public static final String VALID_GIT_SHA1 = "^[a-fA-F0-9]{40}$";
156156
public static final String GITHUB_URL = GitHubServerConfig.GITHUB_URL;
157157
public static final String GITHUB_COM = "github.com";

src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMSourceTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,4 +1044,44 @@ public void testShouldRetrieveNullEvent() throws Exception {
10441044
assertTrue(this.source.shouldRetrieve(mockSCMHeadObserver, null, PullRequestSCMHead.class));
10451045
assertTrue(this.source.shouldRetrieve(mockSCMHeadObserver, null, BranchSCMHead.class));
10461046
}
1047+
1048+
@Test
1049+
@Issue("JENKINS-67946")
1050+
public void testUserNamesWithAndWithoutUnderscores() {
1051+
// https://docs.github.com/en/enterprise-cloud@latest/admin/identity-and-access-management/managing-iam-for-your-enterprise/username-considerations-for-external-authentication#about-usernames-for-managed-user-accounts
1052+
// https://github.com/github/docs/blob/bfe96c289aee3113724495a2e498c21e2ec404e4/content/admin/identity-and-access-management/using-enterprise-managed-users-for-iam/about-enterprise-managed-users.md#about--data-variablesproductprodname_emus-
1053+
assertTrue("user_organization".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1054+
assertTrue("username".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1055+
assertTrue("user-name".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1056+
assertTrue("user-name_organization".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1057+
assertTrue("abcd".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1058+
assertTrue("1234".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1059+
assertTrue("user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1060+
assertTrue("user123-org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1061+
assertTrue("123-456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1062+
assertTrue("user123_org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1063+
assertTrue("user123-org456-code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1064+
assertTrue("user123-org456_code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1065+
assertTrue(
1066+
"abcdefghijqlmnopkrstuvwxyz-123456789012".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1067+
assertTrue("a".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1068+
assertTrue("0".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1069+
assertTrue("a-b-c-d-e-f-g".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1070+
1071+
// Valid names should contain alphanumeric characters or single hyphens, and cannot begin or end
1072+
// with a hyphen, and have a 39 char limit
1073+
assertFalse(
1074+
"abcdefghijqlmnopkrstuvwxyz-1234567890123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1075+
assertFalse("user123@org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1076+
assertFalse("user123.org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1077+
assertFalse("user123--org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1078+
assertFalse("user123-".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1079+
assertFalse("-user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1080+
assertFalse("user123__org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1081+
assertFalse("user123_".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1082+
assertFalse("_user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1083+
assertFalse("user123-_org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1084+
assertFalse("user123_org456-code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1085+
assertFalse("user123_org456_code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
1086+
}
10471087
}

0 commit comments

Comments
 (0)