package org.jenkinsci.plugins.p4.scm; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; import hudson.model.TaskListener; import jenkins.scm.api.SCMHead; import jenkins.scm.api.SCMHeadCategory; import jenkins.scm.api.SCMRevision; import jenkins.scm.impl.ChangeRequestSCMHeadCategory; import jenkins.scm.impl.UncategorizedSCMHeadCategory; import jenkins.util.NonLocalizable; import org.jenkinsci.Symbol; import org.jenkinsci.plugins.p4.PerforceScm; import org.jenkinsci.plugins.p4.browsers.P4Browser; import org.jenkinsci.plugins.p4.browsers.SwarmBrowser; import org.jenkinsci.plugins.p4.changes.P4ChangeRef; import org.jenkinsci.plugins.p4.client.ConnectionHelper; import org.jenkinsci.plugins.p4.review.P4Review; import org.jenkinsci.plugins.p4.swarmAPI.SwarmHelper; import org.jenkinsci.plugins.p4.swarmAPI.SwarmProjectAPI; import org.jenkinsci.plugins.p4.swarmAPI.SwarmReviewAPI; import org.jenkinsci.plugins.p4.swarmAPI.SwarmReviewsAPI; import org.jenkinsci.plugins.p4.tasks.CheckoutStatus; import org.kohsuke.stapler.DataBoundConstructor; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class SwarmScmSource extends AbstractP4ScmSource { private final String project; transient private SwarmHelper swarm; @DataBoundConstructor public SwarmScmSource(String credential, String project, String charset, String format) throws Exception { super(credential); this.project = project; setCharset(charset); setFormat(format); } public String getProject() { return project; } public SwarmHelper getSwarm() throws Exception { if (swarm == null) { try (ConnectionHelper p4 = new ConnectionHelper(getOwner(), credential, null)) { this.swarm = new SwarmHelper(p4, "4"); } } return swarm; } @Override public P4Browser getBrowser() { try { return new SwarmBrowser(getSwarm().getBaseUrl()); } catch (Exception e) { return null; } } @Override public List<P4Head> getTags(@NonNull TaskListener listener) throws Exception { List<P4Head> list = new ArrayList<>(); List<SwarmReviewsAPI.Reviews> reviews = getSwarm().getActiveReviews(project); for (SwarmReviewsAPI.Reviews review : reviews) { String reviewID = String.valueOf(review.getId()); List<String> branches = getBranchesInReview(reviewID, project); for (String branch : branches) { List<P4Path> paths = getPathsInBranch(branch, project); String trgName = branch + "-" + reviewID; P4Head target = new P4Head(trgName, paths); P4ChangeRequestSCMHead tag = new P4ChangeRequestSCMHead(trgName, reviewID, paths, target); list.add(tag); } } return list; } @Override public List<P4Head> getHeads(@NonNull TaskListener listener) throws Exception { List<P4Head> list = new ArrayList<>(); List<SwarmProjectAPI.Branch> branches = getSwarm().getBranchesInProject(project); for (SwarmProjectAPI.Branch branch : branches) { List<P4Path> paths = branch.getPaths(); P4Head head = new P4Head(branch.getId(), paths); list.add(head); } return list; } @Override public P4Revision getRevision(P4Head head, TaskListener listener) throws Exception { if (head instanceof P4ChangeRequestSCMHead) { P4ChangeRequestSCMHead changeRequest = (P4ChangeRequestSCMHead) head; String review = changeRequest.getReview(); long change = getLastChangeInReview(review); P4Revision revision = new P4Revision(head, new P4ChangeRef(change)); return revision; } return super.getRevision(head, listener); } @Override public PerforceScm build(SCMHead head, SCMRevision revision) { PerforceScm scm = super.build(head, revision); if (head instanceof P4ChangeRequestSCMHead) { P4Review review = new P4Review(head.getName(), CheckoutStatus.SHELVED); scm.setReview(review); } return scm; } protected boolean isCategoryEnabled(@NonNull SCMHeadCategory category) { return true; } private List<String> getBranchesInReview(String review, String project) throws Exception { SwarmReviewAPI api = getSwarm().getSwarmReview(review); HashMap<String, List<String>> projects = api.getReview().getProjects(); List<String> branches = projects.get(project); return branches; } private long getLastChangeInReview(String review) throws Exception { SwarmReviewAPI api = getSwarm().getSwarmReview(review); List<Long> changes = api.getReview().getChanges(); long lastChange = 0; for (Long change : changes) { if (change > lastChange) { lastChange = change; } } return lastChange; } private List<P4Path> getPathsInBranch(String id, String project) throws Exception { List<SwarmProjectAPI.Branch> branches = getSwarm().getBranchesInProject(project); for (SwarmProjectAPI.Branch branch : branches) { if (id.equals(branch.getId())) { List<P4Path> paths = branch.getPaths(); return paths; } } return new ArrayList<>(); } @Extension @Symbol("multiSwarm") public static final class DescriptorImpl extends P4ScmSourceDescriptor { @Override public String getDisplayName() { return "Helix Swarm"; } @NonNull @Override protected SCMHeadCategory[] createCategories() { return new SCMHeadCategory[]{ new UncategorizedSCMHeadCategory(new NonLocalizable("Branches")), new ChangeRequestSCMHeadCategory(new NonLocalizable("Reviews")) }; } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 24478 | Paul Allen |
Merging using p4-jenkins_main>dev @24454 |
||
#1 | 23685 | Paul Allen | Copy main -> dev | ||
//guest/perforce_software/p4jenkins/main/src/main/java/org/jenkinsci/plugins/p4/scm/SwarmScmSource.java | |||||
#11 | 23320 | Paul Allen | Remove deprecated ID for SCMSource constructor. | ||
#10 | 22937 | Paul Allen | Use Unirest for all Swarm API calls. | ||
#9 | 22881 | Paul Allen | Cache the BasicAuth token to avoid multiple login calls. | ||
#8 | 22875 | Paul Allen | Refactor P4Path and Swarm API | ||
#7 | 22866 | Paul Allen |
Merge pull request #55 from jenkinsci/hth Global Pipeline Library support. JENKINS-46121 JENKINS-46550 |
||
#6 | 22863 | Paul Allen | Store change in P4Revision as P4Ref. | ||
#5 | 22841 | Paul Allen |
Switch to `P4.Swarm.URL` property for Swarm url. JENKINS-45464 |
||
#4 | 22684 | Paul Allen | Added @Symbol to Extension classes for Pipeline Syntax. | ||
#3 | 22678 | Paul Allen |
MultiBranch support for Helix4Git (GitHub PR model). Update MultiBranch product names to Helix. |
||
#2 | 22398 | Paul Allen | Pass Perforce ticket using BasicAuth to Swarm API. | ||
#1 | 22396 | Paul Allen |
MultiBranch support for Perforce Swarm Reviews. A work in progress - adds Swarm API support to find branches and reviews from a Swarm project. #review-22354 |