Search in sources :

Example 1 with Authoring

use of com.google.copybara.authoring.Authoring in project copybara by google.

the class DummyOriginTest method testCanSpecifyMessage.

@Test
public void testCanSpecifyMessage() throws Exception {
    DummyOrigin origin = new DummyOrigin().addSimpleChange(/*timestamp*/
    4242, "foo msg");
    Authoring authoring = new Authoring(new Author("foo", "default.example.com"), AuthoringMappingMode.OVERWRITE, ImmutableSet.of());
    Reader<DummyRevision> reader = origin.newReader(Glob.ALL_FILES, authoring);
    ImmutableList<Change<DummyRevision>> changes = reader.changes(/*fromRef*/
    null, /*toRef*/
    origin.resolve("0")).getChanges();
    assertThat(changes).hasSize(1);
    assertThat(changes.get(0).getMessage()).isEqualTo("foo msg");
}
Also used : Authoring(com.google.copybara.authoring.Authoring) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 2 with Authoring

use of com.google.copybara.authoring.Authoring in project copybara by google.

the class GithubPROrigin method newReader.

@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
    return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
    false, submoduleStrategy, firstParent) {

        /**
         * Disable rebase since this is controlled by useMerge field.
         */
        @Override
        protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
        }

        @Override
        public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
            if (!baselineFromBranch) {
                return super.findBaseline(startRevision, label);
            }
            return findBaselinesWithoutLabel(startRevision, /*limit=*/
            1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
        }

        @Override
        public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
            String baseline = startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1);
            Preconditions.checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
            GitRevision baselineRev = getRepository().resolveReference(baseline);
            // Don't skip the first change as it is already the baseline
            BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
            false);
            visitChanges(baselineRev, visitor);
            return visitor.getResult();
        }

        @Override
        public Endpoint getFeedbackEndPoint() {
            return new GitHubEndPoint(githubOptions, url);
        }

        /**
         * Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
         * commit doesn't work for this case.
         */
        @Override
        public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException {
            if (!useMerge) {
                return super.changes(fromRef, toRef);
            }
            GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
            // Fast-forward merge
            if (merge.getParents().size() == 1) {
                return super.changes(fromRef, toRef);
            }
            // HEAD of the Pull Request
            GitRevision gitRevision = merge.getParents().get(1);
            ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
            // origin_files
            if (prChanges.isEmpty()) {
                return prChanges;
            }
            try {
                return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
            } catch (EmptyChangeException e) {
                throw new RepoException("Error getting the merge commit information: " + merge, e);
            }
        }

        @Nullable
        @Override
        public String getGroupIdentity(GitRevision rev) throws RepoException {
            return rev.associatedLabels().get(GITHUB_PR_NUMBER_LABEL);
        }
    };
}
Also used : Path(java.nio.file.Path) Iterables(com.google.common.collect.Iterables) Origin(com.google.copybara.Origin) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) Collections2(com.google.common.collect.Collections2) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Issue(com.google.copybara.git.github.api.Issue) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Label(com.google.copybara.git.github.api.Issue.Label) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ImmutableMap(com.google.common.collect.ImmutableMap) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Set(java.util.Set) Console(com.google.copybara.util.console.Console) Sets(com.google.common.collect.Sets) GithubUtil.getProjectNameFromUrl(com.google.copybara.git.github.util.GithubUtil.getProjectNameFromUrl) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubUtil.asGithubUrl(com.google.copybara.git.github.util.GithubUtil.asGithubUrl) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GithubUtil(com.google.copybara.git.github.util.GithubUtil) Collections(java.util.Collections) GithubPrUrl(com.google.copybara.git.github.util.GithubUtil.GithubPrUrl) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Change(com.google.copybara.Change) RepoException(com.google.copybara.exception.RepoException) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 3 with Authoring

use of com.google.copybara.authoring.Authoring in project copybara by google.

the class MetadataModuleTest method setup.

