Search in sources :

Example 16 with StarlarkMethod

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

the class GitModule method githubPrDestination.

@SuppressWarnings("unused")
@StarlarkMethod(name = "github_pr_destination", doc = "Creates changes in a new pull request in the destination.", parameters = { @Param(name = "url", named = true, doc = "Url of the GitHub project. For example" + " \"https://github.com/google/copybara'\""), @Param(name = "destination_ref", named = true, doc = "Destination reference for the change.", defaultValue = "'master'"), @Param(name = "pr_branch", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, defaultValue = "None", named = true, positional = false, doc = "Customize the pull request branch. Any variable present in the message in the " + "form of ${CONTEXT_REFERENCE} will be replaced by the corresponding stable " + "reference (head, PR number, Gerrit change number, etc.)."), @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 = "allow_empty_diff", defaultValue = "True", named = true, positional = false, doc = "By default, copybara migrates changes without checking existing PRs. " + "If set, copybara will skip pushing a change to an existing PR " + "only if the git three of the pending migrating change is the same " + "as the existing PR."), @Param(name = "title", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, defaultValue = "None", named = true, positional = false, doc = "When creating (or updating if `update_description` is set) a pull request, use" + " this title. By default it uses the change first line. This field accepts" + " a template with labels. For example: `\"Change ${CONTEXT_REFERENCE}\"`"), @Param(name = "body", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, defaultValue = "None", named = true, positional = false, doc = "When creating (or updating if `update_description` is set) a pull request, use" + " this body. By default it uses the change summary. This field accepts" + " a template with labels. For example: `\"Change ${CONTEXT_REFERENCE}\"`"), @Param(name = "integrates", allowedTypes = { @ParamType(type = Sequence.class, generic1 = GitIntegrateChanges.class), @ParamType(type = NoneType.class) }, named = true, defaultValue = "None", doc = "Integrate changes from a url present in the migrated change" + " label. Defaults to a semi-fake merge if COPYBARA_INTEGRATE_REVIEW label is" + " present in the message", positional = false), @Param(name = "api_checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker for the GitHub API endpoint provided for after_migration hooks. " + "This field is not required if the workflow hooks don't use the " + "origin/destination endpoints.", named = true, positional = false), @Param(name = "update_description", defaultValue = "False", named = true, positional = false, doc = "By default, Copybara only set the title and body of the PR when creating" + " the PR. If this field is set to true, it will update those fields for" + " every update."), @Param(name = "primary_branch_migration", defaultValue = "False", named = true, positional = false, doc = "When enabled, copybara will ignore the 'desination_ref' param if it is 'master' or" + " 'main' and instead try to establish the default git branch. If this fails," + " it will fall back to the param's declared value.\n" + "This is intended to help migrating to the new standard of using 'main'" + " without breaking users relying on the legacy default."), @Param(name = "checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker that validates the commit files & message. If `api_checker` is not" + " set, it will also be used for checking API calls. If only `api_checker`" + "is used, that checker will only apply to API calls.", named = true, positional = false) }, useStarlarkThread = true)
@UsesFlags({ GitDestinationOptions.class, GitHubDestinationOptions.class })
@Example(title = "Common usage", before = "Create a branch by using copybara's computerIdentity algorithm:", code = "git.github_pr_destination(\n" + "        url = \"https://github.com/google/copybara\",\n" + "        destination_ref = \"master\",\n" + "    )")
@Example(title = "Using pr_branch with label", before = "Customize pr_branch with context reference:", code = "git.github_pr_destination(\n" + "        url = \"https://github.com/google/copybara\",\n" + "         destination_ref = \"master\",\n" + "         pr_branch = 'test_${CONTEXT_REFERENCE}',\n" + "    )")
@Example(title = "Using pr_branch with constant string", before = "Customize pr_branch with a constant string:", code = "git.github_pr_destination(\n" + "        url = \"https://github.com/google/copybara\",\n" + "        destination_ref = \"master\",\n" + "        pr_branch = 'test_my_branch',\n" + "    )")
public GitHubPrDestination githubPrDestination(String url, String destinationRef, Object prBranch, Boolean partialFetch, Boolean allowEmptyDiff, Object title, Object body, Object integrates, Object apiChecker, Boolean updateDescription, Boolean primaryBranchMigrationMode, Object checker, StarlarkThread thread) throws EvalException {
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    // This restricts to github.com, we will have to revisit this to support setups like GitHub
    // Enterprise.
    check(GITHUB_COM.isGitHubUrl(url), "'%s' is not a valid GitHub url", url);
    GitDestinationOptions destinationOptions = options.get(GitDestinationOptions.class);
    GitHubOptions gitHubOptions = options.get(GitHubOptions.class);
    String destinationPrBranch = convertFromNoneable(prBranch, null);
    Checker apiCheckerObj = convertFromNoneable(apiChecker, null);
    Checker checkerObj = convertFromNoneable(checker, null);
    return new GitHubPrDestination(fixHttp(checkNotEmpty(firstNotNull(destinationOptions.url, url), "url"), thread.getCallerLocation()), destinationRef, convertFromNoneable(prBranch, null), partialFetch, generalOptions, options.get(GitHubOptions.class), destinationOptions, options.get(GitHubDestinationOptions.class), options.get(GitOptions.class), new GitHubPrWriteHook(generalOptions, url, gitHubOptions, destinationPrBranch, partialFetch, allowEmptyDiff, getGeneralConsole(), GITHUB_COM), Starlark.isNullOrNone(integrates) ? defaultGitIntegrate : Sequence.cast(integrates, GitIntegrateChanges.class, "integrates"), convertFromNoneable(title, null), convertFromNoneable(body, null), mainConfigFile, apiCheckerObj != null ? apiCheckerObj : checkerObj, updateDescription, GITHUB_COM, primaryBranchMigrationMode, checkerObj);
}
Also used : GeneralOptions(com.google.copybara.GeneralOptions) Checker(com.google.copybara.checks.Checker) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags) Example(com.google.copybara.doc.annotations.Example)

