Skip to content

Commit ac2e2dd

Browse files
authored
Merge pull request #59 from mrbazzan/gh-profiler-workflow
Add 'gh-profiler' workflow to the CI
2 parents aebc93d + 65e8003 commit ac2e2dd

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Run gh-profiler against the author of each new issue and PR.
2+
# Delete the profile output when the issue or PR is closed.
3+
4+
name: Profile contributors
5+
6+
on:
7+
issues:
8+
types: [opened, closed]
9+
# This workflow never checks out or executes PR code; it only reads the
10+
# author login to post a comment, so pull_request_target is safe here.
11+
pull_request_target: # zizmor: ignore[dangerous-triggers]
12+
types: [opened, closed]
13+
14+
jobs:
15+
profile-author:
16+
if: github.event.action == 'opened'
17+
18+
permissions:
19+
issues: write
20+
pull-requests: write
21+
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- uses: astral-sh/setup-uv@d0cc045d04ccac9d8b7881df0226f9e82c39688e # v6.8.0
26+
- name: Determine target login and issue number
27+
id: meta
28+
env:
29+
EVENT_NAME: ${{ github.event_name }}
30+
ISSUE_USER_LOGIN: ${{ github.event.issue.user.login }}
31+
ISSUE_NUMBER: ${{ github.event.issue.number }}
32+
PR_USER_LOGIN: ${{ github.event.pull_request.user.login }}
33+
PR_NUMBER: ${{ github.event.pull_request.number }}
34+
run: |
35+
if [ "$EVENT_NAME" = "issues" ]; then
36+
echo "login=$ISSUE_USER_LOGIN" >> "$GITHUB_OUTPUT"
37+
echo "number=$ISSUE_NUMBER" >> "$GITHUB_OUTPUT"
38+
echo "issue_pr=issue" >> "$GITHUB_OUTPUT"
39+
else
40+
echo "login=$PR_USER_LOGIN" >> "$GITHUB_OUTPUT"
41+
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
42+
echo "issue_pr=PR" >> "$GITHUB_OUTPUT"
43+
fi
44+
45+
- name: Run gh-profiler (=== Expand this to see full profile output ===)
46+
env:
47+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
LOGIN: ${{ steps.meta.outputs.login }}
49+
run: |
50+
uvx gh-profiler@latest "$LOGIN" --concise > profiler.txt
51+
echo
52+
echo "======================== Full profile ========================="
53+
echo
54+
uvx gh-profiler@latest "$LOGIN"
55+
echo
56+
echo "==============================================================="
57+
58+
- name: Comment on issue or PR
59+
env:
60+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
61+
SERVER_URL: ${{ github.server_url }}
62+
REPOSITORY: ${{ github.repository }}
63+
RUN_ID: ${{ github.run_id }}
64+
CHECK_RUN_ID: ${{ job.check_run_id }}
65+
NUMBER: ${{ steps.meta.outputs.number }}
66+
ISSUE_PR: ${{ steps.meta.outputs.issue_pr }}
67+
run: |
68+
{
69+
echo "<!-- gh-profiler:profile-link run-id=$RUN_ID -->"
70+
echo "Profile summary:"
71+
echo
72+
echo '```text'
73+
cat profiler.txt
74+
echo '```'
75+
echo
76+
echo "<a href=\"$SERVER_URL/$REPOSITORY/actions/runs/$RUN_ID/job/$CHECK_RUN_ID\">Full profile</a>"
77+
echo
78+
echo "(This comment and the linked profile output will auto-delete when this $ISSUE_PR is closed.)"
79+
} > body.txt
80+
81+
jq -Rs '{body: .}' body.txt > payload.json
82+
83+
gh api \
84+
"repos/$REPOSITORY/issues/$NUMBER/comments" \
85+
--input payload.json
86+
87+
delete-profile:
88+
if: github.event.action == 'closed'
89+
90+
permissions:
91+
actions: write
92+
issues: write
93+
pull-requests: write
94+
95+
runs-on: ubuntu-latest
96+
97+
steps:
98+
- name: Determine issue or PR number
99+
id: meta
100+
env:
101+
EVENT_NAME: ${{ github.event_name }}
102+
ISSUE_NUMBER: ${{ github.event.issue.number }}
103+
PR_NUMBER: ${{ github.event.pull_request.number }}
104+
run: |
105+
if [ "$EVENT_NAME" = "issues" ]; then
106+
echo "number=$ISSUE_NUMBER" >> "$GITHUB_OUTPUT"
107+
else
108+
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
109+
fi
110+
111+
- name: Delete profile comment
112+
env:
113+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
114+
REPOSITORY: ${{ github.repository }}
115+
NUMBER: ${{ steps.meta.outputs.number }}
116+
run: |
117+
gh api --paginate \
118+
"repos/$REPOSITORY/issues/$NUMBER/comments" \
119+
--jq '
120+
.[]
121+
| select(.user.login == "github-actions[bot]")
122+
| select(.body | startswith("<!-- gh-profiler:profile-link run-id="))
123+
| [.id, (.body | capture("gh-profiler:profile-link run-id=(?<id>[0-9]+)").id)]
124+
| @tsv
125+
' |
126+
while IFS=$'\t' read -r comment_id run_id; do
127+
echo "Deleting logs from workflow run $run_id."
128+
gh api --method DELETE \
129+
"repos/$REPOSITORY/actions/runs/$run_id/logs"
130+
131+
echo "Deleting profile comment $comment_id."
132+
gh api --method DELETE \
133+
"repos/$REPOSITORY/issues/comments/$comment_id"
134+
done

0 commit comments

Comments
 (0)