Skip to content

Commit b635851

Browse files
committed
Allow GitHub origin to accept any host
To better support GitHub enterprise, the GitHub Origin will just assume any host it's given is a GitHub or Github enterprise host.
1 parent c4dfa6f commit b635851

File tree

5 files changed

+59
-86
lines changed

5 files changed

+59
-86
lines changed

docs/reference.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3379,7 +3379,6 @@ Parameter | Description
33793379
Name | Type | Description
33803380
---- | ---- | -----------
33813381
<span style="white-space: nowrap;">`--allstar-app-ids`</span> | *list* | Flag used to set AllStar GitHub app id aliases. See https://github.com/ossf/allstar.
3382-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
33833382
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
33843383
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
33853384
<span style="white-space: nowrap;">`--gql-commit-history-override`</span> | *list* | Flag used to target GraphQL params 'first' arguments in the event the defaults are over or underusing the api ratelimit. The flag value should be semicolon separated. This should be rarely used for repos that don't fit well in our defaults. E.g. '50;5;5' represent 50 commits, 5 PRs for each commit, 5 reviews per PR
@@ -3428,7 +3427,6 @@ Name | Type | Description
34283427
<span style="white-space: nowrap;">`--git-destination-push`</span> | *string* | If set, overrides the git destination push reference.
34293428
<span style="white-space: nowrap;">`--git-destination-url`</span> | *string* | If set, overrides the git destination URL.
34303429
<span style="white-space: nowrap;">`--git-skip-checker`</span> | *boolean* | If true and git.destination has a configured checker, it will not be used in the migration.
3431-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
34323430
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
34333431
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
34343432
<span style="white-space: nowrap;">`--gql-commit-history-override`</span> | *list* | Flag used to target GraphQL params 'first' arguments in the event the defaults are over or underusing the api ratelimit. The flag value should be semicolon separated. This should be rarely used for repos that don't fit well in our defaults. E.g. '50;5;5' represent 50 commits, 5 PRs for each commit, 5 reviews per PR
@@ -3437,7 +3435,7 @@ Name | Type | Description
34373435
<a id="git.github_origin" aria-hidden="true"></a>
34383436
### git.github_origin
34393437

3440-
Defines a Git origin for a Github repository. This origin should be used for public branches. Use github_pr_origin for importing Pull Requests.
3438+
Defines a Git origin for a GitHub or GitHub Enterprise repository. This origin should be used for public branches. Use github_pr_origin for importing Pull Requests.
34413439

