Skip to content

Commit

Permalink
Added support for table format for TOC
Browse files Browse the repository at this point in the history
  • Loading branch information
greglockwood committed Jan 9, 2021
1 parent baf00e3 commit 08da522
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 29 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,32 @@ You can override the base branch to use when creating PRs by passing the `--base
over the main branch specified in the config file.

e.g. `git pr-train -p -c -b feat/my-feature-base`

## Formatting the Table of Contents as an actual table

If you would prefer that the Table of Contents use an actual markdown table format instead,
simply set `prs.toc-format` in the `.pr-train.yml` file to be `table`.

e.g.

```yml
prs:
# other config
toc-format: table

trains:
# existing train config
```

This will yield something like the following:

| | PR | Description |
| --- | --- | ----------- |
| | #1 | Foo |
| 👉 | #17 | Bar |
| | #3 | Baz |
| | #4 | Qux |
| | #5 | Quux |



2 changes: 2 additions & 0 deletions cfg_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ prs:
# until you are ready. This option can be overridden on the command line using the
# -d/--draft or --no-draft options, which set draft mode to true or false, respectively.
draft-by-default: true
# Specify the format for the Table of Contents in each PR. Either `text` (default) or `table`
# toc-format: table

trains:
# This is an example. This PR train would have 4 branches plus 1 "combined" branch to run tests etc on.
Expand Down
1 change: 1 addition & 0 deletions consts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
DEFAULT_REMOTE: 'origin',
DEFAULT_MAIN_BRANCH: 'master',
DEFAULT_FORMAT: 'text',
MERGE_STEP_DELAY_MS: 1000,
MERGE_STEP_DELAY_WAIT_FOR_LOCK: 2500,
}
41 changes: 28 additions & 13 deletions github.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const fs = require('fs');
const get = require('lodash/get');
const colors = require('colors');
const emoji = require('node-emoji');
const simpleGit = require('simple-git/promise');
const table = require('markdown-table');
const width = require('string-width');

/**
*
Expand All @@ -31,19 +32,31 @@ async function constructPrMsg(sg, branch) {
* @param {Object.<string, {title: string, pr: number}>} branchToPrDict
* @param {string} currentBranch
* @param {string} combinedBranch
* @param {'text'|'table'} format
*/
function constructTrainNavigation(branchToPrDict, currentBranch, combinedBranch) {
let contents = '<pr-train-toc>\n\n#### PR chain:\n';
contents = Object.keys(branchToPrDict).reduce((output, branch) => {
const maybeHandRight = branch === currentBranch ? '👉 ' : '';
function constructTrainNavigation(branchToPrDict, currentBranch, combinedBranch, format) {
let contents = '<pr-train-toc>\n\n';
let tableData = [['', 'PR', 'Description']];
Object.keys(branchToPrDict).forEach((branch) => {
const maybeHandRight = branch === currentBranch ? '👉 ' : ' ';
const maybeHandLeft = branch === currentBranch ? ' 👈 **YOU ARE HERE**' : '';
const combinedInfo = branch === combinedBranch ? ' **[combined branch]** ' : ' ';
output += `${maybeHandRight}#${branchToPrDict[branch].pr}${combinedInfo}(${branchToPrDict[
branch
].title.trim()})${maybeHandLeft}`;
return output + '\n';
}, contents);
contents += '\n</pr-train-toc>';
const prTitle = branchToPrDict[branch].title.trim();
const prNumber = `#${branchToPrDict[branch].pr}`;
const prInfo = format === 'text'
? `${combinedInfo}(${prTitle})`
: `${combinedInfo}${prTitle}`.trim();
const parts = [maybeHandRight, prNumber, prInfo, format === 'text' && maybeHandLeft].filter(Boolean);
if (format === 'text') {
contents += parts.join('') + '\n';
} else {
tableData.push(parts);
}
});
if (format === 'table') {
contents += table(tableData, { stringLength: width }) + '\n';
}
contents += '\n</pr-train-toc>'
return contents;
}

Expand Down Expand Up @@ -99,14 +112,16 @@ function checkAndReportInvalidBaseError(e, base) {
* @param {boolean} draft
* @param {string} remote
* @param {string} baseBranch
* @param {'text'|'table'} format
*/
async function ensurePrsExist({
sg,
allBranches,
combinedBranch,
draft,
remote = DEFAULT_REMOTE,
baseBranch = DEFAULT_MAIN_BRANCH
baseBranch = DEFAULT_MAIN_BRANCH,
format= 'text'
}) {
//const allBranches = combinedBranch ? sortedBranches.concat(combinedBranch) : sortedBranches;
const octoClient = octo.client(readGHKey());
Expand Down Expand Up @@ -228,7 +243,7 @@ async function ensurePrsExist({
branch === combinedBranch ?
getCombinedBranchPrMsg() :
await constructPrMsg(sg, branch);
const navigation = constructTrainNavigation(prDict, branch, combinedBranch);
const navigation = constructTrainNavigation(prDict, branch, combinedBranch, format);
const newBody = upsertNavigationInBody(navigation, body);
process.stdout.write(`Updating PR for branch ${branch}...`);
await ghPr.updateAsync({
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const colors = require('colors');
const {
DEFAULT_REMOTE,
DEFAULT_MAIN_BRANCH,
DEFAULT_FORMAT,
MERGE_STEP_DELAY_MS,
MERGE_STEP_DELAY_WAIT_FOR_LOCK,
} = require('./consts');
Expand Down Expand Up @@ -330,6 +331,7 @@ async function main() {
remote: program.remote,
draft,
baseBranch,
format: getConfigOption(ymlConfig, 'prs.toc-format') || DEFAULT_FORMAT,
});
return;
}
Expand Down
80 changes: 65 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
"lodash": "^4.17.15",
"lodash.difference": "^4.5.0",
"lodash.sortby": "^4.7.0",
"markdown-table": "^2.0.0",
"node-emoji": "^1.8.1",
"octonode": "^0.9.3",
"progress": "^2.0.0",
"promptly": "^3.0.3",
"shelljs": "^0.8.3",
"simple-git": "^1.102.0"
"simple-git": "^1.102.0",
"string-width": "^4.2.0"
},
"devDependencies": {
"@types/js-yaml": "^3.11.2"
Expand Down

0 comments on commit 08da522

Please sign in to comment.