Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discovery default branch for repository #75

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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. |
Expand Down
68 changes: 54 additions & 14 deletions start.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down