Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.testRuleset = exports.updateOrCreateRelease = exports.updateRelease = exports.createRelease = exports.createRuleset = exports.deleteRuleset = exports.getRulesetId = exports.listAllRulesets = exports.listRulesets = exports.getRulesetContent = exports.listAllReleases = exports.listReleases = exports.getLatestRulesetName = void 0;
  4. const api_1 = require("../api");
  5. const apiv2_1 = require("../apiv2");
  6. const logger_1 = require("../logger");
  7. const utils = require("../utils");
  8. const API_VERSION = "v1";
  9. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.rulesOrigin, apiVersion: API_VERSION });
  10. function _handleErrorResponse(response) {
  11. if (response.body && response.body.error) {
  12. return utils.reject(response.body.error, { code: 2 });
  13. }
  14. logger_1.logger.debug("[rules] error:", response.status, response.body);
  15. return utils.reject("Unexpected error encountered with rules.", {
  16. code: 2,
  17. });
  18. }
  19. async function getLatestRulesetName(projectId, service) {
  20. const releases = await listAllReleases(projectId);
  21. const prefix = `projects/${projectId}/releases/${service}`;
  22. const release = releases.find((r) => r.name.startsWith(prefix));
  23. if (!release) {
  24. return null;
  25. }
  26. return release.rulesetName;
  27. }
  28. exports.getLatestRulesetName = getLatestRulesetName;
  29. const MAX_RELEASES_PAGE_SIZE = 10;
  30. async function listReleases(projectId, pageToken = "") {
  31. const response = await apiClient.get(`/projects/${projectId}/releases`, {
  32. queryParams: {
  33. pageSize: MAX_RELEASES_PAGE_SIZE,
  34. pageToken,
  35. },
  36. });
  37. if (response.status === 200) {
  38. return response.body;
  39. }
  40. return _handleErrorResponse(response);
  41. }
  42. exports.listReleases = listReleases;
  43. async function listAllReleases(projectId) {
  44. let pageToken;
  45. let releases = [];
  46. do {
  47. const response = await listReleases(projectId, pageToken);
  48. if (response.releases && response.releases.length > 0) {
  49. releases = releases.concat(response.releases);
  50. }
  51. pageToken = response.nextPageToken;
  52. } while (pageToken);
  53. return releases.sort((a, b) => b.createTime.localeCompare(a.createTime));
  54. }
  55. exports.listAllReleases = listAllReleases;
  56. async function getRulesetContent(name) {
  57. const response = await apiClient.get(`/${name}`, {
  58. skipLog: { resBody: true },
  59. });
  60. if (response.status === 200) {
  61. const source = response.body.source;
  62. return source.files;
  63. }
  64. return _handleErrorResponse(response);
  65. }
  66. exports.getRulesetContent = getRulesetContent;
  67. const MAX_RULESET_PAGE_SIZE = 100;
  68. async function listRulesets(projectId, pageToken = "") {
  69. const response = await apiClient.get(`/projects/${projectId}/rulesets`, {
  70. queryParams: {
  71. pageSize: MAX_RULESET_PAGE_SIZE,
  72. pageToken,
  73. },
  74. skipLog: { resBody: true },
  75. });
  76. if (response.status === 200) {
  77. return response.body;
  78. }
  79. return _handleErrorResponse(response);
  80. }
  81. exports.listRulesets = listRulesets;
  82. async function listAllRulesets(projectId) {
  83. let pageToken;
  84. let rulesets = [];
  85. do {
  86. const response = await listRulesets(projectId, pageToken);
  87. if (response.rulesets) {
  88. rulesets = rulesets.concat(response.rulesets);
  89. }
  90. pageToken = response.nextPageToken;
  91. } while (pageToken);
  92. return rulesets.sort((a, b) => b.createTime.localeCompare(a.createTime));
  93. }
  94. exports.listAllRulesets = listAllRulesets;
  95. function getRulesetId(ruleset) {
  96. return ruleset.name.split("/").pop();
  97. }
  98. exports.getRulesetId = getRulesetId;
  99. async function deleteRuleset(projectId, id) {
  100. const response = await apiClient.delete(`/projects/${projectId}/rulesets/${id}`);
  101. if (response.status === 200) {
  102. return;
  103. }
  104. return _handleErrorResponse(response);
  105. }
  106. exports.deleteRuleset = deleteRuleset;
  107. async function createRuleset(projectId, files) {
  108. const payload = { source: { files } };
  109. const response = await apiClient.post(`/projects/${projectId}/rulesets`, payload, { skipLog: { body: true } });
  110. if (response.status === 200) {
  111. logger_1.logger.debug("[rules] created ruleset", response.body.name);
  112. return response.body.name;
  113. }
  114. return _handleErrorResponse(response);
  115. }
  116. exports.createRuleset = createRuleset;
  117. async function createRelease(projectId, rulesetName, releaseName) {
  118. const payload = {
  119. name: `projects/${projectId}/releases/${releaseName}`,
  120. rulesetName,
  121. };
  122. const response = await apiClient.post(`/projects/${projectId}/releases`, payload);
  123. if (response.status === 200) {
  124. logger_1.logger.debug("[rules] created release", response.body.name);
  125. return response.body.name;
  126. }
  127. return _handleErrorResponse(response);
  128. }
  129. exports.createRelease = createRelease;
  130. async function updateRelease(projectId, rulesetName, releaseName) {
  131. const payload = {
  132. release: {
  133. name: `projects/${projectId}/releases/${releaseName}`,
  134. rulesetName,
  135. },
  136. };
  137. const response = await apiClient.patch(`/projects/${projectId}/releases/${releaseName}`, payload);
  138. if (response.status === 200) {
  139. logger_1.logger.debug("[rules] updated release", response.body.name);
  140. return response.body.name;
  141. }
  142. return _handleErrorResponse(response);
  143. }
  144. exports.updateRelease = updateRelease;
  145. async function updateOrCreateRelease(projectId, rulesetName, releaseName) {
  146. logger_1.logger.debug("[rules] releasing", releaseName, "with ruleset", rulesetName);
  147. return updateRelease(projectId, rulesetName, releaseName).catch(() => {
  148. logger_1.logger.debug("[rules] ruleset update failed, attempting to create instead");
  149. return createRelease(projectId, rulesetName, releaseName);
  150. });
  151. }
  152. exports.updateOrCreateRelease = updateOrCreateRelease;
  153. function testRuleset(projectId, files) {
  154. return apiClient.post(`/projects/${encodeURIComponent(projectId)}:test`, { source: { files } }, { skipLog: { body: true } });
  155. }
  156. exports.testRuleset = testRuleset;