Skip to content

Commit b10282c

Browse files
authored
[CI] Refactor PR actions workflow (#3955)
1 parent bcd9bcd commit b10282c

File tree

1 file changed

+37
-84
lines changed

1 file changed

+37
-84
lines changed

.github/workflows/pr-actions.yml

+37-84
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,13 @@ env:
1111
USER_NAME: opentelemetrybot
1212

1313
jobs:
14-
fix-format:
15-
name: /fix:format
14+
pr-action:
15+
name: Run PR action
1616
runs-on: ubuntu-latest
1717

1818
if: |
1919
github.event.issue.pull_request &&
20-
contains(github.event.comment.body, '/fix:format')
21-
permissions:
22-
contents: write
23-
pull-requests: write
24-
25-
steps:
26-
- name: Context info
27-
run: |
28-
echo $PR_NUM
29-
echo $COMMENT
30-
31-
- uses: actions/checkout@v4
32-
33-
- name: Write start comment
34-
run: |
35-
gh pr comment $PR_NUM -b "You triggered fix:format action run at $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
36-
env:
37-
GH_TOKEN: ${{ github.token }}
38-
39-
- run: gh pr checkout $PR_NUM -b "pr-action-${RANDOM}"
40-
env:
41-
GH_TOKEN: ${{ github.token }}
42-
43-
- name: Create NPM cache-hash input file
44-
run: |
45-
mkdir -p tmp
46-
jq '{devDependencies, dependencies, engines, gitHubActionCacheKey}' package.json > tmp/package-ci.json
47-
48-
- uses: actions/setup-node@v4
49-
with:
50-
node-version-file: .nvmrc
51-
cache: npm
52-
cache-dependency-path: tmp/package-ci.json
53-
54-
- run: |
55-
npm run format
56-
git status
57-
git branch -v
58-
59-
- name: Commit and push changes, if any
60-
run: |
61-
git config --local user.email "$USER_EMAIL"
62-
git config --local user.name "$USER_NAME"
63-
if [[ $(git status --porcelain) ]]; then
64-
git add -A
65-
current_branch=$(git rev-parse --abbrev-ref HEAD)
66-
echo current_branch=$current_branch
67-
# gh pr checkout sets some git configs that we can use to make sure
68-
# we push to the right repo & to the right branch
69-
remote_repo=$(git config --get branch.${current_branch}.remote)
70-
echo remote_repo=$remote_repo
71-
remote_branch=$(git config --get branch.${current_branch}.merge)
72-
echo remote_branch=$remote_branch
73-
git commit -m 'Results from /fix:format'
74-
git push ${remote_repo} HEAD:${remote_branch}
75-
else
76-
echo "No changes to commit"
77-
fi
78-
env:
79-
GH_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
80-
81-
- name: Report an error in the case of failure
82-
if: ${{ failure() || cancelled() }}
83-
run: |
84-
gh pr comment $PR_NUM -b "fix:format run failed, please check $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for details"
85-
env:
86-
GH_TOKEN: ${{ github.token }}
87-
88-
fix-refcache:
89-
name: /fix:refcache
90-
runs-on: ubuntu-latest
91-
92-
if: |
93-
github.event.issue.pull_request &&
94-
contains(github.event.comment.body, '/fix:refcache')
20+
startsWith(github.event.comment.body, '/fix:')
9521
permissions:
9622
contents: write
9723
pull-requests: write
@@ -100,6 +26,19 @@ jobs:
10026
DEPTH: --depth 100 # submodule clone depth
10127

10228
steps:
29+
- name: Extract action name
30+
id: extract_action_name
31+
run: |
32+
PR_ACTION=$(echo $COMMENT | grep -oP '/fix:\K\w+')
33+
echo "Action is $PR_ACTION"
34+
ACTION_NAMES="format|submodules|refcache|all"
35+
if [[ ! "$PR_ACTION" =~ ^($ACTION_NAMES)$ ]]; then
36+
echo "Invalid action name: $PR_ACTION"
37+
echo "Action name should be one of: $ACTION_NAMES"
38+
exit 1
39+
fi
40+
echo "PR_ACTION=$PR_ACTION" >> "$GITHUB_ENV"
41+
10342
- name: Context info
10443
run: |
10544
echo $PR_NUM
@@ -109,12 +48,10 @@ jobs:
10948

11049
- name: Write start comment
11150
run: |
112-
gh pr comment $PR_NUM -b "You triggered fix:refcache action run at $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
51+
gh pr comment $PR_NUM -b "You triggered fix:${PR_ACTION} action run at $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID"
11352
env:
11453
GH_TOKEN: ${{ github.token }}
11554

116-
# By providing a branch name the checkout will not break if a branch with the
117-
# same name exists already upstream (e.g. patch-X)
11855
- run: gh pr checkout $PR_NUM -b "pr-action-${RANDOM}"
11956
env:
12057
GH_TOKEN: ${{ github.token }}
@@ -130,9 +67,25 @@ jobs:
13067
cache: npm
13168
cache-dependency-path: tmp/package-ci.json
13269

133-
- run: npm install --omit=optional
13470
- run: |
135-
npm run check:links
71+
case $PR_ACTION in
72+
format)
73+
npm run format
74+
;;
75+
submodules)
76+
npm run sync
77+
;;
78+
refcache)
79+
npm install --omit=optional
80+
npm run check:links
81+
;;
82+
all)
83+
npm install --omit=optional
84+
npm run sync
85+
npm run format
86+
npm run check:links
87+
;;
88+
esac
13689
git status
13790
git branch -v
13891
@@ -150,7 +103,7 @@ jobs:
150103
echo remote_repo=$remote_repo
151104
remote_branch=$(git config --get branch.${current_branch}.merge)
152105
echo remote_branch=$remote_branch
153-
git commit -m 'Results from /fix:refcache'
106+
git commit -m "Results from /fix:${PR_ACTION}"
154107
git push ${remote_repo} HEAD:${remote_branch}
155108
else
156109
echo "No changes to commit"
@@ -161,6 +114,6 @@ jobs:
161114
- name: Report an error in the case of failure
162115
if: ${{ failure() || cancelled() }}
163116
run: |
164-
gh pr comment $PR_NUM -b "fix:recache run failed, please check $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for details"
117+
gh pr comment $PR_NUM -b "fix:${PR_ACTION} run failed, please check $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for details"
165118
env:
166119
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)