Skip to content

Commit ea48ff4

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. GitOrigin will continue to accept GitHub.com URLs.
1 parent c4f4a50 commit ea48ff4

File tree

9 files changed

+154
-123
lines changed

9 files changed

+154
-123
lines changed

docs/reference.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,7 +3394,6 @@ Parameter | Description
33943394
Name | Type | Description
33953395
---- | ---- | -----------
33963396
<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.
3397-
<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.
33983397
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
33993398
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
34003399
<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
@@ -3443,7 +3442,6 @@ Name | Type | Description
34433442
<span style="white-space: nowrap;">`--git-destination-push`</span> | *string* | If set, overrides the git destination push reference.
34443443
<span style="white-space: nowrap;">`--git-destination-url`</span> | *string* | If set, overrides the git destination URL.
34453444
<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.
3446-
<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.
34473445
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
34483446
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
34493447
<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
@@ -3452,7 +3450,7 @@ Name | Type | Description
34523450
<a id="git.github_origin" aria-hidden="true"></a>
34533451
### git.github_origin
34543452

3455-
Defines a Git origin for a Github repository. This origin should be used for public branches. Use github_pr_origin for importing Pull Requests.
3453+
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.
34563454

34573455
<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>
34583456

@@ -3485,7 +3483,6 @@ Name | Type | Description
34853483
<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.
34863484
<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.
34873485
<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.
3488-
<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.
34893486
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
34903487
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
34913488
<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
@@ -3581,7 +3578,6 @@ Name | Type | Description
35813578
<span style="white-space: nowrap;">`--git-destination-push`</span> | *string* | If set, overrides the git destination push reference.
35823579
<span style="white-space: nowrap;">`--git-destination-url`</span> | *string* | If set, overrides the git destination URL.
35833580
<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.
3584-
<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.
35853581
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
35863582
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
35873583
<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
@@ -3649,7 +3645,6 @@ Name | Type | Description
36493645
<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.
36503646
<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.
36513647
<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.
3652-
<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.
36533648
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
36543649
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
36553650
<span style="white-space: nowrap;">`--github-force-import`</span> | *boolean* | Force import regardless of the state of the PR
@@ -3689,7 +3684,6 @@ Parameter | Description
36893684
Name | Type | Description
36903685
---- | ---- | -----------
36913686
<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.
3692-
<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.
36933687
<span style="white-space: nowrap;">`--github-api-bearer-auth`</span> | *boolean* | If using a token for GitHub access, bearer auth might be required
36943688
<span style="white-space: nowrap;">`--github-destination-delete-pr-branch`</span> | *boolean* | Overwrite git.github_destination delete_pr_branch field
36953689
<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: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.base.Preconditions;
2525
import com.google.common.collect.ImmutableList;
26+
import com.google.common.collect.Sets;
2627
import com.google.copybara.GeneralOptions;
2728
import com.google.copybara.LazyResourceLoader;
2829
import com.google.copybara.Option;
@@ -41,6 +42,7 @@
4142
import com.google.copybara.jcommander.SemicolonSeparatedListSplitter;
4243
import com.google.copybara.starlark.StarlarkUtil;
4344
import com.google.copybara.util.console.Console;
45+
import java.util.Set;
4446
import net.starlark.java.eval.EvalException;
4547

4648
import java.io.IOException;
@@ -89,26 +91,8 @@ public class GitHubOptions implements Option {
8991
arity = 1)
9092
public boolean gitHubApiBearerAuth = false;
9193

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;
94+
public GitHubHost getGitHubHost(String url) throws ValidationException {
95+
return GitHubHost.fromUrl(url);
11296
}
11397

11498
public GitHubOptions(GeneralOptions generalOptions, GitOptions gitOptions) {
@@ -151,6 +135,26 @@ public GitHubApi newGitHubRestApi(GitHubHost ghHost,
151135
gitHubProject, /* checker= */ null, credentials, generalOptions.console());
152136
}
153137

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

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

0 commit comments

Comments
 (0)