Search in sources :

Example 1 with GiteaPullRequest

use of org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest in project gitea-plugin by jenkinsci.

the class GiteaPullSCMEvent method headsFor.

/**
 * {@inheritDoc}
 */
@NonNull
@Override
public Map<SCMHead, SCMRevision> headsFor(GiteaSCMSource source) {
    Map<SCMHead, SCMRevision> result = new HashMap<>();
    try (GiteaSCMSourceRequest request = new GiteaSCMSourceContext(null, SCMHeadObserver.none()).withTraits(source.getTraits()).newRequest(source, null)) {
        if (!request.isFetchPRs()) {
            return result;
        }
        final GiteaPullRequest p = getPayload().getPullRequest();
        if (p == null) {
            // the pull request has been deleted, sadly we have no way to determine where the PR was from
            // so we just blast hit with all potential cases and using dummy values.
            // the values are not important as the event consumers will be matching on SCMHead.getName()
            // and so will see that there is a new "head" for the old name, reconfirm the revision
            // and find that the head no longer exists... and our job is done!
            Set<ChangeRequestCheckoutStrategy> strategies = EnumSet.noneOf(ChangeRequestCheckoutStrategy.class);
            if (request.isFetchForkPRs()) {
                strategies.addAll(request.getForkPRStrategies());
            }
            if (request.isFetchOriginPRs()) {
                strategies.addAll(request.getOriginPRStrategies());
            }
            for (ChangeRequestCheckoutStrategy strategy : strategies) {
                result.put(new PullRequestSCMHead("PR-" + getPayload().getNumber() + (strategies.size() > 1 ? "-" + strategy.name().toLowerCase(Locale.ENGLISH) : ""), getPayload().getNumber(), new BranchSCMHead("dummy-name"), ChangeRequestCheckoutStrategy.MERGE, SCMHeadOrigin.DEFAULT, source.getRepoOwner(), source.getRepository(), "dummy-name"), null);
            }
        } else {
            String originOwner = p.getHead().getRepo().getOwner().getUsername();
            String originRepository = p.getHead().getRepo().getName();
            Set<ChangeRequestCheckoutStrategy> strategies = request.getPRStrategies(!StringUtils.equalsIgnoreCase(source.getRepoOwner(), originOwner) && StringUtils.equalsIgnoreCase(source.getRepository(), originRepository));
            for (ChangeRequestCheckoutStrategy strategy : strategies) {
                PullRequestSCMHead h = new PullRequestSCMHead("PR-" + p.getNumber() + (strategies.size() > 1 ? "-" + strategy.name().toLowerCase(Locale.ENGLISH) : ""), p.getNumber(), new BranchSCMHead(p.getBase().getRef()), strategy, StringUtils.equalsIgnoreCase(originOwner, source.getRepoOwner()) && StringUtils.equalsIgnoreCase(originRepository, source.getRepository()) ? SCMHeadOrigin.DEFAULT : new SCMHeadOrigin.Fork(originOwner + "/" + originRepository), originOwner, originRepository, p.getHead().getRef());
                result.put(h, getPayload().getAction() == GiteaPullRequestEventType.CLOSED ? null : new PullRequestSCMRevision(h, new BranchSCMRevision(h.getTarget(), p.getBase().getSha()), new BranchSCMRevision(new BranchSCMHead(h.getOriginName()), p.getHead().getSha())));
            }
        }
    } catch (IOException e) {
    // ignore
    }
    return result;
}
Also used : SCMHead(jenkins.scm.api.SCMHead) HashMap(java.util.HashMap) IOException(java.io.IOException) ChangeRequestCheckoutStrategy(jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy) SCMRevision(jenkins.scm.api.SCMRevision) GiteaPullRequest(org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with GiteaPullRequest

use of org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest in project gitea-plugin by jenkinsci.

the class GiteaSCMSource method retrieve.

@Override
protected void retrieve(SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer, SCMHeadEvent<?> event, @NonNull final TaskListener listener) throws IOException, InterruptedException {
    try (GiteaConnection c = gitea().open()) {
        listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
        giteaRepository = c.fetchRepository(repoOwner, repository);
        sshRemote = giteaRepository.getSshUrl();
        if (giteaRepository.isEmpty()) {
            listener.getLogger().format("Repository %s is empty%n", repository);
            return;
        }
        try (GiteaSCMSourceRequest request = new GiteaSCMSourceContext(criteria, observer).withTraits(getTraits()).newRequest(this, listener)) {
            request.setConnection(c);
            if (request.isFetchBranches()) {
                request.setBranches(c.fetchBranches(giteaRepository));
            }
            if (request.isFetchPRs()) {
                if (giteaRepository.isMirror()) {
                    listener.getLogger().format("%n  Ignoring pull requests as repository is a mirror...%n");
                } else {
                    request.setPullRequests(c.fetchPullRequests(giteaRepository));
                }
            }
            if (request.isFetchBranches()) {
                int count = 0;
                listener.getLogger().format("%n  Checking branches...%n");
                for (final GiteaBranch b : c.fetchBranches(giteaRepository)) {
                    count++;
                    listener.getLogger().format("%n    Checking branch %s%n", HyperlinkNote.encodeTo(UriTemplate.buildFromTemplate(giteaRepository.getHtmlUrl()).literal("/src").path("branch").build().set("branch", b.getName()).expand(), b.getName()));
                    if (request.process(new BranchSCMHead(b.getName()), new SCMSourceRequest.RevisionLambda<BranchSCMHead, BranchSCMRevision>() {

                        @NonNull
                        @Override
                        public BranchSCMRevision create(@NonNull BranchSCMHead head) throws IOException, InterruptedException {
                            return new BranchSCMRevision(head, b.getCommit().getId());
                        }
                    }, new SCMSourceRequest.ProbeLambda<BranchSCMHead, BranchSCMRevision>() {

                        @NonNull
                        @Override
                        public SCMSourceCriteria.Probe create(@NonNull BranchSCMHead head, @Nullable BranchSCMRevision revision) throws IOException, InterruptedException {
                            return createProbe(head, revision);
                        }
                    }, new SCMSourceRequest.Witness() {

                        @Override
                        public void record(@NonNull SCMHead head, SCMRevision revision, boolean isMatch) {
                            if (isMatch) {
                                listener.getLogger().format("    Met criteria%n");
                            } else {
                                listener.getLogger().format("    Does not meet criteria%n");
                            }
                        }
                    })) {
                        listener.getLogger().format("%n  %d branches were processed (query completed)%n", count);
                        return;
                    }
                }
                listener.getLogger().format("%n  %d branches were processed%n", count);
            }
            if (request.isFetchPRs() && !giteaRepository.isMirror() && !(request.getForkPRStrategies().isEmpty() && request.getOriginPRStrategies().isEmpty())) {
                int count = 0;
                listener.getLogger().format("%n  Checking pull requests...%n");
                for (final GiteaPullRequest p : c.fetchPullRequests(giteaRepository, EnumSet.of(GiteaIssueState.OPEN))) {
                    if (p == null) {
                        continue;
                    }
                    count++;
                    listener.getLogger().format("%n  Checking pull request %s%n", HyperlinkNote.encodeTo(UriTemplate.buildFromTemplate(giteaRepository.getHtmlUrl()).literal("/pulls").path("number").build().set("number", p.getNumber()).expand(), "#" + p.getNumber()));
                    String originOwner = p.getHead().getRepo().getOwner().getUsername();
                    String originRepository = p.getHead().getRepo().getName();
                    Set<ChangeRequestCheckoutStrategy> strategies = request.getPRStrategies(!StringUtils.equalsIgnoreCase(repoOwner, originOwner) && StringUtils.equalsIgnoreCase(repository, originRepository));
                    for (ChangeRequestCheckoutStrategy strategy : strategies) {
                        if (request.process(new PullRequestSCMHead("PR-" + p.getNumber() + (strategies.size() > 1 ? "-" + strategy.name().toLowerCase(Locale.ENGLISH) : ""), p.getNumber(), new BranchSCMHead(p.getBase().getRef()), strategy, StringUtils.equalsIgnoreCase(originOwner, repoOwner) && StringUtils.equalsIgnoreCase(originRepository, repository) ? SCMHeadOrigin.DEFAULT : new SCMHeadOrigin.Fork(originOwner + "/" + originRepository), originOwner, originRepository, p.getHead().getRef()), new SCMSourceRequest.RevisionLambda<PullRequestSCMHead, PullRequestSCMRevision>() {

                            @NonNull
                            @Override
                            public PullRequestSCMRevision create(@NonNull PullRequestSCMHead head) throws IOException, InterruptedException {
                                return new PullRequestSCMRevision(head, new BranchSCMRevision(head.getTarget(), p.getBase().getSha()), new BranchSCMRevision(new BranchSCMHead(head.getOriginName()), p.getHead().getSha()));
                            }
                        }, new SCMSourceRequest.ProbeLambda<PullRequestSCMHead, PullRequestSCMRevision>() {

                            @NonNull
                            @Override
                            public SCMSourceCriteria.Probe create(@NonNull PullRequestSCMHead h, @Nullable PullRequestSCMRevision r) throws IOException, InterruptedException {
                                return createProbe(h, r);
                            }
                        }, new SCMSourceRequest.Witness() {

                            @Override
                            public void record(@NonNull SCMHead head, SCMRevision revision, boolean isMatch) {
                                if (isMatch) {
                                    listener.getLogger().format("    Met criteria%n");
                                } else {
                                    listener.getLogger().format("    Does not meet criteria%n");
                                }
                            }
                        })) {
                            listener.getLogger().format("%n  %d pull requests were processed (query completed)%n", count);
                            return;
                        }
                    }
                }
                listener.getLogger().format("%n  %d pull requests were processed%n", count);
            }
        }
    }
}
Also used : GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) SCMSourceRequest(jenkins.scm.api.trait.SCMSourceRequest) SCMProbe(jenkins.scm.api.SCMProbe) NonNull(edu.umd.cs.findbugs.annotations.NonNull) SCMHead(jenkins.scm.api.SCMHead) GiteaBranch(org.jenkinsci.plugin.gitea.client.api.GiteaBranch) IOException(java.io.IOException) ChangeRequestCheckoutStrategy(jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy) SCMHeadOrigin(jenkins.scm.api.SCMHeadOrigin) SCMRevision(jenkins.scm.api.SCMRevision) GiteaPullRequest(org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest)

