Search in sources :

Example 1 with Checker

use of com.google.copybara.checks.Checker in project copybara by google.

the class GitModule method gerritTrigger.

@SuppressWarnings("unused")
@StarlarkMethod(name = GERRIT_TRIGGER, doc = "Defines a feedback trigger based on updates on a Gerrit change.", 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 provided by this trigger.", named = true) }, useStarlarkThread = true)
@UsesFlags(GerritOptions.class)
public GerritTrigger gerritTrigger(String url, Object checkerObj, StarlarkThread thread) throws EvalException {
    checkNotEmpty(url, "url");
    url = fixHttp(url, thread.getCallerLocation());
    Checker checker = convertFromNoneable(checkerObj, null);
    validateEndpointChecker(checker, GERRIT_TRIGGER);
    GerritOptions gerritOptions = options.get(GerritOptions.class);
    return new GerritTrigger(gerritOptions.newGerritApiSupplier(url, checker), url, getGeneralConsole());
}
Also used : Checker(com.google.copybara.checks.Checker) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags)

Example 2 with Checker

use of com.google.copybara.checks.Checker 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 3 with Checker

use of com.google.copybara.checks.Checker 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 4 with Checker

use of com.google.copybara.checks.Checker 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 5 with Checker

use of com.google.copybara.checks.Checker in project copybara by google.

the class GitModule method gitHubDestination.

@SuppressWarnings("unused")
@StarlarkMethod(name = "github_destination", doc = "Creates a commit in a GitHub repository branch (for example master). For creating Pull" + "Request use git.github_pr_destination.", 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 = "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 = "pr_branch_to_update", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, doc = "A template string that refers to a pull request branch in the same repository" + " will be updated to current commit of this push branch only if" + " pr_branch_to_update exists. The reason behind this field is that" + " presubmiting changes creates and leaves a pull request open. By using" + " this, we can automerge/close this type of pull requests. As a result," + " users will see this pr_branch_to_update as merged to this push branch." + " Usage: Users can use a string or a string with a label. For instance" + " ${label}_pr_branch_name. And the value of label must be in changes' label" + " list. Otherwise, nothing will happen.", defaultValue = "None"), @Param(name = "partial_fetch", defaultValue = "False", named = true, doc = "This is an experimental feature that only works for certain origin globs."), @Param(name = "delete_pr_branch", allowedTypes = { @ParamType(type = Boolean.class), @ParamType(type = NoneType.class) }, named = true, doc = "When `pr_branch_to_update` is enabled, it will delete the branch reference" + " after the push to the branch and main branch (i.e master) happens. This" + " allows to cleanup temporary branches created for testing.", defaultValue = "None"), @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 = "api_checker", allowedTypes = { @ParamType(type = Checker.class), @ParamType(type = NoneType.class) }, defaultValue = "None", doc = "A checker for the Gerrit 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 = "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 = "tag_name", allowedTypes = { @ParamType(type = String.class), @ParamType(type = NoneType.class) }, named = true, positional = false, doc = "A template string that specifies to a tag name. If the tag already exists, " + "copybara will only overwrite it if the --git-tag-overwrite flag is set." + "\nNote that tag creation is " + "best-effort and the 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, positional = false, doc = "A template string that refers to the commit msg for a tag. If set, copybara will" + "create an annotated tag with this custom message\n" + "Usage: Labels in the string will be resolved. E.g. .${label}_message." + "By default, the tag will be created with the labeled commit's message.", defaultValue = "None"), @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)
// Used to detect in the future users that don't set it and change the default
@DocDefault(field = "delete_pr_branch", value = "False")
public GitDestination gitHubDestination(String url, String push, Object fetch, Object prBranchToUpdate, Boolean partialFetch, Object deletePrBranchParam, Object integrates, Object apiChecker, Boolean primaryBranchMigration, Object tagName, Object tagMsg, 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);
    String repoUrl = fixHttp(checkNotEmpty(firstNotNull(destinationOptions.url, url), "url"), thread.getCallerLocation());
    String branchToUpdate = convertFromNoneable(prBranchToUpdate, null);
    Boolean deletePrBranch = convertFromNoneable(deletePrBranchParam, null);
    check(branchToUpdate != null || deletePrBranch == null, "'delete_pr_branch' can only be set if 'pr_branch_to_update' is used");
    GitHubOptions gitHubOptions = options.get(GitHubOptions.class);
    WorkflowOptions workflowOptions = options.get(WorkflowOptions.class);
    String effectivePrBranchToUpdate = branchToUpdate;
    if (options.get(WorkflowOptions.class).isInitHistory()) {
        generalOptions.console().infoFmt("Ignoring field 'pr_branch_to_update' as '--init-history' is set.");
        effectivePrBranchToUpdate = null;
    }
    // First flag has priority, then field, and then (for now) we set it to false.
    // TODO(malcon): Once this is stable the default will be 'branchToUpdate != null'
    boolean effectiveDeletePrBranch = gitHubOptions.gitHubDeletePrBranch != null ? gitHubOptions.gitHubDeletePrBranch : deletePrBranch != null ? deletePrBranch : false;
    Checker apiCheckerObj = convertFromNoneable(apiChecker, null);
    Checker checkerObj = convertFromNoneable(checker, null);
    return new GitDestination(repoUrl, 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 GitHubWriteHook(generalOptions, repoUrl, gitHubOptions, effectivePrBranchToUpdate, effectiveDeletePrBranch, getGeneralConsole(), apiCheckerObj != null ? apiCheckerObj : checkerObj, GITHUB_COM), Starlark.isNullOrNone(integrates) ? defaultGitIntegrate : Sequence.cast(integrates, GitIntegrateChanges.class, "integrates"), checkerObj);
}
Also used : GeneralOptions(com.google.copybara.GeneralOptions) Checker(com.google.copybara.checks.Checker) WorkflowOptions(com.google.copybara.WorkflowOptions) StarlarkMethod(net.starlark.java.annot.StarlarkMethod) UsesFlags(com.google.copybara.doc.annotations.UsesFlags) DocDefault(com.google.copybara.doc.annotations.DocDefault)

Aggregations

Checker (com.google.copybara.checks.Checker)9 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)8 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)8 GeneralOptions (com.google.copybara.GeneralOptions)3 DocDefault (com.google.copybara.doc.annotations.DocDefault)2 WorkflowOptions (com.google.copybara.WorkflowOptions)1 ApiChecker (com.google.copybara.checks.ApiChecker)1 Example (com.google.copybara.doc.annotations.Example)1 ChangeIdPolicy (com.google.copybara.git.GerritDestination.ChangeIdPolicy)1 NotifyOption (com.google.copybara.git.GerritDestination.NotifyOption)1 DefaultWriteHook (com.google.copybara.git.GitDestination.WriterImpl.DefaultWriteHook)1 GitHubEventType (com.google.copybara.git.github.api.GitHubEventType)1 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)1 LinkedHashSet (java.util.LinkedHashSet)1 Before (org.junit.Before)1