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
targos committed Nov 3, 2021
1 parent c071846 commit 48d4641
Show file tree
Hide file tree
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');
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions lib/landing_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 48d4641

Please sign in to comment.