Skip to content

Commit 47d21a2

Browse files
committed
Pull Request API.
1 parent 6bcb26f commit 47d21a2

File tree

12 files changed

+567
-231
lines changed

12 files changed

+567
-231
lines changed

.classpath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<classpathentry kind="src" path="core/src/main/resources"/>
66
<classpathentry kind="src" path="core/src/test/java"/>
77
<classpathentry kind="src" path="core/src/test/resources"/>
8-
<classpathentry kind="src" path="examples/src/resources"/>
98
<classpathentry kind="src" path="examples/src/java"/>
109
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
1110
<classpathentry kind="lib" path="dev-lib/junit-4.5.jar"/>

core/src/main/java/com/github/api/v2/services/PullRequestService.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
*/
1717
package com.github.api.v2.services;
1818

19-
import java.io.InputStream;
2019
import java.util.List;
2120

22-
import com.github.api.v2.schema.Blob;
23-
import com.github.api.v2.schema.Tree;
21+
import com.github.api.v2.schema.PullRequest;
22+
import com.github.api.v2.schema.Issue.State;
2423

2524
/**
2625
* The Interface ObjectService.
@@ -39,49 +38,37 @@ public interface PullRequestService extends GitHubService {
3938
*
4039
* @return the tree
4140
*/
42-
public List<Tree> getTree(String userName, String repositoryName, String treeSha);
41+
public List<PullRequest> getPullRequests(String userName, String repositoryName);
4342

4443
/**
45-
* Gets the blob.
44+
* Gets the tree.
4645
*
4746
* @param userName
4847
* the user name
4948
* @param repositoryName
5049
* the repository name
5150
* @param treeSha
5251
* the tree sha
53-
* @param filePath
54-
* the file path
5552
*
56-
* @return the blob
53+
* @return the tree
5754
*/
58-
public Blob getBlob(String userName, String repositoryName, String treeSha, String filePath);
59-
55+
public List<PullRequest> getPullRequests(String userName, String repositoryName, State state);
56+
6057
/**
61-
* Gets the blobs.
58+
* Gets the blob.
6259
*
6360
* @param userName
6461
* the user name
6562
* @param repositoryName
6663
* the repository name
6764
* @param treeSha
6865
* the tree sha
66+
* @param filePath
67+
* the file path
6968
*
70-
* @return the blobs
69+
* @return the blob
7170
*/
72-
public List<Blob> getBlobs(String userName, String repositoryName, String treeSha);
71+
public PullRequest getPullRequest(String userName, String repositoryName, int issueNumber);
7372

74-
/**
75-
* Gets the object content.
76-
*
77-
* @param userName
78-
* the user name
79-
* @param repositoryName
80-
* the repository name
81-
* @param objectSha
82-
* the object sha
83-
*
84-
* @return the object content
85-
*/
86-
public InputStream getObjectContent(String userName, String repositoryName, String objectSha);
73+
public void createPullRequest(String userName, String repositoryName, String base, String head, String title, String body);
8774
}

core/src/main/java/com/github/api/v2/services/constant/ParameterNames.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,9 @@ public interface ParameterNames {
163163
public static final String PERMISSION = "permission";
164164
public static final String REPO_NAMES = "repo_names";
165165
public static final String BILLING_EMAIL = "billing_email";
166+
167+
public static final String BASE = "base";
168+
169+
public static final String HEAD = "head";
166170

167171
}

