Search in sources :

Example 1 with RedundantChangeException

use of com.google.copybara.exception.RedundantChangeException in project copybara by google.

the class GitHubPrWriteHook method beforePush.

@Override
public void beforePush(GitRepository scratchClone, MessageInfo messageInfo, boolean skipPush, List<? extends Change<?>> originChanges) throws ValidationException, RepoException {
    if (skipPush || generalOptions.allowEmptyDiff(allowEmptyDiff)) {
        return;
    }
    for (Change<?> originalChange : originChanges) {
        String configProjectName = ghHost.getProjectNameFromUrl(repoUrl);
        GitHubApi api = gitHubOptions.newGitHubApi(configProjectName);
        try {
            ImmutableList<PullRequest> pullRequests = api.getPullRequests(configProjectName, PullRequestListParams.DEFAULT.withHead(String.format("%s:%s", ghHost.getUserNameFromUrl(repoUrl), this.prBranchToUpdate)));
            // We don't want to throw EmptyChangeException for some of the prs with the empty diff.
            if (pullRequests.size() != 1) {
                return;
            }
            SameGitTree sameGitTree = new SameGitTree(scratchClone, repoUrl, generalOptions, partialFetch);
            PullRequest pullRequest = pullRequests.get(0);
            if (sameGitTree.hasSameTree(pullRequest.getHead().getSha())) {
                throw new RedundantChangeException(String.format("Skipping push to the existing pr %s/pull/%s as the change %s is empty.", repoUrl, pullRequest.getNumber(), originalChange.getRef()), pullRequest.getHead().getSha());
            }
        } catch (GitHubApiException e) {
            if (e.getResponseCode() == ResponseCode.NOT_FOUND || e.getResponseCode() == ResponseCode.UNPROCESSABLE_ENTITY) {
                console.verboseFmt("Branch %s does not exist", this.prBranchToUpdate);
            }
            throw e;
        }
    }
}
Also used : GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) GitHubApiException(com.google.copybara.git.github.api.GitHubApiException) RedundantChangeException(com.google.copybara.exception.RedundantChangeException)

Example 2 with RedundantChangeException

use of com.google.copybara.exception.RedundantChangeException in project copybara by google.

the class GerritDestinationTest method testNoAllowEmptyPatchSet.

