-
Notifications
You must be signed in to change notification settings - Fork 209
chore: comment diffs automatically #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f3a7127
50578b4
4646a40
446c798
81f08de
12a5675
727a5ff
e3962a5
59bdf13
4aa22eb
05d5b98
1d55b2f
d65b373
ced5523
6d3e11f
42fab43
c780129
48430ea
ddd736c
861faf6
2f28d65
7142ae2
0e7c4c0
5c55180
8cb2442
9233c89
758cbef
ab05b40
5d09f44
bbc8d71
ca5236e
6427e96
9bb7c49
b8ecb19
708243d
78eda9b
43551e4
0c5ba94
6bc1ee8
4fc3364
af0c05f
5a37d39
fe42250
07fd0b3
9f2a251
f76ce15
59c8f28
66d2e33
bc1416a
b8ecccc
39b34fa
3823909
16f8161
1d9cf97
9edccdd
6a48500
cb4f26e
b6a572c
4c824ca
28e882f
95171c4
1921a3b
f649673
66ec847
02baa78
338b468
7cd895c
ce13683
94d0448
3a5bcd4
7a9d4d0
0565764
9b16757
0844fb1
004221a
706850d
b950b67
c3f3a18
a81b48f
1b2c5b1
5142fbc
ef93dd3
f6ad6ad
53a6f8a
9410da1
8e654cd
822a7b3
a6ffcbf
268a1df
1fe2aec
a8d263c
9709466
7d822c4
f4659e7
6e047d1
cc7e3c4
8e8ee89
9c5f810
57b1bf9
385dda1
31b5c88
6b2d77c
43d7dc7
beb4b3e
8769d41
2caa685
2f25705
5a04c9b
28750f1
6227760
09937ad
dc46718
6647039
e2a0ece
a7f737a
feb1ecb
efcb3c2
3a059b7
35dbfe9
effaada
1bb4547
64fa7c2
de2b97b
f361d84
c24360d
e01ee62
a477ad4
4e0628f
74b4abf
0ac7337
c7e923a
5b1e84e
d3d14c7
8016ada
3d58cf5
0038348
73a3a35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
name: Comment template diffs | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
branches: | ||
- main | ||
paths: | ||
- '.github/workflows/comment-diffs.yml' | ||
- 'packages/create-react-native-library/**' | ||
|
||
jobs: | ||
generate-diffs-if-needed: | ||
name: Generate diffs | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Copy config matrix to persist it | ||
run: | | ||
mkdir ../configMatrix | ||
cp ./.github/workflows/configMatrix.sh ../configMatrix/ | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: .nvmrc | ||
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should already be handled by the next job |
||
|
||
- name: Setup | ||
uses: ./.github/actions/setup | ||
|
||
- name: Build crnl | ||
run: | | ||
yarn workspace create-react-native-library prepare | ||
|
||
- name: Create old version libraries | ||
run: | | ||
# Source all the configuration values to load $languages, $types, and $exclude | ||
source ../configMatrix/configMatrix.sh | ||
|
||
create_library() { | ||
library_type=$1 | ||
language=$2 | ||
|
||
echo "Generating $library_type/$language" | ||
path_prefix="../test/$library_type/$language" | ||
target_path="$path_prefix/new-version" | ||
|
||
npx create-react-native-library "$target_path" \ | ||
--slug @bob/react-native-test \ | ||
--description test \ | ||
--author-name test \ | ||
--author-email test@test \ | ||
--author-url https://test.test \ | ||
--repo-url https://test.test \ | ||
--type "$library_type" \ | ||
--languages "$language" \ | ||
--no-example \ | ||
--no-local | ||
|
||
# Remove the .git folder of the created library | ||
rm -rf "$target_path/.git" | ||
} | ||
|
||
for library_type in "${libraryTypes[@]}"; do | ||
for language in "${languages[@]}"; do | ||
if [[ ! "${exclude[*]}" =~ ${library_type}/${language} ]]; then | ||
create_library "$library_type" "$language" | ||
fi | ||
done | ||
done | ||
Comment on lines
+65
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe JS script would be better for readability instead of bash |
||
|
||
- name: Setup | ||
uses: ./.github/actions/setup | ||
|
||
- name: Remove old build and build again | ||
run: | | ||
rm -rf ./packages/create-react-native-library/lib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. running |
||
yarn workspace create-react-native-library prepare | ||
|
||
- name: Create library again | ||
run: | | ||
# Source all the configuration values to load $languages, $types, and $exclude | ||
source ../configMatrix/configMatrix.sh | ||
|
||
git fetch origin main | ||
git checkout origin/main | ||
git pull || true | ||
Comment on lines
+86
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this happen BEFORE installing dependencies and building the library? |
||
|
||
create_library() { | ||
library_type=$1 | ||
language=$2 | ||
|
||
echo "Running $library_type/$language" | ||
path_prefix="../test/$library_type/$language" | ||
target_path="$path_prefix/old-version" | ||
|
||
npx create-react-native-library "$target_path" \ | ||
--slug @bob/react-native-test \ | ||
--description test \ | ||
--author-name test \ | ||
--author-email test@test \ | ||
--author-url https://test.test \ | ||
--repo-url https://test.test \ | ||
--type "$library_type" \ | ||
--languages "$language" \ | ||
--no-example \ | ||
--no-local | ||
|
||
# Remove the .git folder of the created library | ||
rm -rf "$target_path/.git" | ||
} | ||
|
||
for library_type in "${libraryTypes[@]}"; do | ||
for language in "${languages[@]}"; do | ||
if [[ ! "${exclude[*]}" =~ ${library_type}/${language} ]]; then | ||
create_library "$library_type" "$language" | ||
fi | ||
done | ||
done | ||
Comment on lines
+90
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code is the same as before right? in that case maybe you can move this whole thing to the script instead of only having matrix to reduce duplication, or 2 scripts? and pass the folder name to the script which seems to be the only difference |
||
|
||
- name: Remove everything in the working directory | ||
run: for i in $(ls) ; do rm -rf "$i"; done; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the loop needed? |
||
|
||
- name: Create new empty repo | ||
run: git init | ||
|
||
- name: Create diffs | ||
id: create-diffs | ||
run: | | ||
# Source all the configuration values to load $languages, $types, and $exclude | ||
source ../configMatrix/configMatrix.sh | ||
|
||
output_path="../outputs" | ||
mkdir -p "$output_path" | ||
|
||
# Set author | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "create-react-native-library diff generator action" | ||
|
||
function copy_commit_diff(){ | ||
library_type=$1 | ||
language=$2 | ||
|
||
output_file="$output_path/$library_type+$language.txt" | ||
|
||
path_prefix="../test/$library_type/$language" | ||
target_path_new_version="$path_prefix/new-version" | ||
target_path_old_version="$path_prefix/old-version" | ||
|
||
# Remove everything except the .git folder | ||
for i in $(ls | grep -v ".git") ; do rm -rf "$i"; done; | ||
|
||
# Copy the old version | ||
cp -r "$target_path_old_version/." . | ||
|
||
# Add all files and commit | ||
git add -A || true | ||
|
||
if git commit -qm "Automatic commit"; then | ||
echo "Commit successful" | ||
else | ||
echo "No changes" | ||
return | ||
fi | ||
|
||
# Remove everything except the .git folder | ||
for i in $(ls | grep -v ".git") ; do rm -rf "$i"; done; | ||
|
||
# Copy the new version | ||
cp -r "$target_path_new_version/." . | ||
|
||
# Add all files and commit | ||
git add -A || true | ||
|
||
if git commit -qm "Automatic commit"; then | ||
echo "Commit successful" | ||
else | ||
echo "No changes" | ||
return | ||
fi | ||
|
||
# Check if there is a diff | ||
if git diff --quiet HEAD~; then | ||
echo "No diff" | ||
else | ||
# Push the branches | ||
# git push --set-upstream origin diffs | ||
|
||
# Get new version remote commit hash | ||
# new_version_commit_hash=$(git rev-parse --short origin/diffs) | ||
|
||
# Get old version remote commit hash | ||
# old_version_commit_hash=$(git rev-parse --short origin/diffs~) | ||
|
||
# Add output to file | ||
# echo "[$library_type/$language](https://github.com/${{github.repository}}/compare/$old_version_commit_hash..$new_version_commit_hash)" >> "$output_file" | ||
|
||
# Write the diff to the output file | ||
git diff HEAD~ >> $output_file | ||
fi | ||
} | ||
|
||
for library_type in "${libraryTypes[@]}"; do | ||
for language in "${languages[@]}"; do | ||
if [[ ! "${exclude[*]}" =~ ${library_type}/${language} ]]; then | ||
copy_commit_diff "$library_type" "$language" | ||
fi | ||
done | ||
done | ||
|
||
if ls $output_path/*.txt; then | ||
echo "DIFF_IS_NON_EMPTY=1" >> $GITHUB_OUTPUT | ||
else | ||
echo "DIFF_IS_NON_EMPTY=0" >> $GITHUB_OUTPUT | ||
fi | ||
|
||
# Remove outputs folder in cwd if it exists | ||
rm -rf ./outputs || true | ||
|
||
# Copy the outputs to cwd | ||
cp -r $output_path ./outputs | ||
|
||
- uses: actions/upload-artifact@v3 | ||
if: ${{ steps.create-diffs.outputs.DIFF_IS_NON_EMPTY == '1' }} | ||
with: | ||
name: outputs | ||
path: outputs/*.txt | ||
|
||
read-artifacts-and-comment: | ||
name: Read the artifacts and comment | ||
runs-on: ubuntu-latest | ||
needs: [generate-diffs-if-needed] | ||
steps: | ||
- name: Load outputs | ||
uses: actions/download-artifact@v3 | ||
continue-on-error: true | ||
with: | ||
name: outputs | ||
path: outputs | ||
|
||
- name: Output artifacts | ||
id: artifacts | ||
run: | | ||
if ls outputs/*.txt; then | ||
result=1 # Artifacts are not empty | ||
else | ||
result=0 # Artifacts are empty | ||
fi | ||
|
||
echo "ARTIFACTS_ARE_NON_EMPTY=$result" >> $GITHUB_OUTPUT | ||
|
||
- name: Comment on PR | ||
if: ${{ steps.artifacts.outputs.ARTIFACTS_ARE_NON_EMPTY == '1' }} | ||
uses: actions/github-script@v3 | ||
with: | ||
github-token: ${{secrets.GITHUB_TOKEN}} | ||
script: | | ||
const fs = require('fs'); | ||
|
||
const replaceTable = { | ||
"module-legacy": "Native module", | ||
"module-mixed": "Turbo module with backward compat", | ||
"module-new": "Turbo module", | ||
"view-legacy": "Native view", | ||
"view-mixed": "Fabric view with backward compat", | ||
"view-new": "Fabric view", | ||
"java-objc": "Java and Objective C", | ||
"java-swift": "Java and Swift", | ||
"kotlin-objc": "Kotlin and Objective C", | ||
"kotlin-swift": "Kotlin and Swift", | ||
".txt": "" | ||
} | ||
Comment on lines
+261
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having the matrix in JSON would be nice, since this table could also be colocated. though maybe we should add a command to |
||
|
||
const parsedArtifacts = fs.readdirSync('./outputs').map(fileName => { | ||
const [type, language] = fileName.split("+"); | ||
|
||
const title = Object.entries(replaceTable).reduce((acc, [key, value]) => { | ||
return acc.replace(new RegExp(key, "g"), value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't |
||
}, `${language} ${type}`); | ||
|
||
const fileContents = fs.readFileSync(`./outputs/${fileName}`, 'utf8'); | ||
|
||
return `<details><summary>${title}</summary>\n\n\`\`\`diff\n${fileContents}\n\`\`\`\n</details>\n\n`; | ||
}) | ||
|
||
const body = `🤓☝️ This PR changes the output of \`create-react-native-library\`. You can find the diffs of affected templates below: | ||
|
||
${parsedArtifacts.join("")} | ||
`; | ||
|
||
const comments = await github.issues.listComments({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
if (comments.data.some(comment => comment.body === body)) { | ||
return; | ||
} | ||
github.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's move this to also use |
||
|
||
# Source this file to set up the environment for the tests. | ||
|
||
export libraryTypes=( | ||
module-legacy | ||
module-mixed | ||
module-new | ||
view-legacy | ||
view-mixed | ||
view-new | ||
) | ||
|
||
export languages=( | ||
java-objc | ||
java-swift | ||
kotlin-objc | ||
kotlin-swift | ||
) | ||
|
||
export exclude=( | ||
module-new/java-swift | ||
module-new/kotlin-swift | ||
module-mixed/java-swift | ||
module-mixed/kotlin-swift | ||
view-new/java-swift | ||
view-new/kotlin-swift | ||
view-mixed/java-swift | ||
view-mixed/kotlin-swift | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can remove this - it doesn't make sense for commenting diff