Skip to content

Commit 60f96bc

Browse files
authored
Refactor PR Style Bot to use reusable action
Updated the PR Style Bot workflow to use a reusable style bot action from the huggingface_hub repository.
1 parent e618073 commit 60f96bc

File tree

1 file changed

+13
-238
lines changed

1 file changed

+13
-238
lines changed
Lines changed: 13 additions & 238 deletions
Original file line numberDiff line numberDiff line change
@@ -1,242 +1,17 @@
1-
# This workflow was originally implemented by the diffusers team and ported into huggingface_hub
2-
# as a reusable workflow. Related PRs:
3-
# - https://github.com/huggingface/diffusers/pull/10274
4-
# - https://github.com/huggingface/diffusers/pull/10931
5-
# - https://github.com/huggingface/diffusers/pull/10908
6-
7-
8-
name: Style Bot Action
1+
name: PR Style Bot
92

103
on:
11-
workflow_call:
12-
inputs:
13-
style_command_type:
14-
required: false
15-
type: string
16-
description: "Which style command to run (options: 'default' (make style && make quality), 'quality_only', 'style_only')"
17-
default: "default"
18-
python_quality_dependencies:
19-
required: true
20-
type: string
21-
description: "Python package extras to install for quality checks (e.g. '[quality]')"
22-
python_version:
23-
required: false
24-
type: string
25-
description: "Python version to run code formatter"
26-
default: "3.10"
27-
secrets:
28-
bot_token:
29-
required: true
30-
description: "GitHub token with permissions to comment and push to PR"
31-
32-
jobs:
33-
check-permissions:
34-
if: >
35-
(github.event_name == 'issue_comment' &&
36-
contains(github.event.comment.body, '@bot /style') &&
37-
github.event.issue.pull_request != null)
38-
runs-on: ubuntu-latest
39-
outputs:
40-
is_authorized: ${{ steps.check_user_permission.outputs.has_permission }}
41-
steps:
42-
- name: Check user permission
43-
id: check_user_permission
44-
uses: actions/github-script@v6
45-
with:
46-
script: |
47-
48-
comment_user = context.payload.comment.user.login;
49-
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
50-
owner: context.repo.owner,
51-
repo: context.repo.repo,
52-
username: comment_user
53-
});
54-
55-
const authorized = ['admin', 'maintain', 'push', 'write'].includes(permission.permission);
56-
console.log(`User ${comment_user} has permission level: ${permission.permission}, authorized: ${authorized} (only users with at least write access are allowed to run this action)`);
57-
core.setOutput('has_permission', authorized);
58-
59-
run-style-bot:
60-
needs: check-permissions
61-
if: needs.check-permissions.outputs.is_authorized == 'true'
62-
runs-on: ubuntu-latest
63-
steps:
64-
- name: Extract PR details
65-
id: pr_info
66-
uses: actions/github-script@v6
67-
with:
68-
script: |
69-
const prNumber = context.payload.issue.number;
70-
console.log(`PR number from env: "${prNumber}"`);
71-
72-
const { data: pr } = await github.rest.pulls.get({
73-
owner: context.repo.owner,
74-
repo: context.repo.repo,
75-
pull_number: prNumber
76-
});
77-
78-
// Set outputs for use in subsequent steps
79-
core.setOutput('headRepoFullName', pr.head.repo.full_name);
80-
core.setOutput('headRef', pr.head.ref);
81-
core.setOutput('baseRef', pr.base.ref);
82-
core.setOutput('prNumber', prNumber);
83-
84-
console.log('PR Details:', {
85-
number: prNumber,
86-
headRepo: pr.head.repo.full_name,
87-
headRef: pr.head.ref,
88-
baseRef: pr.base.ref
89-
});
90-
91-
- name: Check out PR branch
92-
uses: actions/checkout@v3
93-
env:
94-
HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }}
95-
HEADREF: ${{ steps.pr_info.outputs.headRef }}
96-
with:
97-
# Instead of checking out the base repo, use the contributor's repo name
98-
repository: ${{ env.HEADREPOFULLNAME }}
99-
ref: ${{ env.HEADREF }}
100-
# You may need fetch-depth: 0 for being able to push
101-
fetch-depth: 0
102-
token: ${{ secrets.bot_token }}
103-
104-
- name: Check commit timestamps
105-
env:
106-
COMMENT_DATE: ${{ github.event.comment.created_at }}
107-
PR_NUMBER: ${{ steps.pr_info.outputs.prNumber }}
108-
BASE_REPO_URL: https://github.com/${{ github.repository }}
109-
HEADREF: ${{ steps.pr_info.outputs.headRef }}
110-
run: |
111-
echo "--- Checking remotes ---"
112-
git remote -v
113-
echo "--- Checking ref on origin (${{ steps.pr_info.outputs.headRepoFullName }}) ---"
114-
git ls-remote origin refs/pull/$PR_NUMBER/merge || echo "Ref not found on origin (fork)."
115-
echo "--- Checking ref on base (${{ github.repository }}) ---"
116-
git ls-remote $BASE_REPO_URL refs/pull/$PR_NUMBER/merge || echo "Ref not found on base repository."
117-
118-
echo "--- Proceeding with fetch from base repository ---"
119-
git fetch $BASE_REPO_URL refs/pull/$PR_NUMBER/merge:refs/remotes/pull/$PR_NUMBER/merge
120-
git checkout refs/remotes/pull/$PR_NUMBER/merge
121-
echo "PR_MERGE_SHA: $(git log -1 --format=%H)"
122-
PR_MERGE_COMMIT_TIMESTAMP=$(git log -1 --date=unix --format=%cd)
123-
echo "PR_MERGE_COMMIT_TIMESTAMP: $PR_MERGE_COMMIT_TIMESTAMP"
124-
COMMENT_TIMESTAMP=$(date -d "${COMMENT_DATE}" +"%s")
125-
echo "COMMENT_DATE: $COMMENT_DATE"
126-
echo "COMMENT_TIMESTAMP: $COMMENT_TIMESTAMP"
127-
if [ $COMMENT_TIMESTAMP -le $PR_MERGE_COMMIT_TIMESTAMP ]; then
128-
echo "❌ Last commit on the pull request is newer than the issue comment triggering this run! Abort!";
129-
exit -1;
130-
fi
131-
132-
echo "--- Checking out contributor branch ($HEADREF) ---"
133-
git checkout $HEADREF
134-
135-
- name: Comment on PR with workflow run link
136-
id: init_comment
137-
uses: actions/github-script@v6
138-
with:
139-
script: |
140-
const prNumber = parseInt(process.env.prNumber, 10);
141-
const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
142-
143-
const { data: botComment } = await github.rest.issues.createComment({
144-
owner: context.repo.owner,
145-
repo: context.repo.repo,
146-
issue_number: prNumber,
147-
body: `Style fix is beginning .... [View the workflow run here](${runUrl}).`
148-
});
149-
core.setOutput('comment_id', botComment.id);
150-
env:
151-
prNumber: ${{ steps.pr_info.outputs.prNumber }}
4+
issue_comment:
5+
types: [created]
1526