34423440
<code><a href="#origin">origin</a></code> <code>git.github_origin(<a href=#git.github_origin.url>url</a>, <a href=#git.github_origin.ref>ref</a>=None, <a href=#git.github_origin.submodules>submodules</a>='NO', <a href=#git.github_origin.excluded_submodules>excluded_submodules</a>=[], <a href=#git.github_origin.first_parent>first_parent</a>=True, <a href=#git.github_origin.partial_fetch>partial_fetch</a>=False, <a href=#git.github_origin.patch>patch</a>=None, <a href=#git.github_origin.describe_version>describe_version</a>=None, <a href=#git.github_origin.version_selector>version_selector</a>=None, <a href=#git.github_origin.primary_branch_migration>primary_branch_migration</a>=False, <a href=#git.github_origin.enable_lfs>enable_lfs</a>=False, <a href=#git.github_origin.credentials>credentials</a>=None)</code>
34433441

@@ -3470,7 +3468,6 @@ Name | Type | Description
34703468
<span style="white-space: nowrap;">`--git-origin-log-batch`</span> | *int* | Read the origin git log in batches of n commits. Might be needed for large migrations resulting in git logs of more than 1 GB.
34713469
<span style="white-space: nowrap;">`--git-origin-non-linear-history`</span> | *boolean* | Read the full git log and skip changes before the from ref rather than using a log path.
34723470
<span style="white-space: nowrap;">`--git-origin-rebase-ref`</span> | *string* | When importing a change from a Git origin ref, it will be rebased to this ref, if set. A common use case: importing a Github PR, rebase it to the main branch (usually 'master'). Note that, if the repo uses submodules, they won't be rebased.
3473-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
34743471
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
34753472
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
34763473
<span style="white-space: nowrap;">`--gql-commit-history-override`</span> | *list* | Flag used to target GraphQL params 'first' arguments in the event the defaults are over or underusing the api ratelimit. The flag value should be semicolon separated. This should be rarely used for repos that don't fit well in our defaults. E.g. '50;5;5' represent 50 commits, 5 PRs for each commit, 5 reviews per PR
@@ -3566,7 +3563,6 @@ Name | Type | Description
35663563
<span style="white-space: nowrap;">`--git-destination-push`</span> | *string* | If set, overrides the git destination push reference.
35673564
<span style="white-space: nowrap;">`--git-destination-url`</span> | *string* | If set, overrides the git destination URL.
35683565
<span style="white-space: nowrap;">`--git-skip-checker`</span> | *boolean* | If true and git.destination has a configured checker, it will not be used in the migration.
3569-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
35703566
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
35713567
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
35723568
<span style="white-space: nowrap;">`--github-destination-pr-branch`</span> | *string* | If set, uses this branch for creating the pull request instead of using a generated one
@@ -3634,7 +3630,6 @@ Name | Type | Description
36343630
<span style="white-space: nowrap;">`--git-origin-log-batch`</span> | *int* | Read the origin git log in batches of n commits. Might be needed for large migrations resulting in git logs of more than 1 GB.
36353631
<span style="white-space: nowrap;">`--git-origin-non-linear-history`</span> | *boolean* | Read the full git log and skip changes before the from ref rather than using a log path.
36363632
<span style="white-space: nowrap;">`--git-origin-rebase-ref`</span> | *string* | When importing a change from a Git origin ref, it will be rebased to this ref, if set. A common use case: importing a Github PR, rebase it to the main branch (usually 'master'). Note that, if the repo uses submodules, they won't be rebased.
3637-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
36383633
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
36393634
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
36403635
<span style="white-space: nowrap;">`--github-force-import`</span> | *boolean* | Force import regardless of the state of the PR
@@ -3674,7 +3669,6 @@ Parameter | Description
36743669
Name | Type | Description
36753670
---- | ---- | -----------
36763671
<span style="white-space: nowrap;">`--allstar-app-ids`</span> | *list* | Flag used to set AllStar GitHub app id aliases. See https://github.com/ossf/allstar.
3677-
<span style="white-space: nowrap;">`--github-allowed-hosts`</span> | *list* | If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported.
36783672
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
36793673
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
36803674
<span style="white-space: nowrap;">`--gql-commit-history-override`</span> | *list* | Flag used to target GraphQL params 'first' arguments in the event the defaults are over or underusing the api ratelimit. The flag value should be semicolon separated. This should be rarely used for repos that don't fit well in our defaults. E.g. '50;5;5' represent 50 commits, 5 PRs for each commit, 5 reviews per PR

java/com/google/copybara/git/GitHubOptions.java

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,8 @@ public class GitHubOptions implements Option {
8989
arity = 1)
9090
public boolean gitHubApiBearerAuth = false;
9191

92-
@Parameter(
93-
names = "--github-allowed-hosts",
94-
description = "If using GitHub Enterprise, one needs to specify valid hosts. By default only `github.com` is supported."
95-
)
96-
public List<String> gitHubAllowedHosts = ImmutableList.of("github.com");
97-
98-
public GitHubHost getGitHubHost(String url) throws EvalException {
99-
GitHubHost host = GitHubHost.fromUrl(url);
100-
StarlarkUtil.check(gitHubAllowedHosts.contains(host.getHost()), "'%s' is not a valid GitHub url", host.getHost());
101-
return host;
102-
}
103-
104-
public boolean isGithubUrl(String url)
105-
{
106-
GitHubHost host = GitHubHost.fromUrl(url);
107-
if(gitHubAllowedHosts.contains(host.getHost())){
108-
return host.isGitHubUrl(url);
109-
}
110-
111-
return false;
92+
public GitHubHost getGitHubHost(String url) {
93+
return GitHubHost.fromUrl(url);
11294
}
11395

11496
public GitHubOptions(GeneralOptions generalOptions, GitOptions gitOptions) {
@@ -151,6 +133,26 @@ public GitHubApi newGitHubRestApi(GitHubHost ghHost,
151133
gitHubProject, /* checker= */ null, credentials, generalOptions.console());
152134
}
153135

136+
/**
137+
* Returns a new Github.com specific {@link GitHubApi} instance for the given project enforcing the given {@link
138+
* Checker}.
139+
*
140+
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
141+
*
142+
* @param gitHubProject the project
143+
* @param checker the checker to enforce
144+
* @param credentials the credentials to use for GitHub API auth
145+
* @param console the console, used for logging
146+
* @return the instance
147+
* @throws RepoException if there is a failure in using the credentials
148+
*/
149+
public GitHubApi newGitHubRestApi(String gitHubProject,
150+
@Nullable Checker checker,
151+
@Nullable CredentialFileHandler credentials,
152+
Console console) throws RepoException {
153+
return newGitHubRestApi(GitHubHost.GITHUB_COM, gitHubProject, checker, credentials, console);
154+
}
155+
154156
/**
155157
* Returns a new {@link GitHubApi} instance for the given project enforcing the given {@link
156158
* Checker}.
@@ -187,6 +189,27 @@ public GitHubGraphQLApi newGitHubGraphQLApi(GitHubHost ghHost,
187189
gitHubProject, /* checker= */ null, credentials, generalOptions.console());
188190
}
189191

