Skip to content

Commit

Permalink
[JENKINS-67946] Support underscores in user names (GitHub Enterprise …
Browse files Browse the repository at this point in the history
…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 <aburdajewicz@cloudbees.com>
  • Loading branch information
rkivisto and Dohbedoh authored Nov 3, 2022
1 parent 88de84e commit 3a76035
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class GitHubSCMSource extends AbstractGitSCMSource {

public static final String VALID_GITHUB_REPO_NAME = "^[0-9A-Za-z._-]+$";
public static final String VALID_GITHUB_USER_NAME =
"^[A-Za-z0-9](?:[A-Za-z0-9]|-(?=[A-Za-z0-9])){0,38}$";
"^(?=[A-Za-z0-9-_]{1,39}$)([A-Za-z0-9]((?:[A-Za-z0-9]+|-(?=[A-Za-z0-9]+))*)(_(?:[A-Za-z0-9]+))?)";
public static final String VALID_GIT_SHA1 = "^[a-fA-F0-9]{40}$";
public static final String GITHUB_URL = GitHubServerConfig.GITHUB_URL;
public static final String GITHUB_COM = "github.com";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,4 +1044,44 @@ public void testShouldRetrieveNullEvent() throws Exception {
assertTrue(this.source.shouldRetrieve(mockSCMHeadObserver, null, PullRequestSCMHead.class));
assertTrue(this.source.shouldRetrieve(mockSCMHeadObserver, null, BranchSCMHead.class));
}

@Test
@Issue("JENKINS-67946")
public void testUserNamesWithAndWithoutUnderscores() {
// 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
// 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-
assertTrue("user_organization".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("username".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user-name".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user-name_organization".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("abcd".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("1234".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user123-org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("123-456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user123_org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user123-org456-code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("user123-org456_code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue(
"abcdefghijqlmnopkrstuvwxyz-123456789012".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("a".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("0".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertTrue("a-b-c-d-e-f-g".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));

// Valid names should contain alphanumeric characters or single hyphens, and cannot begin or end
// with a hyphen, and have a 39 char limit
assertFalse(
"abcdefghijqlmnopkrstuvwxyz-1234567890123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123@org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123.org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123--org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123-".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("-user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123__org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123_".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("_user123".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123-_org456".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123_org456-code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
assertFalse("user123_org456_code789".matches(GitHubSCMSource.VALID_GITHUB_USER_NAME));
}
}

0 comments on commit 3a76035

Please sign in to comment.