Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize PR instructions based on files modified #6685

Merged
merged 6 commits into from
Apr 28, 2024
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
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({
anthonypz marked this conversation as resolved.
Show resolved Hide resolved
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}
```
```