Example 17 with StarlarkMethod

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

the class GitModule method destination.

@SuppressWarnings("unused")
@StarlarkMethod(name = "destination", doc = "Creates a commit in a git repository using the transformed worktree.<br><br>For" + " GitHub use git.github_destination. For creating Pull Requests in GitHub, use" + " git.github_pr_destination. For creating a Gerrit change use" + " git.gerrit_destination.<br><br>Given that Copybara doesn't ask" + " for user/password in the console when doing the push to remote repos, you have to" + " use ssh protocol, have the credentials cached or use a credential manager.", parameters = { @Param(name = "url", named = true, doc = "Indicates the URL to push to as well as the URL from which to get the parent " + "commit"), @Param(name = "push", named = true, doc = "Reference to use for pushing the change, for example 'main'.", defaultValue = "'master'"), @Param(name = "tag_name", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "A template string that refers to a tag name. If tag_name exists, overwrite " + "this tag only if flag git-tag-overwrite is set. Note that tag creation is " + "best-effort and migration will succeed even if the tag cannot be created. " + "Usage: Users can use a string or a string with a label. " + "For instance ${label}_tag_name. And the value of label must be " + "in changes' label list. Otherwise, tag won't be created.", defaultValue = "None"), @Param(name = "tag_msg", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "A template string that refers to the commit msg of a tag. If set, we will " + "create an annotated tag when tag_name is set. Usage: Users can use a string " + "or a string with a label. For instance ${label}_message. And the value of " + "label must be in changes' label list. Otherwise, tag will be created with " + "sha1's commit msg.", defaultValue = "None"), @Param(name = "fetch", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "Indicates the ref from which to get the parent commit. Defaults to push value" + " if None", defaultValue = "None"), @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 = "integrates", named = true, allowedTypes = { @ParamType(type = Sequence.class, generic1 = GitIntegrateChanges.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "Integrate changes from a url present in the migrated change" + " label. Defaults to a semi-fake merge if COPYBARA_INTEGRATE_REVIEW label is" + " present in the message", positional = false), @Param(name = "primary_branch_migration", allowedTypes = { @ParamType(type = Boolean.class) }, defaultValue = "False", named = true, positional = false, doc = "When enabled, copybara will ignore the 'push' and 'fetch' params if either is" + " 'master' or 'main' and instead try to establish the default git branch. If" + " this fails, it will fall back to the param's declared value.\n" + "This is intended to help migrating to the new standard of using 'main'" + " without breaking users relying on the legacy default."), @Param(name = "checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker that can check leaks or other checks in the commit created. ", named = true, positional = false) }, useStarlarkThread = true)
@UsesFlags(GitDestinationOptions.class)
public GitDestination destination(String url, String push, Object tagName, Object tagMsg, Object fetch, boolean partialFetch, Object integrates, Boolean primaryBranchMigration, Object checker, StarlarkThread thread) throws EvalException {
    GitDestinationOptions destinationOptions = options.get(GitDestinationOptions.class);
    String resolvedPush = checkNotEmpty(firstNotNull(destinationOptions.push, push), "push");
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    Checker maybeChecker = convertFromNoneable(checker, null);
    if (maybeChecker != null && options.get(GitDestinationOptions.class).skipGitChecker) {
        maybeChecker = null;
        getGeneralConsole().warn("Skipping git checker for git.destination. Note that this could" + " cause leaks or other problems");
    }
    return new GitDestination(fixHttp(checkNotEmpty(firstNotNull(destinationOptions.url, url), "url"), thread.getCallerLocation()), checkNotEmpty(firstNotNull(destinationOptions.fetch, convertFromNoneable(fetch, null), resolvedPush), "fetch"), resolvedPush, partialFetch, primaryBranchMigration, convertFromNoneable(tagName, null), convertFromNoneable(tagMsg, null), destinationOptions, options.get(GitOptions.class), generalOptions, new DefaultWriteHook(), Starlark.isNullOrNone(integrates) ? defaultGitIntegrate : Sequence.cast(integrates, GitIntegrateChanges.class, "integrates"), maybeChecker);
}
Also used : DefaultWriteHook(com.google.copybara.git.GitDestination.WriterImpl.DefaultWriteHook) GeneralOptions(com.google.copybara.GeneralOptions) Checker(com.google.copybara.checks.Checker) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 18 with StarlarkMethod

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

the class GitModule method githubApi.

@SuppressWarnings("unused")
@StarlarkMethod(name = GITHUB_API, doc = "Defines a feedback API endpoint for GitHub, that exposes relevant GitHub API" + " operations.", 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.", named = true) }, useStarlarkThread = true)
@UsesFlags(GitHubOptions.class)
public EndpointProvider<GitHubEndPoint> githubApi(String url, Object checkerObj, StarlarkThread thread) throws EvalException {
    checkNotEmpty(url, "url");
    String cleanedUrl = fixHttp(url, thread.getCallerLocation());
    Checker checker = convertFromNoneable(checkerObj, null);
    validateEndpointChecker(checker, GITHUB_API);
    GitHubOptions gitHubOptions = options.get(GitHubOptions.class);
    return EndpointProvider.wrap(new GitHubEndPoint(gitHubOptions.newGitHubApiSupplier(cleanedUrl, checker, GITHUB_COM), cleanedUrl, getGeneralConsole(), GITHUB_COM));
}
Also used : Checker(com.google.copybara.checks.Checker) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 19 with StarlarkMethod

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

the class RemoteFileModule method gitHubTarball.

@SuppressWarnings("unused")
@StarlarkMethod(name = "github_archive", doc = "A tarball for a specific SHA1 on GitHub. Experimental.", documented = false, parameters = { @Param(name = "project", named = true, defaultValue = "[]", doc = "The GitHub project from which to load the file, e.g. google/copybara"), @Param(name = "revision", named = true, defaultValue = "[]", doc = "The revision to download from the project, typically a commit SHA1."), @Param(name = "type", named = true, defaultValue = "'TARBALL'", doc = "Archive type to download, options are 'TARBALL' or 'ZIP'.") })
@UsesFlags(RemoteFileOptions.class)
public GithubArchive gitHubTarball(String project, String revision, String type) throws EvalException {
    GeneralOptions generalOptions = options.get(GeneralOptions.class);
    RemoteFileOptions remoteFileOptions = options.get(RemoteFileOptions.class);
    try {
        return new GithubArchive(project, revision, Enums.getIfPresent(GithubArchive.Type.class, type).toJavaUtil().orElseThrow(() -> errorf("Unsupported archive type: '%s'. " + "Supported values: %s", type, Arrays.asList(GithubArchive.Type.values()))), remoteFileOptions.getTransport(), generalOptions.profiler(), generalOptions.console());
    } catch (ValidationException e) {
        throw Starlark.errorf("Error setting up remote http file: %s", e.getMessage());
    }
}
Also used : GeneralOptions(com.google.copybara.GeneralOptions) ValidationException(com.google.copybara.exception.ValidationException) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 20 with StarlarkMethod

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

the class Core method todoReplace.

@SuppressWarnings("unused")
@StarlarkMethod(name = "todo_replace", doc = "Replace Google style TODOs. For example `TODO(username, othername)`.", parameters = { @Param(name = "tags", named = true, allowedTypes = { @ParamType(type = net.starlark.java.eval.Sequence.class, generic1 = String.class) }, doc = "Prefix tag to look for", defaultValue = "['TODO', 'NOTE']"), @Param(name = "mapping", named = true, doc = "Mapping of users/strings", defaultValue = "{}"), @Param(name = "mode", named = true, doc = "Mode for the replace:<ul><li>'MAP_OR_FAIL': Try to use the mapping and if not" + " found fail.</li><li>'MAP_OR_IGNORE': Try to use the mapping but ignore if" + " no mapping found.</li><li>'MAP_OR_DEFAULT': Try to use the mapping and use" + " the default if not found.</li><li>'SCRUB_NAMES': Scrub all names from" + " TODOs. Transforms 'TODO(foo)' to 'TODO'</li><li>'USE_DEFAULT': Replace any" + " TODO(foo, bar) with TODO(default_string)</li></ul>", defaultValue = "'MAP_OR_IGNORE'"), @Param(name = "paths", named = true, allowedTypes = { @ParamType(type = Glob.class), @ParamType(type = NoneType.class) }, doc = "A glob expression relative to the workdir representing the files to apply the" + " transformation. For example, glob([\"**.java\"]), matches all java files" + " recursively. Defaults to match all the files recursively.", defaultValue = "None"), @Param(name = "default", named = true, allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, doc = "Default value if mapping not found. Only valid for 'MAP_OR_DEFAULT' or" + " 'USE_DEFAULT' modes", defaultValue = "None"), @Param(name = "ignore", named = true, allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, doc = "If set, elements within TODO (with usernames) that match the regex will be " + "ignored. For example ignore = \"foo\" would ignore \"foo\" in " + "\"TODO(foo,bar)\" but not \"bar\".", defaultValue = "None") }, useStarlarkThread = true)
@DocDefault(field = "paths", value = "glob([\"**\"])")
@Example(title = "Simple update", before = "Replace TODOs and NOTES for users in the mapping:", code = "core.todo_replace(\n" + "  mapping = {\n" + "    'test1' : 'external1',\n" + "    'test2' : 'external2'\n" + "  }\n" + ")", after = "Would replace texts like TODO(test1) or NOTE(test1, test2) with TODO(external1)" + " or NOTE(external1, external2)")
@Example(title = "Scrubbing", before = "Remove text from inside TODOs", code = "core.todo_replace(\n" + "  mode = 'SCRUB_NAMES'\n" + ")", after = "Would replace texts like TODO(test1): foo or NOTE(test1, test2):foo with TODO:foo" + " and NOTE:foo")
@Example(title = "Ignoring Regex Patterns", before = "Ignore regEx inside TODOs when scrubbing/mapping", code = "core.todo_replace(\n" + "  mapping = { 'aaa' : 'foo'},\n" + "  ignore = 'b/.*'\n)", after = "Would replace texts like TODO(b/123, aaa) with TODO(b/123, foo)")
public TodoReplace todoReplace(// <String>
net.starlark.java.eval.Sequence<?> skyTags, // <String, String>
Dict<?, ?> skyMapping, String modeStr, Object paths, Object skyDefault, Object regexToIgnore, StarlarkThread thread) throws EvalException {
    Mode mode = stringToEnum("mode", modeStr, Mode.class);
    Map<String, String> mapping = SkylarkUtil.convertStringMap(skyMapping, "mapping");
    String defaultString = convertFromNoneable(skyDefault, /*defaultValue=*/
    null);
    ImmutableList<String> tags = ImmutableList.copyOf(SkylarkUtil.convertStringList(skyTags, "tags"));
    String ignorePattern = convertFromNoneable(regexToIgnore, null);
    Pattern regexIgnorelist = ignorePattern != null ? Pattern.compile(ignorePattern) : null;
    check(!tags.isEmpty(), "'tags' cannot be empty");
    if (mode == Mode.MAP_OR_DEFAULT || mode == Mode.USE_DEFAULT) {
        check(defaultString != null, "'default' needs to be set for mode '%s'", mode);
    } else {
        check(defaultString == null, "'default' cannot be used for mode '%s'", mode);
    }
    if (mode == Mode.USE_DEFAULT || mode == Mode.SCRUB_NAMES) {
        check(mapping.isEmpty(), "'mapping' cannot be used with mode %s", mode);
    }
    return new TodoReplace(thread.getCallerLocation(), convertFromNoneable(paths, Glob.ALL_FILES), tags, mode, mapping, defaultString, workflowOptions.parallelizer(), regexIgnorelist);
}
Also used : Pattern(com.google.re2j.Pattern) Mode(com.google.copybara.transform.TodoReplace.Mode) TodoReplace(com.google.copybara.transform.TodoReplace) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) DocDefault(com.google.copybara.doc.annotations.DocDefault) 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