Search in sources :

Example 16 with RepoException

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

the class GitRepository method showRef.

/**
 * Execute show-ref git command in the local repository and returns a map from reference name to
 * GitReference(SHA-1).
 */
protected ImmutableMap<String, GitRevision> showRef(Iterable<String> refs) throws RepoException {
    ImmutableMap.Builder<String, GitRevision> result = ImmutableMap.builder();
    CommandOutput commandOutput = gitAllowNonZeroExit(CommandRunner.NO_INPUT, ImmutableList.<String>builder().add("show-ref").addAll(refs).build());
    if (!commandOutput.getStderr().isEmpty()) {
        throw new RepoException(String.format("Error executing show-ref on %s git repo:\n%s", getGitDir(), commandOutput.getStderr()));
    }
    for (String line : Splitter.on('\n').split(commandOutput.getStdout())) {
        if (line.isEmpty()) {
            continue;
        }
        List<String> strings = Splitter.on(' ').splitToList(line);
        Preconditions.checkState(strings.size() == 2 && SHA1_PATTERN.matcher(strings.get(0)).matches(), "Cannot parse line: '%s'", line);
        // Ref -> SHA1
        result.put(strings.get(1), new GitRevision(this, strings.get(0)));
    }
    return result.build();
}
Also used : CommandOutput(com.google.copybara.util.CommandOutput) RepoException(com.google.copybara.exception.RepoException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 17 with RepoException

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

the class GitRepository method lsRemote.

/**
 * Runs a git ls-remote from the current directory for a repository url. Assumes the path to the
 * git binary is already set. You don't have to be in a git repository to run this command. Does
 * not work with remote names.
 *
 * @param url - see <repository> in git help ls-remote
 * @param refs - see <refs> in git help ls-remote
 * @param env - determines where the Git binaries are
 * @param maxLogLines - Limit log lines to the number specified. -1 for unlimited
 * @return - a map of refs to sha1 from the git ls-remote output.
 */
public static Map<String, String> lsRemote(String url, Collection<String> refs, Map<String, String> env, int maxLogLines) throws RepoException {
    ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
    List<String> args = Lists.newArrayList("ls-remote", validateUrl(url));
    args.addAll(refs);
    CommandOutputWithStatus output;
    try {
        output = executeGit(FileSystems.getDefault().getPath("."), args, env, false, maxLogLines);
    } catch (BadExitStatusWithOutputException e) {
        throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s': Exit code %s, Output:\n%s", url, refs, e.getOutput().getTerminationStatus().getExitCode(), e.getOutput().getStderr()), e);
    } catch (CommandException e) {
        throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s'", url, refs), e);
    }
    if (output.getTerminationStatus().success()) {
        for (String line : Splitter.on('\n').split(output.getStdout())) {
            if (line.isEmpty()) {
                continue;
            }
            Matcher matcher = LS_REMOTE_OUTPUT_LINE.matcher(line);
            if (!matcher.matches()) {
                throw new RepoException("Unexpected format for ls-remote output: " + line);
            }
            result.put(matcher.group(2), matcher.group(1));
        }
    }
    return result.build();
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) Matcher(com.google.re2j.Matcher) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 18 with RepoException

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

the class GithubPROrigin method newReader.

@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
    return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
    false, submoduleStrategy, firstParent) {

        /**
         * Disable rebase since this is controlled by useMerge field.
         */
        @Override
        protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
        }

        @Override
        public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
            if (!baselineFromBranch) {
                return super.findBaseline(startRevision, label);
            }
            return findBaselinesWithoutLabel(startRevision, /*limit=*/
            1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
        }

        @Override
        public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
            String baseline = startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1);
            Preconditions.checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
            GitRevision baselineRev = getRepository().resolveReference(baseline);
            // Don't skip the first change as it is already the baseline
            BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
            false);
            visitChanges(baselineRev, visitor);
            return visitor.getResult();
        }

        @Override
        public Endpoint getFeedbackEndPoint() {
            return new GitHubEndPoint(githubOptions, url);
        }

        /**
         * Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
         * commit doesn't work for this case.
         */
        @Override
        public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException {
            if (!useMerge) {
                return super.changes(fromRef, toRef);
            }
            GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
            // Fast-forward merge
            if (merge.getParents().size() == 1) {
                return super.changes(fromRef, toRef);
            }
            // HEAD of the Pull Request
            GitRevision gitRevision = merge.getParents().get(1);
            ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
            // origin_files
            if (prChanges.isEmpty()) {
                return prChanges;
            }
            try {
                return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
            } catch (EmptyChangeException e) {
                throw new RepoException("Error getting the merge commit information: " + merge, e);
            }
        }

        @Nullable
        @Override
        public String getGroupIdentity(GitRevision rev) throws RepoException {
            return rev.associatedLabels().get(GITHUB_PR_NUMBER_LABEL);
        }
    };
}
Also used : Path(java.nio.file.Path) Iterables(com.google.common.collect.Iterables) Origin(com.google.copybara.Origin) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) Collections2(com.google.common.collect.Collections2) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Issue(com.google.copybara.git.github.api.Issue) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Label(com.google.copybara.git.github.api.Issue.Label) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ImmutableMap(com.google.common.collect.ImmutableMap) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Set(java.util.Set) Console(com.google.copybara.util.console.Console) Sets(com.google.common.collect.Sets) GithubUtil.getProjectNameFromUrl(com.google.copybara.git.github.util.GithubUtil.getProjectNameFromUrl) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubUtil.asGithubUrl(com.google.copybara.git.github.util.GithubUtil.asGithubUrl) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GithubUtil(com.google.copybara.git.github.util.GithubUtil) Collections(java.util.Collections) GithubPrUrl(com.google.copybara.git.github.util.GithubUtil.GithubPrUrl) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Change(com.google.copybara.Change) RepoException(com.google.copybara.exception.RepoException) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 19 with RepoException

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

