Search in sources :

Example 31 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class GitModule method gerritApi.

@SuppressWarnings("unused")
@StarlarkMethod(name = GERRIT_API, doc = "" + "Defines a feedback API endpoint for Gerrit, that exposes relevant Gerrit API " + "operations.", parameters = { @Param(name = "url", doc = "Indicates the Gerrit repo URL.", named = true), @Param(name = "checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker for the Gerrit API transport.", named = true) }, useStarlarkThread = true)
@UsesFlags(GerritOptions.class)
public EndpointProvider<GerritEndpoint> gerritApi(String url, Object checkerObj, StarlarkThread thread) throws EvalException {
    checkNotEmpty(url, "url");
    String cleanedUrl = fixHttp(url, thread.getCallerLocation());
    Checker checker = convertFromNoneable(checkerObj, null);
    validateEndpointChecker(checker, GERRIT_API);
    GerritOptions gerritOptions = options.get(GerritOptions.class);
    return EndpointProvider.wrap(new GerritEndpoint(gerritOptions.newGerritApiSupplier(cleanedUrl, checker), cleanedUrl, getGeneralConsole()));
}
Also used : Checker(com.google.copybara.checks.Checker) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 32 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class GitModule method gitHubTrigger.

@SuppressWarnings("unused")
@StarlarkMethod(name = GITHUB_TRIGGER, doc = "Defines a feedback trigger based on updates on a GitHub PR.", parameters = { @Param(name = "url", doc = "Indicates the GitHub repo URL.", named = true), @Param(name = "checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker for the GitHub API transport provided by this trigger.", named = true), @Param(name = "events", allowedTypes = { @ParamType(type = Sequence.class, generic1 = String.class), @ParamType(type = Dict.class, generic1 = Sequence.class) }, named = true, defaultValue = "[]", doc = "Type of events to subscribe. Valid values are: `'ISSUES'`, `'ISSUE_COMMENT'`," + " `'PULL_REQUEST'`,  `'PULL_REQUEST_REVIEW_COMMENT'`, `'PUSH'`," + " `'STATUS'`, `'CHECK_RUNS'`") }, useStarlarkThread = true)
@UsesFlags(GitHubOptions.class)
public GitHubTrigger gitHubTrigger(String url, Object checkerObj, Object events, StarlarkThread thread) throws EvalException {
    checkNotEmpty(url, "url");
    url = fixHttp(url, thread.getCallerLocation());
    Checker checker = convertFromNoneable(checkerObj, null);
    LinkedHashSet<EventTrigger> eventBuilder = new LinkedHashSet<>();
    LinkedHashSet<GitHubEventType> types = new LinkedHashSet<>();
    ImmutableSet<EventTrigger> parsedEvents = handleEventTypes(events, eventBuilder, types);
    validateEndpointChecker(checker, GITHUB_TRIGGER);
    GitHubOptions gitHubOptions = options.get(GitHubOptions.class);
    return new GitHubTrigger(gitHubOptions.newGitHubApiSupplier(url, checker, GITHUB_COM), url, parsedEvents, getGeneralConsole(), GITHUB_COM);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Checker(com.google.copybara.checks.Checker) GitHubEventType(com.google.copybara.git.github.api.GitHubEventType) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 33 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod 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)

Example 34 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class RemoteFileModule method remoteArchiveOrigin.

@StarlarkMethod(name = "origin", doc = "Defines a remote file origin. This is a WIP and experimental. Do not use. ", parameters = { @Param(name = "unpack_method", defaultValue = "None", doc = "The method by which to unpack the remote file. Currently 'zip' allowed. 'tar' and" + " 'as-is' to be added soon.", named = true), @Param(name = "author", defaultValue = "'Copybara <noreply@copybara.io>'", doc = "Author to attribute the change to", named = true), // TODO(joshgoldman): support labels in addition to message
@Param(name = "message", defaultValue = "'Placeholder message'", doc = "Message to attach to the change", named = true) })
@UsesFlags(RemoteFileOptions.class)
public RemoteArchiveOrigin remoteArchiveOrigin(String fileType, String author, String message) throws EvalException, ValidationException {
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    RemoteFileOptions remoteFileOptions = options.get(RemoteFileOptions.class);
    return new RemoteArchiveOrigin(fileType, Author.parse(author), message, remoteFileOptions.getTransport(), generalOptions.profiler(), remoteFileOptions);
}
Also used : GeneralOptions(com.google.copybara.GeneralOptions) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 35 with StarlarkMethod

use of net.starlark.java.annot.StarlarkMethod in project copybara by google.

the class MetadataModule method scrubber.

