Repository navigation
Expand file tree
/
Copy pathgithub.nu
More file actions
executable file
·147 lines (131 loc) · 3.96 KB
/
Copy pathgithub.nu
File metadata and controls
executable file
·147 lines (131 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env nu
# ==================================================================================================
# GitHub Administration
# ==================================================================================================
const SETTINGS_DIR = path self "../.github/settings"
const ACTIONS_APP_ID = 15368
# Bind a required check to the app in its trailing @APP-ID, or to GitHub Actions without one.
def required-check [check: string]: nothing -> record {
let app_check = ($check | parse --regex '^(?<context>.+)@(?<id>\d+)$')
if ($app_check | is-empty) {
{context: $check, integration_id: $ACTIONS_APP_ID}
} else {
{context: $app_check.0.context, integration_id: ($app_check.0.id | into int)}
}
}
# Show the repository administration commands.
def main [] {
help main
}
# Apply repository and workflow settings, security defaults, and immutable releases.
def "main settings" [
repo: string # Target repository as OWNER/REPO
--private # Skip private vulnerability reporting and secret scanning for a private repository
] {
let repository_settings = ($SETTINGS_DIR | path join "repository.json")
let workflow_permissions = ($SETTINGS_DIR | path join "workflow-permissions.json")
let security_settings = ($SETTINGS_DIR | path join "security.json")
(
^gh api $"repos/($repo)"
--method PATCH
--input $repository_settings
)
(
^gh api $"repos/($repo)/actions/permissions/workflow"
--method PUT
--input $workflow_permissions
)
^gh api $"repos/($repo)/vulnerability-alerts" --method PUT
^gh api $"repos/($repo)/automated-security-fixes" --method DELETE
if $private {
print -e "Skipped private vulnerability reporting for a private repository."
print -e "Skipped secret scanning for a private repository."
} else {
^gh api $"repos/($repo)/private-vulnerability-reporting" --method PUT
(
^gh api $"repos/($repo)"
--method PATCH
--input $security_settings
)
}
^gh api $"repos/($repo)/immutable-releases" --method PUT
}
# Create or replace the shared main ruleset, adding any extra required checks.
def "main ruleset" [
repo: string # Target repository as OWNER/REPO
...checks: string # Extra required checks as CONTEXT from GitHub Actions or CONTEXT@APP-ID
--private # Skip the ruleset for a private repository
] {
if $private {
print -e "Skipped the ruleset for a private repository."
return
}
let main_ruleset = ($SETTINGS_DIR | path join "rulesets/main.json")
let required = ($checks | each {|check| required-check $check })
let ruleset = (
open $main_ruleset
| update rules {
each {|rule|
if $rule.type == "required_status_checks" {
$rule | update parameters.required_status_checks { append $required }
} else {
$rule
}
}
}
)
let rulesets = $"repos/($repo)/rulesets"
let id = (
^gh api $rulesets
--paginate
--jq $"first\(.[] | select\(.name == ($ruleset.name | to json)\) | .id\)"
| lines
| first
)
let target = if $id == null {
{
endpoint: $rulesets
method: "POST"
}
} else {
{
endpoint: $"($rulesets)/($id)"
method: "PUT"
}
}
(
$ruleset
| to json
| ^gh api $target.endpoint
--method $target.method
--input -
)
}
# Create or update the shared labels.
def "main labels" [
repo: string # Target repository as OWNER/REPO
...areas: string # Optional component names without the area: prefix
] {
let labels_config = ($SETTINGS_DIR | path join "labels.json")
let shared = (open $labels_config)
let components = (
$areas
| each {|area|
{
name: $"area: ($area)"
color: "bfd4f2"
description: $"Changes to ($area)"
}
}
)
let labels = ($shared | append $components)
for label in $labels {
(
^gh label create $label.name
--repo $repo
--color $label.color
--description $label.description
--force
)
}
}