Skip to content

Commit 48d4641

Browse files
authored
feat(land): avoid landing on the wrong default branch (#586)
Fail if the user is trying to land a commit on "master" or "main", and the default branch name doesn't match.
1 parent c071846 commit 48d4641

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

components/git/land.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

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

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

166-
async function main(state, argv, cli, req, dir) {
166+
async function main(state, argv, cli, dir) {
167+
const credentials = await auth({
168+
github: true
169+
});
170+
const req = new Request(credentials);
167171
let session = new LandingSession(cli, req, dir);
168172

169-
if (state !== AMEND && state !== CONTINUE && session.warnForWrongBranch()) {
173+
if (state !== AMEND &&
174+
state !== CONTINUE &&
175+
await session.warnForWrongBranch()) {
170176
return;
171177
}
172178

lib/landing_session.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,22 @@ class LandingSession extends Session {
456456
async status() {
457457
// TODO
458458
}
459+
460+
async warnForWrongBranch() {
461+
if (super.warnForWrongBranch()) {
462+
return true;
463+
}
464+
const rev = this.getCurrentBranch();
465+
const { repository: { defaultBranchRef } } = await this.req.gql(
466+
'DefaultBranchRef',
467+
{ owner: this.owner, repo: this.repo });
468+
if ((rev === 'master' || rev === 'main') && defaultBranchRef.name !== rev) {
469+
this.cli.warn(`You are running git-node-land on \`${rev}\`,` +
470+
` but the default branch is \`${defaultBranchRef.name}\`.`);
471+
this.cli.setExitCode(1);
472+
return true;
473+
}
474+
}
459475
}
460476

461477
module.exports = LandingSession;

lib/queries/DefaultBranchRef.gql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query DefaultBranchRef($owner: String!, $repo: String!) {
2+
repository(owner: $owner, name: $repo) {
3+
defaultBranchRef {
4+
name
5+
}
6+
}
7+
}
8+

0 commit comments

Comments
 (0)