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

Commit f99290d

Browse files
authored
Merge pull request #11 from groupon/dbushong/feature/master/moar-pr-tmpl
support more PULL_REQUEST_TEMPLATE.md locs
2 parents eae719e + 96c1fd6 commit f99290d

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

lib/commands/pr.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
'use strict';
3434

3535
const open = require('open');
36-
const process = require('process');
3736
const fs = require('fs');
38-
const { join } = require('path');
37+
const path = require('path');
3938

4039
const {
4140
ghURL,
@@ -76,6 +75,22 @@ function forkPrefix(git) {
7675
});
7776
}
7877

78+
function prTemplateBodySuffix() {
79+
// See: https://docs.github.com/en/free-pro-team@latest/github/building-a-strong-community/creating-a-pull-request-template-for-your-repository
80+
for (const subDir of ['', '.github', 'docs']) {
81+
for (const file of [
82+
'PULL_REQUEST_TEMPLATE.md',
83+
'pull_request_template.md',
84+
]) {
85+
const pathToPrTemplate = path.resolve(subDir, file);
86+
if (fs.existsSync(pathToPrTemplate)) {
87+
return `${fs.readFileSync(pathToPrTemplate)}\n`;
88+
}
89+
}
90+
}
91+
return '';
92+
}
93+
7994
/** @type {import('../typedefs').ActionFn} */
8095
async function prAction({ deps: { git, log }, opts }) {
8196
log('Ensuring all work is pushed to remote');
@@ -124,18 +139,7 @@ async function prAction({ deps: { git, log }, opts }) {
124139
}
125140

126141
body += '\n';
127-
128-
if (!opts.ignorePrTemplate) {
129-
const pathToPrTemplate = join(
130-
process.cwd(),
131-
'.github',
132-
'PULL_REQUEST_TEMPLATE.md'
133-
);
134-
if (fs.existsSync(pathToPrTemplate)) {
135-
body += `${fs.readFileSync(pathToPrTemplate)}\n\n`;
136-
}
137-
}
138-
142+
if (!opts.ignorePrTemplate) body += prTemplateBodySuffix();
139143
body += `\n\n\n---\n_This PR was started by: ${cmdLine(true)}_`;
140144

141145
const prURL = await ghURL(git, `/compare/${parent}...${remoteBranch}`, {

test/pr.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const addHooks = require('./test-common');
99
const verifySetup = require('../lib/setup');
1010
const { action: startAction } = require('../lib/commands/start');
1111
const { action: prAction } = require('../lib/commands/pr');
12+
const { writeFileSync, mkdirSync } = require('fs');
1213

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

6667
it('respects the contents of a PULL_REQUEST_TEMPLATE', async () => {
68+
writeFileSync('PULL_REQUEST_TEMPLATE.md', 'Important stuff');
6769
const url = await setupForPR(t, ['feat: use a PR template']);
6870
assert.include(
6971
'contents of a PULL_REQUEST_TEMPLATE.md file',
70-
"Please ensure you adequately describe both the problem you're solving for",
72+
'Important stuff',
73+
url.query.body
74+
);
75+
});
76+
77+
it('respects the contents of a .github/pull_request_template', async () => {
78+
mkdirSync('.github');
79+
writeFileSync('.github/pull_request_template.md', 'Other stuff');
80+
const url = await setupForPR(t, ['feat: use a PR template']);
81+
assert.include(
82+
'contents of a pull_request_template.md file',
83+
'Other stuff',
7184
url.query.body
7285
);
7386
});
7487

7588
it('optionally ignores PULL_REQUEST_TEMPLATE', async () => {
89+
writeFileSync('PULL_REQUEST_TEMPLATE.md', 'Important stuff');
7690
const url = await setupForPR(t, ['feat: use a PR template'], {
7791
ignorePrTemplate: true,
7892
});
7993
assert.notInclude(
8094
'contents of the PULL REQUEST TEMPLATE are not present',
81-
"Please ensure you adequately describe both the problem you're solving for",
95+
'Important stuff',
8296
url.query.body
8397
);
8498
});

test/test-common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,23 @@ function addHooks(main = 'master') {
122122
};
123123

124124
let savedUser;
125+
let savedDir;
125126

126127
beforeEach('setup git dir', async () => {
127128
savedUser = process.env.USER;
129+
savedDir = process.cwd();
128130
process.env.USER = FAKE_USER;
129131
[t.ghDir, t.ghGit] = await setupGitHubDir();
130132
[t.localDir, t.git] = await setupLocalDir(t.ghDir, t.ghGit, main);
131133
[t.localDir2, t.git2] = await setupLocalDir2(t.ghDir);
132134
[t.forkDir, t.gitFork] = await setupForkDir(t.ghDir);
133135
t.logged = '';
136+
process.chdir(t.localDir);
134137
});
135138

136139
afterEach(() => {
137140
process.env.USER = savedUser;
141+
process.chdir(savedDir);
138142
delete t.forceBool;
139143
return Promise.all(
140144
[t.ghDir, t.localDir, t.localDir2, t.forkDir].map(dir =>

0 commit comments

Comments
 (0)