153-
- name: Set up Python
154-
uses: actions/setup-python@v4
155-
with:
156-
python-version: ${{ inputs.python_version }}
7+
permissions:
8+
contents: write
9+
pull-requests: write
15710

158-
- name: Install dependencies
159-
env:
160-
python_quality_dependencies: ${{ inputs.python_quality_dependencies }}
161-
run: |
162-
python -m pip install --upgrade pip
163-
pip install .$python_quality_dependencies
164-
echo ${{ steps.init_comment.outputs.comment_id }}
165-
166-
- name: Run style command
167-
id: run_style
168-
env:
169-
STYLECOMMANDTYPE : ${{ inputs.style_command_type }}
170-
run: |
171-
case "$STYLECOMMANDTYPE" in
172-
"default")
173-
echo "Running default style and quality checks"
174-
make style && make quality
175-
;;
176-
"quality_only")
177-
echo "Running quality checks only"
178-
make quality
179-
;;
180-
"style_only")
181-
echo "Running style checks only"
182-
make style
183-
;;
184-
*)
185-
echo "Invalid style_command_type: $STYLECOMMANDTYPE"
186-
echo "Valid options are: 'default', 'quality_only', 'style_only'"
187-
exit 1
188-
;;
189-
esac
190-
191-
- name: Commit and push changes
192-
id: commit_and_push
193-
env:
194-
HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }}
195-
HEADREF: ${{ steps.pr_info.outputs.headRef }}
196-
PRNUMBER: ${{ steps.pr_info.outputs.prNumber }}
197-
GITHUB_TOKEN: ${{ secrets.bot_token }}
198-
run: |
199-
echo "HEADREPOFULLNAME: $HEADREPOFULLNAME, HEADREF: $HEADREF"
200-
# Configure git with the Actions bot user
201-
git config user.name "github-actions[bot]"
202-
git config user.email "github-actions[bot]@users.noreply.github.com"
203-
204-
# Make sure your 'origin' remote is set to the contributor's fork
205-
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/$HEADREPOFULLNAME.git"
206-
207-
# If there are changes after running style/quality, commit them
208-
if [ -n "$(git status --porcelain)" ]; then
209-
git add .
210-
git commit -m "Apply style fixes"
211-
# Push to the original contributor's forked branch
212-
git push origin HEAD:$HEADREF
213-
echo "changes_pushed=true" >> $GITHUB_OUTPUT
214-
else
215-
echo "No changes to commit."
216-
echo "changes_pushed=false" >> $GITHUB_OUTPUT
217-
fi
218-
219-
- name: Prepare final comment message
220-
id: prepare_final_comment
221-
run: |
222-
if [ "${{ steps.commit_and_push.outputs.changes_pushed }}" = 'true' ]; then
223-
echo "final_comment=Style bot fixed some files and pushed the changes." >> $GITHUB_OUTPUT
224-
else
225-
echo "final_comment=Style fix runs successfully without any file modified." >> $GITHUB_OUTPUT
226-
fi
227-
228-
- name: Comment on PR
229-
uses: actions/github-script@v6
230-
with:
231-
script: |
232-
const prNumber = parseInt(process.env.prNumber, 10);
233-
const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
234-
235-
await github.rest.issues.updateComment({
236-
owner: context.repo.owner,
237-
repo: context.repo.repo,
238-
comment_id: ${{ steps.init_comment.outputs.comment_id }},
239-
body: `${{ steps.prepare_final_comment.outputs.final_comment }}`
240-
});
241-
env:
242-
prNumber: ${{ steps.pr_info.outputs.prNumber }}
11+
jobs:
12+
style:
13+
uses: huggingface/huggingface_hub/.github/workflows/style-bot-action.yml@main
14+
with:
15+
python_quality_dependencies: "[quality]"
16+
secrets:
17+
bot_token: ${{ secrets.HF_STYLE_BOT_ACTION }}

0 commit comments

Comments
 (0)