View Javadoc

1   package org.apache.maven.plugins.patchtracker.patching.github;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.http.HttpResponse;
25  import org.apache.http.client.ClientProtocolException;
26  import org.apache.http.client.methods.HttpGet;
27  import org.apache.http.impl.client.DefaultHttpClient;
28  import org.apache.maven.plugin.logging.Log;
29  import org.apache.maven.plugins.patchtracker.patching.PatchRepository;
30  import org.apache.maven.plugins.patchtracker.patching.PatchRepositoryException;
31  import org.apache.maven.plugins.patchtracker.patching.PatchRepositoryRequest;
32  import org.apache.maven.plugins.patchtracker.patching.PatchRepositoryResult;
33  import org.codehaus.jackson.JsonProcessingException;
34  import org.codehaus.jackson.map.DeserializationConfig;
35  import org.codehaus.jackson.map.ObjectMapper;
36  
37  import java.io.IOException;
38  
39  /**
40   * @author Olivier Lamy
41   * @plexus.component role="org.apache.maven.plugins.patchtracker.patching.PatchRepository" role-hint="github"
42   */
43  public class GitHubPatchRepository
44      implements PatchRepository
45  {
46      DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
47  
48      public PatchRepositoryResult getPatch( PatchRepositoryRequest patchRepositoryRequest, Log log )
49          throws PatchRepositoryException
50      {
51          try
52          {
53              //curl -v https://api.github.com/repos/apache/directmemory/pulls/1
54              String baseUrl = patchRepositoryRequest.getUrl();
55              if ( StringUtils.isEmpty( baseUrl ) )
56              {
57                  baseUrl = "https://api.github.com";
58                  log.info( "github api url is empty use default:" + baseUrl );
59              }
60              String url = patchRepositoryRequest.getUrl() + "/repos/" + patchRepositoryRequest.getOrganization() + "/"
61                  + patchRepositoryRequest.getRepository() + "/pulls/" + patchRepositoryRequest.getId();
62              log.debug( "url" + url );
63  
64              HttpGet httpGet = new HttpGet( url );
65  
66              HttpResponse httpResponse = defaultHttpClient.execute( httpGet );
67  
68              String response = IOUtils.toString( httpResponse.getEntity().getContent() );
69  
70              log.debug( "response:" + response );
71  
72              PatchRepositoryResult patchRepositoryResult = fromJson( response );
73  
74              log.debug( "patchRepositoryResult:" + patchRepositoryResult );
75  
76              patchRepositoryResult.setPatchContent( getPatchContent( patchRepositoryResult ) );
77  
78              log.debug( "pullRequest:" + patchRepositoryResult.toString() );
79              return patchRepositoryResult;
80          }
81          catch ( IOException e )
82          {
83              throw new PatchRepositoryException( e.getMessage(), e );
84          }
85      }
86  
87      protected String getPatchContent( PatchRepositoryResult pullRequest )
88          throws IOException, ClientProtocolException
89      {
90          HttpGet httpGet = new HttpGet( pullRequest.getPatchUrl() );
91  
92          HttpResponse httpResponse = defaultHttpClient.execute( httpGet );
93  
94          return IOUtils.toString( httpResponse.getEntity().getContent() );
95      }
96  
97      /**
98       * parse json from gitbut see http://developer.github.com/v3/pulls/
99       *
100      * @param jsonContent
101      * @return
102      * @throws IOException
103      * @throws JsonProcessingException
104      */
105     protected PatchRepositoryResult fromJson( String jsonContent )
106         throws IOException, JsonProcessingException
107     {
108         ObjectMapper objectMapper = new ObjectMapper();
109 
110         // we don't parse all stuff
111         // se json format below or documentation
112         objectMapper.configure( DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false );
113 
114         //objectMapper.
115 
116         return objectMapper.reader( GithubPatchRepositoryResult.class ).readValue( jsonContent );
117     }
118 
119     /**
120      * json format
121      * <p/>
122      * curl -v https://api.github.com/repos/apache/directmemory/pulls/1
123      * <p/>
124      * {
125      * "merged_by": null,
126      * "merged": false,
127      * "title": "test wonderful patch ",
128      * "head": {
129      * "repo": {
130      * "watchers": 1,
131      * "forks": 0,
132      * "pushed_at": "2011-11-26T15:17:32Z",
133      * "svn_url": "https://svn.github.com/olamy/directmemory",
134      * "description": "Mirror of Apache DirectMemory",
135      * "created_at": "2011-11-26T09:39:35Z",
136      * "url": "https://api.github.com/repos/olamy/directmemory",
137      * "fork": true,
138      * "language": "Java",
139      * "git_url": "git://github.com/olamy/directmemory.git",
140      * "clone_url": "https://github.com/olamy/directmemory.git",
141      * "open_issues": 0,
142      * "private": false,
143      * "html_url": "https://github.com/olamy/directmemory",
144      * "homepage": null,
145      * "size": 112,
146      * "master_branch": "trunk",
147      * "updated_at": "2011-11-26T15:17:32Z",
148      * "owner": {
149      * "avatar_url": "https://secure.gravatar.com/avatar/bab29f762bea8e578505424443d8cd41?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
150      * "url": "https://api.github.com/users/olamy",
151      * "login": "olamy",
152      * "gravatar_id": "bab29f762bea8e578505424443d8cd41",
153      * "id": 19728
154      * },
155      * "name": "directmemory",
156      * "id": 2854963,
157      * "ssh_url": "git@github.com:olamy/directmemory.git"
158      * },
159      * "user": {
160      * "avatar_url": "https://secure.gravatar.com/avatar/bab29f762bea8e578505424443d8cd41?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
161      * "url": "https://api.github.com/users/olamy",
162      * "login": "olamy",
163      * "gravatar_id": "bab29f762bea8e578505424443d8cd41",
164      * "id": 19728
165      * },
166      * "ref": "trunk",
167      * "label": "olamy:trunk",
168      * "sha": "73d403199b415108a6f2a72c95b9ec684e6f1dc1"
169      * },
170      * "_links": {
171      * "html": {
172      * "href": "https://github.com/apache/directmemory/pull/1"
173      * },
174      * "review_comments": {
175      * "href": "https://api.github.com/repos/apache/directmemory/pulls/1/comments"
176      * },
177      * "self": {
178      * "href": "https://api.github.com/repos/apache/directmemory/pulls/1"
179      * },
180      * "comments": {
181      * "href": "https://api.github.com/repos/apache/directmemory/issues/1/comments"
182      * }
183      * },
184      * "merged_at": null,
185      * "created_at": "2011-11-26T14:59:52Z",
186      * "state": "open",
187      * "url": "https://api.github.com/repos/apache/directmemory/pulls/1",
188      * "commits": 3,
189      * "deletions": 0,
190      * "changed_files": 2,
191      * "review_comments": 0,
192      * "closed_at": null,
193      * "user": {
194      * "avatar_url": "https://secure.gravatar.com/avatar/bab29f762bea8e578505424443d8cd41?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
195      * "url": "https://api.github.com/users/olamy",
196      * "login": "olamy",
197      * "gravatar_id": "bab29f762bea8e578505424443d8cd41",
198      * "id": 19728
199      * },
200      * "base": {
201      * "repo": {
202      * "watchers": 3,
203      * "forks": 2,
204      * "pushed_at": "2011-11-03T00:02:25Z",
205      * "svn_url": "https://svn.github.com/apache/directmemory",
206      * "description": "Mirror of Apache DirectMemory",
207      * "created_at": "2011-10-15T07:00:08Z",
208      * "url": "https://api.github.com/repos/apache/directmemory",
209      * "fork": false,
210      * "language": "Java",
211      * "git_url": "git://github.com/apache/directmemory.git",
212      * "clone_url": "https://github.com/apache/directmemory.git",
213      * "open_issues": 0,
214      * "private": false,
215      * "html_url": "https://github.com/apache/directmemory",
216      * "homepage": null,
217      * "size": 3076,
218      * "master_branch": "trunk",
219      * "updated_at": "2011-11-26T09:39:35Z",
220      * "owner": {
221      * "avatar_url": "https://secure.gravatar.com/avatar/a676c0bf448fcd49f588249ead719b4c?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
222      * "url": "https://api.github.com/users/apache",
223      * "login": "apache",
224      * "gravatar_id": "a676c0bf448fcd49f588249ead719b4c",
225      * "id": 47359
226      * },
227      * "name": "directmemory",
228      * "id": 2580771,
229      * "ssh_url": "git@github.com:apache/directmemory.git"
230      * },
231      * "user": {
232      * "avatar_url": "https://secure.gravatar.com/avatar/a676c0bf448fcd49f588249ead719b4c?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png",
233      * "url": "https://api.github.com/users/apache",
234      * "login": "apache",
235      * "gravatar_id": "a676c0bf448fcd49f588249ead719b4c",
236      * "id": 47359
237      * },
238      * "ref": "trunk",
239      * "label": "apache:trunk",
240      * "sha": "f9ddc283dbc4f391a0876b5bd36453ac7e563b79"
241      * },
242      * "html_url": "https://github.com/apache/directmemory/pull/1",
243      * "updated_at": "2011-11-26T15:17:33Z",
244      * "number": 1,
245      * "issue_url": "https://github.com/apache/directmemory/issues/1",
246      * "mergeable": true,
247      * "additions": 6,
248      * "patch_url": "https://github.com/apache/directmemory/pull/1.patch",
249      * "id": 530496,
250      * "comments": 0,
251      * "body": "test email works to directmemory ml .",
252      * "diff_url": "https://github.com/apache/directmemory/pull/1.diff"
253      * }
254      */
255 
256 }