Search in sources :

Example 1 with GiteaConnection

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

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

the class GiteaSCMSource method retrieveActions.

@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMHead head, SCMHeadEvent event, @NonNull TaskListener listener) throws IOException, InterruptedException {
    if (giteaRepository == null) {
        try (GiteaConnection c = gitea().open()) {
            listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
            giteaRepository = c.fetchRepository(repoOwner, repository);
        }
    }
    List<Action> result = new ArrayList<>();
    if (head instanceof BranchSCMHead) {
        String branchUrl = UriTemplate.buildFromTemplate(serverUrl).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).path("src").path(UriTemplateBuilder.var("branch")).build().set("owner", repoOwner).set("repository", repository).set("branch", head.getName()).expand();
        result.add(new ObjectMetadataAction(null, null, branchUrl));
        result.add(new GiteaLink("icon-gitea-branch", branchUrl));
        if (head.getName().equals(giteaRepository.getDefaultBranch())) {
            result.add(new PrimaryInstanceMetadataAction());
        }
    } else if (head instanceof PullRequestSCMHead) {
        String pullUrl = UriTemplate.buildFromTemplate(serverUrl).path(UriTemplateBuilder.var("owner")).path(UriTemplateBuilder.var("repository")).path("pulls").path(UriTemplateBuilder.var("id")).build().set("owner", repoOwner).set("repository", repository).set("id", ((PullRequestSCMHead) head).getId()).expand();
        result.add(new ObjectMetadataAction(null, null, pullUrl));
        result.add(new GiteaLink("icon-gitea-branch", pullUrl));
    }
    return result;
}
Also used : ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) Action(hudson.model.Action) PrimaryInstanceMetadataAction(jenkins.scm.api.metadata.PrimaryInstanceMetadataAction) ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) PrimaryInstanceMetadataAction(jenkins.scm.api.metadata.PrimaryInstanceMetadataAction) NamedArrayList(jenkins.scm.impl.form.NamedArrayList) ArrayList(java.util.ArrayList) GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 3 with GiteaConnection

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

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

the class GiteaSCMNavigator method retrieveActions.

@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner, SCMNavigatorEvent event, @NonNull TaskListener listener) throws IOException, InterruptedException {
    if (this.giteaOwner == null) {
        try (GiteaConnection c = gitea(owner).open()) {
            this.giteaOwner = c.fetchUser(repoOwner);
            if (StringUtils.isBlank(giteaOwner.getEmail())) {
                this.giteaOwner = c.fetchOrganization(repoOwner);
            }
        }
    }
    List<Action> result = new ArrayList<>();
    String objectUrl = UriTemplate.buildFromTemplate(serverUrl).path("owner").build().set("owner", repoOwner).expand();
    result.add(new ObjectMetadataAction(Util.fixEmpty(giteaOwner.getFullName()), null, objectUrl));
    if (StringUtils.isNotBlank(giteaOwner.getAvatarUrl())) {
        result.add(new GiteaAvatar(giteaOwner.getAvatarUrl()));
    }
    result.add(new GiteaLink("icon-gitea-org", objectUrl));
    if (giteaOwner instanceof GiteaOrganization) {
        String website = ((GiteaOrganization) giteaOwner).getWebsite();
        if (StringUtils.isBlank(website)) {
            listener.getLogger().println("Organization URL: unspecified");
            listener.getLogger().printf("Organization URL: %s%n", HyperlinkNote.encodeTo(website, StringUtils.defaultIfBlank(giteaOwner.getFullName(), website)));
        }
    }
    return result;
}
Also used : ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) Action(hudson.model.Action) GiteaOrganization(org.jenkinsci.plugin.gitea.client.api.GiteaOrganization) ObjectMetadataAction(jenkins.scm.api.metadata.ObjectMetadataAction) NamedArrayList(jenkins.scm.impl.form.NamedArrayList) ArrayList(java.util.ArrayList) GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with GiteaConnection

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

the class GiteaSCMNavigator method visitSources.

