Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3450,7 +3450,7 @@ Name | Type | Description
<a id="git.github_origin" aria-hidden="true"></a>
### git.github_origin

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

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

Expand Down
1 change: 1 addition & 0 deletions java/com/google/copybara/git/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ java_library(
"//java/com/google/copybara/monitor",
"//java/com/google/copybara/profiler",
"//java/com/google/copybara/revision",
"//java/com/google/copybara/starlark",
"//java/com/google/copybara/templatetoken",
"//java/com/google/copybara/transform",
"//java/com/google/copybara/transform/patch",
Expand Down
71 changes: 61 additions & 10 deletions java/com/google/copybara/git/GitHubOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.copybara.GeneralOptions;
import com.google.copybara.LazyResourceLoader;
import com.google.copybara.Option;
Expand All @@ -39,7 +40,11 @@
import com.google.copybara.jcommander.DurationConverter;
import com.google.copybara.jcommander.GreaterThanZeroListValidator;
import com.google.copybara.jcommander.SemicolonSeparatedListSplitter;
import com.google.copybara.starlark.StarlarkUtil;
import com.google.copybara.util.console.Console;
import java.util.Set;
import net.starlark.java.eval.EvalException;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
Expand Down Expand Up @@ -86,6 +91,9 @@ public class GitHubOptions implements Option {
arity = 1)
public boolean gitHubApiBearerAuth = false;

public GitHubHost getGitHubHost(String url) throws ValidationException {
return GitHubHost.fromUrl(url);
}

public GitHubOptions(GeneralOptions generalOptions, GitOptions gitOptions) {
this.generalOptions = Preconditions.checkNotNull(generalOptions);
Expand All @@ -100,7 +108,7 @@ public LazyResourceLoader<GitHubApi> newGitHubApiSupplier(
GitHubHost ghHost) {
return (console) -> {
String project = ghHost.getProjectNameFromUrl(url);
return newGitHubRestApi(project, checker, credentials, console);
return newGitHubRestApi(ghHost, project, checker, credentials, console);
};
}

Expand All @@ -112,7 +120,7 @@ public LazyResourceLoader<GitHubGraphQLApi> newGitHubGraphQLApiSupplier(
GitHubHost ghHost) {
return (console) -> {
String project = ghHost.getProjectNameFromUrl(url);
return newGitHubGraphQLApi(project, checker, credentials, console);
return newGitHubGraphQLApi(ghHost, project, checker, credentials, console);
};
}

Expand All @@ -121,19 +129,40 @@ public LazyResourceLoader<GitHubGraphQLApi> newGitHubGraphQLApiSupplier(
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
*/
public GitHubApi newGitHubRestApi(
public GitHubApi newGitHubRestApi(GitHubHost ghHost,
String gitHubProject, @Nullable CredentialFileHandler credentials) throws RepoException {
return newGitHubRestApi(
return newGitHubRestApi(ghHost,
gitHubProject, /* checker= */ null, credentials, generalOptions.console());
}

/**
* Returns a new Github.com specific {@link GitHubApi} instance for the given project enforcing the given {@link
* Checker}.
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
*
* @param gitHubProject the project
* @param checker the checker to enforce
* @param credentials the credentials to use for GitHub API auth
* @param console the console, used for logging
* @return the instance
* @throws RepoException if there is a failure in using the credentials
*/
public GitHubApi newGitHubRestApi(String gitHubProject,
@Nullable Checker checker,
@Nullable CredentialFileHandler credentials,
Console console) throws RepoException {
return newGitHubRestApi(GitHubHost.GITHUB_COM, gitHubProject, checker, credentials, console);
}

/**
* Returns a new {@link GitHubApi} instance for the given project enforcing the given {@link
* Checker}.
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
*/
public GitHubApi newGitHubRestApi(
GitHubHost ghHost,
String gitHubProject,
@Nullable Checker checker,
@Nullable CredentialFileHandler credentials,
Expand All @@ -144,7 +173,7 @@ public GitHubApi newGitHubRestApi(
if (storePath == null) {
storePath = "~/.git-credentials";
}
GitHubApiTransport transport = newTransport(repo, storePath, console);
GitHubApiTransport transport = newTransport(ghHost, repo, storePath, console);
if (checker != null) {
transport = new GitHubApiTransportWithChecker(transport, new ApiChecker(checker, console));
}
Expand All @@ -156,19 +185,41 @@ public GitHubApi newGitHubRestApi(
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
*/
public GitHubGraphQLApi newGitHubGraphQLApi(
public GitHubGraphQLApi newGitHubGraphQLApi(GitHubHost ghHost,
String gitHubProject, @Nullable CredentialFileHandler credentials) throws RepoException {
return newGitHubGraphQLApi(
return newGitHubGraphQLApi(ghHost,
gitHubProject, /* checker= */ null, credentials, generalOptions.console());
}

/**
* Returns a new GitHub.com specific {@link GitHubApi} instance for the given project enforcing the given {@link
* Checker}.
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
* @param gitHubProject the GitHub project
* @param checker the checker to enforce
* @param credentials the credentials to use for the GitHub API
* @param console the console, for logging
* @return the instance
* @throws RepoException if there is an issue using the provided credentials
*/
public GitHubGraphQLApi newGitHubGraphQLApi(
String gitHubProject,
@Nullable Checker checker,
@Nullable CredentialFileHandler credentials,
Console console)
throws RepoException {
return newGitHubGraphQLApi(GitHubHost.GITHUB_COM, gitHubProject, checker, credentials, console);
}

/**
* Returns a new {@link GitHubApi} instance for the given project enforcing the given {@link
* Checker}.
*
* <p>The project for 'https://github.com/foo/bar' is 'foo/bar'.
*/
public GitHubGraphQLApi newGitHubGraphQLApi(
GitHubHost ghHost,
String gitHubProject,
@Nullable Checker checker,
@Nullable CredentialFileHandler credentials,
Expand All @@ -180,7 +231,7 @@ public GitHubGraphQLApi newGitHubGraphQLApi(
if (storePath == null) {
storePath = "~/.git-credentials";
}
GitHubApiTransport transport = newTransport(repo, storePath, console);
GitHubApiTransport transport = newTransport(ghHost, repo, storePath, console);
if (checker != null) {
transport = new GitHubApiTransportWithChecker(transport, new ApiChecker(checker, console));
}
Expand Down Expand Up @@ -210,9 +261,9 @@ public void validateEndpointChecker(@Nullable Checker checker) throws Validation
// Accept any by default
}

private GitHubApiTransport newTransport(
private GitHubApiTransport newTransport(GitHubHost ghHost,
GitRepository repo, String storePath, Console console) {
return new GitHubApiTransportImpl(
return new GitHubApiTransportImpl(ghHost,
repo, newHttpTransport(), storePath, gitHubApiBearerAuth, console);
}

Expand Down
4 changes: 2 additions & 2 deletions java/com/google/copybara/git/GitHubPrDestination.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public ImmutableList<DestinationEffect> write(
return result.build();
}

GitHubApi api = gitHubOptions.newGitHubRestApi(getProjectName(), credentials);
GitHubApi api = gitHubOptions.newGitHubRestApi(ghHost, getProjectName(), credentials);

ImmutableList<PullRequest> pullRequests =
api.getPullRequests(
Expand Down Expand Up @@ -346,7 +346,7 @@ public Endpoint getFeedbackEndPoint(Console console) throws ValidationException
}

private String asHttpsUrl() throws ValidationException {
return "https://github.com/" + getProjectName();
return ghHost.projectAsUrl(getProjectName());
}

@VisibleForTesting
Expand Down
6 changes: 3 additions & 3 deletions java/com/google/copybara/git/GitHubPrOrigin.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public String showDiff(GitRevision revisionFrom, GitRevision revisionTo) throws
/** Given a commit SHA, use the GitHub API to (try to) look up info for a corresponding PR. */
private PullRequest getPrFromSha(String project, String sha)
throws RepoException, ValidationException {
GitHubApi gitHubApi = gitHubOptions.newGitHubRestApi(project, credentials);
GitHubApi gitHubApi = gitHubOptions.newGitHubRestApi(ghHost, project, credentials);
IssuesAndPullRequestsSearchResults searchResults =
gitHubApi.getIssuesOrPullRequestsSearchResults(
new IssuesAndPullRequestsSearchRequestParams(
Expand Down Expand Up @@ -299,13 +299,13 @@ private PullRequest getPrFromSha(String project, String sha)
private PullRequest getPrFromNumber(String project, long prNumber)
throws RepoException, ValidationException {
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_pr")) {
return gitHubOptions.newGitHubRestApi(project, credentials).getPullRequest(project, prNumber);
return gitHubOptions.newGitHubRestApi(ghHost, project, credentials).getPullRequest(project, prNumber);
}
}

private GitRevision getRevisionForPR(String project, PullRequest prData)
throws RepoException, ValidationException {
GitHubApi api = gitHubOptions.newGitHubRestApi(project, credentials);
GitHubApi api = gitHubOptions.newGitHubRestApi(ghHost, project, credentials);
int prNumber = (int) prData.getNumber();
boolean actuallyUseMerge = this.useMerge;
ImmutableListMultimap.Builder<String, String> labels = ImmutableListMultimap.builder();
Expand Down
2 changes: 1 addition & 1 deletion java/com/google/copybara/git/GitHubPrWriteHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void beforePush(
}
for (Change<?> originalChange : originChanges) {
String projectName = ghHost.getProjectNameFromUrl(repoUrl);
GitHubApi api = gitHubOptions.newGitHubRestApi(projectName, creds);
GitHubApi api = gitHubOptions.newGitHubRestApi(ghHost, projectName, creds);

try {
ImmutableList<PullRequest> pullRequests =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public ImmutableList<ChangeWithApprovals> tryPresubmitUserValidation(
ImmutableList.builder();
ImmutableList<Review> reviews = null;
try {
reviews = this.githubOptions.newGitHubRestApi(projectId, creds)
reviews = this.githubOptions.newGitHubRestApi(githubHost, projectId, creds)
.getReviews(projectId, prNumber);
} catch (RepoException | ValidationException e) {
console.warnFmt(
Expand Down
6 changes: 3 additions & 3 deletions java/com/google/copybara/git/GitHubWriteHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class GitHubWriteHook extends DefaultWriteHook {
private PullRequest getPrFromNumber(String project, long prNumber)
throws RepoException, ValidationException {
try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_pr")) {
return gitHubOptions.newGitHubRestApi(project, creds).getPullRequest(project, prNumber);
return gitHubOptions.newGitHubRestApi(ghHost, project, creds).getPullRequest(project, prNumber);
}
}

Expand All @@ -106,7 +106,7 @@ public void beforePush(
throws ValidationException, RepoException {

String configProjectName = ghHost.getProjectNameFromUrl(repoUrl);
GitHubApi api = gitHubOptions.newGitHubRestApi(configProjectName, creds);
GitHubApi api = gitHubOptions.newGitHubRestApi(ghHost, configProjectName, creds);


// TODO(joshgoldman): add credentials to the GitRepository object for pushing to the fork
Expand Down Expand Up @@ -204,7 +204,7 @@ public ImmutableList<DestinationEffect> afterPush(String serverResponse, Message
return baseEffects.build();
}
String projectId = ghHost.getProjectNameFromUrl(repoUrl);
GitHubApi api = gitHubOptions.newGitHubRestApi(projectId, creds);
GitHubApi api = gitHubOptions.newGitHubRestApi(ghHost, projectId, creds);

if (!originChanges.isEmpty()) {
if (gitHubOptions.githubPrBranchDeletionDelay != null) {
Expand Down
Loading