Example 3 with GiteaPullRequest

use of org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest in project gitea-plugin by jenkinsci.

the class GiteaSCMSource method retrieve.

@Override
protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener) throws IOException, InterruptedException {
    try (GiteaConnection c = gitea().open()) {
        if (head instanceof BranchSCMHead) {
            listener.getLogger().format("Querying the current revision of branch %s...%n", head.getName());
            String revision = c.fetchBranch(repoOwner, repository, head.getName()).getCommit().getId();
            listener.getLogger().format("Current revision of branch %s is %s%n", head.getName(), revision);
            return new BranchSCMRevision((BranchSCMHead) head, revision);
        } else if (head instanceof PullRequestSCMHead) {
            PullRequestSCMHead h = (PullRequestSCMHead) head;
            listener.getLogger().format("Querying the current revision of pull request #%s...%n", h.getId());
            GiteaPullRequest pr = c.fetchPullRequest(repoOwner, repository, Long.parseLong(h.getId()));
            if (pr.getState() == GiteaIssueState.OPEN) {
                listener.getLogger().format("Current revision of pull request #%s is %s%n", h.getId(), pr.getHead().getSha());
                return new PullRequestSCMRevision(h, new BranchSCMRevision(h.getTarget(), pr.getBase().getSha()), new BranchSCMRevision(new BranchSCMHead(h.getOriginName()), pr.getHead().getSha()));
            } else {
                listener.getLogger().format("Pull request #%s is CLOSED%n", h.getId());
                return null;
            }
        } else {
            listener.getLogger().format("Unknown head: %s of type %s%n", head.getName(), head.getClass().getName());
            return null;
        }
    }
}
Also used : GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) GiteaPullRequest(org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest)

