Skip to content

Commit

Permalink
Customize PR instructions based on files modified (#6685)
Browse files Browse the repository at this point in the history
* Customize PR message based on files modified

* Update GitHub REST API method in create-instruction.js

* Handle unresolved promises on GHA workflow and script files

* Always include a header instructions and add heavy line breaks between sections

* Fix: convert tabs to spaces
  • Loading branch information
anthonypz committed Apr 28, 2024
1 parent c0e2069 commit 4295425
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/pr-instructions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 48 additions & 10 deletions github-actions/pr-instructions/create-instruction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- Note: Commandline instructions are added into where the placeholder string first appears --->

------------------
---

Note that CONTRIBUTING.md cannot previewed locally; rather it should be previewed at this URL:

Expand Down
3 changes: 3 additions & 0 deletions github-actions/pr-instructions/pr-instructions-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- This header instruction is added into every Pull Request -->

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!
4 changes: 2 additions & 2 deletions github-actions/pr-instructions/pr-instructions-template.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Note: Commandline instructions are added into where the placeholder string first appears --->

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}
```
```

0 comments on commit 4295425

Please sign in to comment.