Skip to content

Commit

Permalink
feat(pr-template): show commits below template description (#419)
Browse files Browse the repository at this point in the history
* feat(pr-template): show commits below template description

* support legacy view

Co-authored-by: Ronald Rey <reyronald@gmail.com>
  • Loading branch information
jfcampos1 and reyronald committed Nov 4, 2021
1 parent b5e8d19 commit 9921788
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ new OptionsSync().define({
augmentPrEntry: true,
comparePagePullRequest: true,
prTemplateEnabled: true,
prTemplateCommits: true,
prTemplateUrl: null,
mergeCommitMessageEnabled: false,
mergeCommitMessageUrl: null,
Expand Down
5 changes: 4 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ function init(config) {
pullrequestListRelatedFeatures(config)
} else if (isCreatePullRequestURL()) {
if (config.prTemplateEnabled) {
insertPullrequestTemplate(config.prTemplateUrl)
insertPullrequestTemplate(
config.prTemplateUrl,
config.prTemplateCommits
)
}

if (config.closeAnchorBranch) {
Expand Down
6 changes: 6 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@
</label>
<br />
<br />
<label>
<input type="checkbox" name="prTemplateCommits" />
Add commit list after the PR template when creating a PR
</label>
<br />
<br />

<label>
<input type="checkbox" name="mergeCommitMessageEnabled" /> Use a
Expand Down
21 changes: 16 additions & 5 deletions src/pullrequest-template/pullrequest-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getRepoURL } from '../page-detect'
import { getFirstFileContents, getMainBranch } from '../utils'

export default async function fetchAndInsertPullrequestTemplate(
externalUrl: string
externalUrl: string,
prTemplateCommits: boolean
) {
const pullrequestTemplateUrls = getPullrequestTemplateUrls()

Expand All @@ -16,7 +17,7 @@ export default async function fetchAndInsertPullrequestTemplate(
)

if (template) {
insertPullrequestTemplate(template)
insertPullrequestTemplate(template, prTemplateCommits)
}
}

Expand All @@ -35,7 +36,10 @@ export function getPullrequestTemplateUrls() {
return pullrequestTemplateUrls
}

export async function insertPullrequestTemplate(template: string) {
export async function insertPullrequestTemplate(
template: string,
prTemplateCommits: boolean
) {
const defaultEditor = elementReady('textarea[id="id_description"]')
const atlassianEditor = elementReady(
'#ak_editor_description div[contenteditable="true"]'
Expand All @@ -46,9 +50,16 @@ export async function insertPullrequestTemplate(template: string) {
editorPromises.forEach(p => requestAnimationFrame(() => p.cancel()))

if (editor instanceof HTMLTextAreaElement) {
editor.value = template
if (prTemplateCommits) {
editor.value = template + editor.value
} else {
editor.value = template
}
} else if (editor instanceof HTMLDivElement) {
const html = marked(template)
let html = marked(template)
if (prTemplateCommits) {
html += editor.innerHTML
}
editor.innerHTML = html
} else {
console.warn(
Expand Down
44 changes: 42 additions & 2 deletions src/pullrequest-template/pullrequest-template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,49 @@ test.serial(
)

test.serial(
'should insert pull request template in Atlassian editor',
'should insert pull request template in Atlassian editor with commits',
async t => {
setDocumentBodyAttributes()
const prCommits = true

const templateContents = 'pull request template contents'

const atlassianEditor = (
<ak-editor-bitbucket id="ak_editor_description">
<div contenteditable="true">
<ul class="ak-ul" data-indent-level="1">
<li>
<p>feat: example commit in list</p>
</li>
<li>
<p>feat: second commit example in list</p>
</li>
</ul>
</div>
</ak-editor-bitbucket>
)
const editorCurrentCommits = atlassianEditor.firstChild.innerHTML

delay(100).then(() => document.body.appendChild(atlassianEditor))

await insertPullrequestTemplate(templateContents, prCommits)

let richTemplateContents = marked(templateContents)
if (prCommits) {
richTemplateContents += editorCurrentCommits
}
t.is(atlassianEditor.firstChild.innerHTML, richTemplateContents)

cleanDocumentBody()
await delay(16)
}
)

test.serial(
'should insert pull request template in Atlassian editor without commits',
async t => {
setDocumentBodyAttributes()
const prCommits = false

const templateContents = 'pull request template contents'

Expand All @@ -110,7 +150,7 @@ test.serial(
)
delay(100).then(() => document.body.appendChild(atlassianEditor))

await insertPullrequestTemplate(templateContents)
await insertPullrequestTemplate(templateContents, prCommits)

const richTemplateContents = marked(templateContents)
t.is(atlassianEditor.firstChild.innerHTML, richTemplateContents)
Expand Down

0 comments on commit 9921788

Please sign in to comment.