Skip to content

Commit 0ec25bf

Browse files
committed
step 1
1 parent aedf327 commit 0ec25bf

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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
9+
10+
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+
outputs:
32+
new_commit_sha:
33+
description: "The new commit sha if any"
34+
value: ${{ jobs.run-style-bot.outputs.new_commit_sha }}
35+
pr_number:
36+
description: "The PR number"
37+
value: ${{ jobs.run-style-bot.outputs.pr_number }}
38+
39+
jobs:
40+
check-permissions:
41+
if: >
42+
(github.event_name == 'issue_comment' &&
43+
contains(github.event.comment.body, '@bot /style') &&
44+
github.event.issue.pull_request != null)
45+
runs-on: ubuntu-latest
46+
outputs:
47+
is_authorized: ${{ steps.check_user_permission.outputs.has_permission }}
48+
steps:
49+
- name: Check user permission
50+
id: check_user_permission
51+
uses: actions/github-script@v6
52+
with:
53+
script: |
54+
55+
comment_user = context.payload.comment.user.login;
56+
const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
username: comment_user
60+
});
61+
62+
const authorized = ['admin', 'maintain', 'push', 'write'].includes(permission.permission);
63+
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)`);
64+
core.setOutput('has_permission', authorized);
65+
66+
run-style-bot:
67+
needs: check-permissions
68+
if: needs.check-permissions.outputs.is_authorized == 'true'
69+
runs-on: ubuntu-latest
70+
outputs:
71+
pr_number: ${{ steps.pr_info.outputs.prNumber }}
72+
new_commit_sha: ${{ steps.commit_and_push.outputs.new_commit_sha }}
73+
steps:
74+
- name: Extract PR details
75+
id: pr_info
76+
uses: actions/github-script@v6
77+
with:
78+
script: |
79+
const prNumber = context.payload.issue.number;
80+
console.log(`PR number from env: "${prNumber}"`);
81+
82+
const { data: pr } = await github.rest.pulls.get({
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
pull_number: prNumber
86+
});
87+
88+
// Set outputs for use in subsequent steps
89+
core.setOutput('headRepoFullName', pr.head.repo.full_name);
90+
core.setOutput('headRef', pr.head.ref);
91+
core.setOutput('baseRef', pr.base.ref);
92+
core.setOutput('prNumber', prNumber);
93+
94+
console.log('PR Details:', {
95+
number: prNumber,
96+
headRepo: pr.head.repo.full_name,
97+
headRef: pr.head.ref,
98+
baseRef: pr.base.ref
99+
});
100+
101+
- name: Check out PR branch
102+
uses: actions/checkout@v3
103+
env:
104+
HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }}
105+
HEADREF: ${{ steps.pr_info.outputs.headRef }}
106+
with:
107+
# Instead of checking out the base repo, use the contributor's repo name
108+
repository: ${{ env.HEADREPOFULLNAME }}
109+
ref: ${{ env.HEADREF }}
110+
# You may need fetch-depth: 0 for being able to push
111+
fetch-depth: 0
112+
token: ${{ secrets.bot_token }}
113+
114+
- name: Check commit timestamps
115+
env:
116+
COMMENT_DATE: ${{ github.event.comment.created_at }}
117+
PR_NUMBER: ${{ steps.pr_info.outputs.prNumber }}
118+
BASE_REPO_URL: https://github.com/${{ github.repository }}
119+
HEADREF: ${{ steps.pr_info.outputs.headRef }}
120+
run: |
121+
echo "--- Checking remotes ---"
122+
git remote -v
123+
echo "--- Checking ref on origin (${{ steps.pr_info.outputs.headRepoFullName }}) ---"
124+
git ls-remote origin refs/pull/$PR_NUMBER/merge || echo "Ref not found on origin (fork)."
125+
echo "--- Checking ref on base (${{ github.repository }}) ---"
126+
git ls-remote $BASE_REPO_URL refs/pull/$PR_NUMBER/merge || echo "Ref not found on base repository."
127+
128+
echo "--- Proceeding with fetch from base repository ---"
129+
git fetch $BASE_REPO_URL refs/pull/$PR_NUMBER/merge:refs/remotes/pull/$PR_NUMBER/merge
130+
git checkout refs/remotes/pull/$PR_NUMBER/merge
131+
echo "PR_MERGE_SHA: $(git log -1 --format=%H)"
132+
PR_MERGE_COMMIT_TIMESTAMP=$(git log -1 --date=unix --format=%cd)
133+
echo "PR_MERGE_COMMIT_TIMESTAMP: $PR_MERGE_COMMIT_TIMESTAMP"
134+
COMMENT_TIMESTAMP=$(date -d "${COMMENT_DATE}" +"%s")
135+
echo "COMMENT_DATE: $COMMENT_DATE"
136+
echo "COMMENT_TIMESTAMP: $COMMENT_TIMESTAMP"
137+
if [ $COMMENT_TIMESTAMP -le $PR_MERGE_COMMIT_TIMESTAMP ]; then
138+
echo "❌ Last commit on the pull request is newer than the issue comment triggering this run! Abort!";
139+
exit -1;
140+
fi
141+
142+
echo "--- Checking out contributor branch ($HEADREF) ---"
143+
git checkout $HEADREF
144+
145+
- name: Set up Python
146+
uses: actions/setup-python@v4
147+
with:
148+
python-version: ${{ inputs.python_version }}
149+
150+
- name: Install dependencies
151+
env:
152+
python_quality_dependencies: ${{ inputs.python_quality_dependencies }}
153+
run: |
154+
python -m pip install --upgrade pip
155+
pip install .$python_quality_dependencies
156+
157+
- name: Run style command
158+
id: run_style
159+
run: |
160+
case "${{ inputs.style_command_type }}" in
161+
"default")
162+
echo "Running default style and quality checks"
163+
make style && make quality
164+
;;
165+
"quality_only")
166+
echo "Running quality checks only"
167+
make quality
168+
;;
169+
"style_only")
170+
echo "Running style checks only"
171+
make style
172+
;;
173+
*)
174+
echo "Invalid style_command_type: ${{ inputs.style_command_type }}"
175+
echo "Valid options are: 'default', 'quality_only', 'style_only'"
176+
exit 1
177+
;;
178+
esac
179+
180+
- name: Commit and push changes
181+
id: commit_and_push
182+
env:
183+
HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }}
184+
HEADREF: ${{ steps.pr_info.outputs.headRef }}
185+
PRNUMBER: ${{ steps.pr_info.outputs.prNumber }}
186+
GITHUB_TOKEN: ${{ secrets.bot_token }}
187+
run: |
188+
echo "HEADREPOFULLNAME: $HEADREPOFULLNAME, HEADREF: $HEADREF"
189+
# Configure git with the Actions bot user
190+
git config user.name "github-actions[bot]"
191+
git config user.email "github-actions[bot]@users.noreply.github.com"
192+
193+
# Make sure your 'origin' remote is set to the contributor's fork
194+
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/$HEADREPOFULLNAME.git"
195+
196+
# If there are changes after running style/quality, commit them
197+
if [ -n "$(git status --porcelain)" ]; then
198+
git add .
199+
git commit -m "Apply style fixes"
200+
echo "new_commit_sha=$(git log -n 1 --format=format:%H)" >> $GITHUB_OUTPUT
201+
# Push to the original contributor's forked branch
202+
git push origin HEAD:$HEADREF
203+
echo "changes_pushed=true" >> $GITHUB_OUTPUT
204+
else
205+
echo "No changes to commit."
206+
echo "changes_pushed=false" >> $GITHUB_OUTPUT
207+
fi
208+
209+
- name: Comment on PR with workflow run link
210+
if: steps.commit_and_push.outputs.changes_pushed == 'true'
211+
uses: actions/github-script@v6
212+
with:
213+
script: |
214+
const prNumber = parseInt(process.env.prNumber, 10);
215+
const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`
216+
217+
await github.rest.issues.createComment({
218+
owner: context.repo.owner,
219+
repo: context.repo.repo,
220+
issue_number: prNumber,
221+
body: `Style fixes have been applied. [View the workflow run here](${runUrl}).`
222+
});
223+
env:
224+
prNumber: ${{ steps.pr_info.outputs.prNumber }}

0 commit comments

Comments
 (0)