Skip to content

Commit

Permalink
Add automation output for commits
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Apr 17, 2023
1 parent 4d63cfb commit 8378698
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 7 deletions.
40 changes: 40 additions & 0 deletions .github/scripts/auto-respond-pr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 ev = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
)
const prNumber = ev.pull_request.number

const octokit = new Octokit({
auth: githubToken
})

async function main () {
const diffOutput = fs.readFileSync('diff_output.txt', 'utf-8')

let commentBody = `
Generated file outputs:
${diffOutput}
`

if (commentBody.length > 65536) {
commentBody = '❌ Generated diff output is too large to post as a comment, run locally to see the diff and validate'
}

await octokit.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: commentBody
})
}

main().catch((err) => {
console.error(err)
process.exit(1)
})
123 changes: 123 additions & 0 deletions .github/scripts/diff-directories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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
}

/**
* Removes the version from the file contents to imrpove diff readability
* @param {string} fileContent
* @param {string} filePath
* @returns {string}
*/
function mungeFileContents (fileContent, filePath) {
if (filePath.endsWith('.json')) {
const fileJSON = JSON.parse(fileContent)
delete fileJSON.version
if ('features' in fileJSON) {
for (const key of Object.keys(fileJSON.features)) {
if ('hash' in fileJSON.features[key]) {
delete fileJSON.features[key].hash
}
}
}
return JSON.stringify(fileJSON, null, 4)
}
return fileContent
}

function displayDiffs (dir1Files, dir2Files, isOpen) {
const out = []
for (const [filePath, fileContent] of Object.entries(dir1Files)) {
let diffOut = ''
if (filePath in dir2Files) {
const fileOut = mungeFileContents(fileContent, filePath)
const file2Out = mungeFileContents(dir2Files[filePath], filePath)
if (fileOut === file2Out) {
diffOut = `⚠️ File ${filePath} is identical`
} else {
// Slice of file header from diff output
const fileDiff = diff.createPatch(filePath, fileOut, file2Out).split('\n').slice(2).join('\n')
if (fileDiff) {
fileDiffOut =
diffOut = `
\`\`\`diff\n
${fileDiff}
\`\`\`
`
}
}

delete dir2Files[filePath]
} else {
diffOut = `❌ File ${filePath} only exists in old changeset`
}
out.push(renderDetails(filePath, diffOut, isOpen))
}

for (const filePath of Object.keys(dir2Files)) {
out.push(`❌ File ${filePath} only exists in new changeset`)
}
return out
}

function renderDetails (section, text, isOpen) {
const open = isOpen ? 'open' : ''
return `<details ${open}>
<summary>${section}</summary>
${text}
</details>`
}

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 sections = {
legacy: {},
latest: {}
}
function sortFiles (dirFiles, dirName) {
for (const [filePath, fileContent] of Object.entries(dirFiles)) {
if (filePath.startsWith('v2')) {
sections.latest[dirName] = sections.latest[dirName] || {}
sections.latest[dirName][filePath] = fileContent
} else {
sections.legacy[dirName] = sections.legacy[dirName] || {}
sections.legacy[dirName][filePath] = fileContent
}
}
}
sortFiles(readFilesRecursively(dir1), 'dir1')
sortFiles(readFilesRecursively(dir2), 'dir2')

for (const [section, files] of Object.entries(sections)) {
const isOpen = section === 'latest'
const fileOut = displayDiffs(files.dir1, files.dir2, isOpen).join('\n')
console.log(renderDetails(section, fileOut, isOpen))
}
53 changes: 53 additions & 0 deletions .github/workflows/auto-respond-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Auto Respond to PR

on:
pull_request:
types:
- opened
- synchronize

jobs:
auto_respond:
runs-on: ubuntu-latest

steps:
- name: Checkout main branch
uses: actions/checkout@v2
with:
ref: main
repository: ${{ github.event.pull_request.head.repo.full_name }}
path: main

- name: Checkout PR branch
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
path: pr

- name: Install dependencies
run: |
npm install @octokit/rest
npm install diff
- name: Run build script on main branch
run: |
cd main
npm install
node index.js
cd ..
- name: Run build script on PR branch
run: |
cd pr
npm install
node index.js
cd ..
- name: Create diff of file outputs
run: node pr/.github/scripts/diff-directories.js main/generated pr/generated > diff_output.txt

- name: Run auto response script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node pr/.github/scripts/auto-respond-pr.js
2 changes: 1 addition & 1 deletion features/runtime-checks.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"domain": "facebook.com"
},
{
"domain": "youtube.com"
"disabled_domain": "youtube.com"
},
{
"disabled_domain": "stackoverflow.com"
Expand Down
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 8378698

Please sign in to comment.