Skip to content

Commit

Permalink
fix(requests): got calls will be retried on response code 429 semanti…
Browse files Browse the repository at this point in the history
…c-release#361

If got receives a response code 429 - too many requests - it will retry up to three times for GET and POST requests.
This uses the retry API https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md
  • Loading branch information
JonasSchubert committed Jun 1, 2024
1 parent 36e4081 commit aaf1091
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/definitions/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const HOME_URL = 'https://github.com/semantic-release/semantic-release';

export const RELEASE_NAME = 'GitLab release';

export const DEFAULT_RETRY = {
limit: 3,
methods: ["GET", "POST"],
statusCodes: [429],
};
8 changes: 7 additions & 1 deletion lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
import resolveConfig from "./resolve-config.js";
import getRepoId from "./get-repo-id.js";
import getFailComment from "./get-fail-comment.js";
import { DEFAULT_RETRY } from "./definitions/constants.js";

export default async (pluginConfig, context) => {
const {
Expand All @@ -20,7 +21,12 @@ export default async (pluginConfig, context) => {
);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
const apiOptions = {
headers: {
"PRIVATE-TOKEN": gitlabToken,
},
retry: DEFAULT_RETRY,
};

if (failComment === false || failTitle === false) {
logger.log("Skip issue creation.");
Expand Down
3 changes: 2 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const debug = _debug("semantic-release:gitlab");
import resolveConfig from "./resolve-config.js";
import getRepoId from "./get-repo-id.js";
import getAssets from "./glob-assets.js";
import { RELEASE_NAME } from "./definitions/constants.js";
import { DEFAULT_RETRY, RELEASE_NAME } from "./definitions/constants.js";

const isUrlScheme = (value) => /^(https|http|ftp):\/\//.test(value);

Expand Down Expand Up @@ -46,6 +46,7 @@ export default async (pluginConfig, context) => {
},
],
},
retry: DEFAULT_RETRY,
};

debug("repoId: %o", repoId);
Expand Down
8 changes: 7 additions & 1 deletion lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
import resolveConfig from "./resolve-config.js";
import getRepoId from "./get-repo-id.js";
import getSuccessComment from "./get-success-comment.js";
import { DEFAULT_RETRY } from "./definitions/constants.js";

export default async (pluginConfig, context) => {
const {
Expand All @@ -18,7 +19,12 @@ export default async (pluginConfig, context) => {
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, proxy } = resolveConfig(pluginConfig, context);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
const apiOptions = {
headers: {
"PRIVATE-TOKEN": gitlabToken,
},
retry: DEFAULT_RETRY,
};

if (successComment === false) {
logger.log("Skip commenting on issues and pull requests.");
Expand Down
25 changes: 25 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,28 @@ test.serial("Publish a release with error response", async (t) => {
t.is(error.message, `Response code 499 (Something went wrong)`);
t.true(gitlab.isDone());
});

test.serial("Publish a release with error response of 429 - too many requests - with persist", async (t) => {
const owner = "test_user";
const repo = "test_repo";
const env = { GITLAB_TOKEN: "gitlab_token" };
const pluginConfig = {};
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
const gitlab = authenticate(env)
.post(`/projects/${encodedRepoId}/releases`, {
tag_name: nextRelease.gitTag,
description: nextRelease.notes,
assets: {
links: [],
},
})
.reply(429, { message: "Too many requests" })
.persist();

const error = await t.throwsAsync(publish(pluginConfig, { env, options, nextRelease, logger: t.context.logger }));
t.is(error.message, `Response code 429 (Too many requests)`);
t.true(gitlab.isDone());
});

0 comments on commit aaf1091

Please sign in to comment.