Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

support more PULL_REQUEST_TEMPLATE.md locs #11

Merged
merged 2 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/commands/pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
'use strict';

const open = require('open');
const process = require('process');
const fs = require('fs');
const { join } = require('path');
const path = require('path');

const {
ghURL,
Expand Down Expand Up @@ -76,6 +75,22 @@ function forkPrefix(git) {
});
}

function prTemplateBodySuffix() {
// See: https://docs.github.com/en/free-pro-team@latest/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository
for (const subDir of ['', '.github', 'docs']) {
for (const file of [
'PULL_REQUEST_TEMPLATE.md',
'pull_request_template.md',
]) {
const pathToPrTemplate = path.resolve(subDir, file);
if (fs.existsSync(pathToPrTemplate)) {
return `${fs.readFileSync(pathToPrTemplate)}\n`;
}
}
}
return '';
}

/** @type {import('../typedefs').ActionFn} */
async function prAction({ deps: { git, log }, opts }) {
log('Ensuring all work is pushed to remote');
Expand Down Expand Up @@ -124,18 +139,7 @@ async function prAction({ deps: { git, log }, opts }) {
}

body += '\n';

if (!opts.ignorePrTemplate) {
const pathToPrTemplate = join(
process.cwd(),
'.github',
'PULL_REQUEST_TEMPLATE.md'
);
if (fs.existsSync(pathToPrTemplate)) {
body += `${fs.readFileSync(pathToPrTemplate)}\n\n`;
}
}

if (!opts.ignorePrTemplate) body += prTemplateBodySuffix();
body += `\n\n\n---\n_This PR was started by: ${cmdLine(true)}_`;

const prURL = await ghURL(git, `/compare/${parent}...${remoteBranch}`, {
Expand Down
18 changes: 16 additions & 2 deletions test/pr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const addHooks = require('./test-common');
const verifySetup = require('../lib/setup');
const { action: startAction } = require('../lib/commands/start');
const { action: prAction } = require('../lib/commands/pr');
const { writeFileSync, mkdirSync } = require('fs');

function extractURL(logs) {
return URL.parse(logs.match(/^Opening (https:\S+)/m)[1], true);
Expand Down Expand Up @@ -64,21 +65,34 @@ describe('pr', () => {
});

it('respects the contents of a PULL_REQUEST_TEMPLATE', async () => {
writeFileSync('PULL_REQUEST_TEMPLATE.md', 'Important stuff');
const url = await setupForPR(t, ['feat: use a PR template']);
assert.include(
'contents of a PULL_REQUEST_TEMPLATE.md file',
"Please ensure you adequately describe both the problem you're solving for",
'Important stuff',
url.query.body
);
});

it('respects the contents of a .github/pull_request_template', async () => {
mkdirSync('.github');
writeFileSync('.github/pull_request_template.md', 'Other stuff');
const url = await setupForPR(t, ['feat: use a PR template']);
assert.include(
'contents of a pull_request_template.md file',
'Other stuff',
url.query.body
);
});

it('optionally ignores PULL_REQUEST_TEMPLATE', async () => {
writeFileSync('PULL_REQUEST_TEMPLATE.md', 'Important stuff');
const url = await setupForPR(t, ['feat: use a PR template'], {
ignorePrTemplate: true,
});
assert.notInclude(
'contents of the PULL REQUEST TEMPLATE are not present',
"Please ensure you adequately describe both the problem you're solving for",
'Important stuff',
url.query.body
);
});
Expand Down
4 changes: 4 additions & 0 deletions test/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,23 @@ function addHooks(main = 'master') {
};

let savedUser;
let savedDir;

beforeEach('setup git dir', async () => {
savedUser = process.env.USER;
savedDir = process.cwd();
process.env.USER = FAKE_USER;
[t.ghDir, t.ghGit] = await setupGitHubDir();
[t.localDir, t.git] = await setupLocalDir(t.ghDir, t.ghGit, main);
[t.localDir2, t.git2] = await setupLocalDir2(t.ghDir);
[t.forkDir, t.gitFork] = await setupForkDir(t.ghDir);
t.logged = '';
process.chdir(t.localDir);
});

afterEach(() => {
process.env.USER = savedUser;
process.chdir(savedDir);
delete t.forceBool;
return Promise.all(
[t.ghDir, t.localDir, t.localDir2, t.forkDir].map(dir =>
Expand Down