Search in sources :

Example 1 with GiteaBranch

use of org.jenkinsci.plugin.gitea.client.api.GiteaBranch 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 2 with GiteaBranch

use of org.jenkinsci.plugin.gitea.client.api.GiteaBranch 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 3 with GiteaBranch

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

the class MockGiteaConnection method withBranch.

public MockGiteaConnection withBranch(GiteaRepository repo, GiteaBranch branch) {
    GiteaBranch clone = branch.clone();
    this.branches.get(keyOf(repo)).put(clone.getName(), clone);
    Map<String, byte[]> content = this.files.get(keyOf(repo)).get(clone.getCommit().getId());
    if (content == null) {
        this.files.get(keyOf(repo)).put(clone.getCommit().getId(), new HashMap<String, byte[]>());
    }
    return this;
}
Also used : GiteaBranch(org.jenkinsci.plugin.gitea.client.api.GiteaBranch)

Aggregations

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