diff --git a/.github/workflows/pr-instructions.yml b/.github/workflows/pr-instructions.yml index 04e9fc687d..15a585de64 100644 --- a/.github/workflows/pr-instructions.yml +++ b/.github/workflows/pr-instructions.yml @@ -19,8 +19,9 @@ jobs: with: script: | const script = require('./github-actions/pr-instructions/create-instruction.js') - const instruction = script({g: github, c: context}) - return JSON.stringify({ instruction: instruction, issueNum: context.payload.number }) + return script({g: github, c: context}).then((instruction) => { + return JSON.stringify({ instruction: instruction, issueNum: context.payload.number }) + }) # Create an artifact with the message - name: Create Artifacts diff --git a/github-actions/pr-instructions/create-instruction.js b/github-actions/pr-instructions/create-instruction.js index bf650ae70d..cfb21d0ba9 100644 --- a/github-actions/pr-instructions/create-instruction.js +++ b/github-actions/pr-instructions/create-instruction.js @@ -9,10 +9,16 @@ const fs = require('fs'); * @param {Object} c - context object * @returns {string} string containing commandline instructions, URI encoded since the backtick character causes a problem in * the artifact */ -function main({ g, c }) { +async function main({ g, c }) { github = g; context = c; - return encodeURI(compositeInstruction()); + return encodeURI(await compositeInstruction()); +} + +function formatHeaderInstruction() { + const path = './github-actions/pr-instructions/pr-instructions-header.md' + const headerInstruction = fs.readFileSync(path).toString('utf-8') + return headerInstruction } function formatPullComment(instruction) { @@ -23,7 +29,7 @@ function formatPullComment(instruction) { } function formatContribComment(instruction){ - const path = './github-actions/pr-instructions/pr-instructions-contrib-template.md' + const path = './github-actions/pr-instructions/pr-instructions-contrib-template.md' const text = fs.readFileSync(path).toString('utf-8'); const completedInstructions = text.replace('${previewContribInstructions}', instruction); return completedInstructions; @@ -41,16 +47,48 @@ git pull ${cloneURL} ${nameOfFromBranch}` } function createContribInstruction(){ - const nameOfCollaborator = context.payload.pull_request.head.repo.owner.login; + const nameOfCollaborator = context.payload.pull_request.head.repo.owner.login; const nameOfFromBranch = context.payload.pull_request.head.ref; - const previewContribURL = `https://github.com/${nameOfCollaborator}/website/blob/${nameOfFromBranch}/CONTRIBUTING.md` - return previewContribURL; + const previewContribURL = `https://github.com/${nameOfCollaborator}/website/blob/${nameOfFromBranch}/CONTRIBUTING.md` + return previewContribURL; +} + +async function getModifiedFiles() { + const prNumber = context.payload.pull_request.number; + const repoName = context.payload.pull_request.head.repo.name; + const ownerName = context.payload.pull_request.head.repo.owner.login; + + // Gets the list of files modified in the pull request and destructures the data object into a files variable + const { data: files } = await github.rest.pulls.listFiles({ + owner: ownerName, + repo: repoName, + pull_number: prNumber + }); + // Maps the files array to only include the filename of each file + const modifiedFiles = files.map(file => file.filename); + + return modifiedFiles; } -function compositeInstruction() { - const completedPullInstruction = formatPullComment(createPullInstruction()); - const completedContribInstruction = formatContribComment(createContribInstruction()); - return completedPullInstruction + completedContribInstruction; +async function compositeInstruction() { + const modifiedFiles = await getModifiedFiles(); + const isContributingModified = modifiedFiles.includes('CONTRIBUTING.md'); + const isOnlyContributingModified = isContributingModified && modifiedFiles.length === 1; + + const pullRequestHeader = formatHeaderInstruction(); + let completedPullInstruction = ''; + let completedContribInstruction = ''; + + // Only includes the pull request instructions if multiple files, including CONTRIBUTING.md, are modified + if (!isOnlyContributingModified) { + completedPullInstruction = formatPullComment(createPullInstruction()); + } + // Only include the contributing instructions if the CONTRIBUTING.md file is modified + if (isContributingModified) { + completedContribInstruction = formatContribComment(createContribInstruction()); + } + + return pullRequestHeader + completedPullInstruction + completedContribInstruction; } module.exports = main \ No newline at end of file diff --git a/github-actions/pr-instructions/pr-instructions-contrib-template.md b/github-actions/pr-instructions/pr-instructions-contrib-template.md index 6d27dc68c9..3a23b92d86 100644 --- a/github-actions/pr-instructions/pr-instructions-contrib-template.md +++ b/github-actions/pr-instructions/pr-instructions-contrib-template.md @@ -1,6 +1,6 @@ ------------------- +--- Note that CONTRIBUTING.md cannot previewed locally; rather it should be previewed at this URL: diff --git a/github-actions/pr-instructions/pr-instructions-header.md b/github-actions/pr-instructions/pr-instructions-header.md new file mode 100644 index 0000000000..039398b6eb --- /dev/null +++ b/github-actions/pr-instructions/pr-instructions-header.md @@ -0,0 +1,3 @@ + + +Want to review this pull request? Take a look at [this documentation](https://github.com/hackforla/website/wiki/How-to-Review-Pull-Requests) for a step by step guide! diff --git a/github-actions/pr-instructions/pr-instructions-template.md b/github-actions/pr-instructions/pr-instructions-template.md index e5f7a1fa86..a538bfe289 100644 --- a/github-actions/pr-instructions/pr-instructions-template.md +++ b/github-actions/pr-instructions/pr-instructions-template.md @@ -1,9 +1,9 @@ -Want to review this pull request? Take a look at [this documentation](https://github.com/hackforla/website/wiki/How-to-Review-Pull-Requests) for a step by step guide! +--- From your project repository, check out a new branch and test the changes. ``` ${commandlineInstructions} -``` +``` \ No newline at end of file