Search in sources :

Example 1 with Action

use of com.google.copybara.action.Action in project copybara by google.

the class Core method feedback.

@SuppressWarnings("unused")
@StarlarkMethod(name = "feedback", doc = "Defines a migration of changes' metadata, that can be invoked via the Copybara command" + " in the same way as a regular workflow migrates the change itself.\n" + "\n" + "It is considered change metadata any information associated with a change" + " (pending or submitted) that is not core to the change itself. A few examples:\n" + "<ul>\n" + "  <li> Comments: Present in any code review system. Examples: GitHub PRs or" + " Gerrit     code reviews.</li>\n" + "  <li> Labels: Used in code review systems for approvals and/or CI results.    " + " Examples: GitHub labels, Gerrit code review labels.</li>\n" + "</ul>\n" + "For the purpose of this workflow, it is not considered metadata the commit" + " message in Git, or any of the contents of the file tree.\n" + "\n", parameters = { @Param(name = "name", doc = "The name of the feedback workflow.", positional = false, named = true), @Param(name = "origin", doc = "The trigger of a feedback migration.", positional = false, named = true), @Param(name = "destination", doc = "Where to write change metadata to. This is usually a code review system like " + "Gerrit or GitHub PR.", positional = false, named = true), @Param(name = "actions", doc = "" + "A list of feedback actions to perform, with the following semantics:\n" + "  - There is no guarantee of the order of execution.\n" + "  - Actions need to be independent from each other.\n" + "  - Failure in one action might prevent other actions from executing.\n", defaultValue = "[]", positional = false, named = true), @Param(name = "description", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, positional = false, doc = "A description of what this workflow achieves", defaultValue = "None") }, useStarlarkThread = true)
@UsesFlags({ FeedbackOptions.class })
public /*TODO(danielromero): Add default values*/
NoneType feedback(String workflowName, Trigger trigger, EndpointProvider<?> destination, net.starlark.java.eval.Sequence<?> feedbackActions, Object description, StarlarkThread thread) throws EvalException {
    ImmutableList<Action> actions = convertFeedbackActions(feedbackActions, printHandler);
    Feedback migration = new Feedback(workflowName, convertFromNoneable(description, null), mainConfigFile, trigger, destination.getEndpoint(), actions, generalOptions);
    Module module = Module.ofInnermostEnclosingStarlarkFunction(thread);
    registerGlobalMigration(workflowName, migration, module);
    return Starlark.NONE;
}
Also used : StarlarkAction(com.google.copybara.action.StarlarkAction) Action(com.google.copybara.action.Action) Feedback(com.google.copybara.feedback.Feedback) LabelsAwareModule(com.google.copybara.config.LabelsAwareModule) FolderModule(com.google.copybara.folder.FolderModule) Module(net.starlark.java.eval.Module) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 2 with Action

use of com.google.copybara.action.Action in project copybara by google.

the class Mirror method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, IOException, ValidationException {
    try (ProfilerTask ignore = generalOptions.profiler().start("run/" + name)) {
        GitRepository repo = gitOptions.cachedBareRepoForUrl(origin);
        if (Iterables.isEmpty(actions)) {
            defaultMirror(repo);
        } else {
            ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
            for (Action action : actions) {
                GitMirrorContext context = new GitMirrorContext(action, new SkylarkConsole(generalOptions.console()), sourceRefs, refspec, origin, destination, generalOptions.isForced(), repo, generalOptions.getDirFactory(), Dict.empty());
                try {
                    action.run(context);
                    ActionResult actionResult = context.getActionResult();
                    allResultsBuilder.add(actionResult);
                    // First error aborts the execution of the other actions unless --force is used
                    ValidationException.checkCondition(generalOptions.isForced() || actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
                } catch (NonFastForwardRepositoryException e) {
                    allResultsBuilder.add(ActionResult.error(action.getName() + ": " + e.getMessage()));
                    if (!generalOptions.isForced()) {
                        throw e;
                    }
                    logger.atWarning().withCause(e).log();
                } finally {
                    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(context.getNewDestinationEffects()), getOriginDescription(), getDestinationDescription())));
                }
            }
            ImmutableList<ActionResult> allResults = allResultsBuilder.build();
            if (allResults.stream().anyMatch(a -> a.getResult() == Result.ERROR)) {
                String errors = allResults.stream().filter(a -> a.getResult() == Result.ERROR).map(ActionResult::getMsg).collect(Collectors.joining("\n - "));
                throw new ValidationException("One or more errors happened during the migration:\n" + " - " + errors);
            }
            // This check also returns true if there are no actions
            if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
                String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
                throw new EmptyChangeException(String.format("git.mirror migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
            }
        }
    }
    // More fine grain events based on the references created/updated/deleted:
    ChangeMigrationFinishedEvent event = new ChangeMigrationFinishedEvent(ImmutableList.of(new DestinationEffect(generalOptions.dryRunMode ? DestinationEffect.Type.NOOP : DestinationEffect.Type.UPDATED, generalOptions.dryRunMode ? "Refspecs " + refspec + " can be mirrored" : "Refspecs " + refspec + " mirrored successfully", // TODO(danielromero): Populate OriginRef here
    ImmutableList.of(), new DestinationRef(getOriginDestinationRef(destination), "mirror", /*url=*/
    null))), getOriginDescription(), getDestinationDescription());
    generalOptions.eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(event));
}
Also used : Action(com.google.copybara.action.Action) ValidationException(com.google.copybara.exception.ValidationException) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) DestinationRef(com.google.copybara.DestinationEffect.DestinationRef) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 3 with Action

