Skip to content

Commit e435ccd

Browse files
authored
1 parent 2ff5850 commit e435ccd

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/github.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@ import * as semver from 'semver';
33
import * as core from '@actions/core';
44
import * as httpm from '@actions/http-client';
55

6+
const maxRetries = 10;
7+
const timeoutMs = 1000;
8+
const withRetry = async <T>(operation: () => Promise<T>): Promise<T> => {
9+
let lastError: Error;
10+
11+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
12+
try {
13+
return await operation();
14+
} catch (error) {
15+
lastError = error as Error;
16+
17+
if (attempt === maxRetries) {
18+
break;
19+
}
20+
21+
core.debug(`Attempt ${attempt + 1} failed, retrying in ${timeoutMs}: ${lastError.message}`);
22+
await new Promise(resolve => setTimeout(resolve, timeoutMs));
23+
}
24+
}
25+
26+
throw lastError;
27+
};
28+
629
export interface GitHubRelease {
730
tag_name: string;
831
}
@@ -37,14 +60,20 @@ export const getReleaseTag = async (distribution: string, version: string): Prom
3760
const tag: string = (await resolveVersion(distribution, version)) || version;
3861
const suffix: string = goreleaser.distribSuffix(distribution);
3962
const url = `https://goreleaser.com/static/releases${suffix}.json`;
40-
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
41-
const resp: httpm.HttpClientResponse = await http.get(url);
42-
const body = await resp.readBody();
43-
const statusCode = resp.message.statusCode || 500;
44-
if (statusCode >= 400) {
45-
throw new Error(`Failed to get GoReleaser release ${version} from ${url} with status code ${statusCode}: ${body}`);
46-
}
47-
const releases = <Array<GitHubRelease>>JSON.parse(body);
63+
64+
const releases = await withRetry(async () => {
65+
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
66+
const resp: httpm.HttpClientResponse = await http.get(url);
67+
const body = await resp.readBody();
68+
const statusCode = resp.message.statusCode || 500;
69+
if (statusCode >= 400) {
70+
throw new Error(
71+
`Failed to get GoReleaser release ${version} from ${url} with status code ${statusCode}: ${body}`
72+
);
73+
}
74+
return <Array<GitHubRelease>>JSON.parse(body);
75+
});
76+
4877
const res = releases.filter(r => r.tag_name === tag).shift();
4978
if (res) {
5079
return res;
@@ -78,12 +107,13 @@ interface GitHubTag {
78107
}
79108

80109
const getAllTags = async (distribution: string): Promise<Array<string>> => {
81-
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
82110
const suffix: string = goreleaser.distribSuffix(distribution);
83111
const url = `https://goreleaser.com/static/releases${suffix}.json`;
84112
core.debug(`Downloading ${url}`);
85-
const getTags = http.getJson<Array<GitHubTag>>(url);
86-
return getTags.then(response => {
113+
114+
return withRetry(async () => {
115+
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
116+
const response = await http.getJson<Array<GitHubTag>>(url);
87117
if (response.result == null) {
88118
return [];
89119
}

0 commit comments

Comments
 (0)