Discovery default branch for repository (#75)
This commit is contained in:
parent
3a0787a996
commit
968e14041f
@ -3,6 +3,7 @@
|
||||
The GitHub Actions for pushing to GitHub repository local changes authorizing using GitHub token.
|
||||
|
||||
With ease:
|
||||
|
||||
- update new code placed in the repository, e.g. by running a linter on it,
|
||||
- track changes in script results using Git as archive,
|
||||
- publish page using GitHub-Pages,
|
||||
@ -43,7 +44,7 @@ jobs:
|
||||
| name | value | default | description |
|
||||
| ---- | ----- | ------- | ----------- |
|
||||
| github_token | string | | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}`. |
|
||||
| branch | string | main | Destination branch to push changes. Can be passed in using `${{ github.ref }}`. |
|
||||
| branch | string | (default) | Destination branch to push changes. Can be passed in using `${{ github.ref }}`. |
|
||||
| force | boolean | false | Determines if force push is used. |
|
||||
| tags | boolean | false | Determines if `--tags` is used. |
|
||||
| directory | string | '.' | Directory to change to before pushing. |
|
||||
|
68
start.js
68
start.js
@ -1,22 +1,62 @@
|
||||
'use strict';
|
||||
const spawn = require('child_process').spawn;
|
||||
const path = require("path");
|
||||
const https = require('https');
|
||||
|
||||
const exec = (cmd, args=[]) => new Promise((resolve, reject) => {
|
||||
console.log(`Started: ${cmd} ${args.join(" ")}`)
|
||||
const app = spawn(cmd, args, { stdio: 'inherit' });
|
||||
app.on('close', code => {
|
||||
if(code !== 0){
|
||||
err = new Error(`Invalid status code: ${code}`);
|
||||
err.code = code;
|
||||
return reject(err);
|
||||
};
|
||||
return resolve(code);
|
||||
});
|
||||
app.on('error', reject);
|
||||
});
|
||||
const get = (url, options = {}) => new Promise((resolve, reject) => https
|
||||
.get(url, options, (res) => {
|
||||
const chunks = [];
|
||||
res.on('data', (chunk) => chunks.push(chunk));
|
||||
res.on('end', () => {
|
||||
const body = Buffer.concat(chunks).toString('utf-8');
|
||||
if (res.statusCode < 200 || res.statusCode > 300) {
|
||||
return reject(Object.assign(
|
||||
new Error(`Invalid status code: ${res.statusCode}`),
|
||||
{ res, body }
|
||||
));
|
||||
}
|
||||
return resolve(body)
|
||||
});
|
||||
})
|
||||
.on('error', reject)
|
||||
)
|
||||
|
||||
const exec = (cmd, args = [], options = {}) => new Promise((resolve, reject) =>
|
||||
spawn(cmd, args, { stdio: 'inherit', ...options })
|
||||
.on('close', code => {
|
||||
if (code !== 0) {
|
||||
return reject(Object.assign(
|
||||
new Error(`Invalid exit code: ${code}`),
|
||||
{ code }
|
||||
));
|
||||
};
|
||||
return resolve(code);
|
||||
})
|
||||
.on('error', reject)
|
||||
);
|
||||
|
||||
const trimLeft = (value, charlist = '/') => value.replace(new RegExp(`^[${charlist}]*`), '');
|
||||
const trimRight = (value, charlist = '/') => value.replace(new RegExp(`[${charlist}]*$`), '');
|
||||
const trim = (value, charlist) => trimLeft(trimRight(value, charlist));
|
||||
|
||||
const main = async () => {
|
||||
await exec('bash', [path.join(__dirname, './start.sh')]);
|
||||
let branch = process.env.INPUT_BRANCH;
|
||||
const repository = trim(process.env.INPUT_REPOSITORY);
|
||||
if (!branch) {
|
||||
const headers = {
|
||||
'User-Agent': 'github.com/ad-m/github-push-action'
|
||||
};
|
||||
if (process.env.GITHUB_TOKEN) headers.Authorization = `token ${process.env.GITHUB_TOKEN}`;
|
||||
const body = JSON.parse(await get(`https://api.github.com/repos/${repository}`, { headers }))
|
||||
branch = body.default_branch;
|
||||
}
|
||||
await exec('bash', [path.join(__dirname, './start.sh')], {
|
||||
env: {
|
||||
...process.env,
|
||||
INPUT_BRANCH: branch,
|
||||
INPUT_REPOSITORY: repository,
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
main().catch(err => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user