Search in sources :

Example 1 with GithubApi

use of com.google.copybara.git.github.api.GithubApi in project copybara by google.

the class GithubPrDestination method newWriter.

@Override
public Writer<GitRevision> newWriter(Glob destinationFiles, boolean dryRun, @Nullable String groupId, @Nullable Writer<GitRevision> oldWriter) throws ValidationException {
    WriterImpl gitOldWriter = (WriterImpl) oldWriter;
    boolean effectiveSkipPush = GithubPrDestination.this.effectiveSkipPush || dryRun;
    GithubWriterState state;
    String pushBranchName = branchFromGroupId(groupId);
    if (oldWriter != null && gitOldWriter.skipPush == effectiveSkipPush) {
        state = (GithubWriterState) ((WriterImpl) oldWriter).state;
    } else {
        state = new GithubWriterState(localRepo, destinationOptions.localRepoPath != null ? pushBranchName : "copybara/push-" + UUID.randomUUID() + (dryRun ? "-dryrun" : ""));
    }
    return new WriterImpl<GithubWriterState>(destinationFiles, effectiveSkipPush, url, destinationRef, pushBranchName, generalOptions, commitGenerator, processPushOutput, state, /*nonFastForwardPush=*/
    true, integrates, destinationOptions.lastRevFirstParent, destinationOptions.ignoreIntegrationErrors, destinationOptions.localRepoPath, destinationOptions.committerName, destinationOptions.committerEmail, destinationOptions.rebaseWhenBaseline(), gitOptions.visitChangePageSize) {

        @Override
        public ImmutableList<DestinationEffect> write(TransformResult transformResult, Console console) throws ValidationException, RepoException, IOException {
            ImmutableList.Builder<DestinationEffect> result = ImmutableList.<DestinationEffect>builder().addAll(super.write(transformResult, console));
            if (effectiveSkipPush || state.pullRequestNumber != null) {
                return result.build();
            }
            if (!githubDestinationOptions.createPullRequest) {
                console.infoFmt("Please create a PR manually following this link: %s/compare/%s...%s" + " (Only needed once)", asHttpsUrl(), destinationRef, pushBranchName);
                state.pullRequestNumber = -1L;
                return result.build();
            }
            GithubApi api = githubOptions.getApi(GithubUtil.getProjectNameFromUrl(url));
            for (PullRequest pr : api.getPullRequests(getProjectName())) {
                if (pr.isOpen() && pr.getHead().getRef().equals(pushBranchName)) {
                    console.infoFmt("Pull request for branch %s already exists as %s/pull/%s", pushBranchName, asHttpsUrl(), pr.getNumber());
                    if (!pr.getBase().getRef().equals(destinationRef)) {
                        // TODO(malcon): Update PR or create a new one?
                        console.warnFmt("Current base branch '%s' is different from the PR base branch '%s'", destinationRef, pr.getBase().getRef());
                    }
                    result.add(new DestinationEffect(DestinationEffect.Type.UPDATED, String.format("Pull Request %s updated", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl()), ImmutableList.of()));
                    return result.build();
                }
            }
            ChangeMessage msg = ChangeMessage.parseMessage(transformResult.getSummary());
            PullRequest pr = api.createPullRequest(getProjectName(), new CreatePullRequest(title == null ? msg.firstLine() : title, body == null ? msg.getText() : body, pushBranchName, destinationRef));
            console.infoFmt("Pull Request %s/pull/%s created using branch '%s'.", asHttpsUrl(), pr.getNumber(), pushBranchName);
            state.pullRequestNumber = pr.getNumber();
            result.add(new DestinationEffect(DestinationEffect.Type.CREATED, String.format("Pull Request %s created", pr.getHtmlUrl()), transformResult.getChanges().getCurrent(), new DestinationEffect.DestinationRef(Long.toString(pr.getNumber()), "pull_request", pr.getHtmlUrl()), ImmutableList.of()));
            return result.build();
        }
    };
}
Also used : TransformResult(com.google.copybara.TransformResult) ImmutableList(com.google.common.collect.ImmutableList) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubApi(com.google.copybara.git.github.api.GithubApi) CreatePullRequest(com.google.copybara.git.github.api.CreatePullRequest) WriterImpl(com.google.copybara.git.GitDestination.WriterImpl) DestinationEffect(com.google.copybara.DestinationEffect) Console(com.google.copybara.util.console.Console) ChangeMessage(com.google.copybara.ChangeMessage)

Example 2 with GithubApi

use of com.google.copybara.git.github.api.GithubApi in project copybara by google.