@Test
public void testNoAllowEmptyPatchSet() throws Exception {
    // TODO(b/111567027): Add a test setting options.gitDestination.localRepoPath = some_folder.
    // Currently it doesn't work for unrelated reasons (refs/for/master is not a branch name
    // so we gene
    Path workTree = Files.createTempDirectory("populate");
    GitRepository repo = repo().withWorkTree(workTree);
    writeFile(workTree, "foo.txt", "" + "Base file\n" + "This is the original content\n" + "\n" + "This is a common part\n" + "That needs to be changed\n" + "And this remains constant\n");
    writeFile(workTree, "non_relevant.txt", "foo");
    repo.add().all().run();
    repo.simpleCommand("commit", "-m", "Old parent");
    GitRevision oldParent = repo.resolveReference("HEAD");
    String firstChange = "" + "Base file\n" + "This is the original content\n" + "\n" + "This is a common part\n" + "The content is changed\n" + "And this remains constant\n";
    writeFile(workdir, "foo.txt", firstChange);
    writeFile(workdir, "non_relevant.txt", "foo");
    mockNoChangesFound();
    runAllowEmptyPatchSetFalse(oldParent.getSha1());
    String primaryBranch = repo.getPrimaryBranch();
    assertThatGerritCheckout(repo(), "refs/for/" + primaryBranch).containsFile("non_relevant.txt", "foo").containsFile("foo.txt", firstChange).containsNoMoreFiles();
    GitRevision currentRev = repo.resolveReference(getGerritRef(repo, "refs/for/" + primaryBranch));
    repo.simpleCommand("update-ref", "refs/changes/10/12310/1", currentRev.getSha1());
    // To avoid the non-fastforward. In real Gerrit this is not a problem
    repo.simpleCommand("update-ref", "-d", getGerritRef(repo, "refs/for/" + primaryBranch));
    repo.forceCheckout(primaryBranch);
    writeFile(workTree, "foo.txt", "" + "Base file modified\n" + "This is the modified content\n" + "This is the modified content\n" + "This is the modified content\n" + "\n" + "This is a common part\n" + "That needs to be changed\n" + "And this remains constant\n");
    writeFile(workTree, "non_relevant.txt", "bar");
    repo.add().all().run();
    repo.simpleCommand("commit", "-m", "New parent");
    GitRevision newParent = repo.resolveReference("HEAD");
    String secondChange = "" + "Base file modified\n" + "This is the modified content\n" + "This is the modified content\n" + "This is the modified content\n" + "\n" + "This is a common part\n" + "The content is changed\n" + "And this remains constant\n";
    writeFile(workdir, "foo.txt", secondChange);
    writeFile(workdir, "non_relevant.txt", "bar");
    mockChangeFound(currentRev, 12310);
    RedundantChangeException e = assertThrows(RedundantChangeException.class, () -> runAllowEmptyPatchSetFalse(newParent.getSha1()));
    assertThat(e).hasMessageThat().contains("Skipping creating a new Gerrit PatchSet for change" + " https://localhost:33333/foo/bar/q/12310 since the diff is the same from the" + " previous PatchSet");
    // No push happened
    assertThat(repo().refExists("refs/for/" + primaryBranch)).isFalse();
    String thirdChange = "" + "Base file modified\n" + "This is the modified content\n" + "This is the modified content\n" + "This is the modified content\n" + "\n" + "This is a common part--> But we are changing it now\n" + "The content is changed\n" + "And this remains constant\n";
    writeFile(workdir, "foo.txt", thirdChange);
    writeFile(workdir, "non_relevant.txt", "bar");
    runAllowEmptyPatchSetFalse(newParent.getSha1());
    assertThatGerritCheckout(repo(), "refs/for/" + primaryBranch).containsFile("non_relevant.txt", "bar").containsFile("foo.txt", thirdChange).containsNoMoreFiles();
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) RedundantChangeException(com.google.copybara.exception.RedundantChangeException) Test(org.junit.Test)

Example 3 with RedundantChangeException

use of com.google.copybara.exception.RedundantChangeException in project copybara by google.

the class GitHubPrDestinationTest method emptyChange.

@Test
public void emptyChange() throws Exception {
    Writer<GitRevision> writer = getWriterForTestEmptyDiff();
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    String baseline = remote.resolveReference("HEAD").getSha1();
    addFiles(remote, "test_feature", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    String changeHead = remote.resolveReference("HEAD").getSha1();
    gitUtil.mockApi("GET", getPullRequestsUrl("test_feature"), mockResponse("[{" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"closed\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"," + "  \"head\": {\"sha\": \"" + changeHead + "\"}," + "  \"base\": {\"sha\": \"" + baseline + "\"}" + "}]"));
    writeFile(this.workdir, "foo.txt", "test");
    RedundantChangeException e = assertThrows(RedundantChangeException.class, () -> writer.write(TransformResults.of(this.workdir, new DummyRevision("one")).withBaseline(baseline).withChanges(new Changes(ImmutableList.of(toChange(new DummyRevision("feature"), new Author("Foo Bar", "foo@bar.com"))), ImmutableList.of())).withLabelFinder(Functions.forMap(ImmutableMap.of("aaa", ImmutableList.of("first a", "second a")))), Glob.ALL_FILES, console));
    assertThat(e).hasMessageThat().contains("Skipping push to the existing pr https://github.com/foo/pull/12345 " + "as the change feature is empty.");
}
Also used : Changes(com.google.copybara.Changes) DummyRevision(com.google.copybara.testing.DummyRevision) RedundantChangeException(com.google.copybara.exception.RedundantChangeException) Author(com.google.copybara.authoring.Author) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

RedundantChangeException (com.google.copybara.exception.RedundantChangeException)3 Test (org.junit.Test)2 Changes (com.google.copybara.Changes)1 Author (com.google.copybara.authoring.Author)1 GitHubApi (com.google.copybara.git.github.api.GitHubApi)1 GitHubApiException (com.google.copybara.git.github.api.GitHubApiException)1 PullRequest (com.google.copybara.git.github.api.PullRequest)1 DummyRevision (com.google.copybara.testing.DummyRevision)1 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)1 Path (java.nio.file.Path)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1