integrations.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. addSuccessMessage,
  5. clearIndicators,
  6. } from 'sentry/actionCreators/indicator';
  7. import {Client} from 'sentry/api';
  8. import {t, tct} from 'sentry/locale';
  9. import type {Integration, Repository} from 'sentry/types';
  10. const api = new Client();
  11. /**
  12. * Removes an integration from a project.
  13. *
  14. * @param orgSlug Organization Slug
  15. * @param projectId Project Slug
  16. * @param integration The organization integration to remove
  17. */
  18. export function removeIntegrationFromProject(
  19. orgSlug: string,
  20. projectId: string,
  21. integration: Integration
  22. ) {
  23. const endpoint = `/projects/${orgSlug}/${projectId}/integrations/${integration.id}/`;
  24. addLoadingMessage();
  25. return api.requestPromise(endpoint, {method: 'DELETE'}).then(
  26. () => {
  27. addSuccessMessage(t('Disabled %s for %s', integration.name, projectId));
  28. },
  29. () => {
  30. addErrorMessage(t('Failed to disable %s for %s', integration.name, projectId));
  31. }
  32. );
  33. }
  34. /**
  35. * Add an integration to a project
  36. *
  37. * @param orgSlug Organization Slug
  38. * @param projectId Project Slug
  39. * @param integration The organization integration to add
  40. */
  41. export function addIntegrationToProject(
  42. orgSlug: string,
  43. projectId: string,
  44. integration: Integration
  45. ) {
  46. const endpoint = `/projects/${orgSlug}/${projectId}/integrations/${integration.id}/`;
  47. addLoadingMessage();
  48. return api.requestPromise(endpoint, {method: 'PUT'}).then(
  49. () => {
  50. addSuccessMessage(t('Enabled %s for %s', integration.name, projectId));
  51. },
  52. () => {
  53. addErrorMessage(t('Failed to enabled %s for %s', integration.name, projectId));
  54. }
  55. );
  56. }
  57. /**
  58. * Delete a respository
  59. *
  60. * @param client ApiClient
  61. * @param orgSlug Organization Slug
  62. * @param repositoryId Repository ID
  63. */
  64. export function deleteRepository(client: Client, orgSlug: string, repositoryId: string) {
  65. addLoadingMessage();
  66. const promise = client.requestPromise(
  67. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  68. {
  69. method: 'DELETE',
  70. }
  71. );
  72. promise.then(
  73. () => clearIndicators(),
  74. () => addErrorMessage(t('Unable to delete repository.'))
  75. );
  76. return promise;
  77. }
  78. /**
  79. * Cancel the deletion of a respository
  80. *
  81. * @param client ApiClient
  82. * @param orgSlug Organization Slug
  83. * @param repositoryId Repository ID
  84. */
  85. export function cancelDeleteRepository(
  86. client: Client,
  87. orgSlug: string,
  88. repositoryId: string
  89. ) {
  90. addLoadingMessage();
  91. const promise = client.requestPromise(
  92. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  93. {
  94. method: 'PUT',
  95. data: {status: 'visible'},
  96. }
  97. );
  98. promise.then(
  99. () => clearIndicators(),
  100. () => addErrorMessage(t('Unable to cancel deletion.'))
  101. );
  102. return promise;
  103. }
  104. /**
  105. * Delete a repository by setting its status to hidden.
  106. *
  107. * @param client ApiClient
  108. * @param orgSlug Organization Slug
  109. * @param repositoryId Repository ID
  110. */
  111. export function hideRepository(client: Client, orgSlug: string, repositoryId: string) {
  112. addLoadingMessage();
  113. const promise = client.requestPromise(
  114. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  115. {
  116. method: 'PUT',
  117. data: {status: 'hidden'},
  118. }
  119. );
  120. promise.then(
  121. () => clearIndicators(),
  122. () => addErrorMessage(t('Unable to delete repository.'))
  123. );
  124. return promise;
  125. }
  126. function applyRepositoryAddComplete(promise: Promise<Repository>) {
  127. promise.then(
  128. (repo: Repository) => {
  129. const message = tct('[repo] has been successfully added.', {
  130. repo: repo.name,
  131. });
  132. addSuccessMessage(message);
  133. },
  134. errorData => {
  135. const text = errorData.responseJSON.errors
  136. ? errorData.responseJSON.errors.__all__
  137. : t('Unable to add repository.');
  138. addErrorMessage(text);
  139. }
  140. );
  141. return promise;
  142. }
  143. /**
  144. * Migrate a repository to a new integration.
  145. *
  146. * @param client ApiClient
  147. * @param orgSlug Organization Slug
  148. * @param repositoryId Repository ID
  149. * @param integration Integration provider data.
  150. */
  151. export function migrateRepository(
  152. client: Client,
  153. orgSlug: string,
  154. repositoryId: string,
  155. integration: Integration
  156. ): Promise<Repository> {
  157. const data = {integrationId: integration.id};
  158. addLoadingMessage();
  159. const promise = client.requestPromise(
  160. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  161. {
  162. data,
  163. method: 'PUT',
  164. }
  165. );
  166. return applyRepositoryAddComplete(promise);
  167. }
  168. /**
  169. * Add a repository
  170. *
  171. * @param client ApiClient
  172. * @param orgSlug Organization Slug
  173. * @param name Repository identifier/name to add
  174. * @param integration Integration provider data.
  175. */
  176. export function addRepository(
  177. client: Client,
  178. orgSlug: string,
  179. name: string,
  180. integration: Integration
  181. ): Promise<Repository> {
  182. const data = {
  183. installation: integration.id,
  184. identifier: name,
  185. provider: `integrations:${integration.provider.key}`,
  186. };
  187. addLoadingMessage();
  188. const promise = client.requestPromise(`/organizations/${orgSlug}/repos/`, {
  189. method: 'POST',
  190. data,
  191. });
  192. return applyRepositoryAddComplete(promise);
  193. }