16
16
*/
17
17
package com .github .api .v2 .services .impl ;
18
18
19
- import java .io . InputStream ;
19
+ import java .util . HashMap ;
20
20
import java .util .List ;
21
+ import java .util .Map ;
21
22
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 ;
24
25
import com .github .api .v2 .services .PullRequestService ;
25
26
import com .github .api .v2 .services .constant .GitHubApiUrls ;
26
27
import com .github .api .v2 .services .constant .ParameterNames ;
27
28
import com .github .api .v2 .services .constant .GitHubApiUrls .GitHubApiUrlBuilder ;
29
+ import com .google .gson .GsonBuilder ;
28
30
import com .google .gson .JsonObject ;
29
31
import com .google .gson .reflect .TypeToken ;
30
32
@@ -38,49 +40,55 @@ public class PullRequestServiceImpl extends BaseGitHubService implements
38
40
* @see com.github.api.v2.services.ObjectService#getBlob(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
39
41
*/
40
42
@ 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 ();
45
46
JsonObject json = unmarshall (callApiGet (apiUrl ));
46
47
47
- return unmarshall (new TypeToken <Blob >(){}, json .get ("blob " ));
48
+ return unmarshall (new TypeToken <PullRequest >(){}, json .get ("pull " ));
48
49
}
49
50
50
51
/* (non-Javadoc)
51
52
* @see com.github.api.v2.services.ObjectService#getBlobs(java.lang.String, java.lang.String, java.lang.String)
52
53
*/
53
54
@ 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 ();
58
58
JsonObject json = unmarshall (callApiGet (apiUrl ));
59
59
60
- return unmarshall (new TypeToken <List <Blob >>(){}, json .get ("blobs " ));
60
+ return unmarshall (new TypeToken <List <PullRequest >>(){}, json .get ("pulls " ));
61
61
}
62
62
63
63
/* (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)
65
65
*/
66
66
@ 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" ));
72
73
}
73
-
74
+
74
75
/* (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)
76
77
*/
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 ;
85
93
}
86
94
}
0 commit comments