Example 4 with GiteaPullRequest

use of org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest in project gitea-plugin by jenkinsci.

the class MockGiteaConnection method withRepo.

public MockGiteaConnection withRepo(GiteaRepository repo) {
    GiteaRepository clone = repo.clone();
    clone.setId(nextId.incrementAndGet());
    clone.setCreatedAt(new Date());
    clone.setUpdatedAt(new Date());
    this.repositories.put(keyOf(clone), clone);
    this.repoHooks.put(keyOf(clone), new ArrayList<GiteaHook>());
    this.branches.put(keyOf(clone), new HashMap<String, GiteaBranch>());
    this.pulls.put(keyOf(clone), new HashMap<Long, GiteaPullRequest>());
    this.issues.put(keyOf(clone), new HashMap<Long, GiteaIssue>());
    this.collaborators.put(keyOf(clone), new TreeSet<String>());
    this.files.put(keyOf(clone), new TreeMap<String, Map<String, byte[]>>());
    return this;
}
Also used : GiteaIssue(org.jenkinsci.plugin.gitea.client.api.GiteaIssue) GiteaBranch(org.jenkinsci.plugin.gitea.client.api.GiteaBranch) GiteaHook(org.jenkinsci.plugin.gitea.client.api.GiteaHook) Date(java.util.Date) AtomicLong(java.util.concurrent.atomic.AtomicLong) GiteaPullRequest(org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) GiteaRepository(org.jenkinsci.plugin.gitea.client.api.GiteaRepository)