core/src/main/java/com/github/api/v2/services/impl/BaseGitHubService.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.InputStream;
2020
import java.io.InputStreamReader;
21-
import java.lang.reflect.Field;
2221
import java.lang.reflect.Type;
2322
import java.nio.charset.Charset;
2423
import java.util.ArrayList;
@@ -38,7 +37,6 @@
3837
import com.github.api.v2.services.constant.ApplicationConstants;
3938
import com.github.api.v2.services.constant.GitHubApiUrls.GitHubApiUrlBuilder;
4039
import com.google.gson.FieldNamingPolicy;
41-
import com.google.gson.FieldNamingStrategy;
4240
import com.google.gson.Gson;
4341
import com.google.gson.GsonBuilder;
4442
import com.google.gson.JsonDeserializationContext;
@@ -130,19 +128,19 @@ protected GsonBuilder getGsonBuilder() {
130128
GsonBuilder builder = new GsonBuilder();
131129
builder.setDateFormat(ApplicationConstants.DATE_FORMAT);
132130
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
133-
builder.setFieldNamingStrategy(new FieldNamingStrategy() {
134-
@Override
135-
public String translateName(Field field) {
136-
if (field.getType().equals(Repository.Visibility.class)) {
137-
return "private";
138-
} else if (field.getType().equals(Gist.Visibility.class)) {
139-
return "public";
140-
} else {
141-
return field.getName();
142-
}
143-
}
144-
145-
});
131+
// builder.setFieldNamingStrategy(new FieldNamingStrategy() {
132+
// @Override
133+
// public String translateName(Field field) {
134+
// if (field.getType().equals(Repository.Visibility.class)) {
135+
// return "private";
136+
// } else if (field.getType().equals(Gist.Visibility.class)) {
137+
// return "public";
138+
// } else {
139+
// return field.getName();
140+
// }
141+
// }
142+
//
143+
// });
146144
builder.registerTypeAdapter(Issue.State.class, new JsonDeserializer<Issue.State>() {
147145
@Override
148146
public Issue.State deserialize(JsonElement arg0, Type arg1,

core/src/main/java/com/github/api/v2/services/impl/PullRequestServiceImpl.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616
*/
1717
package com.github.api.v2.services.impl;
1818

19-
import java.io.InputStream;
19+
import java.util.HashMap;
2020
import java.util.List;
21+
import java.util.Map;
2122

22-
import com.github.api.v2.schema.Blob;
23-
import com.github.api.v2.schema.Tree;
23+
import com.github.api.v2.schema.PullRequest;
24+
import com.github.api.v2.schema.Issue.State;
2425
import com.github.api.v2.services.PullRequestService;
2526
import com.github.api.v2.services.constant.GitHubApiUrls;
2627
import com.github.api.v2.services.constant.ParameterNames;
2728
import com.github.api.v2.services.constant.GitHubApiUrls.GitHubApiUrlBuilder;
29+
import com.google.gson.GsonBuilder;
2830
import com.google.gson.JsonObject;
2931
import com.google.gson.reflect.TypeToken;
3032

@@ -38,49 +40,55 @@ public class PullRequestServiceImpl extends BaseGitHubService implements
3840
* @see com.github.api.v2.services.ObjectService#getBlob(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
3941
*/
4042
@Override
41-
public Blob getBlob(String userName, String repositoryName, String treeSha,
42-
String filePath) {
43-
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.ObjectApiUrls.GET_BLOBS_URL);
44-
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.SHA, treeSha).withField(ParameterNames.FILE_PATH, filePath).buildUrl();
43+
public PullRequest getPullRequest(String userName, String repositoryName, int issueNumber) {
44+
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.PullRequestApiUrls.GET_PULL_REQUEST_URL);
45+
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.ISSUE_NUMBER, String.valueOf(issueNumber)).buildUrl();
4546
JsonObject json = unmarshall(callApiGet(apiUrl));
4647

47-
return unmarshall(new TypeToken<Blob>(){}, json.get("blob"));
48+
return unmarshall(new TypeToken<PullRequest>(){}, json.get("pull"));
4849
}
4950

5051
/* (non-Javadoc)
5152
* @see com.github.api.v2.services.ObjectService#getBlobs(java.lang.String, java.lang.String, java.lang.String)
5253
*/
5354
@Override
54-
public List<Blob> getBlobs(String userName, String repositoryName,
55-
String treeSha) {
56-
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.ObjectApiUrls.GET_BLOBS_URL);
57-
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.SHA, treeSha).buildUrl();
55+
public List<PullRequest> getPullRequests(String userName, String repositoryName) {
56+
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.PullRequestApiUrls.GET_PULL_REQUESTS_URL);
57+
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.STATE, "").buildUrl();
5858
JsonObject json = unmarshall(callApiGet(apiUrl));
5959

60-
return unmarshall(new TypeToken<List<Blob>>(){}, json.get("blobs"));
60+
return unmarshall(new TypeToken<List<PullRequest>>(){}, json.get("pulls"));
6161
}
6262

