Skip to content

Commit

Permalink
fix: increase retry limit and make it configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher committed Dec 3, 2024
1 parent d82152e commit 0a141c9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ If you need to bypass the proxy for some hosts, configure the `NO_PROXY` environ
| `failCommentCondition` | Use this as condition, when to comment on or create an issues in case of failures. See [failCommentCondition](#failCommentCondition). | - |
| `labels` | The [labels](https://docs.gitlab.com/ee/user/project/labels.html#labels) to add to the issue created when a release fails. Set to `false` to not add any label. Labels should be comma-separated as described in the [official docs](https://docs.gitlab.com/ee/api/issues.html#new-issue), e.g. `"semantic-release,bot"`. | `semantic-release` |
| `assignee` | The [assignee](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#assignee) to add to the issue created when a release fails. | - |
| `retryLimit` | The maximum number of retries for failing HTTP requests. | `3` |

#### assets

Expand Down
7 changes: 5 additions & 2 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export default async (pluginConfig, context) => {
errors,
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee } =
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee, retryLimit } =
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: { limit: retryLimit }
};

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 @@ -22,7 +22,7 @@ export default async (pluginConfig, context) => {
nextRelease: { gitTag, gitHead, notes, version },
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy } = resolveConfig(pluginConfig, context);
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy, retryLimit } = resolveConfig(pluginConfig, context);
const assetsList = [];
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
Expand All @@ -46,6 +46,7 @@ export default async (pluginConfig, context) => {
},
],
},
retry: { limit: retryLimit }
};

debug("repoId: %o", repoId);
Expand Down
3 changes: 3 additions & 0 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default (
failCommentCondition,
labels,
assignee,
retryLimit,
},
{
envCi: { service } = {},
Expand All @@ -34,6 +35,7 @@ export default (
},
}
) => {
const DEFAULT_RETRY_LIMIT = 3
const userGitlabApiPathPrefix = isNil(gitlabApiPathPrefix)
? isNil(GL_PREFIX)
? GITLAB_PREFIX
Expand Down Expand Up @@ -64,6 +66,7 @@ export default (
failCommentCondition,
labels: isNil(labels) ? "semantic-release" : labels === false ? false : labels,
assignee,
retryLimit: retryLimit ?? DEFAULT_RETRY_LIMIT
};
};

Expand Down
7 changes: 5 additions & 2 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ export default async (pluginConfig, context) => {
commits,
releases,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy } = resolveConfig(
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy, retryLimit } = 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: { limit: retryLimit }
};

if (successComment === false) {
logger.log("Skip commenting on issues and pull requests.");
Expand Down
5 changes: 4 additions & 1 deletion test/resolve-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultOptions = {
labels: "semantic-release",
assignee: undefined,
proxy: {},
retryLimit: 3
};

test("Returns user config", (t) => {
Expand All @@ -27,10 +28,11 @@ test("Returns user config", (t) => {
const postComments = true;
const proxy = {};
const labels = false;
const retryLimit = 42

t.deepEqual(
resolveConfig(
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels },
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels, retryLimit },
{ env: { GITLAB_TOKEN: gitlabToken } }
),
{
Expand All @@ -40,6 +42,7 @@ test("Returns user config", (t) => {
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
labels: false,
retryLimit
}
);

Expand Down
28 changes: 28 additions & 0 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,31 @@ test.serial("Does not post comments when successCommentCondition is set to false

t.true(gitlab.isDone());
});


test.serial("Retries requests when rate limited", async (t) => {
const owner = "test_user";
const repo = "test_repo";
const env = { GITLAB_TOKEN: "gitlab_token" };
const pluginConfig = {};
const nextRelease = { version: "1.0.0" };
const releases = [{ name: RELEASE_NAME, url: "https://gitlab.com/test_user/test_repo/-/releases/v1.0.0" }];
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const commits = [{ hash: "abcdef" }];
const retryLimit = 3
const gitlab = authenticate(env)
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
.times(retryLimit)
.reply(429)
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
.reply(200, [{ project_id: 100, iid: 1, state: "merged" }])
.get(`/projects/100/merge_requests/1/closes_issues`)
.reply(200, [])
.post(`/projects/100/merge_requests/1/notes`)
.reply(200)

await success(pluginConfig, { env, options, nextRelease, logger: t.context.logger, commits, releases, retryLimit });

t.true(gitlab.isDone());
});

0 comments on commit 0a141c9

Please sign in to comment.