Skip to content
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

chore: comment diffs automatically #478

Open
wants to merge 137 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
f3a7127
chore: remove all actions and add new action for commenting diffs
atlj Oct 6, 2023
50578b4
chore: dummy change
atlj Oct 6, 2023
4646a40
chore: add diff commenting CI
atlj Oct 6, 2023
446c798
chore: use macos runner
atlj Oct 6, 2023
81f08de
chore: use origin/main
atlj Oct 6, 2023
12a5675
chore: add fetch before checkout
atlj Oct 6, 2023
727a5ff
chore: disable setup again for testing
atlj Oct 6, 2023
e3962a5
chore: comment diff
atlj Oct 6, 2023
59bdf13
chore: fix string
atlj Oct 6, 2023
4aa22eb
chore: remove url from action
atlj Oct 6, 2023
05d5b98
chore: add no example
atlj Oct 6, 2023
1d55b2f
chore: fix names
atlj Oct 6, 2023
d65b373
chore: add continue on error
atlj Oct 6, 2023
ced5523
chore: fix diff parms
atlj Oct 6, 2023
6d3e11f
chore: add || true
atlj Oct 6, 2023
42fab43
chore: print diff
atlj Oct 6, 2023
c780129
chore: customize a file for diff
atlj Oct 6, 2023
48430ea
chore: ls
atlj Oct 6, 2023
ddd736c
chore: add pipe operator
atlj Oct 6, 2023
861faf6
chore: add no ignore file case
atlj Oct 6, 2023
2f28d65
chore: add delimeter
atlj Oct 6, 2023
7142ae2
chore: add debug txt
atlj Oct 6, 2023
0e7c4c0
chore: divide fetch main and checkout main
atlj Oct 6, 2023
5c55180
chore: cat diff
atlj Oct 6, 2023
8cb2442
chore: remove params from git switch
atlj Oct 6, 2023
9233c89
chore: uncomment echo
atlj Oct 6, 2023
758cbef
chore: add delimeter
atlj Oct 6, 2023
ab05b40
chore: add EOF
atlj Oct 6, 2023
5d09f44
chore: use github output
atlj Oct 6, 2023
bbc8d71
chore: use `
atlj Oct 6, 2023
ca5236e
chore: improve formatting
atlj Oct 6, 2023
6427e96
chore: clean unnecessary parts on script
atlj Oct 6, 2023
9bb7c49
chore: formatting
atlj Oct 6, 2023
b8ecb19
chore: change formatting
atlj Oct 6, 2023
708243d
chore: add commit hash
atlj Oct 6, 2023
78eda9b
chore: remove hash step
atlj Oct 6, 2023
43551e4
chore: remove diff hashes
atlj Oct 6, 2023
0c5ba94
chore: remove timestamps
atlj Oct 6, 2023
6bc1ee8
chore: commit
atlj Oct 7, 2023
4fc3364
chore: commit to diffview branch
atlj Oct 7, 2023
af0c05f
chore: use ubuntu latest
atlj Oct 7, 2023
5a37d39
chore: debug ssh address
atlj Oct 7, 2023
fe42250
chore: use context to get vars
atlj Oct 7, 2023
07fd0b3
chore: add git config
atlj Oct 7, 2023
9f2a251
chore: use ssh agent
atlj Oct 7, 2023
f76ce15
chore: set upstream while pushing
atlj Oct 7, 2023
59c8f28
chore: use old version
atlj Oct 7, 2023
66d2e33
chore: dont fail if git didnt change
atlj Oct 7, 2023
bc1416a
chore: fix diff
atlj Oct 7, 2023
b8ecccc
chore: fix upstream branch name
atlj Oct 7, 2023
39b34fa
chore: print the github diff link
atlj Oct 7, 2023
3823909
chore: add =
atlj Oct 7, 2023
16f8161
chore: get commit hashes of remote
atlj Oct 7, 2023
1d9cf97
chore: add missing / :)
atlj Oct 7, 2023
9edccdd
chore: comment on PR
atlj Oct 7, 2023
6a48500
chore: use copy with -r
atlj Oct 7, 2023
cb4f26e
chore: echo url
atlj Oct 7, 2023
b6a572c
chore: add branch prefix
atlj Oct 7, 2023
4c824ca
chore: change pr number method
atlj Oct 7, 2023
28e882f
chore: change branch prefixes
atlj Oct 7, 2023
95171c4
chore: fix syntax
atlj Oct 7, 2023
1921a3b
chore: fix prefix
atlj Oct 7, 2023
f649673
chore: add matrix
atlj Oct 7, 2023
66ec847
chore: init libs correctly
atlj Oct 7, 2023
02baa78
chore: generate folder
atlj Oct 7, 2023
338b468
chore: change the diff
atlj Oct 7, 2023
7cd895c
chore: conditionally push diffs
atlj Oct 7, 2023
ce13683
chore: use script to switch branch
atlj Oct 7, 2023
94d0448
chore: add cd
atlj Oct 7, 2023
3a5bcd4
chore: remove switch script
atlj Oct 7, 2023
7a9d4d0
chore: comment diff links if they are non empty
atlj Oct 7, 2023
0565764
chore: add formatting
atlj Oct 7, 2023
9b16757
chore: add link for formatting
atlj Oct 7, 2023
0844fb1
chore: change formatting
atlj Oct 7, 2023
004221a
chore: change message
atlj Oct 7, 2023
706850d
chore: more descriptive names
atlj Oct 7, 2023
b950b67
chore: use shorter names
atlj Oct 7, 2023
c3f3a18
chore: fix typo
atlj Oct 7, 2023
a81b48f
chore: uncomment templates
atlj Oct 7, 2023
1b2c5b1
chore: use ubuntu
atlj Oct 14, 2023
5142fbc
chore: add continue on errors to artifact fetching
atlj Oct 14, 2023
ef93dd3
chore: add setup again
atlj Oct 14, 2023
f6ad6ad
chore: test config matrix
atlj Oct 14, 2023
53a6f8a
chore: test
atlj Oct 14, 2023
9410da1
chore: try \
atlj Oct 14, 2023
8e654cd
chore: try loop
atlj Oct 14, 2023
822a7b3
chore: create libs
atlj Oct 14, 2023
a6ffcbf
chore: add diff
atlj Oct 14, 2023
268a1df
chore: change something
atlj Oct 14, 2023
1fe2aec
chore: use ssh key
atlj Oct 14, 2023
a8d263c
chore: remove secret
atlj Oct 14, 2023
9709466
chore: add write all permission
atlj Oct 14, 2023
7d822c4
chore: write diffs
atlj Oct 14, 2023
f4659e7
chore: outputs
atlj Oct 14, 2023
6e047d1
chore: testing
atlj Oct 14, 2023
cc7e3c4
chore: force install
atlj Oct 14, 2023
8e8ee89
chore: change remove
atlj Oct 14, 2023
9c5f810
chore: remove everything
atlj Oct 14, 2023
57b1bf9
chore: comment some library types
atlj Oct 14, 2023
385dda1
chore: return if commit fails
atlj Oct 14, 2023
31b5c88
chore: add diff view
atlj Oct 14, 2023
6b2d77c
chore: test
atlj Oct 14, 2023
43d7dc7
chore: add git pull
atlj Oct 14, 2023
beb4b3e
chore: add \
atlj Oct 14, 2023
8769d41
chore: use different install
atlj Oct 14, 2023
2caa685
chore: let everything run
atlj Oct 14, 2023
2f25705
chore: parse
atlj Oct 14, 2023
5a04c9b
chore: fix indenting
atlj Oct 14, 2023
28750f1
chore: format
atlj Oct 14, 2023
6227760
chore: write cwd
atlj Oct 14, 2023
09937ad
chore: tsetingg
atlj Oct 14, 2023
dc46718
chore: fix indenting
atlj Oct 14, 2023
6647039
chore: change stuff
atlj Oct 14, 2023
e2a0ece
chore: fix
atlj Oct 14, 2023
a7f737a
chore: debug
atlj Oct 14, 2023
feb1ecb
chore: quiet
atlj Oct 14, 2023
efcb3c2
chore: use +
atlj Oct 14, 2023
3a059b7
chore: test
atlj Oct 14, 2023
35dbfe9
chore: test
atlj Oct 14, 2023
effaada
chore: join string
atlj Oct 14, 2023
1bb4547
chore: fix
atlj Oct 14, 2023
64fa7c2
chore: change order
atlj Oct 14, 2023
de2b97b
chore: add condition to install again
atlj Oct 14, 2023
f361d84
chore: add leftpad to tset
atlj Oct 14, 2023
c24360d
chore: disable some of the caching
atlj Oct 14, 2023
e01ee62
chore: always install
atlj Oct 14, 2023
a477ad4
chore: run yarn install at second time
atlj Oct 14, 2023
4e0628f
chore: cleanup
atlj Oct 14, 2023
74b4abf
chore: remove some changes
atlj Oct 14, 2023
0ac7337
Revert "chore: add leftpad to tset"
atlj Oct 14, 2023
c7e923a
chore: improvements
atlj Oct 14, 2023
5b1e84e
chore: add temporarely
atlj Oct 14, 2023
d3d14c7
chore: use rn 0.73.0 rc on build templates workflow
atlj Oct 15, 2023
8016ada
Revert "chore: use rn 0.73.0 rc on build templates workflow"
atlj Oct 15, 2023
3d58cf5
chore: fix typo
atlj Oct 15, 2023
0038348
chore: use the old install action
atlj Oct 27, 2023
73a3a35
chore: improve readability
atlj Oct 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
306 changes: 306 additions & 0 deletions .github/workflows/comment-diffs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
name: Comment template diffs
on:
workflow_dispatch:
Copy link
Member

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

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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running prepare already deletes this folder due to --delete-dir-on-start

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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the loop needed? rm -r * .* should do the job as well, right?


- 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.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
Copy link
Member

Choose a reason for hiding this comment

The 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 create-react-native-library, create-react-native-library --list-types and create-react-native-library --list-languages or something that lists all available templates instead hardcoding. then we could use that for matrix in build-templates as well as here. i have forgotten to change the matrix when i change templates in past (worse when something gets added and there's no test failure), and it'll avoid this.


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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't return acc.replace(key, value); work here?

}, `${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
})

30 changes: 30 additions & 0 deletions .github/workflows/configMatrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to scripts/ at root. i'm not a fan of files that are not workflows inside the workflows folder

also use snake-case for bash scripts


# 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
)
Loading