Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit f2c08bc

Browse files
committed
Added ability to create a pull request.
This fixes issue hub4j#79.
1 parent 688d8ed commit f2c08bc

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
<version>1.5.3</version>
9898
<optional>true</optional>
9999
</dependency>
100+
<dependency>
101+
<groupId>org.kohsuke</groupId>
102+
<artifactId>wordnet-random-name</artifactId>
103+
<version>1.2</version>
104+
<scope>test</scope>
105+
</dependency>
100106
</dependencies>
101107
<repositories>
102108
<repository>

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public List<GHIssue> getIssues(GHIssueState state) throws IOException {
153153
public List<GHIssue> getIssues(GHIssueState state, GHMilestone milestone) throws IOException {
154154
return Arrays.asList(GHIssue.wrap(root.retrieve()
155155
.to(String.format("/repos/%s/%s/issues?state=%s&milestone=%s", owner.login, name,
156-
state.toString().toLowerCase(), milestone == null ? "none" : "" + milestone.getNumber()),
156+
state.toString().toLowerCase(), milestone == null ? "none" : "" + milestone.getNumber()),
157157
GHIssue[].class
158158
), this));
159159
}
@@ -469,6 +469,29 @@ protected void wrapUp(GHPullRequest[] page) {
469469
};
470470
}
471471

472+
/**
473+
* Creates a new pull request.
474+
*
475+
* @param title
476+
* Required. The title of the pull request.
477+
* @param head
478+
* Required. The name of the branch where your changes are implemented.
479+
* For cross-repository pull requests in the same network,
480+
* namespace head with a user like this: username:branch.
481+
* @param base
482+
* Required. The name of the branch you want your changes pulled into.
483+
* This should be an existing branch on the current repository.
484+
* @param body
485+
* The contents of the pull request. This is the markdown description
486+
* of a pull request.
487+
*/
488+
public GHPullRequest createPullRequest(String title, String head, String base, String body) throws IOException {
489+
return new Requester(root).with("title",title)
490+
.with("head",head)
491+
.with("base",base)
492+
.with("body",body).to(getApiTailUrl("pulls"),GHPullRequest.class).wrapUp(this);
493+
}
494+
472495
/**
473496
* Retrieves the currently configured hooks.
474497
*/
@@ -498,7 +521,7 @@ public GHCompare getCompare(String id1, String id2) throws IOException {
498521
}
499522

500523
public GHCompare getCompare(GHCommit id1, GHCommit id2) throws IOException {
501-
return getCompare(id1.getSHA1(),id2.getSHA1());
524+
return getCompare(id1.getSHA1(), id2.getSHA1());
502525
}
503526

504527
public GHCompare getCompare(GHBranch id1, GHBranch id2) throws IOException {

src/test/java/org/kohsuke/github/AbstractGitHubApiTestBase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.apache.commons.io.IOUtils;
44
import org.junit.Assert;
55
import org.junit.Before;
6-
import org.kohsuke.github.GitHub;
6+
import org.kohsuke.randname.RandomNameGenerator;
77

88
import java.io.FileInputStream;
99
import java.util.Properties;
@@ -31,4 +31,6 @@ public void setUp() throws Exception {
3131
gitHub = GitHub.connect();
3232
}
3333
}
34+
35+
protected static final RandomNameGenerator rnd = new RandomNameGenerator();
3436
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author Kohsuke Kawaguchi
7+
*/
8+
public class PullRequestTest extends AbstractGitHubApiTestBase {
9+
@Test
10+
public void createPullRequest() throws Exception {
11+
GHRepository j = gitHub.getOrganization("github-api-test-org").getRepository("jenkins");
12+
String name = rnd.next();
13+
GHPullRequest p = j.createPullRequest(name, "stable", "master", "## test");
14+
System.out.println(p.getUrl());
15+
assertEquals(name, p.getTitle());
16+
p.close();
17+
}
18+
}

0 commit comments

Comments
 (0)