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

Allow specifying the base branch #30

Merged
merged 5 commits into from
Jan 30, 2021

Conversation

greglockwood
Copy link
Contributor

@greglockwood greglockwood commented Jan 9, 2021

PR Description
👉 #30 Added ability to customise the base branch
greglockwood#3 Draft PR support
greglockwood#4 Cater for body being nullish or empty
greglockwood#5 Putting the table back in 'table of contents'!
greglockwood#6 Added support to print links to PRs in terminal
greglockwood#7 Work when remote URL doesn't have .git suffix
#31 [combined branch]

Which problem does this PR solve?

There are a few scenarios not covered by the current functionality.

  1. Create a chain of PRs off a branch that is not master, meaning the base should be set to the "parent" branch of the first branch in the chain, not master.
  2. GitHub is encouraging everyone to rename their main branch to something other than the slavery-inspired master. New repos default to using main instead.

Main changes

1. Add --base to allow customizing the base branch

The first part was adding a new command line flag and propagating it through the functions to allow specifying a custom base branch to use other than master.

2. Adding a configuration option to specify a default base branch

This is the first PR in a chain because I started a new convention where configuration options about the behavior can be specified in an optional prs section in the .pr-train.yml config file. By chaining the remaining PRs off this one, I can re-use the helper method for getting the config value.

Most of the PRs in the chain add a configuration option to this section, and this one is no different. This one adds a main-base-branch setting to allow customizing the main branch for the repository you are working on, which fixes issue #24 and may help with #18 and #10.

Specifying this main-base-branch option sets the base branch to use when one is not specified explicitly with the --base command-line option mentioned above.

Suggested CR ordering of files?

  1. consts.js and cfg_template.yml, the "easy" ones.
  2. README.md for the addition to the docs.
  3. index.js
  4. github.js

How I tested it works

I have tested the following scenarios:

  1. No main-base-branch setting, no --base specified via command-line. First and combined PRs use master as the base, which matches the current behavior.
  2. Setting main-base-branch: main, no --base specified via command-line. First and combined PRs use main as the base, which is correct.
  3. Setting main-base-branch: main and --base master specified via command-line. First and combined PRs use master as the base, which is correct.
  4. No main-base-branch setting, --base main specified via command-line. First and combined PRs use main as the base, which is correct.

@greglockwood greglockwood changed the title Adding folder for Webstorm project files to .gitignorea Allow specifying the base branch Jan 9, 2021
Copy link
Contributor Author

@greglockwood greglockwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review

Comment on lines +2 to +5
# Intellij IDE project settings
.idea
# Because dogfooding
.pr-train.yml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to remove these changes, or undo them. I just needed to make them to have a clean working directory when rebasing and so on.

cfg_template.yml Outdated
@@ -1,3 +1,7 @@
prs:
# Change this if you use a different name for your default branch (e.g. main)
main-branch-name: master
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be commented out by default and have a different value to the hardcoded default, by way of example?

Comment on lines +79 to +91
function checkAndReportInvalidBaseError(e, base) {
const { field, code } = get(e, 'body.errors[0]', {});
if (field === 'base' && code === 'invalid') {
console.log([
emoji.get('no_entry'),
`\n${emoji.get('confounded')} This is embarrassing. `,
`The base branch of ${base.bold} doesn't seem to exist on the remote.`,
`\nDid you forget to ${emoji.get('arrow_up')} push it?`,
].join(''));
return true;
}
return false;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caters for scenarios where you are trying to specify a base branch that hasn't been pushed to the remote yet. The intention is to try to help out scatter-brained developers if this scenario does arise.

It certainly did for me, so I added this helpful suggestion to shortcut fixing the problem if anyone else encounters it in the future.

I'm happy to reword it if you feel the tone is inconsistent.

@@ -163,11 +186,14 @@ async function ensurePrsExist(sg, allBranches, combinedBranch, remote = DEFAULT_
title,
body,
};
process.stdout.write(`Creating PR for branch "${branch}"...`);
const baseMessage = base === baseBranch ? colors.dim(` (against ${base})`) : '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the base branch in the console output is helpful for visually confirming what base branch is being used since it can now change.

Comment on lines +182 to +212
const sg = simpleGit();
if (!(await sg.checkIsRepo())) {
console.log('Not a git repo'.red);
process.exit(1);
}

// try to create or init the config first so we can read values from it
if (process.argv.includes('--init')) {
if (fs.existsSync(await getConfigPath(sg))) {
console.log('.pr-train.yml already exists');
process.exit(1);
}
const root = path.dirname(require.main.filename);
const cfgTpl = fs.readFileSync(`${root}/cfg_template.yml`);
fs.writeFileSync(await getConfigPath(sg), cfgTpl);
console.log(`Created a ".pr-train.yml" file. Please make sure it's gitignored.`);
process.exit(0);
}

let ymlConfig;
try {
ymlConfig = await loadConfig(sg);
} catch (e) {
if (e instanceof yaml.YAMLException) {
console.log('There seems to be an error in `.pr-train.yml`.');
console.log(e.message);
process.exit(1);
}
console.log('`.pr-train.yml` file not found. Please run `git pr-train --init` to create one.'.red);
process.exit(1);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By moving this block here, I was able to use the values read from the config to inform the command line parameters added or their default values.

@@ -309,6 +337,6 @@ async function main() {
}

main().catch(e => {
console.log(`${emoji.get('x')} An error occured. Was there a conflict perhaps?`.red);
console.log(`${emoji.get('x')} An error occurred. Was there a conflict perhaps?`.red);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I threw in a bonus spelling mistake fix.

Copy link
Owner

@realyze realyze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is truly awesome, thanks @greglockwood! Just two super minor suggestions and we're ready to merge.

cfg_template.yml Outdated Show resolved Hide resolved
consts.js Outdated Show resolved Hide resolved
@realyze
Copy link
Owner

realyze commented Jan 11, 2021

I'll have a look at the other PRs in the next days!

Looking at my code from two years ago, I really wish I:

  1. used typescript
  2. wrote tests

:)

This tool was a bit of a proof of concept to see if I could make things easier (for myself and possibly for others)...so I'm happy to say it mostly worked out OK. :)

Thanks again for the PRs, I really appreciate it.

@realyze realyze merged commit 1333e46 into realyze:master Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants