Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jreij committed Feb 19, 2025
1 parent 4c1003f commit 3fcd290
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
88 changes: 88 additions & 0 deletions .github/workflows/check_dependency_changes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Check dependency changes

on:
workflow_call:

jobs:
check_dependency_changes:
runs-on: ubuntu-latest
name: Check dependency changes

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup gradle
uses: gradle/actions/setup-gradle@v4

- uses: actions/setup-python@v5
with:
python-version: '3.13'

- name: Compare dependencies between base and head
id: compare_deps
env:
BASE_REF: ${{ github.base_ref }}
HEAD_REF: ${{ github.head_ref }}
run: |
chmod +x scripts/check_dependency_changes.py
python scripts/check_dependency_changes.py dependency_changes.md
if [ -s dependency_changes.md ]; then
echo "Found dependency changes in this PR"
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "No dependency changes found in this PR"
echo "has_changes=false" >> $GITHUB_OUTPUT
fi
- name: Add comment on PR
uses: thollander/actions-comment-pull-request@v3
if: steps.compare_deps.outputs.has_changes == 'true'
with:
file-path: "${{ github.workspace }}/dependency_changes.md"
comment-tag: dependency_changes
mode: recreate

- name: Delete comment from PR
uses: thollander/actions-comment-pull-request@v3
if: steps.compare_deps.outputs.has_changes == 'false'
with:
comment-tag: dependency_changes
mode: delete

- name: Find first modified file
id: find_modified_file
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: steps.compare_deps.outputs.has_changes == 'true'
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
REPO=${{ github.repository }}
# Fetch the list of modified files in the PR
FILE_PATH=$(gh api repos/$REPO/pulls/$PR_NUMBER/files --jq '.[0].filename')
if [[ -z "$FILE_PATH" ]]; then
echo "No files found in the PR. Exiting."
exit 1
fi
echo "First modified file: $FILE_PATH"
echo "FILE_PATH=$FILE_PATH" >> $GITHUB_ENV
- name: Add Inline Review Comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: steps.compare_deps.outputs.has_changes == 'true'
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
FILE_PATH=${{ env.FILE_PATH }}
LINE_NUMBER=1 # Post comment on the first line
gh api repos/${{ github.repository }}/pulls/$PR_NUMBER/comments \
-X POST \
-F body='Some dependencies have been modified in this PR, review the modifications in the comments before merging.' \
-F commit_id=${{ github.event.pull_request.head.sha }} \
-F path="$FILE_PATH" \
-F line="$LINE_NUMBER"
3 changes: 3 additions & 0 deletions .github/workflows/check_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ jobs:
verify_dependencies:
name: Validate dependencies
uses: ./.github/workflows/validate_dependencies.yml
check_dependency_changes:
name: Check dependency changes
uses: ./.github/workflows/check_dependency_changes.yml
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ appcompat = "1.7.0"
autofill = "1.3.0-beta01"
browser = "1.8.0"
coroutines = "1.9.0"
fragment = "1.8.5"
fragment = "1.8.4"
lifecycle = "2.8.7"
material = "1.12.0"
recyclerview = "1.4.0"
Expand Down
60 changes: 60 additions & 0 deletions scripts/check_dependency_changes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

import os
import subprocess
import sys
import difflib

# You can test this script locally with this command:
# BASE_REF="BASE_REFERENCE" HEAD_REF="HEAD_REFERENCE" python ./scripts/check_dependency_changes.py test_output.txt

def compare_dependency_list(base_ref: str, head_ref: str):
base_dependency_list = get_dependency_list(base_ref)
head_dependency_list = get_dependency_list(head_ref)

dependency_diff = diff_strings(base_dependency_list, head_dependency_list)
output = generate_github_comment(dependency_diff)

with open(sys.argv[1], 'w+') as f:
f.write(output)
if os.getenv('GITHUB_STEP_SUMMARY'): os.environ[os.getenv('GITHUB_STEP_SUMMARY')] = output

def get_dependency_list(git_ref: str) -> str:
subprocess.Popen(["git", "checkout", "-f", git_ref])
return subprocess.check_output(["./gradlew", "dependencyList", "-q", "--no-configuration-cache"]
).decode(sys.stdout.encoding
).strip()

def diff_strings(first: str, second: str) -> str:
diff = difflib.unified_diff(first.splitlines(keepends=True), second.splitlines(keepends=True))
return ''.join(diff)

def generate_github_comment(dependency_diff: str) -> str:
if not dependency_diff:
return ""

return '''\
The following dependencies have been modified in this PR:
```diff
{diff}
```
To check the affected modules run the `dependencyList` gradle task with `includeModules=true`.
'''.format(diff=dependency_diff)

def main():
red = '\033[31m'

base_ref = os.getenv('BASE_REF')
if base_ref is None:
print(red + 'BASE_REF is not provided. Please provide it in env list. Exiting...')
sys.exit(1)

head_ref = os.getenv('HEAD_REF')
if head_ref is None:
print(red + 'HEAD_REF is not provided. Please provide it in env list. Exiting...')
sys.exit(1)

compare_dependency_list(base_ref, head_ref)

if __name__ == '__main__':
main()

0 comments on commit 3fcd290

Please sign in to comment.