Search in sources :

Example 21 with RepoException

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

the class WorkflowRunHelper method migrate.

/**
 * Performs a full migration, including checking out files from the origin, deleting excluded
 * files, transforming the code, and writing to the destination. This writes to the destination
 * exactly once.
 *
 * @param rev revision to the version which will be written to the destination
 * @param lastRev last revision that was migrated
 * @param processConsole console to use to print progress messages
 * @param metadata metadata of the change to be migrated
 * @param changes changes included in this migration
 * @param destinationBaseline it not null, use this baseline in the destination
 * @param changeIdentityRevision the revision to be used for computing the change identity
 */
ImmutableList<DestinationEffect> migrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
    ImmutableList<DestinationEffect> effects = ImmutableList.of();
    boolean callPerMigrationHook = true;
    try {
        eventMonitor().onChangeMigrationStarted(new ChangeMigrationStartedEvent());
        effects = doMigrate(rev, lastRev, processConsole, metadata, changes, destinationBaseline, changeIdentityRevision);
        return effects;
    } catch (EmptyChangeException empty) {
        effects = ImmutableList.of(new DestinationEffect(Type.NOOP, empty.getMessage(), changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of()));
        throw empty;
    } catch (ValidationException | IOException | RepoException | RuntimeException e) {
        effects = ImmutableList.of(new DestinationEffect(Type.ERROR, "Errors happened during the migration", changes.getCurrent(), /*destinationRef=*/
        null, ImmutableList.of(e.getMessage() != null ? e.getMessage() : e.toString())));
        callPerMigrationHook = e instanceof ValidationException;
        throw e;
    } finally {
        eventMonitor().onChangeMigrationFinished(new ChangeMigrationFinishedEvent(effects));
        if (callPerMigrationHook) {
            FinishHookContext finishHookContext = new FinishHookContext(getOriginReader().getFeedbackEndPoint(), getDestinationWriter().getFeedbackEndPoint(), effects, resolvedRef, new SkylarkConsole(getConsole()));
            try (ProfilerTask ignored = profiler().start("finish_hooks")) {
                for (Action action : workflow.getAfterMigrationActions()) {
                    try (ProfilerTask ignored2 = profiler().start(action.getName())) {
                        logger.log(Level.INFO, "Running after migration hook: " + action.getName());
                        action.run(finishHookContext);
                    }
                }
            }
        }
    }
}
Also used : Action(com.google.copybara.feedback.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) FinishHookContext(com.google.copybara.feedback.FinishHookContext) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 22 with RepoException

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

the class FolderOrigin method newReader.

@Override
public Reader<FolderRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
    return new Reader<FolderRevision>() {

        @Override
        public void checkout(FolderRevision ref, Path workdir) throws RepoException, ValidationException {
            try {
                FileUtil.copyFilesRecursively(ref.path, workdir, copySymlinkStrategy, originFiles);
                FileUtil.addPermissionsAllRecursively(workdir, FILE_PERMISSIONS);
            } catch (AbsoluteSymlinksNotAllowed e) {
                throw new ValidationException("Cannot copy files into the workdir: Some" + " symlinks refer to locations outside of the folder and" + " 'materialize_outside_symlinks' config option was not used:\n" + "  %s -> %s\n", e.getSymlink(), e.getDestinationFile());
            } catch (IOException e) {
                throw new RepoException(String.format("Cannot copy files into the workdir:\n" + "  origin folder: %s\n" + "  workdir: %s", ref.path, workdir), e);
            }
        }

        @Override
        public ChangesResponse<FolderRevision> changes(@Nullable FolderRevision fromRef, FolderRevision toRef) throws RepoException {
            // Ignore fromRef since a folder doesn't have history of changes
            return ChangesResponse.forChanges(ImmutableList.of(change(toRef)));
        }

        @Override
        public boolean supportsHistory() {
            return false;
        }

        @Override
        public Change<FolderRevision> change(FolderRevision ref) throws RepoException {
            return new Change<>(ref, author, message, ref.readTimestamp(), ImmutableMap.of());
        }

        @Override
        public void visitChanges(FolderRevision start, ChangesVisitor visitor) throws RepoException {
            visitor.visit(change(start));
        }
    };
}
Also used : Path(java.nio.file.Path) AbsoluteSymlinksNotAllowed(com.google.copybara.util.AbsoluteSymlinksNotAllowed) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) Change(com.google.copybara.Change) Nullable(javax.annotation.Nullable)

Example 23 with RepoException

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

the class GitMirrorTest method testMirrorConflict.

@Test
public void testMirrorConflict() throws Exception {
    Migration mirror = prepareForConflict();
    try {
        mirror.run(workdir, /*sourceRef=*/
        null);
        fail();
    } catch (RepoException e) {
        assertThat(e.getMessage()).contains("[rejected]");
    }
}
Also used : Migration(com.google.copybara.config.Migration) RepoException(com.google.copybara.exception.RepoException) Test(org.junit.Test)

Example 24 with RepoException

use of com.google.copybara.exception.RepoException 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)

Example 25 with RepoException

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

the class WorkflowTest method checkIterativeModeWithError.

@SuppressWarnings("unchecked")
private <T extends Exception> T checkIterativeModeWithError(T exception) throws IOException, ValidationException {
    for (int timestamp = 0; timestamp < 10; timestamp++) {
        origin.addSimpleChange(timestamp);
    }
    // Override destination with one that always throws EmptyChangeException.
    options.testingOptions.destination = new RecordsProcessCallDestination() {

        @Override
        public Writer newWriter(Glob destinationFiles, boolean dryRun, @Nullable String groupId, @Nullable Writer oldWriter) {
            return new WriterImpl(destinationFiles, dryRun) {

                @Override
                public ImmutableList<DestinationEffect> write(TransformResult transformResult, Console console) throws ValidationException, RepoException, IOException {
                    assert exception != null;
                    Throwables.propagateIfPossible(exception, ValidationException.class, RepoException.class);
                    throw new RuntimeException(exception);
                }
            };
        }
    };
    Workflow workflow = iterativeWorkflow(/*previousRef=*/
    "1");
    try {
        workflow.run(workdir, /*sourceRef=*/
        "3");
        fail();
    } catch (Exception expected) {
        assertThat(expected).isInstanceOf(expected.getClass());
        return (T) expected;
    }
    return exception;
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) ValidationException(com.google.copybara.exception.ValidationException) ImmutableList(com.google.common.collect.ImmutableList) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) VoidOperationException(com.google.copybara.exception.VoidOperationException) NotADestinationFileException(com.google.copybara.exception.NotADestinationFileException) RepoException(com.google.copybara.exception.RepoException) ExpectedException(org.junit.rules.ExpectedException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Console(com.google.copybara.util.console.Console) Glob(com.google.copybara.util.Glob)

Aggregations

RepoException (com.google.copybara.exception.RepoException)28 IOException (java.io.IOException)14 ValidationException (com.google.copybara.exception.ValidationException)11 ImmutableList (com.google.common.collect.ImmutableList)8 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)8 Path (java.nio.file.Path)8 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)7 Before (org.junit.Before)7 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)6 TestGitOptions (com.google.copybara.testing.git.GitTestUtil.TestGitOptions)6 HttpTransport (com.google.api.client.http.HttpTransport)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)5 Glob (com.google.copybara.util.Glob)5 GenericUrl (com.google.api.client.http.GenericUrl)4 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)4 Iterables (com.google.common.collect.Iterables)4 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)4 DummyRevision (com.google.copybara.testing.DummyRevision)4 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)4