192+
/**
193+
* Returns a new GitHub.com specific {@link GitHubApi} instance for the given project enforcing the given {@link
194+
* Checker}.
195+
*
196+
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
197+
* @param gitHubProject the GitHub project
198+
* @param checker the checker to enforce
199+
* @param credentials the credentials to use for the GitHub API
200+
* @param console the console, for logging
201+
* @return the instance
202+
* @throws RepoException if there is an issue using the provided credentials
203+
*/
204+
public GitHubGraphQLApi newGitHubGraphQLApi(
205+
String gitHubProject,
206+
@Nullable Checker checker,
207+
@Nullable CredentialFileHandler credentials,
208+
Console console)
209+
throws RepoException {
210+
return newGitHubGraphQLApi(GitHubHost.GITHUB_COM, gitHubProject, checker, credentials, console);
211+
}
212+
190213
/**
191214
* Returns a new {@link GitHubApi} instance for the given project enforcing the given {@link
192215
* Checker}.

java/com/google/copybara/git/GitModule.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ public GitOrigin origin(
356356
CredentialFileHandler credentialHandler = getCredentialHandler(fixedUrl, credentials);
357357

358358
GitHubOptions githubOptions = options.get(GitHubOptions.class);
359-
boolean isGitHubUrl = githubOptions.isGithubUrl(url);
359+
// This does not support GitHub Enterprise. For that, use githubOrigin.
360+
boolean isGitHubUrl = GITHUB_COM.isGitHubUrl(url);
360361

361362
return GitOrigin.newGitOrigin(
362363
options,
@@ -1263,7 +1264,7 @@ public Origin<GitRevision> githubPrOrigin(
12631264
@StarlarkMethod(
12641265
name = "github_origin",
12651266
doc =
1266-
"Defines a Git origin for a Github repository. This origin should be used for public"
1267+
"Defines a Git origin for a GitHub or GitHub Enterprise repository. This origin should be used for public"
12671268
+ " branches. Use "
12681269
+ GITHUB_PR_ORIGIN_NAME
12691270
+ " for importing Pull Requests.",
@@ -3400,7 +3401,7 @@ private String fixHttp(String url, Location location) {
34003401
/** Do not use this for github origins */
34013402
protected ApprovalsProvider approvalsProvider(String url) {
34023403
Preconditions.checkArgument(
3403-
!options.get(GitHubOptions.class).isGithubUrl(url),
3404+
!GITHUB_COM.isGitHubUrl(url),
34043405
"Git origins with github should use github approval providers!");
34053406
return options.get(GitOriginOptions.class).approvalsProvider;
34063407
}
@@ -3517,13 +3518,9 @@ protected LazyResourceLoader<EndpointProvider<?>> maybeGetGerritApi(
35173518
protected LazyResourceLoader<EndpointProvider<?>> maybeGetGitHubApi(
35183519
String url, @Nullable Checker checker, @Nullable CredentialFileHandler creds,
35193520
StarlarkThread thread) {
3520-
try {
3521-
GitHubOptions githubOptions = options.get(GitHubOptions.class);
3522-
GitHubHost ghHost = githubOptions.getGitHubHost(url);
3523-
if (!ghHost.isGitHubUrl(url)) {
3524-
return null;
3525-
}
3526-
} catch (EvalException e) {
3521+
GitHubOptions githubOptions = options.get(GitHubOptions.class);
3522+
GitHubHost ghHost = githubOptions.getGitHubHost(url);
3523+
if (!ghHost.isGitHubUrl(url)) {
35273524
return null;
35283525
}
35293526
return (console) -> {
@@ -3553,15 +3550,11 @@ protected LazyResourceLoader<EndpointProvider<?>> maybeGetGitHubApi(
35533550
@Nullable protected CredentialFileHandler getCredentialHandler(
35543551
String url, @Nullable Object starlarkValue) {
35553552
try {
3556-
try {
3557-
GitHubOptions githubOptions = options.get(GitHubOptions.class);
3558-
GitHubHost ghHost = githubOptions.getGitHubHost(url);
3559-
if (ghHost.isGitHubUrl(url)) {
3560-
url = ghHost.normalizeUrl(url);
3561-
}
3562-
} catch (EvalException e) {
3563-
// nothing to-do, it is valid that this is not an GitHub URL.
3564-
}
3553+
GitHubOptions githubOptions = options.get(GitHubOptions.class);
3554+
GitHubHost ghHost = githubOptions.getGitHubHost(url);
3555+
if (ghHost.isGitHubUrl(url)) {
3556+
url = ghHost.normalizeUrl(url);
3557+
}
35653558
URI uri = URI.create(url);
35663559
return getCredentialHandler(uri.getHost(), uri.getPath(), starlarkValue);
35673560
} catch (ValidationException | IllegalArgumentException parseEx) {

java/com/google/copybara/git/github/util/GitHubHost.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ public GitHubHost(String host) {
4141
this.gitHubPrUrlPattern = Pattern.compile("https://\\Q" + host + "\\E/(.+)/pull/([0-9]+)");
4242
}
4343

44-
static public GitHubHost fromUrl(String url)
45-
{
46-
url = url.replaceAll("http\\:\\/\\/|https\\:\\/\\/|git\\+|git@", "").replaceAll(":.*|/.*", "");
47-
return new GitHubHost(url);
44+
static public GitHubHost fromUrl(String url) {
45+
return new GitHubHost(URI.create(url).getHost());
4846
}
4947

5048
/**
@@ -90,7 +88,7 @@ public String getProjectNameFromUrl(String url) throws ValidationException {
9088
return name;
9189
}
9290

93-
/** Returns true if url is a GitHub url for a given GitHub or Enterprise host. */
91+
/** Returns true if URL belongs to the host that this object is initialized with. */
9492
public boolean isGitHubUrl(String url) {
9593
try {
9694
getProjectNameFromUrl(url);

javatests/com/google/copybara/git/GitOriginTest.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ public void testGithubOrigin() throws Exception {
338338

339339
@Test
340340
public void testGithubOriginForEnterpriseUrl() throws Exception {
341-
options.github.gitHubAllowedHosts = ImmutableList.of("some.github-enterprise.net");
342341
origin = skylark.eval("result",
343342
"result = git.github_origin(\n"
344343
+ " url = 'https://some.github-enterprise.net/copybara',\n"
@@ -354,40 +353,6 @@ public void testGithubOriginForEnterpriseUrl() throws Exception {
354353
+ "}");
355354
}
356355

357-
@Test
358-
public void testInvalidGithubUrl() throws Exception {
359-
ValidationException expected =
360-
assertThrows(
361-
ValidationException.class,
362-
() ->
363-
skylark.eval(
364-
"result",
365-
"result = git.github_origin(\n"
366-
+ " url = 'https://foo.com/copybara',\n"
367-
+ " ref = 'main',\n"
368-
+ ")"));
369-
console
370-
.assertThat()
371-
.onceInLog(MessageType.ERROR, ".*'foo.com' is not a valid GitHub url.*");
372-
}
373-
374-
@Test
375-
public void testInvalidGithubUrlWithGithubString() throws Exception {
376-
ValidationException expected =
377-
assertThrows(
378-
ValidationException.class,
379-
() ->
380-
skylark.eval(
381-
"result",
382-
"result = git.github_origin(\n"
383-
+ " url = 'https://foo.com/github.com',\n"
384-
+ " ref = 'main',\n"
385-
+ ")"));
386-
console
387-
.assertThat()
388-
.onceInLog(MessageType.ERROR, ".*'foo.com' is not a valid GitHub url.*");
389-
}
390-
391356
@Test
392357
public void testResolveWithUrl() throws Exception {
393358
assertThat(origin.resolve(defaultBranch).getUrl()).isEqualTo(url);

0 commit comments

Comments
 (0)