Skip to content

Commit 5c331a0

Browse files
committed
Add retry
1 parent b8c869e commit 5c331a0

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

start.js

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,75 @@ const spawn = require('child_process').spawn;
33
const path = require("path");
44
const https = require('https');
55

6-
const get = (url, options = {}) => new Promise((resolve, reject) => https
7-
.get(url, options, (res) => {
8-
const chunks = [];
9-
res.on('data', (chunk) => chunks.push(chunk));
10-
res.on('end', () => {
11-
const body = Buffer.concat(chunks).toString('utf-8');
12-
if (res.statusCode < 200 || res.statusCode > 300) {
13-
return reject(Object.assign(
14-
new Error(`Invalid status code '${res.statusCode}' for url '${url}'`),
15-
{ res, body }
16-
));
17-
}
18-
return resolve(body)
19-
});
20-
})
21-
.on('error', reject)
22-
)
6+
const get = (url, options = {}) =>
7+
new Promise((resolve, reject) => https
8+
.get(url, options, (res) => {
9+
const chunks = [];
10+
res.on('data', (chunk) => chunks.push(chunk));
11+
res.on('end', () => {
12+
const body = Buffer.concat(chunks).toString('utf-8');
13+
if (res.statusCode < 200 || res.statusCode > 300) {
14+
return reject(Object.assign(
15+
new Error(`Invalid status code '${res.statusCode}' for url '${url}'`),
16+
{ res, body }
17+
));
18+
}
19+
return resolve(body);
20+
});
21+
})
22+
.on('error', reject));
2323

2424
const exec = (cmd, args = [], options = {}) => new Promise((resolve, reject) =>
25-
spawn(cmd, args, { stdio: 'inherit', ...options })
26-
.on('close', code => {
27-
if (code !== 0) {
28-
return reject(Object.assign(
29-
new Error(`Invalid exit code: ${code}`),
30-
{ code }
31-
));
32-
};
33-
return resolve(code);
34-
})
35-
.on('error', reject)
25+
spawn(cmd, args, { stdio: 'inherit', ...options })
26+
.on('close', code => {
27+
if (code !== 0) {
28+
return reject(Object.assign(
29+
new Error(`Invalid exit code: ${code}`),
30+
{ code }
31+
));
32+
};
33+
return resolve(code);
34+
})
35+
.on('error', reject)
3636
);
3737

3838
const trimLeft = (value, charlist = '/') => value.replace(new RegExp(`^[${charlist}]*`), '');
3939
const trimRight = (value, charlist = '/') => value.replace(new RegExp(`[${charlist}]*$`), '');
4040
const trim = (value, charlist) => trimLeft(trimRight(value, charlist));
4141

4242
const main = async () => {
43-
let branch = process.env.INPUT_BRANCH;
44-
const repository = trim(process.env.INPUT_REPOSITORY || process.env.GITHUB_REPOSITORY);
45-
const github_url_protocol = trim(process.env.INPUT_GITHUB_URL).split("//")[0];
46-
const github_url = trim(process.env.INPUT_GITHUB_URL).split("//")[1];
47-
if (!branch) {
48-
const headers = {
49-
'User-Agent': 'github.com/ad-m/github-push-action'
50-
};
51-
if (process.env.INPUT_GITHUB_TOKEN) headers.Authorization = `token ${process.env.INPUT_GITHUB_TOKEN}`;
52-
const body = JSON.parse(await get(`${process.env.GITHUB_API_URL}/repos/${repository}`, { headers }))
53-
branch = body.default_branch;
54-
}
43+
let branch = process.env.INPUT_BRANCH;
44+
const repository = trim(process.env.INPUT_REPOSITORY || process.env.GITHUB_REPOSITORY);
45+
const github_url_protocol = trim(process.env.INPUT_GITHUB_URL).split("//")[0];
46+
const github_url = trim(process.env.INPUT_GITHUB_URL).split("//")[1];
47+
const retry = process.env.INPUT_RETRY || 1;
48+
if (!branch) {
49+
const headers = {
50+
'User-Agent': 'github.com/ad-m/github-push-action'
51+
};
52+
if (process.env.INPUT_GITHUB_TOKEN) headers.Authorization = `token ${process.env.INPUT_GITHUB_TOKEN}`;
53+
const body = JSON.parse(await get(`${process.env.GITHUB_API_URL}/repos/${repository}`, { headers }));
54+
branch = body.default_branch;
55+
}
56+
57+
let attempt = 0;
58+
59+
while (attempt < retry) {
60+
++attempt;
5561
await exec('bash', [path.join(__dirname, './start.sh')], {
56-
env: {
57-
...process.env,
58-
INPUT_BRANCH: branch,
59-
INPUT_REPOSITORY: repository,
60-
INPUT_GITHUB_URL_PROTOCOL: github_url_protocol,
61-
INPUT_GITHUB_URL: github_url,
62-
}
62+
env: {
63+
...process.env,
64+
INPUT_BRANCH: branch,
65+
INPUT_REPOSITORY: repository,
66+
INPUT_GITHUB_URL_PROTOCOL: github_url_protocol,
67+
INPUT_GITHUB_URL: github_url,
68+
}
6369
});
70+
}
6471
};
6572

6673
main().catch(err => {
67-
console.error(err);
68-
console.error(err.stack);
69-
process.exit(err.code || -1);
70-
})
74+
console.error(err);
75+
console.error(err.stack);
76+
process.exit(err.code || -1);
77+
});

0 commit comments

Comments
 (0)