use of com.google.copybara.action.Action in project copybara by google.

the class Workflow method runHooks.

ImmutableList<DestinationEffect> runHooks(ImmutableList<DestinationEffect> effects, ImmutableList<Action> actions, LazyResourceLoader<Endpoint> originEndpoint, LazyResourceLoader<Endpoint> destinationEndpoint, Revision resolvedRef) throws ValidationException, RepoException {
    SkylarkConsole console = new SkylarkConsole(getConsole());
    List<DestinationEffect> hookDestinationEffects = new ArrayList<>();
    for (Action action : actions) {
        try (ProfilerTask ignored2 = profiler().start(action.getName())) {
            logger.log(Level.INFO, "Running after migration hook: " + action.getName());
            FinishHookContext context = new FinishHookContext(action, originEndpoint, destinationEndpoint, ImmutableList.copyOf(effects), generalOptions.labels, resolvedRef, console);
            action.run(context);
            hookDestinationEffects.addAll(context.getNewDestinationEffects());
        }
    }
    return ImmutableList.<DestinationEffect>builder().addAll(effects).addAll(hookDestinationEffects).build();
}
Also used : SkylarkConsole(com.google.copybara.transform.SkylarkConsole) Action(com.google.copybara.action.Action) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ArrayList(java.util.ArrayList) FinishHookContext(com.google.copybara.feedback.FinishHookContext)

Example 4 with Action

use of com.google.copybara.action.Action in project copybara by google.

the class Feedback method run.

@Override
public void run(Path workdir, ImmutableList<String> sourceRefs) throws RepoException, ValidationException {
    ImmutableList.Builder<ActionResult> allResultsBuilder = ImmutableList.builder();
    String suffix = Joiner.on('_').join(sourceRefs).replaceAll("([/ ])", "_");
    String root = "run/" + name + "/" + suffix.substring(0, Math.min(suffix.length(), 20));
    try (ProfilerTask ignore = profiler().start(root)) {
        for (Action action : actions) {
            ArrayList<DestinationEffect> effects = new ArrayList<>();
            try (ProfilerTask ignore2 = profiler().start(action.getName())) {
                SkylarkConsole console = new SkylarkConsole(generalOptions.console());
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationStarted(new ChangeMigrationStartedEvent()));
                FeedbackMigrationContext context = new FeedbackMigrationContext(this, action, generalOptions.cliLabels(), sourceRefs, console);
                action.run(context);
                effects.addAll(context.getNewDestinationEffects());
                ActionResult actionResult = context.getActionResult();
                allResultsBuilder.add(actionResult);
                // First error aborts the execution of the other actions
                ValidationException.checkCondition(actionResult.getResult() != Result.ERROR, "Feedback migration '%s' action '%s' returned error: %s. Aborting execution.", name, action.getName(), actionResult.getMsg());
            } finally {
                eventMonitors().dispatchEvent(m -> m.onChangeMigrationFinished(new ChangeMigrationFinishedEvent(ImmutableList.copyOf(effects), getOriginDescription(), getDestinationDescription())));
            }
        }
    }
    ImmutableList<ActionResult> allResults = allResultsBuilder.build();
    // This check also returns true if there are no actions
    if (allResults.stream().allMatch(a -> a.getResult() == Result.NO_OP)) {
        String detailedMessage = allResults.isEmpty() ? "actions field is empty" : allResults.stream().map(ActionResult::getMsg).collect(ImmutableList.toImmutableList()).toString();
        throw new EmptyChangeException(String.format("Feedback migration '%s' was noop. Detailed messages: %s", name, detailedMessage));
    }
}
Also used : Action(com.google.copybara.action.Action) ChangeMigrationFinishedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) SkylarkConsole(com.google.copybara.transform.SkylarkConsole) ActionResult(com.google.copybara.action.ActionResult) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) DestinationEffect(com.google.copybara.DestinationEffect) ChangeMigrationStartedEvent(com.google.copybara.monitor.EventMonitor.ChangeMigrationStartedEvent) EmptyChangeException(com.google.copybara.exception.EmptyChangeException)