the class GithubPrDestinationTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GithubPrDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    localHub = Files.createTempDirectory("localHub");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    options.git = new TestGitOptions(localHub, () -> GithubPrDestinationTest.this.options.general);
    options.github = new GithubOptions(() -> options.general, options.git) {

        @Override
        public GithubApi getApi(String project) throws RepoException {
            assertThat(project).isEqualTo(expectedProject);
            return super.getApi(project);
        }

        @Override
        protected HttpTransport getHttpTransport() {
            return gitApiMockHttpTransport;
        }
    };
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    options.git.credentialHelperStorePath = credentialsFile.toString();
    options.gitDestination = new GitDestinationOptions(() -> options.general, options.git);
    options.gitDestination.committerEmail = "commiter@email";
    options.gitDestination.committerName = "Bara Kopi";
    skylark = new SkylarkTestExecutor(options, GitModule.class);
}
Also used : Path(java.nio.file.Path) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) GithubApi(com.google.copybara.git.github.api.GithubApi) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Before(org.junit.Before)

Example 3 with GithubApi

use of com.google.copybara.git.github.api.GithubApi in project copybara by google.

the class AbstractGithubApiTest method setUpFamework.

@Before
public void setUpFamework() throws Exception {
    Profiler profiler = new Profiler(Ticker.systemTicker());
    profiler.init(ImmutableList.of(new LogProfilerListener()));
    api = new GithubApi(getTransport(), profiler);
}
Also used : Profiler(com.google.copybara.profiler.Profiler) GithubApi(com.google.copybara.git.github.api.GithubApi) LogProfilerListener(com.google.copybara.profiler.LogProfilerListener) Before(org.junit.Before)

Example 4 with GithubApi

use of com.google.copybara.git.github.api.GithubApi in project copybara by google.

the class GithubOptions method getApi.

public GithubApi getApi(String gitHubRepo) throws RepoException {
    GitRepository repo = gitOptions.cachedBareRepoForUrl("just_for_github_api");
    String storePath = gitOptions.getCredentialHelperStorePath();
    if (storePath == null) {
        storePath = "~/.git-credentials";
    }
    return new GithubApi(new GitHubApiTransportImpl(repo, getHttpTransport(), storePath, generalOptionsSupplier.get().console()), generalOptionsSupplier.get().profiler());
}
Also used : GithubApi(com.google.copybara.git.github.api.GithubApi) GitHubApiTransportImpl(com.google.copybara.git.github.api.GitHubApiTransportImpl)

Example 5 with GithubApi

use of com.google.copybara.git.github.api.GithubApi in project copybara by google.

the class GithubPrOriginTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GithubPrDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    localHub = Files.createTempDirectory("localHub");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    options.git = new TestGitOptions(localHub, () -> this.options.general, new Validator() {

        @Override
        public void validateFetch(String url, boolean prune, boolean force, Iterable<String> refspecs) {
            for (String refspec : refspecs) {
                // WARNING! This check is important. While using short names like
                // 'master' in git fetch works for local git invocations, other
                // implementations of GitRepository might have problems if we don't
                // pass the whole reference.
                assertThat(refspec).startsWith("refs/");
                assertThat(refspec).contains(":refs/");
            }
        }
    });
    options.github = new GithubOptions(() -> options.general, options.git) {

        @Override
        public GithubApi getApi(String project) throws RepoException {
            assertThat(project).isEqualTo(expectedProject);
            return super.getApi(project);
        }

        @Override
        protected HttpTransport getHttpTransport() {
            return gitApiMockHttpTransport;
        }
    };
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    options.git.credentialHelperStorePath = credentialsFile.toString();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    skylarkParser = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, FolderModule.class, GitModule.class));
}
Also used : Path(java.nio.file.Path) SkylarkParser(com.google.copybara.config.SkylarkParser) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) GithubApi(com.google.copybara.git.github.api.GithubApi) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) HttpTransport(com.google.api.client.http.HttpTransport) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) Authoring(com.google.copybara.authoring.Authoring) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) Before(org.junit.Before)

Aggregations

GithubApi (com.google.copybara.git.github.api.GithubApi)5 Before (org.junit.Before)3 HttpTransport (com.google.api.client.http.HttpTransport)2 RepoException (com.google.copybara.exception.RepoException)2 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)2 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)2 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)2 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)2 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)2 Path (java.nio.file.Path)2 ImmutableList (com.google.common.collect.ImmutableList)1 ChangeMessage (com.google.copybara.ChangeMessage)1 DestinationEffect (com.google.copybara.DestinationEffect)1 TransformResult (com.google.copybara.TransformResult)1 Authoring (com.google.copybara.authoring.Authoring)1 SkylarkParser (com.google.copybara.config.SkylarkParser)1 WriterImpl (com.google.copybara.git.GitDestination.WriterImpl)1 CreatePullRequest (com.google.copybara.git.github.api.CreatePullRequest)1 GitHubApiTransportImpl (com.google.copybara.git.github.api.GitHubApiTransportImpl)1 PullRequest (com.google.copybara.git.github.api.PullRequest)1