Skip to content

Commit aaf1091

Browse files
committed
fix(requests): got calls will be retried on response code 429 semantic-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
1 parent 36e4081 commit aaf1091

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

lib/definitions/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export const HOME_URL = 'https://github.com/semantic-release/semantic-release';
22

33
export const RELEASE_NAME = 'GitLab release';
4+
5+
export const DEFAULT_RETRY = {
6+
limit: 3,
7+
methods: ["GET", "POST"],
8+
statusCodes: [429],
9+
};

lib/fail.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
66
import resolveConfig from "./resolve-config.js";
77
import getRepoId from "./get-repo-id.js";
88
import getFailComment from "./get-fail-comment.js";
9+
import { DEFAULT_RETRY } from "./definitions/constants.js";
910

1011
export default async (pluginConfig, context) => {
1112
const {
@@ -20,7 +21,12 @@ export default async (pluginConfig, context) => {
2021
);
2122
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2223
const encodedRepoId = encodeURIComponent(repoId);
23-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
24+
const apiOptions = {
25+
headers: {
26+
"PRIVATE-TOKEN": gitlabToken,
27+
},
28+
retry: DEFAULT_RETRY,
29+
};
2430

2531
if (failComment === false || failTitle === false) {
2632
logger.log("Skip issue creation.");

lib/publish.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const debug = _debug("semantic-release:gitlab");
1111
import resolveConfig from "./resolve-config.js";
1212
import getRepoId from "./get-repo-id.js";
1313
import getAssets from "./glob-assets.js";
14-
import { RELEASE_NAME } from "./definitions/constants.js";
14+
import { DEFAULT_RETRY, RELEASE_NAME } from "./definitions/constants.js";
1515

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

@@ -46,6 +46,7 @@ export default async (pluginConfig, context) => {
4646
},
4747
],
4848
},
49+
retry: DEFAULT_RETRY,
4950
};
5051

5152
debug("repoId: %o", repoId);

lib/success.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const debug = _debug("semantic-release:gitlab");
66
import resolveConfig from "./resolve-config.js";
77
import getRepoId from "./get-repo-id.js";
88
import getSuccessComment from "./get-success-comment.js";
9+
import { DEFAULT_RETRY } from "./definitions/constants.js";
910

1011
export default async (pluginConfig, context) => {
1112
const {
@@ -18,7 +19,12 @@ export default async (pluginConfig, context) => {
1819
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, proxy } = resolveConfig(pluginConfig, context);
1920
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2021
const encodedRepoId = encodeURIComponent(repoId);
21-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
22+
const apiOptions = {
23+
headers: {
24+
"PRIVATE-TOKEN": gitlabToken,
25+
},
26+
retry: DEFAULT_RETRY,
27+
};
2228

2329
if (successComment === false) {
2430
logger.log("Skip commenting on issues and pull requests.");

test/publish.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,28 @@ test.serial("Publish a release with error response", async (t) => {
641641
t.is(error.message, `Response code 499 (Something went wrong)`);
642642
t.true(gitlab.isDone());
643643
});
644+
645+
test.serial("Publish a release with error response of 429 - too many requests - with persist", async (t) => {
646+
const owner = "test_user";
647+
const repo = "test_repo";
648+
const env = { GITLAB_TOKEN: "gitlab_token" };
649+
const pluginConfig = {};
650+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
651+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
652+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
653+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
654+
const gitlab = authenticate(env)
655+
.post(`/projects/${encodedRepoId}/releases`, {
656+
tag_name: nextRelease.gitTag,
657+
description: nextRelease.notes,
658+
assets: {
659+
links: [],
660+
},
661+
})
662+
.reply(429, { message: "Too many requests" })
663+
.persist();
664+
665+
const error = await t.throwsAsync(publish(pluginConfig, { env, options, nextRelease, logger: t.context.logger }));
666+
t.is(error.message, `Response code 429 (Too many requests)`);
667+
t.true(gitlab.isDone());
668+
});

0 commit comments

Comments
 (0)