@SuppressWarnings("unused")
@StarlarkMethod(name = "scrubber", doc = "Removes part of the change message using a regex", parameters = { @Param(name = "regex", named = true, doc = "Any text matching the regex will be removed. Note that the regex is" + " runs in multiline mode."), @Param(name = "msg_if_no_match", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "If set, Copybara will use this text when the scrubbing regex doesn't match.", defaultValue = "None"), @Param(name = "fail_if_no_match", named = true, doc = "If set, msg_if_no_match must be None and then fail if the scrubbing " + "regex doesn't match. ", defaultValue = "False"), @Param(name = "replacement", named = true, doc = "Text replacement for the matching substrings. References to regex group" + " numbers can be used in the form of $1, $2, etc.", defaultValue = "''") }, useStarlarkThread = true)
@Example(title = "Remove from a keyword to the end of the message", before = "When change messages are in the following format:\n\n" + "```\n" + "Public change description\n\n" + "This is a public description for a commit\n\n" + "CONFIDENTIAL:\n" + "This fixes internal project foo-bar\n" + "```\n\n" + "Using the following transformation:", code = "metadata.scrubber('(^|\\n)CONFIDENTIAL:(.|\\n)*')", after = "Will remove the confidential part, leaving the message as:\n\n" + "```\n" + "Public change description\n\n" + "This is a public description for a commit\n" + "```\n\n")
@Example(title = "Keep only message enclosed in tags", before = "The previous example is prone to leak confidential information since a developer could" + " easily forget to include the CONFIDENTIAL label. A different approach for this" + " is to scrub everything by default except what is explicitly allowed. For" + " example, the following scrubber would remove anything not enclosed in" + " &lt;public&gt;&lt;/public&gt; tags:\n", code = "metadata.scrubber('^(?:\\n|.)*<public>((?:\\n|.)*)</public>(?:\\n|.)*$', " + "replacement = '$1')", after = "So a message like:\n\n" + "```\n" + "this\nis\nvery confidential<public>but this is public\nvery public\n</public>" + "\nand this is a secret too\n" + "```\n\n" + "would be transformed into:\n\n" + "```\n" + "but this is public\nvery public\n" + "```\n\n")
@Example(title = "Use default msg when the scrubbing regex doesn't match", before = "Assign msg_if_no_match a default msg. For example:\n", code = "metadata.scrubber('^(?:\\n|.)*<public>((?:\\n|.)*)</public>(?:\\n|.)*$', " + "msg_if_no_match = 'Internal Change.', replacement = '$1')", after = "So a message like:\n\n" + "```\n" + "this\nis\nvery confidential\nThis is not public msg.\n" + "\nand this is a secret too\n" + "```\n\n" + "would be transformed into:\n\n" + "```\n" + "Internal Change.\n" + "```\n\n")
@Example(title = "Fail if the scrubbing regex doesn't match", before = "Set fail_if_no_match to true", code = "metadata.scrubber('^(?:\\n|.)*<public>((?:\\n|.)*)</public>(?:\\n|.)*$', " + "fail_if_no_match = True, replacement = '$1')", after = "So a message like:\n\n" + "```\n" + "this\n" + "is\n" + "very confidential\n" + "but this is not public\n" + "\n" + "and this is a secret too\n" + "\n" + "```\n\n" + "This would fail. Error msg:\n\n" + "```\n" + "Scrubber regex: \'^(?:\\n" + "|.)*<public>((?:\\n" + "|.)*)</public>(?:\\n" + "|.)*$\' didn't match for description: this\n" + "is\n" + "very confidential\n" + "but this is not public\n" + "\n" + "and this is a secret too\n" + "```\n\n")
public Transformation scrubber(String regex, Object msgIfNoMatchObj, Boolean failIfNoMatch, String replacement, StarlarkThread thread) throws EvalException {
    Pattern pattern;
    try {
        pattern = Pattern.compile(regex, Pattern.MULTILINE);
    } catch (PatternSyntaxException e) {
        throw Starlark.errorf("Invalid regex expression: %s", e.getMessage());
    }
    String msgIfNoMatch = convertFromNoneable(msgIfNoMatchObj, null);
    check(!failIfNoMatch || msgIfNoMatch == null, "If fail_if_no_match is true, msg_if_no_match should be None.");
    return new Scrubber(pattern, msgIfNoMatch, failIfNoMatch, replacement, thread.getCallerLocation());
}
Also used : Pattern(com.google.re2j.Pattern) PatternSyntaxException(com.google.re2j.PatternSyntaxException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) Example(com.google.copybara.doc.annotations.Example)

Aggregations

StarlarkMethod (net.starlark.java.annot.StarlarkMethod)37 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)17 DocDefault (com.google.copybara.doc.annotations.DocDefault)9 Checker (com.google.copybara.checks.Checker)8 Example (com.google.copybara.doc.annotations.Example)8 GeneralOptions (com.google.copybara.GeneralOptions)7 ValidationException (com.google.copybara.exception.ValidationException)7 Pattern (com.google.re2j.Pattern)6 SubmoduleStrategy (com.google.copybara.git.GitOrigin.SubmoduleStrategy)4 PatchTransformation (com.google.copybara.transform.patch.PatchTransformation)4 ImmutableList (com.google.common.collect.ImmutableList)3 LabelsAwareModule (com.google.copybara.config.LabelsAwareModule)3 SkylarkTransformation (com.google.copybara.transform.SkylarkTransformation)3 Transformations.toTransformation (com.google.copybara.transform.Transformations.toTransformation)3 IOException (java.io.IOException)3 Module (net.starlark.java.eval.Module)3 Action (com.google.copybara.action.Action)2 StarlarkAction (com.google.copybara.action.StarlarkAction)2 RepoException (com.google.copybara.exception.RepoException)2 FolderModule (com.google.copybara.folder.FolderModule)2