6363
/* (non-Javadoc)
64-
* @see com.github.api.v2.services.ObjectService#getObjectContent(java.lang.String, java.lang.String, java.lang.String)
64+
* @see com.github.api.v2.services.ObjectService#getBlobs(java.lang.String, java.lang.String, java.lang.String)
6565
*/
6666
@Override
67-
public InputStream getObjectContent(String userName, String repositoryName,
68-
String objectSha) {
69-
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.ObjectApiUrls.GET_OBJECT_CONTENT_URL);
70-
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.SHA, objectSha).buildUrl();
71-
return callApiGet(apiUrl);
67+
public List<PullRequest> getPullRequests(String userName, String repositoryName, State state) {
68+
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.PullRequestApiUrls.GET_PULL_REQUESTS_URL);
69+
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.STATE, state.value()).buildUrl();
70+
JsonObject json = unmarshall(callApiGet(apiUrl));
71+
72+
return unmarshall(new TypeToken<List<PullRequest>>(){}, json.get("pulls"));
7273
}
73-
74+
7475
/* (non-Javadoc)
75-
* @see com.github.api.v2.services.ObjectService#getTree(java.lang.String, java.lang.String, java.lang.String)
76+
* @see com.github.api.v2.services.ObjectService#getObjectContent(java.lang.String, java.lang.String, java.lang.String)
7677
*/
77-
@Override
78-
public List<Tree> getTree(String userName, String repositoryName,
79-
String treeSha) {
80-
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.ObjectApiUrls.GET_TREE_URL);
81-
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).withField(ParameterNames.SHA, treeSha).buildUrl();
82-
JsonObject json = unmarshall(callApiGet(apiUrl));
83-
84-
return unmarshall(new TypeToken<List<Tree>>(){}, json.get("tree"));
78+
public void createPullRequest(String userName, String repositoryName, String base, String head, String title, String body) {
79+
GitHubApiUrlBuilder builder = createGitHubApiUrlBuilder(GitHubApiUrls.PullRequestApiUrls.CREATE_PULL_REQUEST_URL);
80+
String apiUrl = builder.withField(ParameterNames.USER_NAME, userName).withField(ParameterNames.REPOSITORY_NAME, repositoryName).buildUrl();
81+
Map<String, String> parameters = new HashMap<String, String>();
82+
parameters.put("pull[" + ParameterNames.BASE + "]", base);
83+
parameters.put("pull[" + ParameterNames.HEAD + "]", head);
84+
parameters.put("pull[" + ParameterNames.TITLE + "]", title);
85+
parameters.put("pull[" + ParameterNames.BODY + "]", body);
86+
callApiPost(apiUrl, parameters);
87+
}
88+
89+
protected GsonBuilder getGsonBuilder() {
90+
GsonBuilder gson = super.getGsonBuilder();
91+
gson.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
92+
return gson;
8593
}
8694
}

core/src/main/resources/com/github/api/v2/services/constant/GitHubApiUrls.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ com.github.api.v2.services.organizationService.removeTeamRepository=http://githu
107107

108108
# Pull Request API
109109
com.github.api.v2.services.pullRequestService.createPullRequest=http://github.com/api/{version}/{format}/pulls/{userName}/{repositoryName}
110-
com.github.api.v2.services.pullRequestService.getPullRequest=http://github.com/api/{version}/{format}/pulls/{userName}/{repositoryName}/{requestNumber}
110+
com.github.api.v2.services.pullRequestService.getPullRequest=http://github.com/api/{version}/{format}/pulls/{userName}/{repositoryName}/{issueNumber}
111111
com.github.api.v2.services.pullRequestService.getPullRequests=http://github.com/api/{version}/{format}/pulls/{userName}/{repositoryName}/{state}
112112

113113
# Feed

core/src/test/java/com/github/api/v2/services/PullRequestServiceTest.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
*/
1717
package com.github.api.v2.services;
1818

19-
import java.io.InputStream;
2019
import java.util.List;
2120

2221
import org.junit.After;
2322
import org.junit.Before;
2423
import org.junit.Test;
2524