Example 5 with GiteaPullRequest

use of org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest in project gitea-plugin by jenkinsci.

the class MockGiteaConnection method withPull.

public MockGiteaConnection withPull(GiteaRepository repo, GiteaPullRequest pull) {
    GiteaPullRequest clone = pull.clone();
    clone.setId(nextId.incrementAndGet());
    clone.setNumber(clone.getId());
    clone.setCreatedAt(new Date());
    clone.setUpdatedAt(new Date());
    pulls.get(keyOf(repo)).put(clone.getId(), clone);
    Map<String, byte[]> content = this.files.get(keyOf(repo)).get(pull.getHead().getSha());
    if (content == null) {
        this.files.get(keyOf(repo)).put(pull.getHead().getSha(), new HashMap<String, byte[]>());
    }
    content = this.files.get(keyOf(repo)).get(pull.getBase().getSha());
    if (content == null) {
        this.files.get(keyOf(repo)).put(pull.getBase().getSha(), new HashMap<String, byte[]>());
    }
    GiteaIssue issue = new GiteaIssue();
    issue.setUrl(clone.getUrl());
    issue.setNumber(clone.getNumber());
    issue.setUser(clone.getUser());
    issue.setTitle(clone.getTitle());
    issue.setBody(clone.getBody());
    issue.setLabels(clone.getLabels());
    issue.setMilestone(clone.getMilestone());
    issue.setAssignee(clone.getAssignee());
    issue.setState(clone.getState());
    issue.setComments(clone.getComments());
    issue.setCreatedAt(clone.getCreatedAt());
    issue.setUpdatedAt(clone.getUpdatedAt());
    GiteaIssue.PullSummary summary = new GiteaIssue.PullSummary();
    summary.setMerged(clone.isMerged());
    summary.setMergedAt(clone.getMergedAt());
    issue.setPullRequest(summary);
    issues.get(keyOf(repo)).put(issue.getId(), issue);
    return this;
}
Also used : GiteaIssue(org.jenkinsci.plugin.gitea.client.api.GiteaIssue) GiteaPullRequest(org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest) Date(java.util.Date)

Aggregations

GiteaPullRequest (org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest)5 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 IOException (java.io.IOException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 SCMHead (jenkins.scm.api.SCMHead)2 SCMRevision (jenkins.scm.api.SCMRevision)2 ChangeRequestCheckoutStrategy (jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy)2 GiteaBranch (org.jenkinsci.plugin.gitea.client.api.GiteaBranch)2 GiteaConnection (org.jenkinsci.plugin.gitea.client.api.GiteaConnection)2 GiteaIssue (org.jenkinsci.plugin.gitea.client.api.GiteaIssue)2 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 SCMHeadOrigin (jenkins.scm.api.SCMHeadOrigin)1 SCMProbe (jenkins.scm.api.SCMProbe)1 SCMSourceRequest (jenkins.scm.api.trait.SCMSourceRequest)1 GiteaHook (org.jenkinsci.plugin.gitea.client.api.GiteaHook)1 GiteaRepository (org.jenkinsci.plugin.gitea.client.api.GiteaRepository)1