Skip to content

Commit bbe813b

Browse files
committed
ci: upsert performance comments with gh api
Replace the Bun-based upsert-pr-comment helper with a Nushell script that uses gh api through a Nix shebang. The perf jobs can now update PR comments without invoking Bun for the GitHub API wrapper. The script preserves the existing update-or-create behavior and keeps 401/403 comment write failures non-fatal for restricted tokens.
1 parent c4545e1 commit bbe813b

3 files changed

Lines changed: 91 additions & 125 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env nix
2+
#! nix shell --inputs-from ../.. nixpkgs#nushell nixpkgs#gh --command nu
3+
4+
def main [] {
5+
let repository = (required_env GITHUB_REPOSITORY)
6+
let pr_number = (required_env PR_NUMBER)
7+
let marker = (required_env COMMENT_MARKER)
8+
let body = (open --raw (required_env COMMENT_FILE))
9+
let comments = (gh_api_json [$"repos/($repository)/issues/($pr_number)/comments?per_page=100"])
10+
let existing = (
11+
$comments
12+
| where {|comment|
13+
let login = if (($comment.user? | describe) =~ '^record') {
14+
$comment.user.login?
15+
} else {
16+
null
17+
}
18+
let comment_body = if (($comment.body? | describe) =~ '^string') {
19+
$comment.body
20+
} else {
21+
''
22+
}
23+
$login == 'github-actions[bot]' and ($comment_body | str contains $marker)
24+
}
25+
| get --optional 0
26+
)
27+
28+
if $existing == null {
29+
create_comment $repository $pr_number $body
30+
} else {
31+
let updated = (try_update_comment $repository $existing.id $body)
32+
if not $updated {
33+
print --stderr 'Failed to update existing PR comment; creating a new comment instead.'
34+
create_comment $repository $pr_number $body
35+
}
36+
}
37+
}
38+
39+
def required_env [name: string] {
40+
let value = ($env | get --optional $name)
41+
if $value == null or ($value | is-empty) {
42+
error make { msg: $"($name) is required" }
43+
}
44+
$value
45+
}
46+
47+
def gh_api_json [args: list<string>] {
48+
let result = (gh_api_complete $args)
49+
if $result.exit_code != 0 {
50+
error make { msg: (format_gh_error $args $result) }
51+
}
52+
$result.stdout | from json
53+
}
54+
55+
def create_comment [repository: string, pr_number: string, body: string] {
56+
let result = (gh_api_with_body 'POST' $"repos/($repository)/issues/($pr_number)/comments" $body)
57+
if $result.exit_code != 0 {
58+
if (is_comment_write_auth_failure $result.stderr) {
59+
print --stderr $"Skipping PR comment because GitHub token cannot write comments: ($result.stderr | str trim)"
60+
} else {
61+
error make { msg: (format_gh_error ['create comment'] $result) }
62+
}
63+
}
64+
}
65+
66+
def try_update_comment [repository: string, comment_id: int, body: string] {
67+
let result = (gh_api_with_body 'PATCH' $"repos/($repository)/issues/comments/($comment_id)" $body)
68+
$result.exit_code == 0
69+
}
70+
71+
def gh_api_with_body [method: string, endpoint: string, body: string] {
72+
let payload = (mktemp -t ccusage-pr-comment.XXXXXX | str trim)
73+
{ body: $body } | to json | save --force $payload
74+
let args = [--method $method --header 'Content-Type: application/json' $endpoint --input $payload]
75+
let result = (gh_api_complete $args)
76+
rm --force $payload
77+
$result
78+
}
79+
80+
def gh_api_complete [args: list<string>] {
81+
run-external gh api ...$args | complete
82+
}
83+
84+
def is_comment_write_auth_failure [stderr: string] {
85+
($stderr =~ 'HTTP 401') or ($stderr =~ 'HTTP 403')
86+
}
87+
88+
def format_gh_error [args: list<string>, result: record] {
89+
$"gh api ($args | str join ' ') failed with exit code ($result.exit_code): ($result.stderr | str trim)"
90+
}

‎.github/scripts/upsert-pr-comment.ts‎

Lines changed: 0 additions & 124 deletions
This file was deleted.

‎.github/workflows/ci.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ jobs:
397397
GITHUB_TOKEN: ${{ github.token }}
398398
GITHUB_REPOSITORY: ${{ github.repository }}
399399
PR_NUMBER: ${{ github.event.pull_request.number }}
400-
run: pnpm exec bun .github/scripts/upsert-pr-comment.ts
400+
run: .github/scripts/upsert-pr-comment.nu
401401

402402
action-timeline:
403403
needs:

0 commit comments

Comments
 (0)