Skip to content

Commit 333a8ea

Browse files
committed
fix(requests): got calls will be retried on response code 429 #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 2b23f0d commit 333a8ea

File tree

7 files changed

+56
-5
lines changed

7 files changed

+56
-5
lines changed

Diff for: lib/definitions/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
export const HOME_URL = 'https://github.com/semantic-release/semantic-release';
22

33
export const RELEASE_NAME = 'GitLab release';
4+
5+
export const RETRY_CONFIGURATION = {
6+
retry: {
7+
limit: 3,
8+
statusCodes: [429],
9+
},
10+
};

Diff for: lib/fail.js

+7-1
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 { RETRY_CONFIGURATION } 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_CONFIGURATION,
29+
};
2430

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

Diff for: lib/publish.js

+2-1
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 { RELEASE_NAME, RETRY_CONFIGURATION } 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_CONFIGURATION,
4950
};
5051

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

Diff for: lib/success.js

+7-1
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 { RETRY_CONFIGURATION } 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_CONFIGURATION,
27+
};
2228

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

Diff for: lib/verify.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import AggregateError from "aggregate-error";
77
import resolveConfig from "./resolve-config.js";
88
import getRepoId from "./get-repo-id.js";
99
import getError from "./get-error.js";
10+
import { RETRY_CONFIGURATION } from "./definitions/constants.js";
1011

1112
const isNonEmptyString = (value) => isString(value) && value.trim();
1213
const isStringOrStringArray = (value) =>
@@ -33,6 +34,12 @@ export default async (pluginConfig, context) => {
3334
} = context;
3435
const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(pluginConfig, context);
3536
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
37+
const apiOptions = {
38+
headers: {
39+
"PRIVATE-TOKEN": gitlabToken,
40+
},
41+
...RETRY_CONFIGURATION,
42+
};
3643

3744
debug("apiUrl: %o", gitlabApiUrl);
3845
debug("repoId: %o", repoId);
@@ -65,7 +72,7 @@ export default async (pluginConfig, context) => {
6572
permissions: { project_access: projectAccess, group_access: groupAccess },
6673
} = await got
6774
.get(urlJoin(gitlabApiUrl, `/projects/${encodeURIComponent(repoId)}`), {
68-
headers: { "PRIVATE-TOKEN": gitlabToken },
75+
...apiOptions,
6976
...proxy,
7077
})
7178
.json());

Diff for: test/publish.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,27 @@ 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 gitlab = authenticate(env)
654+
.post(`/projects/${encodedRepoId}/releases`, {
655+
tag_name: nextRelease.gitTag,
656+
description: nextRelease.notes,
657+
assets: {
658+
links: [],
659+
},
660+
})
661+
.reply(429, { message: "Too many requests" })
662+
.persist();
663+
664+
const error = await t.throwsAsync(publish(pluginConfig, { env, options, nextRelease, logger: t.context.logger }));
665+
t.is(error.message, `Response code 429 (Too many requests)`);
666+
t.true(gitlab.isDone());
667+
});

Diff for: test/verify.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ test.serial("Throw error if GitLab API return any other errors", async (t) => {
567567
const owner = "test_user";
568568
const repo = "test_repo";
569569
const env = { GITLAB_TOKEN: "gitlab_token" };
570-
const gitlab = authenticate(env).get(`/projects/${owner}%2F${repo}`).times(3).reply(500);
570+
const gitlab = authenticate(env).get(`/projects/${owner}%2F${repo}`).times(3).reply(500).persist();
571571

572572
const error = await t.throwsAsync(
573573
verify({}, { env, options: { repositoryUrl: `https://gitlab.com:${owner}/${repo}.git` }, logger: t.context.logger })

0 commit comments

Comments
 (0)