Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-33309] Implement ChangeRequestAction.
  • Loading branch information
jglick committed Mar 8, 2016
1 parent e378438 commit 0d2f289
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
@@ -0,0 +1,79 @@
/*
* 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 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 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();
}

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

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

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

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

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

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

}
Expand Up @@ -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.
Expand All @@ -36,12 +40,26 @@ public final class PullRequestSCMHead extends SCMHead {

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 0d2f289

Please sign in to comment.