Skip to content
Permalink
Browse files

Merge pull request #31 from jglick/SCMHead-Actionable-JENKINS-33309

[JENKINS-33309] Implement ChangeRequestAction
  • Loading branch information...
jglick committed Mar 10, 2016
2 parents e378438 + 5bb5843 commit 3486aa81e9468357f9a1902740de0d8a15d53f4d
@@ -33,7 +33,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>1.0</version>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -276,8 +276,7 @@ private void doRetrieve(SCMHeadObserver observer, TaskListener listener, GHRepos
listener.getLogger().format("%n Getting remote pull requests...%n");
int pullrequests = 0;
for (GHPullRequest ghPullRequest : repo.getPullRequests(GHIssueState.OPEN)) {
int number = ghPullRequest.getNumber();
SCMHead head = new PullRequestSCMHead(number);
PullRequestSCMHead head = new PullRequestSCMHead(ghPullRequest);
final String branchName = head.getName();
listener.getLogger().format("%n Checking pull request %s%n", HyperlinkNote.encodeTo(ghPullRequest.getHtmlUrl().toString(), "#" + branchName));
// FYI https://developer.github.com/v3/pulls/#response-1
@@ -291,7 +290,7 @@ private void doRetrieve(SCMHeadObserver observer, TaskListener listener, GHRepos
continue;
}
if (criteria != null) {
SCMSourceCriteria.Probe probe = getProbe(branchName, "pull request", "refs/pull/" + number + "/head", repo, listener);
SCMSourceCriteria.Probe probe = getProbe(branchName, "pull request", "refs/pull/" + head.getNumber() + "/head", repo, listener);
if (criteria.isHead(probe, listener)) {
listener.getLogger().format(" Met criteria%n");
} else {
@@ -0,0 +1,87 @@
/*
* The MIT License
*
* Copyright 2016 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.github_branch_source;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.InvisibleAction;
import java.net.URL;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.actions.ChangeRequestAction;
import org.kohsuke.github.GHPullRequest;

/**
* Metadata about a {@link PullRequestSCMHead}.
*/
final class PullRequestAction extends ChangeRequestAction {

private static final long serialVersionUID = 1L;

private final int number;
private final URL url;
private final String title;
private final String userLogin;
private final String baseRef;

PullRequestAction(GHPullRequest pr) {
number = pr.getNumber();
url = pr.getHtmlUrl();
title = pr.getTitle();
userLogin = pr.getUser().getLogin();
baseRef = pr.getBase().getRef();
}

@NonNull
@Override
public String getId() {
return Integer.toString(number);
}

@NonNull
@Override
public URL getURL() {
return url;
}

@NonNull
@Override
public String getTitle() {
return title;
}

@NonNull
@Override
public String getAuthor() {
return userLogin;
}

// not currently implementing authorDisplayName or authorEmail since these are another round-trip in current GH API

@NonNull
@Override
public SCMHead getTarget() {
return new SCMHead(baseRef);
}

}
@@ -24,7 +24,11 @@

package org.jenkinsci.plugins.github_branch_source;

import hudson.model.Action;
import java.util.LinkedList;
import java.util.List;
import jenkins.scm.api.SCMHead;
import org.kohsuke.github.GHPullRequest;

/**
* Head corresponding to a pull request.
@@ -36,12 +40,26 @@

private static final long serialVersionUID = 1;

public PullRequestSCMHead(int number) {
super(PR_BRANCH_PREFIX + number);
private final PullRequestAction metadata;

PullRequestSCMHead(GHPullRequest pr) {
super(PR_BRANCH_PREFIX + pr.getNumber());
metadata = new PullRequestAction(pr);
}

public int getNumber() {
return Integer.parseInt(getName().substring(PR_BRANCH_PREFIX.length()));
if (metadata != null) {
return Integer.parseInt(metadata.getId());
} else { // settings compatibility
return Integer.parseInt(getName().substring(PR_BRANCH_PREFIX.length()));
}
}

@Override
public List<? extends Action> getAllActions() {
List<Action> actions = new LinkedList<Action>(super.getAllActions());
actions.add(metadata);
return actions;
}

}

0 comments on commit 3486aa8

Please sign in to comment.
You can’t perform that action at this time.