integrations.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 {Integration, Repository} from 'sentry/types';
  10. const api = new Client();
  11. /**
  12. * Removes an integration from a project.
  13. *
  14. * @param {String} orgId Organization Slug
  15. * @param {String} projectId Project Slug
  16. * @param {Object} integration The organization integration to remove
  17. */
  18. export function removeIntegrationFromProject(
  19. orgId: string,
  20. projectId: string,
  21. integration: Integration
  22. ) {
  23. const endpoint = `/projects/${orgId}/${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 {String} orgId Organization Slug
  38. * @param {String} projectId Project Slug
  39. * @param {Object} integration The organization integration to add
  40. */
  41. export function addIntegrationToProject(
  42. orgId: string,
  43. projectId: string,
  44. integration: Integration
  45. ) {
  46. const endpoint = `/projects/${orgId}/${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 {Object} client ApiClient
  61. * @param {String} orgId Organization Slug
  62. * @param {String} repositoryId Repository ID
  63. */
  64. export function deleteRepository(client: Client, orgId: string, repositoryId: string) {
  65. addLoadingMessage();
  66. const promise = client.requestPromise(
  67. `/organizations/${orgId}/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 {Object} client ApiClient
  82. * @param {String} orgId Organization Slug
  83. * @param {String} repositoryId Repository ID
  84. */
  85. export function cancelDeleteRepository(
  86. client: Client,
  87. orgId: string,
  88. repositoryId: string
  89. ) {
  90. addLoadingMessage();
  91. const promise = client.requestPromise(
  92. `/organizations/${orgId}/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. function applyRepositoryAddComplete(promise: Promise<Repository>) {
  105. promise.then(
  106. (repo: Repository) => {
  107. const message = tct('[repo] has been successfully added.', {
  108. repo: repo.name,
  109. });
  110. addSuccessMessage(message);
  111. },
  112. errorData => {
  113. const text = errorData.responseJSON.errors
  114. ? errorData.responseJSON.errors.__all__
  115. : t('Unable to add repository.');
  116. addErrorMessage(text);
  117. }
  118. );
  119. return promise;
  120. }
  121. /**
  122. * Migrate a repository to a new integration.
  123. *
  124. * @param {Object} client ApiClient
  125. * @param {String} orgId Organization Slug
  126. * @param {String} repositoryId Repository ID
  127. * @param {Object} integration Integration provider data.
  128. */
  129. export function migrateRepository(
  130. client: Client,
  131. orgId: string,
  132. repositoryId: string,
  133. integration: Integration
  134. ) {
  135. const data = {integrationId: integration.id};
  136. addLoadingMessage();
  137. const promise = client.requestPromise(
  138. `/organizations/${orgId}/repos/${repositoryId}/`,
  139. {
  140. data,
  141. method: 'PUT',
  142. }
  143. );
  144. return applyRepositoryAddComplete(promise);
  145. }
  146. /**
  147. * Add a repository
  148. *
  149. * @param {Object} client ApiClient
  150. * @param {String} orgId Organization Slug
  151. * @param {String} name Repository identifier/name to add
  152. * @param {Object} integration Integration provider data.
  153. */
  154. export function addRepository(
  155. client: Client,
  156. orgId: string,
  157. name: string,
  158. integration: Integration
  159. ) {
  160. const data = {
  161. installation: integration.id,
  162. identifier: name,
  163. provider: `integrations:${integration.provider.key}`,
  164. };
  165. addLoadingMessage();
  166. const promise = client.requestPromise(`/organizations/${orgId}/repos/`, {
  167. method: 'POST',
  168. data,
  169. });
  170. return applyRepositoryAddComplete(promise);
  171. }