Skip to content

Commit b685290

Browse files
committed
Enhance PR checks by adding SHA generation for lib/ folder and pubspec.yaml, and implement hash comparison to skip steps if unchanged
1 parent 8cd963d commit b685290

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

.github/workflows/pr-to-stable.yaml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,63 @@ jobs:
2626
git clone --branch ${{ github.head_ref }} --single-branch --depth 1 https://github.com/${{ github.repository }} new_version
2727
rm -rf new_version/.git
2828
29+
- name: Generate SHA of lib/ folder
30+
run: |
31+
(cd new_version/lib && find . -type f -exec sha1sum {} \; | sort | sha1sum | awk '{print $1}') > ../lib_folder.sha
32+
cat ../lib_folder.sha
33+
34+
- name: Generate SHA of pubspec.yaml
35+
run: |
36+
sha1sum new_version/pubspec.yaml | awk '{print $1}' > pubspec_yaml.sha
37+
cat pubspec_yaml.sha
38+
39+
- name: Fetch and verify last apitool-meta comment from PR
40+
id: check_hashes
41+
env:
42+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
run: |
44+
COMMENTS=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments')
45+
echo "$COMMENTS" | jq -r '.[] | select(.author.login=="github-actions[bot]") | select(.body | contains("<!-- apitool-meta")) | .body' | tail -n 1 > last_apitool_meta_comment.txt
46+
cat last_apitool_meta_comment.txt
47+
48+
# Extract hashes from the last comment
49+
LAST_LIB_SHA=$(grep 'lib_sha:' last_apitool_meta_comment.txt | awk -F': ' '{print $2}')
50+
LAST_PUBSPEC_SHA=$(grep 'pubspec_yaml_sha:' last_apitool_meta_comment.txt | awk -F': ' '{print $2}')
51+
52+
# Read current hashes
53+
CUR_LIB_SHA=$(cat lib_folder.sha)
54+
CUR_PUBSPEC_SHA=$(cat pubspec_yaml.sha)
55+
56+
# Compare and print result, set output
57+
if [ "$LAST_LIB_SHA" = "$CUR_LIB_SHA" ] && [ "$LAST_PUBSPEC_SHA" = "$CUR_PUBSPEC_SHA" ]; then
58+
echo "Hashes match: lib/ and pubspec.yaml have not changed since last comment."
59+
echo "skip_steps=true" >> $GITHUB_OUTPUT
60+
else
61+
echo "Hashes do not match: lib/ or pubspec.yaml have changed since last comment."
62+
echo "skip_steps=false" >> $GITHUB_OUTPUT
63+
fi
64+
2965
- name: Set up Dart
66+
if: steps.check_hashes.outputs.skip_steps != 'true'
3067
uses: dart-lang/setup-dart@v1
3168

3269
- name: Run API diff and save report
70+
if: steps.check_hashes.outputs.skip_steps != 'true'
3371
continue-on-error: true
3472
run: |
3573
dart pub global activate dart_apitool && dart-apitool diff --old ./old_version --new ./new_version --ignore-prerelease on --report-format markdown --report-file-path apitool_report.md
3674
3775
- name: Comment API diff report on PR
76+
if: steps.check_hashes.outputs.skip_steps != 'true'
3877
env:
3978
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4079
run: |
41-
gh pr comment ${{ github.event.pull_request.number }} --body "$(cat apitool_report.md)"
80+
LIB_SHA=$(cat lib_folder.sha)
81+
PUBSPEC_SHA=$(cat pubspec_yaml.sha)
82+
echo "$(cat apitool_report.md)
83+
84+
<!-- apitool-meta
85+
lib_sha: $LIB_SHA
86+
pubspec_yaml_sha: $PUBSPEC_SHA
87+
-->" > apitool_report_with_meta.md
88+
gh pr comment ${{ github.event.pull_request.number }} --body "$(cat apitool_report_with_meta.md)"

0 commit comments

Comments
 (0)