@Override
public void visitSources(@NonNull final SCMSourceObserver observer) throws IOException, InterruptedException {
    try (GiteaSCMNavigatorRequest request = new GiteaSCMNavigatorContext().withTraits(traits).newRequest(this, observer);
        GiteaConnection c = gitea(observer.getContext()).open()) {
        giteaOwner = c.fetchUser(repoOwner);
        if (StringUtils.isBlank(giteaOwner.getEmail())) {
            giteaOwner = c.fetchOrganization(repoOwner);
        }
        List<GiteaRepository> repositories = c.fetchRepositories(giteaOwner);
        int count = 0;
        observer.getListener().getLogger().format("%n  Checking repositories...%n");
        Set<Long> seen = new HashSet<>();
        for (GiteaRepository r : repositories) {
            // TODO remove this hack for Gitea listing the repositories multiple times
            if (seen.contains(r.getId())) {
                continue;
            } else {
                seen.add(r.getId());
            }
            if (!StringUtils.equalsIgnoreCase(r.getOwner().getUsername(), repoOwner)) {
                // this is the user repos which includes all organizations that they are a member of
                continue;
            }
            count++;
            if (r.isEmpty()) {
                observer.getListener().getLogger().format("%n    Ignoring empty repository %s%n", HyperlinkNote.encodeTo(r.getHtmlUrl(), r.getName()));
                continue;
            }
            observer.getListener().getLogger().format("%n    Checking repository %s%n", HyperlinkNote.encodeTo(r.getHtmlUrl(), r.getName()));
            if (request.process(r.getName(), new SCMNavigatorRequest.SourceLambda() {

                @NonNull
                @Override
                public SCMSource create(@NonNull String projectName) throws IOException, InterruptedException {
                    return new GiteaSCMSourceBuilder(getId() + "::" + projectName, serverUrl, credentialsId, repoOwner, projectName).withTraits(traits).build();
                }
            }, null, new SCMNavigatorRequest.Witness() {

                @Override
                public void record(@NonNull String projectName, boolean isMatch) {
                    if (isMatch) {
                        observer.getListener().getLogger().format("      Proposing %s%n", projectName);
                    } else {
                        observer.getListener().getLogger().format("      Ignoring %s%n", projectName);
                    }
                }
            })) {
                observer.getListener().getLogger().format("%n  %d repositories were processed (query complete)%n", count);
                return;
            }
        }
        observer.getListener().getLogger().format("%n  %d repositories were processed%n", count);
    }
}
Also used : SCMNavigatorRequest(jenkins.scm.api.trait.SCMNavigatorRequest) GiteaConnection(org.jenkinsci.plugin.gitea.client.api.GiteaConnection) IOException(java.io.IOException) SCMSource(jenkins.scm.api.SCMSource) NonNull(edu.umd.cs.findbugs.annotations.NonNull) GiteaRepository(org.jenkinsci.plugin.gitea.client.api.GiteaRepository) HashSet(java.util.HashSet)

Aggregations

GiteaConnection (org.jenkinsci.plugin.gitea.client.api.GiteaConnection)10 NonNull (edu.umd.cs.findbugs.annotations.NonNull)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Action (hudson.model.Action)3 JenkinsLocationConfiguration (jenkins.model.JenkinsLocationConfiguration)3 ObjectMetadataAction (jenkins.scm.api.metadata.ObjectMetadataAction)3 NamedArrayList (jenkins.scm.impl.form.NamedArrayList)3 GiteaEventType (org.jenkinsci.plugin.gitea.client.api.GiteaEventType)3 GiteaHook (org.jenkinsci.plugin.gitea.client.api.GiteaHook)3 GiteaRepository (org.jenkinsci.plugin.gitea.client.api.GiteaRepository)3 GiteaServer (org.jenkinsci.plugin.gitea.servers.GiteaServer)3 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 HashSet (java.util.HashSet)2 SCMRevision (jenkins.scm.api.SCMRevision)2 SCMSource (jenkins.scm.api.SCMSource)2 PrimaryInstanceMetadataAction (jenkins.scm.api.metadata.PrimaryInstanceMetadataAction)2 GiteaOrganization (org.jenkinsci.plugin.gitea.client.api.GiteaOrganization)2 GiteaPullRequest (org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest)2 Result (hudson.model.Result)1