the class GitCredential method fill.

/**
 * Execute 'git credential fill' for a url
 *
 * @param cwd the directory to execute the command. This is important if credential configuration
 * is set in the local git config.
 * @param url url to get the credentials from
 * @return a username and password
 * @throws RepoException If the url doesn't have protocol (For example https://), the url is
 * not valid or username/password couldn't be found
 */
public UserPassword fill(Path cwd, String url) throws RepoException, ValidationException {
    Map<String, String> env = Maps.newHashMap(this.env);
    // Prevent from asking user for the password (It goes directly to the terminal, not to the
    // passed InputStream/OutputStream.
    env.put("GIT_TERMINAL_PROMPT", "0");
    URI uri;
    try {
        uri = URI.create(url);
    } catch (IllegalArgumentException e) {
        throw new ValidationException(e, "Cannot get credentials for " + url);
    }
    String protocol = uri.getScheme();
    if (Strings.isNullOrEmpty(protocol)) {
        throw new ValidationException("Cannot find the protocol for " + url);
    }
    String host = uri.getHost();
    Command cmd = new Command(new String[] { gitBinary, "credential", "fill" }, env, cwd.toFile());
    String request = String.format("protocol=%s\nhost=%s\n", protocol, host);
    if (!Strings.isNullOrEmpty(uri.getPath())) {
        request += String.format("path=%s\n", uri.getPath());
    }
    request += "\n";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    try {
        // DON'T REPLACE THIS WITH CommandRunner.execute(). WE DON'T WANT TO ACCIDENTALLY LOG THE
        // PASSWORD!
        CommandResult result = cmd.execute(new ByteArrayInputStream(request.getBytes(UTF_8)), new TimeoutKillableObserver(timeout.toMillis()), out, err);
        if (!result.getTerminationStatus().success()) {
            throw new RepoException("Error getting credentials:\n" + new String(err.toByteArray(), UTF_8));
        }
    } catch (BadExitStatusException e) {
        String errStr = new String(err.toByteArray(), UTF_8);
        if (errStr.contains("could not read")) {
            throw new ValidationException("Interactive prompting of passwords for git is disabled," + " use git credential store before calling Copybara.");
        }
        throw new RepoException("Error getting credentials:\n" + errStr, e);
    } catch (CommandException e) {
        throw new RepoException("Error getting credentials", e);
    }
    Map<String, String> map = Splitter.on(NEW_LINE).omitEmptyStrings().withKeyValueSeparator("=").split(new String(out.toByteArray(), UTF_8));
    if (!map.containsKey("username")) {
        throw new RepoException("git credentials for " + url + " didn't return a username");
    }
    if (!map.containsKey("password")) {
        throw new RepoException("git credentials for " + url + " didn't return a password");
    }
    return new UserPassword(map.get("username"), map.get("password"));
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) TimeoutKillableObserver(com.google.copybara.shell.TimeoutKillableObserver) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) URI(java.net.URI) CommandResult(com.google.copybara.shell.CommandResult) Command(com.google.copybara.shell.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) BadExitStatusException(com.google.copybara.shell.BadExitStatusException)

Example 20 with RepoException

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

the class GitHubEndPoint method createStatus.

@SkylarkCallable(name = "create_status", doc = "Create or update a status for a commit. Returns the status created.", parameters = { @Param(name = "sha", type = String.class, named = true, doc = "The SHA-1 for which we want to create or update the status"), @Param(name = "state", type = String.class, named = true, doc = "The state of the commit status: 'success', 'error', 'pending' or 'failure'"), @Param(name = "context", type = String.class, doc = "The context for the commit" + " status. Use a value like 'copybara/import_successful' or similar", named = true), @Param(name = "description", type = String.class, named = true, doc = "Description about what happened"), @Param(name = "target_url", type = String.class, named = true, doc = "Url with expanded information about the event", noneable = true, defaultValue = "None") })
public Status createStatus(String sha, String state, String context, String description, Object targetUrl) throws EvalException {
    try {
        checkCondition(State.VALID_VALUES.contains(state), "Invalid value for state. Valid values: %s", State.VALID_VALUES);
        checkCondition(GitRevision.COMPLETE_SHA1_PATTERN.matcher(sha).matches(), "Not a valid complete SHA-1: %s", sha);
        checkCondition(!Strings.isNullOrEmpty(description), "description cannot be empty");
        checkCondition(!Strings.isNullOrEmpty(context), "context cannot be empty");
        String project = GithubUtil.getProjectNameFromUrl(url);
        return githubOptions.getApi(project).createStatus(project, sha, new CreateStatusRequest(State.valueOf(state.toUpperCase()), convertFromNoneable(targetUrl, null), description, context));
    } catch (RepoException | ValidationException e) {
        throw new EvalException(/*location=*/
        null, e);
    }
}
Also used : CreateStatusRequest(com.google.copybara.git.github.api.CreateStatusRequest) ValidationException(com.google.copybara.exception.ValidationException) RepoException(com.google.copybara.exception.RepoException) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

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