@Before
public void setup() throws Exception {
    options = new OptionsBuilder();
    authoring = "authoring.overwrite('" + DEFAULT_AUTHOR + "')";
    workdir = Files.createTempDirectory("workdir");
    Files.createDirectories(workdir);
    origin = new DummyOrigin().setAuthor(ORIGINAL_AUTHOR);
    destination = new RecordsProcessCallDestination();
    options.setConsole(new TestingConsole());
    options.testingOptions.origin = origin;
    options.testingOptions.destination = destination;
    skylark = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, TestingModule.class, MetadataModule.class));
    skylarkExecutor = new SkylarkTestExecutor(options, MetadataModule.class);
    origin.addSimpleChange(0, "first commit\n\nExtended text").setAuthor(FOO_BAR).addSimpleChange(1, "second commit\n\nExtended text").setAuthor(FOO_BAZ).addSimpleChange(2, "third commit\n\nExtended text");
    options.setLastRevision("0");
    // We don't care about already migrated code
    options.setForce(true);
    testingConsole = new TestingConsole();
    options.setConsole(testingConsole);
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) SkylarkParser(com.google.copybara.config.SkylarkParser) Authoring(com.google.copybara.authoring.Authoring) DummyOrigin(com.google.copybara.testing.DummyOrigin) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Before(org.junit.Before)

Example 4 with Authoring

use of com.google.copybara.authoring.Authoring in project copybara by google.

the class WorkflowTest method setup.

@Before
public void setup() throws Exception {
    options = new OptionsBuilder();
    authoring = "authoring.overwrite('" + DEFAULT_AUTHOR + "')";
    includeReleaseNotes = false;
    workdir = Files.createTempDirectory("workdir");
    Files.createDirectories(workdir);
    origin = new DummyOrigin().setAuthor(ORIGINAL_AUTHOR);
    originFiles = "glob(['**'], exclude = ['copy.bara.sky', 'excluded/**'])";
    destinationFiles = "glob(['**'])";
    destination = new RecordsProcessCallDestination();
    transformations = ImmutableList.of("" + "        core.replace(\n" + "             before = '${linestart}${number}',\n" + "             after = '${linestart}" + PREFIX + "${number}',\n" + "             regex_groups = {\n" + "                 'number'    : '[0-9]+',\n" + "                 'linestart' : '^',\n" + "             },\n" + "             multiline = True," + "        )");
    TestingConsole console = new TestingConsole();
    options.setConsole(console);
    options.testingOptions.origin = origin;
    options.testingOptions.destination = destination;
    // Force by default unless we are testing the flag.
    options.setForce(true);
    skylark = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, TestingModule.class, MetadataModule.class, FolderModule.class, GitModule.class));
    eventMonitor = new TestingEventMonitor();
    options.general.withEventMonitor(eventMonitor);
    transformWork = TransformWorks.of(workdir, "example", console);
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) SkylarkParser(com.google.copybara.config.SkylarkParser) Authoring(com.google.copybara.authoring.Authoring) DummyOrigin(com.google.copybara.testing.DummyOrigin) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) Before(org.junit.Before)

Example 5 with Authoring

use of com.google.copybara.authoring.Authoring in project copybara by google.

the class DummyOriginTest method canSetAuthorOfIndividualChanges.

@Test
public void canSetAuthorOfIndividualChanges() throws Exception {
    DummyOrigin origin = new DummyOrigin().setAuthor(new Author("Dummy Origin", "dummy_origin@google.com")).addSimpleChange(/*timestamp*/
    42).setAuthor(new Author("Wise Origin", "wise_origin@google.com")).addSimpleChange(/*timestamp*/
    999);
    Authoring authoring = new Authoring(new Author("foo", "default.example.com"), AuthoringMappingMode.PASS_THRU, ImmutableSet.of());
    ImmutableList<Change<DummyRevision>> changes = origin.newReader(Glob.ALL_FILES, authoring).changes(/*fromRef*/
    null, /*toRef*/
    origin.resolve("1")).getChanges();
    assertThat(changes).hasSize(2);
    assertThat(changes.get(0).getAuthor()).isEqualTo(new Author("Dummy Origin", "dummy_origin@google.com"));
    assertThat(changes.get(1).getAuthor()).isEqualTo(new Author("Wise Origin", "wise_origin@google.com"));
}
Also used : Authoring(com.google.copybara.authoring.Authoring) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

Aggregations

Authoring (com.google.copybara.authoring.Authoring)5 Change (com.google.copybara.Change)3 Author (com.google.copybara.authoring.Author)2 SkylarkParser (com.google.copybara.config.SkylarkParser)2 DummyOrigin (com.google.copybara.testing.DummyOrigin)2 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)2 RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)2 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)2 Before (org.junit.Before)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CharMatcher (com.google.common.base.CharMatcher)1 Preconditions (com.google.common.base.Preconditions)1 Splitter (com.google.common.base.Splitter)1 Collections2 (com.google.common.collect.Collections2)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)1