Skip to content

Commit

Permalink
PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Apr 16, 2023
1 parent 4d63cfb commit 959174c
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 6 deletions.
65 changes: 65 additions & 0 deletions .github/scripts/auto-respond-pr.js
Original file line number Diff line number Diff line change
@@ -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);
});
57 changes: 57 additions & 0 deletions .github/scripts/diff-directories.js
Original file line number Diff line number Diff line change
@@ -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 <directory1> <directory2>');
process.exit(1);
}

const dir1 = process.argv[2];
const dir2 = process.argv[3];

const dir1Files = readFilesRecursively(dir1);
const dir2Files = readFilesRecursively(dir2);

displayDiffs(dir1Files, dir2Files);
54 changes: 54 additions & 0 deletions .github/workflows/auto_respond_pr.yml
Original file line number Diff line number Diff line change
@@ -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 )"
28 changes: 22 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 959174c

Please sign in to comment.