Skip to content

Commit

Permalink
[New] Add --remote flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Ranger11 authored and ljharb committed Nov 18, 2021
1 parent a7da80b commit b0e2faa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@
"rules": {
"id-length": ["error", { "min": 1, "max": 64 }],
},

"overrides": [
{
"files": "bin/**",
"rules": {
"no-throw-literal": "off",
},
},
],
}
43 changes: 40 additions & 3 deletions bin/can-merge
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const {
NODE_ENV,
} = process.env;

const ORIGIN = Object('origin');

const defaultRepo = getRepo();
const REPO = defaultRepo && Object(defaultRepo);

const args = Yargs
.usage('Usage: can-merge -p <pr> [-r <repo>]')
.help()
Expand All @@ -32,10 +37,16 @@ const args = Yargs
describe: 'pull request',
type: 'string',
},
remote: {
default: ORIGIN,
demandOption: false,
describe: 'remote name',
type: 'string',
},
repo: {
alias: 'r',
default: getRepo(),
demandOption: !getRepo(),
default: REPO,
demandOption: !REPO,
describe: 'repository',
type: 'string',
},
Expand All @@ -52,11 +63,37 @@ const args = Yargs
type: 'string',
},
})
.check((argv) => {
if (argv.repo !== REPO && argv.remote !== ORIGIN) {
throw chalk.red('`--repo` and `--remote` are mutually exclusive');
}
return true;
})
.middleware((argv) => {
if (argv.repo === REPO && argv.remote) {
// repo is default, remote is provided
return {
...argv,
...argv.remote && { repo: getRepo(argv.remote) },
};
}
return argv;
})
.check((argv) => {
if (!argv.repo) {
if (argv.remote !== ORIGIN) {
throw chalk.red(`Could not find remote git repo with remote name \`${argv.remote}\``);
}
throw chalk.red('Could not infer any git repository from directory.\nAre you sure you are in a git repository? Otherwise, please specify `--repo=`');
}

return true;
})
.parse();

const token = args.token || GITHUB_TOKEN || GH_TOKEN;

runQuery(args.pr, args.repo, args.sha, token).then((response) => {
runQuery(args.pr, String(args.repo), args.sha, token).then((response) => {
if (NODE_ENV === 'DEBUG') {
console.log(JSON.stringify(response, null, 2));
}
Expand Down
4 changes: 2 additions & 2 deletions utils/getRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const { execSync } = require('child_process');
const isGitRepo = require('git-repo-info');

module.exports = function getRepo() {
module.exports = function getRepo(remote = 'origin') {
if (isGitRepo().sha) {
const getRepos = String(execSync('git ls-remote --get-url'));
const getRepos = String(execSync(`git ls-remote --get-url ${remote}`)).trim();
const pushRepoRegex = /(?<=github\.com(?:\/|:))(?:.*)(?=\.git*)/gm;
return getRepos.match(pushRepoRegex)?.[0];
}
Expand Down

0 comments on commit b0e2faa

Please sign in to comment.