integrations.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. addSuccessMessage,
  5. clearIndicators,
  6. } from 'sentry/actionCreators/indicator';
  7. import type {Client} from 'sentry/api';
  8. import {t, tct} from 'sentry/locale';
  9. import type {Integration, Repository} from 'sentry/types/integrations';
  10. /**
  11. * Cancel the deletion of a respository
  12. *
  13. * @param client ApiClient
  14. * @param orgSlug Organization Slug
  15. * @param repositoryId Repository ID
  16. */
  17. export function cancelDeleteRepository(
  18. client: Client,
  19. orgSlug: string,
  20. repositoryId: string
  21. ) {
  22. addLoadingMessage();
  23. const promise = client.requestPromise(
  24. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  25. {
  26. method: 'PUT',
  27. data: {status: 'visible'},
  28. }
  29. );
  30. promise.then(
  31. () => clearIndicators(),
  32. () => addErrorMessage(t('Unable to cancel deletion.'))
  33. );
  34. return promise;
  35. }
  36. /**
  37. * Delete a repository by setting its status to hidden.
  38. *
  39. * @param client ApiClient
  40. * @param orgSlug Organization Slug
  41. * @param repositoryId Repository ID
  42. */
  43. export function hideRepository(client: Client, orgSlug: string, repositoryId: string) {
  44. addLoadingMessage();
  45. const promise = client.requestPromise(
  46. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  47. {
  48. method: 'PUT',
  49. data: {status: 'hidden'},
  50. }
  51. );
  52. promise.then(
  53. () => clearIndicators(),
  54. () => addErrorMessage(t('Unable to delete repository.'))
  55. );
  56. return promise;
  57. }
  58. function applyRepositoryAddComplete(promise: Promise<Repository>) {
  59. promise.then(
  60. (repo: Repository) => {
  61. const message = tct('[repo] has been successfully added.', {
  62. repo: repo.name,
  63. });
  64. addSuccessMessage(message);
  65. },
  66. errorData => {
  67. const text = errorData.responseJSON.errors
  68. ? errorData.responseJSON.errors.__all__
  69. : t('Unable to add repository.');
  70. addErrorMessage(text);
  71. }
  72. );
  73. return promise;
  74. }
  75. /**
  76. * Migrate a repository to a new integration.
  77. *
  78. * @param client ApiClient
  79. * @param orgSlug Organization Slug
  80. * @param repositoryId Repository ID
  81. * @param integration Integration provider data.
  82. */
  83. export function migrateRepository(
  84. client: Client,
  85. orgSlug: string,
  86. repositoryId: string,
  87. integration: Integration
  88. ): Promise<Repository> {
  89. const data = {integrationId: integration.id};
  90. addLoadingMessage();
  91. const promise = client.requestPromise(
  92. `/organizations/${orgSlug}/repos/${repositoryId}/`,
  93. {
  94. data,
  95. method: 'PUT',
  96. }
  97. );
  98. return applyRepositoryAddComplete(promise);
  99. }
  100. /**
  101. * Add a repository
  102. *
  103. * @param client ApiClient
  104. * @param orgSlug Organization Slug
  105. * @param name Repository identifier/name to add
  106. * @param integration Integration provider data.
  107. */
  108. export function addRepository(
  109. client: Client,
  110. orgSlug: string,
  111. name: string,
  112. integration: Integration
  113. ): Promise<Repository> {
  114. const data = {
  115. installation: integration.id,
  116. identifier: name,
  117. provider: `integrations:${integration.provider.key}`,
  118. };
  119. addLoadingMessage();
  120. const promise = client.requestPromise(`/organizations/${orgSlug}/repos/`, {
  121. method: 'POST',
  122. data,
  123. });
  124. return applyRepositoryAddComplete(promise);
  125. }