Search in sources :

Example 1 with MergeWithGitSCMExtension

use of jenkins.plugins.git.MergeWithGitSCMExtension in project gitea-plugin by jenkinsci.

the class GiteaSCMBuilder method build.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public GitSCM build() {
    final SCMHead h = head();
    final SCMRevision r = revision();
    try {
        withGiteaRemote();
        if (h instanceof PullRequestSCMHead) {
            PullRequestSCMHead head = (PullRequestSCMHead) h;
            if (head.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
                // add the target branch to ensure that the revision we want to merge is also available
                String name = head.getTarget().getName();
                String localName = "remotes/" + remoteName() + "/" + name;
                Set<String> localNames = new HashSet<>();
                boolean match = false;
                String targetSrc = Constants.R_HEADS + name;
                String targetDst = Constants.R_REMOTES + remoteName() + "/" + name;
                for (RefSpec b : asRefSpecs()) {
                    String dst = b.getDestination();
                    assert dst.startsWith(Constants.R_REFS) : "All git references must start with refs/";
                    if (targetSrc.equals(b.getSource())) {
                        if (targetDst.equals(dst)) {
                            match = true;
                        } else {
                            // pick up the configured destination name
                            localName = dst.substring(Constants.R_REFS.length());
                            match = true;
                        }
                    } else {
                        localNames.add(dst.substring(Constants.R_REFS.length()));
                    }
                }
                if (!match) {
                    if (localNames.contains(localName)) {
                        // conflict with intended name
                        localName = "remotes/" + remoteName() + "/upstream-" + name;
                    }
                    if (localNames.contains(localName)) {
                        // conflict with intended alternative name
                        localName = "remotes/" + remoteName() + "/pr-" + head.getId() + "-upstream-" + name;
                    }
                    if (localNames.contains(localName)) {
                        // ok we're just going to mangle our way to something that works
                        Random entropy = new Random();
                        while (localNames.contains(localName)) {
                            localName = "remotes/" + remoteName() + "/pr-" + head.getId() + "-upstream-" + name + "-" + Integer.toHexString(entropy.nextInt(Integer.MAX_VALUE));
                        }
                    }
                    withRefSpec("+refs/heads/" + name + ":refs/" + localName);
                }
                withExtension(new MergeWithGitSCMExtension(localName, r instanceof PullRequestSCMRevision ? ((BranchSCMRevision) ((PullRequestSCMRevision) r).getTarget()).getHash() : null));
            }
            if (r instanceof PullRequestSCMRevision) {
                withRevision(((PullRequestSCMRevision) r).getOrigin());
            }
        }
        return super.build();
    } finally {
        withHead(h);
        withRevision(r);
    }
}
Also used : RefSpec(org.eclipse.jgit.transport.RefSpec) SCMHead(jenkins.scm.api.SCMHead) Random(java.util.Random) SCMRevision(jenkins.scm.api.SCMRevision) MergeWithGitSCMExtension(jenkins.plugins.git.MergeWithGitSCMExtension) HashSet(java.util.HashSet) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 MergeWithGitSCMExtension (jenkins.plugins.git.MergeWithGitSCMExtension)1 SCMHead (jenkins.scm.api.SCMHead)1 SCMRevision (jenkins.scm.api.SCMRevision)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1