forked from duckduckgo/privacy-configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d63cfb
commit 8378698
Showing
6 changed files
with
240 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters