PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/bamboo/plugins/git/rest/resource/GitHubResource.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 193 lines | 152 code | 22 blank | 19 comment | 8 complexity | 85a9277b01a1e8198911c86d5ac095ec MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git.rest.resource;
  2. import com.atlassian.bamboo.plugins.git.GitHubAccessor;
  3. import com.atlassian.bamboo.plugins.git.GitHubRepository;
  4. import com.atlassian.bamboo.plugins.git.github.api.rest.entity.GitHubBranchEntity;
  5. import com.atlassian.bamboo.plugins.git.github.api.rest.entity.GitHubRepositoryEntity;
  6. import com.atlassian.bamboo.plugins.git.rest.commons.RestConstants;
  7. import com.atlassian.bamboo.plugins.git.rest.entity.ListBranchesResponse;
  8. import com.atlassian.bamboo.plugins.git.rest.entity.ListRepositoriesResponse;
  9. import com.atlassian.bamboo.plugins.git.rest.entity.RestRequest;
  10. import com.atlassian.bamboo.repository.Repository;
  11. import com.atlassian.bamboo.repository.RepositoryData;
  12. import com.atlassian.bamboo.repository.RepositoryDataEntity;
  13. import com.atlassian.bamboo.repository.RepositoryDataImpl;
  14. import com.atlassian.bamboo.repository.RepositoryDefinitionManager;
  15. import com.atlassian.bamboo.rest.entity.RestResponse;
  16. import com.atlassian.bamboo.security.EncryptionService;
  17. import com.atlassian.bamboo.util.Narrow;
  18. import com.atlassian.sal.api.message.I18nResolver;
  19. import com.google.common.base.Function;
  20. import com.google.common.collect.ImmutableList;
  21. import com.google.common.collect.Iterables;
  22. import com.sun.jersey.spi.resource.Singleton;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.log4j.Logger;
  25. import org.jetbrains.annotations.Nullable;
  26. import javax.ws.rs.Consumes;
  27. import javax.ws.rs.POST;
  28. import javax.ws.rs.Path;
  29. import javax.ws.rs.PathParam;
  30. import javax.ws.rs.Produces;
  31. import javax.ws.rs.core.Context;
  32. import javax.ws.rs.core.MediaType;
  33. import javax.ws.rs.core.Response;
  34. import javax.ws.rs.core.UriInfo;
  35. import java.io.IOException;
  36. @Path(RestConstants.GITHUB)
  37. @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  38. @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  39. @Singleton
  40. public class GitHubResource
  41. {
  42. @SuppressWarnings("unused")
  43. private static final Logger log = Logger.getLogger(GitHubResource.class);
  44. // ------------------------------------------------------------------------------------------------------- Constants
  45. // ------------------------------------------------------------------------------------------------- Type Properties
  46. // ---------------------------------------------------------------------------------------------------- Dependencies
  47. private final EncryptionService encryptionService;
  48. private final I18nResolver i18nResolver;
  49. private final RepositoryDefinitionManager repositoryDefinitionManager;
  50. // ---------------------------------------------------------------------------------------------------- Constructors
  51. public GitHubResource(EncryptionService encryptionService, I18nResolver i18nResolver, RepositoryDefinitionManager repositoryDefinitionManager)
  52. {
  53. this.encryptionService = encryptionService;
  54. this.i18nResolver = i18nResolver;
  55. this.repositoryDefinitionManager = repositoryDefinitionManager;
  56. }
  57. // ----------------------------------------------------------------------------------------------- Interface Methods
  58. // -------------------------------------------------------------------------------------------------- Public Methods
  59. /**
  60. *
  61. * @param uriInfo
  62. * @param username
  63. * @return
  64. */
  65. @POST
  66. @Path(RestConstants.REPOSITORIES + "/{" + RestConstants.USERNAME + "}")
  67. public Response getAvailableRepositories(@Context UriInfo uriInfo,
  68. @PathParam(RestConstants.USERNAME) String username,
  69. RestRequest request)
  70. {
  71. if (request.getRepositoryId() > 0 && StringUtils.isBlank(request.getPassword()))
  72. {
  73. request.setPassword(getRepositoryPassword(request.getRepositoryId()));
  74. }
  75. RestResponse.Builder builder = RestResponse.builder();
  76. try
  77. {
  78. GitHubAccessor gitHubAccessor = new GitHubAccessor(username, request.getPassword());
  79. ImmutableList<GitHubRepositoryEntity> repositories = GitHubRepositoryEntity.orderingByFullName().immutableSortedCopy(
  80. Iterables.transform(gitHubAccessor.getAccessibleRepositories(),
  81. new Function<String, GitHubRepositoryEntity>()
  82. {
  83. @Override
  84. public GitHubRepositoryEntity apply(@Nullable String input)
  85. {
  86. return GitHubRepositoryEntity.builder().fullName(input).build();
  87. }
  88. })
  89. );
  90. ListRepositoriesResponse response = builder.build(ListRepositoriesResponse.class);
  91. response.setRepositories(repositories);
  92. return Response.ok(response).build();
  93. }
  94. catch (IOException e)
  95. {
  96. log.warn(i18nResolver.getText("repository.github.ajaxError"), e);
  97. builder.error(i18nResolver.getText("repository.github.ajaxError") + e.toString());
  98. }
  99. catch (GitHubAccessor.GitHubException e)
  100. {
  101. log.warn(i18nResolver.getText("repository.github.ajaxError"), e);
  102. builder.error(i18nResolver.getText("repository.github.ajaxError") + e.toString());
  103. }
  104. return Response.ok(builder.build(RestResponse.class)).build();
  105. }
  106. /**
  107. *
  108. * @param uriInfo
  109. * @param owner
  110. * @return
  111. */
  112. @POST
  113. @Path(RestConstants.REPOSITORIES + "/{" + RestConstants.OWNER + "}/{" + RestConstants.NAME + "}/" + RestConstants.BRANCHES)
  114. public Response getBranches(@Context UriInfo uriInfo,
  115. @PathParam(RestConstants.OWNER) String owner,
  116. @PathParam(RestConstants.NAME) String name,
  117. RestRequest request)
  118. {
  119. if (request.getRepositoryId() > 0 && StringUtils.isBlank(request.getPassword()))
  120. {
  121. request.setPassword(getRepositoryPassword(request.getRepositoryId()));
  122. }
  123. RestResponse.Builder builder = RestResponse.builder();
  124. try
  125. {
  126. GitHubAccessor gitHubAccessor = new GitHubAccessor(request.getUsername(), request.getPassword());
  127. ImmutableList<GitHubBranchEntity> branches = GitHubBranchEntity.orderingByName().immutableSortedCopy(
  128. Iterables.transform(gitHubAccessor.getBranches(String.format("%s/%s", owner, name)),
  129. new Function<String, GitHubBranchEntity>()
  130. {
  131. @Override
  132. public GitHubBranchEntity apply(@Nullable String input)
  133. {
  134. return GitHubBranchEntity.builder().name(input).build();
  135. }
  136. })
  137. );
  138. ListBranchesResponse response = builder.build(ListBranchesResponse.class);
  139. response.setBranches(branches);
  140. return Response.ok(response).build();
  141. }
  142. catch (IOException e)
  143. {
  144. log.warn(i18nResolver.getText("repository.github.ajaxError"), e);
  145. builder.error(i18nResolver.getText("repository.github.ajaxError") + e.toString());
  146. }
  147. catch (GitHubAccessor.GitHubException e)
  148. {
  149. log.warn(i18nResolver.getText("repository.github.ajaxError"), e);
  150. builder.error(i18nResolver.getText("repository.github.ajaxError") + e.toString());
  151. }
  152. return Response.ok(builder.build(RestResponse.class)).build();
  153. }
  154. // -------------------------------------------------------------------------------------- Basic Accessors / Mutators
  155. @Nullable
  156. private String getRepositoryPassword(long repositoryId)
  157. {
  158. RepositoryDataEntity repositoryDataEntity = repositoryDefinitionManager.getRepositoryDataEntity(repositoryId);
  159. if (repositoryDataEntity != null)
  160. {
  161. RepositoryData repositoryData = new RepositoryDataImpl(repositoryDataEntity);
  162. Repository repository = repositoryData.getRepository();
  163. GitHubRepository ghRepository = Narrow.to(repository, GitHubRepository.class);
  164. if (ghRepository != null)
  165. {
  166. return encryptionService.decrypt(ghRepository.getEncryptedPassword());
  167. }
  168. }
  169. return null;
  170. }
  171. }