From 959174c7e613b5186b6ada3e8378027e2c145419 Mon Sep 17 00:00:00 2001 From: Jonathan Kingston Date: Sun, 16 Apr 2023 23:03:50 +0100 Subject: [PATCH] PoC --- .github/scripts/auto-respond-pr.js | 65 +++++++++++++++++++++++++++ .github/scripts/diff-directories.js | 57 +++++++++++++++++++++++ .github/workflows/auto_respond_pr.yml | 54 ++++++++++++++++++++++ package-lock.json | 28 +++++++++--- package.json | 1 + 5 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 .github/scripts/auto-respond-pr.js create mode 100644 .github/scripts/diff-directories.js create mode 100644 .github/workflows/auto_respond_pr.yml diff --git a/.github/scripts/auto-respond-pr.js b/.github/scripts/auto-respond-pr.js new file mode 100644 index 000000000..0e4513211 --- /dev/null +++ b/.github/scripts/auto-respond-pr.js @@ -0,0 +1,65 @@ +const { Octokit } = require('@octokit/rest'); +const fs = require('fs'); + +const githubToken = process.env.GITHUB_TOKEN; +const repoFullName = process.env.GITHUB_REPOSITORY; +const [owner, repo] = repoFullName.split('/'); +const prNumber = process.env.GITHUB_EVENT_PATH; + +const octokit = new Octokit({ + auth: githubToken, +}); + +async function postComment(pr) { + const diffOutput = fs.readFileSync('diff_output.txt', 'utf-8'); + + const commentBody = ` +Integration script output: + +\`\`\`diff +${diffOutput} +\`\`\` +`; + + if (quotes.length > 0 || diffOutput) { + await octokit.rest.issues.createComment({ + owner, + repo, + issue_number: pr.number, + body: commentBody, + }); + } +} + +async function main() { + /* + const pr = await octokit.rest.pulls.get({ + owner, + repo, + pull_number: prNumber, + });*/ + + const diffOutput = fs.readFileSync('diff_output.txt', 'utf-8'); + + const commentBody = ` +Integration script output: + +\`\`\`diff +${diffOutput} +\`\`\` +`; + await octokit.rest.issues.createComment({ + owner, + repo, + issue_number: prNumber, + body: commentBody, + }); + + const prBody = pr.data.body; + await postComment(pr.data); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/.github/scripts/diff-directories.js b/.github/scripts/diff-directories.js new file mode 100644 index 000000000..845d37a86 --- /dev/null +++ b/.github/scripts/diff-directories.js @@ -0,0 +1,57 @@ +const fs = require('fs'); +const path = require('path'); +const diff = require('diff'); + +function readFilesRecursively(directory) { + const filenames = fs.readdirSync(directory); + const files = {}; + + filenames.forEach((filename) => { + const filePath = path.join(directory, filename); + const fileStats = fs.statSync(filePath); + + if (fileStats.isDirectory()) { + const nestedFiles = readFilesRecursively(filePath); + for (const [nestedFilePath, nestedFileContent] of Object.entries(nestedFiles)) { + files[path.join(filename, nestedFilePath)] = nestedFileContent; + } + } else { + files[filename] = fs.readFileSync(filePath, 'utf-8'); + } + }); + + return files; +} + +function displayDiffs(dir1Files, dir2Files) { + for (const [filePath, fileContent] of Object.entries(dir1Files)) { + if (dir2Files.hasOwnProperty(filePath)) { + const fileDiff = diff.createPatch(filePath, fileContent, dir2Files[filePath]); + + if (fileDiff) { + console.log(fileDiff); + } + + delete dir2Files[filePath]; + } else { + console.log(`File ${filePath} only exists in directory 1`); + } + } + + for (const filePath of Object.keys(dir2Files)) { + console.log(`File ${filePath} only exists in directory 2`); + } +} + +if (process.argv.length !== 4) { + console.error('Usage: node diff_directories.js '); + process.exit(1); +} + +const dir1 = process.argv[2]; +const dir2 = process.argv[3]; + +const dir1Files = readFilesRecursively(dir1); +const dir2Files = readFilesRecursively(dir2); + +displayDiffs(dir1Files, dir2Files); diff --git a/.github/workflows/auto_respond_pr.yml b/.github/workflows/auto_respond_pr.yml new file mode 100644 index 000000000..de2fde354 --- /dev/null +++ b/.github/workflows/auto_respond_pr.yml @@ -0,0 +1,54 @@ +name: Auto Respond to PR + +on: + pull_request: + types: + - opened + - synchronize + +jobs: + auto_respond: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + ref: main + repository: ${{ github.event.pull_request.head.repo.full_name }} + + - name: Install dependencies + run: npm install + + - name: Build main + run: | + node index.js + + - name: Copy main generated output to tmp + run: | + cp -r generated ${{ runner.temp }} + + - name: Checkout repository + uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + + - name: Install dependencies + run: npm install + + - name: Build branch + run: | + node index.js + + - name: Install dependencies + run: | + npm install @octokit/rest + + - name: Run diff script + run: node .github/scripts/diff-directories.js ${{ runner.temp }}/generated/ generated/ > diff_output.txt + + - uses: github-actions-up-and-running/pr-comment@f1f8ab2bf00dce6880a369ce08758a60c61d6c0b + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + message: "$( cat diff_output.txt )" diff --git a/package-lock.json b/package-lock.json index 5847fa34d..7ad6cdf8a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "devDependencies": { "ajv": "^8.6.3", "chai": "^4.3.4", + "diff": "^5.1.0", "eslint": "^7.32.0", "eslint-config-standard": "^16.0.3", "eslint-plugin-import": "^2.24.2", @@ -609,9 +610,9 @@ } }, "node_modules/diff": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", - "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.1.0.tgz", + "integrity": "sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==", "dev": true, "engines": { "node": ">=0.3.1" @@ -2026,6 +2027,15 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, + "node_modules/mocha/node_modules/diff": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", + "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/mocha/node_modules/has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", @@ -3742,9 +3752,9 @@ } }, "diff": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", - "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.1.0.tgz", + "integrity": "sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==", "dev": true }, "doctrine": { @@ -4781,6 +4791,12 @@ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", "dev": true }, + "diff": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", + "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "dev": true + }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", diff --git a/package.json b/package.json index 4b311e070..14c281dc9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "devDependencies": { "ajv": "^8.6.3", "chai": "^4.3.4", + "diff": "^5.1.0", "eslint": "^7.32.0", "eslint-config-standard": "^16.0.3", "eslint-plugin-import": "^2.24.2",