Example 5 with Action

use of com.google.copybara.action.Action in project copybara by google.

the class GitModule method mirror.

@SuppressWarnings("unused")
@StarlarkMethod(name = "mirror", doc = "Mirror git references between repositories", parameters = { @Param(name = "name", named = true, doc = "Migration name"), @Param(name = "origin", named = true, doc = "Indicates the URL of the origin git repository"), @Param(name = "destination", named = true, doc = "Indicates the URL of the destination git repository"), @Param(name = "refspecs", allowedTypes = { @ParamType(type = Sequence.class, generic1 = String.class) }, named = true, defaultValue = "['refs/heads/*']", doc = "Represents a list of git refspecs to mirror between origin and destination." + " For example 'refs/heads/*:refs/remotes/origin/*' will mirror any reference" + " inside refs/heads to refs/remotes/origin."), @Param(name = "prune", named = true, doc = "Remove remote refs that don't have a origin counterpart. Prune is ignored if" + " actions are used (Action is in charge of doing the pruning)", defaultValue = "False"), @Param(name = "partial_fetch", defaultValue = "False", named = true, positional = false, doc = "This is an experimental feature that only works for certain origin globs."), @Param(name = "description", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, positional = false, doc = "A description of what this migration achieves", defaultValue = "None"), @Param(name = "actions", doc = "Experimental feature. " + "A list of mirror actions to perform, with the following semantics:\n" + "  - There is no guarantee of the order of execution.\n" + "  - Actions need to be independent from each other.\n" + "  - Failure in one action might prevent other actions from executing." + " --force can be used to continue for 'user' errors like non-fast-forward" + " errors.\n" + "\n" + "Actions will be in charge of doing the fetch, push, rebases, merges,etc." + "Only fetches/pushes for the declared refspec are allowed", defaultValue = "[]", positional = false, named = true) }, useStarlarkThread = true)
@UsesFlags(GitMirrorOptions.class)
public NoneType mirror(String name, String origin, String destination, // <String>
Sequence<?> strRefSpecs, Boolean prune, Boolean partialFetch, Object description, net.starlark.java.eval.Sequence<?> mirrorActions, StarlarkThread thread) throws EvalException {
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    GitOptions gitOptions = options.get(GitOptions.class);
    List<Refspec> refspecs = new ArrayList<>();
    for (String refspec : Sequence.cast(strRefSpecs, String.class, "refspecs")) {
        try {
            refspecs.add(Refspec.create(gitOptions.getGitEnvironment(generalOptions.getEnvironment()), generalOptions.getCwd(), refspec));
        } catch (InvalidRefspecException e) {
            throw Starlark.errorf("%s", e.getMessage());
        }
    }
    ImmutableList<Action> actions = convertActions(mirrorActions, printHandler);
    Module module = Module.ofInnermostEnclosingStarlarkFunction(thread);
    GlobalMigrations.getGlobalMigrations(module).addMigration(name, new Mirror(generalOptions, gitOptions, name, fixHttp(origin, thread.getCallerLocation()), fixHttp(destination, thread.getCallerLocation()), refspecs, options.get(GitMirrorOptions.class), prune, partialFetch, mainConfigFile, convertFromNoneable(description, null), actions));
    return Starlark.NONE;
}
Also used : GeneralOptions(com.google.copybara.GeneralOptions) StarlarkAction(com.google.copybara.action.StarlarkAction) Action(com.google.copybara.action.Action) ArrayList(java.util.ArrayList) LabelsAwareModule(com.google.copybara.config.LabelsAwareModule) Module(net.starlark.java.eval.Module) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Aggregations

Action (com.google.copybara.action.Action)5 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)3 SkylarkConsole (com.google.copybara.transform.SkylarkConsole)3 ArrayList (java.util.ArrayList)3 ImmutableList (com.google.common.collect.ImmutableList)2 DestinationEffect (com.google.copybara.DestinationEffect)2 ActionResult (com.google.copybara.action.ActionResult)2 StarlarkAction (com.google.copybara.action.StarlarkAction)2 LabelsAwareModule (com.google.copybara.config.LabelsAwareModule)2 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)2 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)2 ChangeMigrationFinishedEvent (com.google.copybara.monitor.EventMonitor.ChangeMigrationFinishedEvent)2 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)2 Module (net.starlark.java.eval.Module)2 DestinationRef (com.google.copybara.DestinationEffect.DestinationRef)1 GeneralOptions (com.google.copybara.GeneralOptions)1 ValidationException (com.google.copybara.exception.ValidationException)1 Feedback (com.google.copybara.feedback.Feedback)1 FinishHookContext (com.google.copybara.feedback.FinishHookContext)1 FolderModule (com.google.copybara.folder.FolderModule)1