Skip to content

Commit 739e88f

Browse files
refactor(scripts): use match and functional pipelines in Nushell scripts (#1485)
* refactor(ci): dispatch PR comment upsert flows with match Replace the if/else status chain and defensive describe checks with match arms, guard patterns, and record destructuring for comment author and body extraction. Co-authored-by: ryoppippi <[email protected]> * refactor(ensure-native-binary): replace nested if/else with match Split portability, manifest, and version probes into small functions that dispatch via match list patterns and pipeline combinators instead of nested if/else blocks. Co-authored-by: ryoppippi <[email protected]> * refactor(stage-native-package): use match and pipelines Resolve the package dir and binary name through match, dispatch platform finalization with match, and turn the darwin dylib rewrite loop into a where/each pipeline that reports the first failed rewrite. Co-authored-by: ryoppippi <[email protected]> * refactor(verify-native-package): return check issues as values Model binary readiness checks as functions returning an issue string or null, dispatched with match, instead of try/catch around imperative checks. Use path type so directories report 'is not a file' instead of an incidental ls error. Co-authored-by: ryoppippi <[email protected]> * chore(scripts): apply nufmt layout from treefmt-check Match the formatting treefmt expects for ensure/stage helpers, and rewrite the upsert comment filter with intermediate lets so nufmt does not strip parentheses into invalid syntax. Co-authored-by: ryoppippi <[email protected]> * chore(scripts): finish nufmt layout for upsert-pr-comment Wrap the four-arg update_or_recreate_comment signature and long auth-skip print match arms the way nufmt expects. Co-authored-by: ryoppippi <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: ryoppippi <[email protected]>
1 parent 31e084a commit 739e88f

4 files changed

Lines changed: 206 additions & 174 deletions

File tree

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

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,75 @@ def main [] {
55
let pr_number = (required_env PR_NUMBER)
66
let marker = (required_env COMMENT_MARKER)
77
let body = (open --raw (required_env COMMENT_FILE))
8-
let comments = (gh_api_json [
8+
match (find_existing_comment $repository $pr_number $marker) {
9+
null => (create_comment $repository $pr_number $body)
10+
$existing => (update_or_recreate_comment $repository $pr_number $existing.id $body)
11+
}
12+
}
13+
def find_existing_comment [repository: string, pr_number: string, marker: string] {
14+
gh_api_json [
915
--paginate
1016
--slurp
1117
$"repos/($repository)/issues/($pr_number)/comments?per_page=100"
12-
] | flatten)
13-
let existing = (
14-
$comments | where {|comment|
15-
let login = if (($comment.user? | describe) =~ '^record') {
16-
$comment.user.login?
17-
} else {
18-
null
19-
}
20-
let comment_body = if (($comment.body? | describe) =~ '^string') {
21-
$comment.body
22-
} else {
23-
''
24-
}
25-
$login == 'github-actions[bot]' and ($comment_body | str contains $marker)
26-
} | sort-by created_at | reverse | get --optional 0
27-
)
28-
if $existing == null {
29-
create_comment $repository $pr_number $body
30-
} else {
31-
let update = (try_update_comment $repository $existing.id $body)
32-
if $update.status == 'ok' {
33-
return
34-
}
35-
if $update.status == 'missing' {
18+
]
19+
| flatten
20+
| where {|comment|
21+
let login = comment_login $comment
22+
let body = comment_body $comment
23+
$login == 'github-actions[bot]' and ($body | str contains $marker)
24+
}
25+
| sort-by created_at
26+
| reverse
27+
| get --optional 0
28+
}
29+
def comment_login [comment: record] {
30+
match ($comment | get --optional user) {
31+
{login: $login} => $login
32+
_ => null
33+
}
34+
}
35+
def comment_body [comment: record] {
36+
match ($comment | get --optional body) {
37+
$body if ($body | describe) == 'string' => $body
38+
_ => ''
39+
}
40+
}
41+
def update_or_recreate_comment [
42+
repository: string
43+
pr_number: string
44+
comment_id: int
45+
body: string
46+
] {
47+
let update = (try_update_comment $repository $comment_id $body)
48+
match $update.status {
49+
'ok' => null
50+
'missing' => {
3651
print --stderr 'Existing PR comment was missing; creating a new comment instead.'
3752
create_comment $repository $pr_number $body
38-
} else if $update.status == 'auth' {
39-
print --stderr $"Skipping PR comment because GitHub token cannot update comments: ($update.stderr | str trim)"
40-
} else {
41-
error make {
42-
msg: (format_gh_error ['update comment'] $update.result)
43-
}
4453
}
54+
'auth' => (
55+
print --stderr $"Skipping PR comment because GitHub token cannot update comments: ($update.stderr | str trim)"
56+
)
57+
_ => (error make {
58+
msg: (format_gh_error ['update comment'] $update.result)
59+
})
4560
}
4661
}
4762
def required_env [name: string] {
48-
let value = $env | get --optional $name
49-
if $value == null or ($value | is-empty) {
50-
error make {msg: $"($name) is required"}
63+
match ($env | get --optional $name) {
64+
null => (error make {msg: $"($name) is required"})
65+
$value if ($value | is-empty) => (error make {msg: $"($name) is required"})
66+
$value => $value
5167
}
52-
$value
5368
}
5469
def gh_api_json [args: list<string>] {
5570
let result = (gh_api_complete $args)
56-
if $result.exit_code != 0 {
57-
error make {
71+
match $result.exit_code {
72+
0 => ($result.stdout | from json)
73+
_ => (error make {
5874
msg: (format_gh_error $args $result)
59-
}
75+
})
6076
}
61-
$result.stdout | from json
6277
}
6378
def create_comment [repository: string, pr_number: string, body: string] {
6479
let result = (
@@ -68,14 +83,14 @@ def create_comment [repository: string, pr_number: string, body: string] {
6883
$body
6984
)
7085
)
71-
if $result.exit_code != 0 {
72-
if (is_comment_write_auth_failure $result.stderr) {
86+
match $result.exit_code {
87+
0 => null
88+
_ if (is_comment_write_auth_failure $result.stderr) => (
7389
print --stderr $"Skipping PR comment because GitHub token cannot write comments: ($result.stderr | str trim)"
74-
} else {
75-
error make {
76-
msg: (format_gh_error ['create comment'] $result)
77-
}
78-
}
90+
)
91+
_ => (error make {
92+
msg: (format_gh_error ['create comment'] $result)
93+
})
7994
}
8095
}
8196
def try_update_comment [repository: string, comment_id: int, body: string] {
@@ -86,16 +101,13 @@ def try_update_comment [repository: string, comment_id: int, body: string] {
86101
$body
87102
)
88103
)
89-
if $result.exit_code == 0 {
90-
return {status: 'ok', result: $result, stderr: $result.stderr}
91-
}
92-
if $result.stderr =~ 'HTTP 404' {
93-
return {status: 'missing', result: $result, stderr: $result.stderr}
94-
}
95-
if (is_comment_write_auth_failure $result.stderr) {
96-
return {status: 'auth', result: $result, stderr: $result.stderr}
97-
}
98-
{status: 'error', result: $result, stderr: $result.stderr}
104+
let status = (match $result.exit_code {
105+
0 => 'ok'
106+
_ if $result.stderr =~ 'HTTP 404' => 'missing'
107+
_ if (is_comment_write_auth_failure $result.stderr) => 'auth'
108+
_ => 'error'
109+
})
110+
{status: $status, result: $result, stderr: $result.stderr}
99111
}
100112
def gh_api_with_body [method: string, endpoint: string, body: string] {
101113
let payload = mktemp -t ccusage-pr-comment.XXXXXX | str trim

‎apps/ccusage/scripts/ensure-native-binary.nu‎

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ def main [] {
77
)
88
let target_platform = (node_platform)
99
let target_arch = (node_arch)
10-
let binary_name = if $target_platform == 'win32' { 'ccusage.exe' } else { 'ccusage' }
10+
let binary_name = (binary_name $target_platform)
1111
let native_package_root = (matching_native_package_root $repo_root $target_platform $target_arch)
12-
let native_binary = if $native_package_root == null {
13-
null
14-
} else {
15-
$native_package_root | path join bin $binary_name
16-
}
12+
let native_binary = (match $native_package_root {
13+
null => null
14+
$package_root => ($package_root | path join bin $binary_name)
15+
})
1716
let cargo_binary = $repo_root | path join rust target release $binary_name
1817
let version = (expected_version $repo_root)
1918
if (native_package_includes_binary $native_package_root $binary_name) and (has_expected_version $native_binary $version) {
@@ -42,85 +41,79 @@ def node_arch [] {
4241
$other => $other
4342
}
4443
}
44+
def binary_name [platform: string] {
45+
match $platform {
46+
'win32' => 'ccusage.exe'
47+
_ => 'ccusage'
48+
}
49+
}
4550
def matching_native_package_root [repo_root: path, target_platform: string, target_arch: string] {
46-
let candidates = (glob ($repo_root | path join packages 'ccusage-*' package.json) | each {|package_json_path|
47-
let package_json = (open $package_json_path)
48-
let os = $package_json.os?
49-
let cpu = $package_json.cpu?
50-
if (list_contains $os $target_platform) and (list_contains $cpu $target_arch) {
51-
$package_json_path | path dirname
52-
} else {
53-
null
54-
}
55-
} | where {|package_root| $package_root != null })
56-
$candidates | get --optional 0
51+
glob ($repo_root | path join packages 'ccusage-*' package.json)
52+
| where {|package_json_path|
53+
let package_json = (open $package_json_path)
54+
(list_contains $package_json.os? $target_platform) and (list_contains $package_json.cpu? $target_arch)
55+
}
56+
| each {|package_json_path| $package_json_path | path dirname }
57+
| get --optional 0
5758
}
5859
def list_contains [value, needle: string] { (($value | describe) =~ '^list') and ($value | any {|item| $item == $needle }) }
5960
def expected_version [repo_root: path] {
60-
let package_json = open ($repo_root | path join apps ccusage package.json)
61-
if ($package_json.version? | describe) != 'string' {
62-
error make {msg: 'apps/ccusage/package.json version is not configured'}
61+
let version = open ($repo_root | path join apps ccusage package.json) | get --optional version
62+
match ($version | describe) {
63+
'string' => $version
64+
_ => (error make {msg: 'apps/ccusage/package.json version is not configured'})
6365
}
64-
$package_json.version
6566
}
6667
def native_package_includes_binary [package_root, binary_name: string] {
67-
if $package_root == null {
68-
false
69-
} else {
70-
let package_json_path = $package_root | path join package.json
71-
if not ($package_json_path | path exists) {
72-
false
73-
} else {
74-
let package_json = (open $package_json_path)
75-
let files = $package_json.files?
76-
(($files | describe) =~ '^list') and ($files | any {|file| $file == $"bin/($binary_name)" })
77-
}
68+
match $package_root {
69+
null => false
70+
$root => (manifest_lists_binary ($root | path join package.json) $binary_name)
7871
}
7972
}
73+
def manifest_lists_binary [package_json_path: path, binary_name: string] {
74+
($package_json_path | path exists) and (
75+
list_contains (open $package_json_path | get --optional files) $"bin/($binary_name)"
76+
)
77+
}
8078
def is_portable_binary [target_platform: string, binary] {
81-
if $binary == null {
82-
false
83-
} else if $target_platform == 'linux' {
84-
let result = (^ldd $binary | complete)
85-
let output = $"($result.stdout)($result.stderr)"
86-
$output =~ '(?i)not a dynamic executable|statically linked'
87-
} else if $target_platform == 'darwin' {
88-
let result = (^otool -L $binary | complete)
89-
if $result.exit_code != 0 {
90-
false
91-
} else {
92-
let dylibs = (
93-
$result.stdout
94-
| lines
95-
| skip 1
96-
| each {|line| $line | str trim }
97-
| where {|line| ($line | is-not-empty) }
98-
| each {|line| $line | split row --regex '\s+' | get --optional 0 }
99-
| where {|dylib| $dylib != null and ($dylib | is-not-empty) }
100-
)
101-
$dylibs | all {|dylib|
102-
$system_dylib_prefixes | any {|prefix| $dylib | str starts-with $prefix }
103-
}
104-
}
105-
} else {
106-
true
79+
match [$target_platform, $binary] {
80+
[_, null] => false
81+
['linux', _] => (linux_binary_is_static $binary)
82+
['darwin', _] => (darwin_binary_links_only_system_dylibs $binary)
83+
_ => true
84+
}
85+
}
86+
def linux_binary_is_static [binary: path] {
87+
let result = ^ldd $binary | complete
88+
$"($result.stdout)($result.stderr)" =~ '(?i)not a dynamic executable|statically linked'
89+
}
90+
def darwin_binary_links_only_system_dylibs [binary: path] {
91+
let result = ^otool -L $binary | complete
92+
match $result.exit_code {
93+
0 => (
94+
$result.stdout
95+
| lines
96+
| skip 1
97+
| each {|line| $line | str trim }
98+
| where {|line| $line | is-not-empty }
99+
| each {|line| $line | split row --regex '\s+' | first }
100+
| all {|dylib| $system_dylib_prefixes | any {|prefix| $dylib | str starts-with $prefix } }
101+
)
102+
_ => false
107103
}
108104
}
109105
def has_expected_version [binary, version: string] {
110-
if $binary == null or not ($binary | path exists) {
111-
false
112-
} else {
113-
let result = run-external $binary '--version' | complete
114-
if $result.exit_code != 0 {
115-
false
116-
} else {
117-
let actual_version = (
118-
$result.stdout
119-
| str trim
120-
| split row --regex '\s+'
121-
| last
122-
)
123-
$actual_version == $version
124-
}
106+
($binary != null) and ($binary | path exists) and ((reported_version $binary) == $version)
107+
}
108+
def reported_version [binary: path] {
109+
let result = run-external $binary '--version' | complete
110+
match $result.exit_code {
111+
0 => (
112+
$result.stdout
113+
| str trim
114+
| split row --regex '\s+'
115+
| last
116+
)
117+
_ => null
125118
}
126119
}

0 commit comments

Comments
 (0)