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);
}
}
}
}
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;
}
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;
}
Aggregations