diff --git a/.github/workflows/pr-instructions.yml b/.github/workflows/pr-instructions.yml new file mode 100644 index 000000000..43045d995 --- /dev/null +++ b/.github/workflows/pr-instructions.yml @@ -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/ diff --git a/.github/workflows/wr-pr-instructions.yml b/.github/workflows/wr-pr-instructions.yml new file mode 100644 index 000000000..44c34bbdc --- /dev/null +++ b/.github/workflows/wr-pr-instructions.yml @@ -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." \ No newline at end of file diff --git a/github-actions/pr-instructions/create-instruction.js b/github-actions/pr-instructions/create-instruction.js new file mode 100644 index 000000000..d605f5b33 --- /dev/null +++ b/github-actions/pr-instructions/create-instruction.js @@ -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 \ No newline at end of file diff --git a/github-actions/pr-instructions/post-comment.js b/github-actions/pr-instructions/post-comment.js new file mode 100644 index 000000000..56e5e831c --- /dev/null +++ b/github-actions/pr-instructions/post-comment.js @@ -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; diff --git a/github-actions/pr-instructions/pr-instructions-template.md b/github-actions/pr-instructions/pr-instructions-template.md new file mode 100644 index 000000000..e5f7a1fa8 --- /dev/null +++ b/github-actions/pr-instructions/pr-instructions-template.md @@ -0,0 +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} +```