26-
import com.github.api.v2.schema.Blob;
27-
import com.github.api.v2.schema.Tree;
25+
import com.github.api.v2.schema.PullRequest;
2826
import com.github.api.v2.services.constant.TestConstants;
2927

3028
/**
@@ -52,48 +50,35 @@ public void tearDown() throws Exception {
5250
* Test get blob.
5351
*/
5452
@Test
55-
public void testGetBlob() {
53+
public void testGetPullRequest() {
5654
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Username."), TestConstants.TEST_USER_NAME);
5755
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Repository."), TestConstants.TEST_REPOSITORY_NAME);
58-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Tree SHA."), TestConstants.TEST_TREE_SHA);
59-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test File Path."), TestConstants.TEST_FILE_PATH);
60-
Blob blob = service.getBlob(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, TestConstants.TEST_TREE_SHA, TestConstants.TEST_FILE_PATH);
61-
assertNotNull("Blob cannot be null or empty", blob);
56+
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Issue Number."), TestConstants.TEST_ISSUE_NUMBER);
57+
PullRequest pullRequest = service.getPullRequest(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, Integer.parseInt(TestConstants.TEST_ISSUE_NUMBER));
58+
assertNotNull("Pull request cannot be null or empty", pullRequest);
6259
}
6360

6461
/**
6562
* Test get blobs.
6663
*/
6764
@Test
68-
public void testGetBlobs() {
65+
public void testGetPullRequests() {
6966
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Username."), TestConstants.TEST_USER_NAME);
7067
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Repository."), TestConstants.TEST_REPOSITORY_NAME);
7168
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Tree SHA."), TestConstants.TEST_TREE_SHA);
72-
List<Blob> blobs = service.getBlobs(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, TestConstants.TEST_TREE_SHA);
73-
assertNotNullOrEmpty("Blobs cannot be null or empty", blobs);
69+
List<PullRequest> pullRequests = service.getPullRequests(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME);
70+
assertNotNullOrEmpty("Pull requests cannot be null or empty", pullRequests);
7471
}
7572

7673
/**
7774
* Test get object content.
7875
*/
7976
@Test
80-
public void testGetObjectContent() {
81-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Username."), TestConstants.TEST_USER_NAME);
82-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Repository."), TestConstants.TEST_REPOSITORY_NAME);
83-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Tree SHA."), TestConstants.TEST_TREE_SHA);
84-
InputStream objectContent = service.getObjectContent(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, TestConstants.TEST_TREE_SHA);
85-
assertNotNullOrEmpty("Object content cannot be null or empty", convertStreamToString(objectContent));
86-
}
87-
88-
/**
89-
* Test get tree.
90-
*/
91-
@Test
92-
public void testGetTree() {
77+
public void testCreatePullRequest() {
9378
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Username."), TestConstants.TEST_USER_NAME);
9479
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Repository."), TestConstants.TEST_REPOSITORY_NAME);
95-
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Tree SHA."), TestConstants.TEST_TREE_SHA);
96-
List<Tree> trees = service.getTree(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, TestConstants.TEST_TREE_SHA);
97-
assertNotNullOrEmpty("Tree cannot be null or empty", trees);
80+
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Base SHA."), TestConstants.TEST_BASE_SHA);
81+
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, "Test Head SHA."), TestConstants.TEST_HEAD_SHA);
82+
service.createPullRequest(TestConstants.TEST_USER_NAME, TestConstants.TEST_REPOSITORY_NAME, TestConstants.TEST_BASE_SHA, TestConstants.TEST_HEAD_SHA, TestConstants.TEST_ISSUE_TITLE, TestConstants.TEST_ISSUE_BODY);
9883
}
9984
}

core/src/test/java/com/github/api/v2/services/constant/TestConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ public final class TestConstants {
139139
/** The Constant TEST_KEY_ID. */
140140
public static final String TEST_KEY_ID =
141141
testConstants.getProperty("com.github.api.v2.services.testKeyId");
142+
143+
public static final String TEST_BASE_SHA = testConstants.getProperty("com.github.api.v2.services.testBaseSha");
144+
145+
public static final String TEST_HEAD_SHA = testConstants.getProperty("com.github.api.v2.services.testHeadSha");
142146

143147
/**
144148
* Instantiates a new test constants.

0 commit comments

Comments
 (0)