Skip to content

Commit 48c913c

Browse files
PR automation PoC (#1113)
* Automation PR comment * Make updating comment * Move to envs * Use files again as truncated * Add beta flag
1 parent 779cb53 commit 48c913c

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
4+
function readFilesRecursively (directory) {
5+
const filenames = fs.readdirSync(directory)
6+
const files = {}
7+
8+
filenames.forEach((filename) => {
9+
const filePath = path.join(directory, filename)
10+
const fileStats = fs.statSync(filePath)
11+
12+
if (fileStats.isDirectory()) {
13+
const nestedFiles = readFilesRecursively(filePath)
14+
for (const [nestedFilePath, nestedFileContent] of Object.entries(nestedFiles)) {
15+
files[path.join(filename, nestedFilePath)] = nestedFileContent
16+
}
17+
} else {
18+
files[filename] = fs.readFileSync(filePath, 'utf-8')
19+
}
20+
})
21+
22+
return files
23+
}
24+
25+
function upperCaseFirstLetter (string) {
26+
return string.charAt(0).toUpperCase() + string.slice(1)
27+
}
28+
29+
function displayDiffs (dir1Files, dir2Files, isOpen) {
30+
const rollupGrouping = {}
31+
/**
32+
* Rolls up multiple files with the same diff into a single entry
33+
* @param {string} fileName
34+
* @param {string} string
35+
* @param {string} [summary]
36+
*/
37+
function add(fileName, string, summary = undefined) {
38+
if (summary === undefined) {
39+
summary = string
40+
}
41+
if (!(summary in rollupGrouping)) {
42+
rollupGrouping[summary] = { files: [] }
43+
}
44+
rollupGrouping[summary].files.push(fileName)
45+
rollupGrouping[summary].string = string
46+
}
47+
for (const [filePath, fileContent] of Object.entries(dir1Files)) {
48+
let diffOut = ''
49+
let compareOut = undefined
50+
if (filePath in dir2Files) {
51+
const fileOut = fileContent
52+
const file2Out = dir2Files[filePath]
53+
delete dir2Files[filePath]
54+
if (fileOut === file2Out) {
55+
continue
56+
} else {
57+
compareOut = filePath.split('/')[0]
58+
diffOut = `File has changed`
59+
}
60+
} else {
61+
diffOut = '❌ File only exists in old changeset'
62+
compareOut = 'Removed Files'
63+
}
64+
add(filePath, diffOut, compareOut)
65+
}
66+
67+
for (const filePath of Object.keys(dir2Files)) {
68+
add(filePath, '❌ File only exists in new changeset', 'New Files')
69+
}
70+
const outString = Object.keys(rollupGrouping).map(key => {
71+
const rollup = rollupGrouping[key]
72+
let outString = ''
73+
let title = key
74+
if (rollup.files.length) {
75+
for (const file of rollup.files) {
76+
outString += `- ${file}\n`
77+
}
78+
}
79+
outString += '\n\n' + rollup.string
80+
return renderDetails(title, outString, isOpen)
81+
}).join('\n')
82+
return outString
83+
}
84+
85+
function renderDetails (section, text, isOpen) {
86+
if (section == 'dist') {
87+
section = 'apple'
88+
}
89+
const open = section !== 'integration' ? 'open' : ''
90+
return `<details ${open}>
91+
<summary>${upperCaseFirstLetter(section)}</summary>
92+
${text}
93+
</details>`
94+
}
95+
96+
if (process.argv.length !== 4) {
97+
console.error('Usage: node diff_directories.js <directory1> <directory2>')
98+
process.exit(1)
99+
}
100+
101+
const dir1 = process.argv[2]
102+
const dir2 = process.argv[3]
103+
104+
const sections = {
105+
}
106+
function sortFiles (dirFiles, dirName) {
107+
for (const [filePath, fileContent] of Object.entries(dirFiles)) {
108+
sections[dirName] = sections[dirName] || {}
109+
sections[dirName][filePath] = fileContent
110+
}
111+
}
112+
113+
const buildDir = '/build'
114+
const sourcesOutput = '/Sources/ContentScopeScripts/'
115+
sortFiles(readFilesRecursively(dir1 + buildDir), 'dir1')
116+
sortFiles(readFilesRecursively(dir2 + buildDir), 'dir2')
117+
sortFiles(readFilesRecursively(dir1 + sourcesOutput), 'dir1')
118+
sortFiles(readFilesRecursively(dir2 + sourcesOutput), 'dir2')
119+
120+
121+
//console.log(Object.keys(files))
122+
const fileOut = displayDiffs(sections.dir1, sections.dir2, true)
123+
console.log(fileOut)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Auto Respond to PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, closed, ready_for_review]
6+
7+
jobs:
8+
auto_respond:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout base branch
13+
uses: actions/checkout@v3
14+
with:
15+
ref: ${{ github.event.pull_request.base.ref }}
16+
repository: ${{ github.event.pull_request.head.repo.full_name }}
17+
path: base
18+
19+
- name: Checkout PR branch
20+
uses: actions/checkout@v3
21+
with:
22+
ref: ${{ github.event.pull_request.head.ref }}
23+
repository: ${{ github.event.pull_request.head.repo.full_name }}
24+
path: pr
25+
fetch-depth: 0
26+
27+
- name: Run build script on base branch
28+
run: |
29+
cd base
30+
npm install
31+
npm run build
32+
cd ..
33+
34+
- name: Run build script on PR branch
35+
run: |
36+
cd pr
37+
git config --global user.email "[email protected]"
38+
git config --global user.name "dax"
39+
git rebase origin/${{ github.event.pull_request.base.ref }}
40+
npm install
41+
npm run build
42+
cd ..
43+
44+
- name: Create diff of file outputs
45+
run: |
46+
node pr/.github/scripts/diff-directories.js base pr > diff.txt
47+
48+
- name: Find Previous Comment
49+
uses: peter-evans/find-comment@v3
50+
id: find_comment
51+
with:
52+
issue-number: ${{ github.event.pull_request.number }}
53+
comment-author: 'github-actions[bot]'
54+
body-includes: 'Generated file diff'
55+
direction: last
56+
57+
- name: Create Comment Body
58+
uses: actions/github-script@v7
59+
id: create_body
60+
with:
61+
github-token: ${{ secrets.GITHUB_TOKEN }}
62+
script: |
63+
const fs = require('fs');
64+
const prNumber = context.issue.number;
65+
const diffOut = fs.readFileSync('diff.txt', 'utf8');
66+
const commentBody = `
67+
### *[Beta]* Generated file diff
68+
*Time updated:* ${new Date().toUTCString()}
69+
70+
${diffOut}
71+
`;
72+
core.setOutput('comment_body', commentBody);
73+
core.setOutput('pr_number', prNumber);
74+
75+
- name: Create, or Update the Comment
76+
uses: peter-evans/create-or-update-comment@v4
77+
with:
78+
issue-number: ${{ github.event.pull_request.number }}
79+
comment-id: ${{ steps.find_comment.outputs.comment-id }}
80+
body: ${{ steps.create_body.outputs.comment_body }}
81+
edit-mode: replace

0 commit comments

Comments
 (0)