Skip to content

Commit

Permalink
Github Actions: Adding helpful comment with commands for PR review (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPereira authored Mar 9, 2023
1 parent 2820a29 commit 762445f
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/pr-instructions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Add Pull Request Instructions
on:
pull_request:
types: [opened]
branches:
- 'development'

jobs:
Add-Pull-Request-Instructions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# Create the message to post
- name: Create Instruction
uses: actions/github-script@v4
id: instruction
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 })
# Create an artifact with the message
- name: Create Artifacts
run: |
mkdir -p addingPrInstructions/artifact
echo ${{ steps.instruction.outputs.result }} > addingPrInstructions/artifact/artifact.txt
- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: adding-pr-instructions-artifact
path: addingPrInstructions/artifact/
63 changes: 63 additions & 0 deletions .github/workflows/wr-pr-instructions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: WR Add Pull Request Instructions
on:
workflow_run:
workflows: ["Add Pull Request Instructions"]
types: [completed]

jobs:
Last-Workflow-Success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Download artifact
uses: actions/github-script@v4
with:
script: |
// Retrieve metadata about the artifacts of the last workflow
// https://octokit.github.io/rest.js/v18#actions-list-workflow-run-artifacts
const artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const artifactData = artifacts.data.artifacts[0]
// Download artifact with GET API
// https://octokit.github.io/rest.js/v18#actions-download-artifact
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifactData.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/artifact.zip', Buffer.from(download.data));
- run: unzip artifact.zip

- uses: actions/github-script@v4
id: artifact
with:
script: |
// Retrieve pull request and issue number from downloaded artifact
const fs = require('fs')
const artifact = fs.readFileSync('artifact.txt')
const artifactJSON = JSON.parse(artifact);
return artifactJSON
- uses: actions/checkout@v2
# Create the message to post
- name: Post Comment
uses: actions/github-script@v4
with:
script: |
const artifact = ${{ steps.artifact.outputs.result }};
const script = require('./github-actions/pr-instructions/post-comment.js')
script({g: github, c: context}, artifact)
Last-Workflow-Failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: Failed Run
run: echo "The previous GitHub Action failed. Please check the logs for the previous action."
30 changes: 30 additions & 0 deletions github-actions/pr-instructions/create-instruction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Global variables
var github;
var context;

/**
* Uses information from the pull request to create commandline instructions.
* @param {Object} g - github object
* @param {Object} c - context object
* @returns {string} string containing commandline instructions
*/
function main({ g, c }) {
github = g;
context = c;
return createInstruction();
}

function createInstruction() {
const nameOfCollaborator = context.payload.pull_request.head.repo.owner.login;
const nameOfFromBranch = context.payload.pull_request.head.ref;
const nameOfIntoBranch = context.payload.pull_request.base.ref;
const cloneURL = context.payload.pull_request.head.repo.clone_url;

const instructionString =
`git checkout -b ${nameOfCollaborator}-${nameOfFromBranch} ${nameOfIntoBranch}
git pull ${cloneURL} ${nameOfFromBranch}`

return instructionString
}

module.exports = main
49 changes: 49 additions & 0 deletions github-actions/pr-instructions/post-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Import modules
var fs = require("fs");

// Global variables
var github;
var context;

/**
* Formats the commandline instructions into a template, then posts it to the pull request.
* @param {Object} g - github object
* @param {Object} c - context object
* @param {Number} issueNum - the number of the issue where the post will be made
* @param {String} instruction - commandline instructions
*/
async function main({ g, c }, { issueNum, instruction }) {
github = g;
context = c;

const instructions = formatComment(instruction);
postComment(issueNum, instructions);
}

function formatComment(instruction) {
const path = "./github-actions/pr-instructions/pr-instructions-template.md";
const text = fs.readFileSync(path).toString("utf-8");
const completedInstuctions = text.replace(
"${commandlineInstructions}",
instruction
);
return completedInstuctions;
}

async function postComment(issueNum, instructions) {
console.log("Posting comment to PR...");
console.log("context.repo.owner", context.repo.owner);
console.log("context.repo.repo", context.repo.repo);
try {
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNum,
body: instructions,
});
} catch (err) {
throw new Error(err);
}
}

module.exports = main;
9 changes: 9 additions & 0 deletions github-actions/pr-instructions/pr-instructions-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +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 762445f

Please sign in to comment.