Skip to content

Commit

Permalink
feat(land): avoid landing on the wrong default branch (#586)
Browse files Browse the repository at this point in the history
Fail if the user is trying to land a commit on "master" or "main", and
the default branch name doesn't match.
  • Loading branch information
gerkai committed Nov 3, 2021
1 parent 2234f5b commit 4c44814
Showing 3 changed files with 34 additions and 4 deletions.
14 changes: 10 additions & 4 deletions components/git/land.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const auth = require('../../lib/auth');
const { parsePRFromURL } = require('../../lib/links');
const { getMetadata } = require('../metadata');
const CLI = require('../../lib/cli');
@@ -144,10 +145,9 @@ function land(state, argv) {
if (argv.yes) {
cli.setAssumeYes();
}
const req = new Request();
const dir = process.cwd();

return runPromise(main(state, argv, cli, req, dir)).catch((err) => {
return runPromise(main(state, argv, cli, dir)).catch((err) => {
if (cli.spinner.enabled) {
cli.spinner.fail();
}
@@ -163,10 +163,16 @@ module.exports = {
handler
};

async function main(state, argv, cli, req, dir) {
async function main(state, argv, cli, dir) {
const credentials = await auth({
github: true
});
const req = new Request(credentials);
let session = new LandingSession(cli, req, dir);

if (state !== AMEND && state !== CONTINUE && session.warnForWrongBranch()) {
if (state !== AMEND &&
state !== CONTINUE &&
await session.warnForWrongBranch()) {
return;
}

16 changes: 16 additions & 0 deletions lib/landing_session.js
Original file line number Diff line number Diff line change
@@ -456,6 +456,22 @@ class LandingSession extends Session {
async status() {
// TODO
}

async warnForWrongBranch() {
if (super.warnForWrongBranch()) {
return true;
}
const rev = this.getCurrentBranch();
const { repository: { defaultBranchRef } } = await this.req.gql(
'DefaultBranchRef',
{ owner: this.owner, repo: this.repo });
if ((rev === 'master' || rev === 'main') && defaultBranchRef.name !== rev) {
this.cli.warn(`You are running git-node-land on \`${rev}\`,` +
` but the default branch is \`${defaultBranchRef.name}\`.`);
this.cli.setExitCode(1);
return true;
}
}
}

module.exports = LandingSession;
8 changes: 8 additions & 0 deletions lib/queries/DefaultBranchRef.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query DefaultBranchRef($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
defaultBranchRef {
name
}
}
}

0 comments on commit 4c44814

Please sign in to comment.