Skip to content

Commit 4f13c32

Browse files
authored
chore: 🔀 add and refactor several commands (#30)
## Description Add some new commands to help with admin tasks. Plus refactor other ones. No review needed. ## Checklist - [x] Ran `just run-all`
2 parents a199c2e + 680df25 commit 4f13c32

11 files changed

+282
-168
lines changed

README.md

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,8 @@ helper functions!
4040

4141
## Commands
4242

43-
``` bash
44-
spaid_lint_commits_on_branch -h
45-
```
46-
47-
Usage: spaid_lint_commits_on_branch \[-h\]
48-
49-
Run this command on a development Git branch to check if commit messages
50-
follow the Conventional Commit standard. This will only work if:
51-
52-
- you are in a local Git repository
53-
- the repository is a Python project managed by Poetry
54-
- the Poetry project has commitizen installed
55-
- you are on a branch that isn’t main
56-
57-
Otherwise, you’ll get an error or nothing will happen.
43+
Many commands are designed to be used with `xargs` to process multiple
44+
commands at once. A note is given below the relevant commands for how.
5845

5946
``` bash
6047
spaid_setup_precommit -h
@@ -124,7 +111,7 @@ spaid_pr_merge_rebase -h
124111

125112
Usage: spaid_pr_merge_rebase \[-h\]
126113

127-
Approve and do a merge rebase on multiple PRs in a single repository.
114+
Approve and do a merge rebase on one PRs in a single repository.
128115
Requires admin privilege, so not everyone can use this command.
129116

130117
Examples:
@@ -134,9 +121,8 @@ Examples:
134121

135122
Positional arguments:
136123

137-
- org: The name of the GitHub organization.
138-
- repo: The name of the repository in the GitHub organization.
139-
- PR number(s): One or more PR numbers to do the merge rebase for.
124+
- repo_spec: The GitHub repository spec, in the format of ‘owner/repo’.
125+
- PR number: A PR number to do the merge rebase for.
140126

141127
``` bash
142128
spaid_pr_merge_chores -h
@@ -160,24 +146,7 @@ Positional arguments:
160146
If not given, the command will run on all repositories in the
161147
organization.
162148

163-
### GitHub organization management
164-
165-
``` bash
166-
spaid_gh_repo_list -h
167-
```
168-
169-
Usage: spaid_gh_repo_list \[-h\]
170-
171-
Run this script to get a list of repositories within a specific
172-
organization.
173-
174-
Example:
175-
176-
spaid_gh_repo_list seedcase-project
177-
178-
Positional arguments:
179-
180-
1. organization: The GitHub organization name.
149+
### GitHub organization management (invitations, teams)
181150

182151
``` bash
183152
spaid_gh_org_invite -h
@@ -233,8 +202,75 @@ Positional arguments:
233202
2. team_slug: The GitHub organization team ‘slug’ name.
234203
3. username: The GitHub username of the person you want to invite.
235204

205+
### Rulesets
206+
207+
``` bash
208+
spaid_gh_ruleset_list -h
209+
```
210+
211+
Usage: spaid_gh_ruleset_list \[-h\]
212+
213+
Lists the rulesets of a repository.
214+
215+
Examples:
216+
217+
spaid_gh_ruleset_list seedcase-project team
218+
219+
Positional argument:
220+
221+
- repo_spec: The GitHub repository spec, in the format of ‘owner/repo’.
222+
- ruleset_id: Optional. The ID of the ruleset to list. If not provided,
223+
all rulesets will be listed. If provided, the command will return all
224+
the details of the ruleset.
225+
226+
``` bash
227+
spaid_gh_ruleset_basic_protect_main -h
228+
```
229+
230+
Usage: spaid_gh_ruleset_basic_protect_main \[-h\]
231+
232+
Lists all open PRs in an organization. It provides basic protection of
233+
the main (default) branch. Organisation and repository admin can bypass
234+
the rules. Specifically:
235+
236+
- Stop force pushes
237+
- Can’t delete
238+
- Can’t create
239+
240+
Examples:
241+
242+
spaid_gh_ruleset_basic_protect_main seedcase-project/team
243+
244+
Positional argument:
245+
246+
- repo spec: The GitHub repository spec, in the format of ‘owner/repo’.
247+
248+
Can do multiple repos at once with `xargs`:
249+
250+
``` bash
251+
spaid_gh_ruleset_list seedcase-project | xargs spaid_gh_ruleset_basic_protect_main
252+
```
253+
236254
### GitHub repository management
237255

256+
``` bash
257+
spaid_gh_repo_list -h
258+
```
259+
260+
Usage: spaid_gh_repo_list \[-h\]
261+
262+
Run this script to get a list of repositories within a specific
263+
organization. Outputs a repo spec in the format of ‘org/repo’ for each
264+
repository.
265+
266+
Example:
267+
268+
spaid_gh_repo_list seedcase-project
269+
270+
Positional arguments:
271+
272+
1. organization: The GitHub organization name.
273+
238274
``` bash
239275
spaid_gh_set_repo_settings -h
240276
```
@@ -248,17 +284,22 @@ are:
248284
- Omit the wiki.
249285
- Disable discussions.
250286
- Allow PR’s to have an option to auto-merge after approval.
251-
- Allow PR’s to have an option to easily update with the ‘main’ branch.
252287
- Allow merge commits as well as rebase and squash merges.
288+
- Allow PR branch updates from ‘main’.
253289

254290
Examples:
255291

256-
$ spaid_gh_set_repo_settings seedcase-project team
292+
$ spaid_gh_set_repo_settings seedcase-project/team
257293

258294
Positional arguments:
259295

260-
- org: The name of the GitHub organization.
261-
- repo: The name of the repository in the GitHub organization.
296+
- repo spec: The GitHub repository spec, in the format of ‘owner/repo’.
297+
298+
Can do multiple repos at once with `xargs`:
299+
300+
``` bash
301+
spaid_gh_repo_list seedcase-project | xargs spaid_gh_set_repo_settings
302+
```
262303

263304
``` bash
264305
spaid_gh_create_repo_from_local -h

README.qmd

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ helper functions!
4141

4242
## Commands
4343

44-
```{bash}
45-
#| output: asis
46-
spaid_lint_commits_on_branch -h
47-
```
44+
Many commands are designed to be used with `xargs` to process multiple
45+
commands at once. A note is given below the relevant commands for how.
4846

4947
```{bash}
5048
#| output: asis
@@ -73,35 +71,59 @@ spaid_pr_merge_rebase -h
7371
spaid_pr_merge_chores -h
7472
```
7573

76-
### GitHub organization management
74+
### GitHub organization management (invitations, teams)
7775

7876
```{bash}
7977
#| output: asis
80-
spaid_gh_repo_list -h
78+
spaid_gh_org_invite -h
8179
```
8280

8381
```{bash}
8482
#| output: asis
85-
spaid_gh_org_invite -h
83+
spaid_gh_teams_list -h
8684
```
8785

8886
```{bash}
8987
#| output: asis
90-
spaid_gh_teams_list -h
88+
spaid_gh_teams_invite -h
9189
```
9290

91+
### Rulesets
92+
9393
```{bash}
9494
#| output: asis
95-
spaid_gh_teams_invite -h
95+
spaid_gh_ruleset_list -h
96+
```
97+
98+
```{bash}
99+
#| output: asis
100+
spaid_gh_ruleset_basic_protect_main -h
101+
```
102+
103+
Can do multiple repos at once with `xargs`:
104+
105+
``` bash
106+
spaid_gh_ruleset_list seedcase-project | xargs spaid_gh_ruleset_basic_protect_main
96107
```
97108

98109
### GitHub repository management
99110

111+
```{bash}
112+
#| output: asis
113+
spaid_gh_repo_list -h
114+
```
115+
100116
```{bash}
101117
#| output: asis
102118
spaid_gh_set_repo_settings -h
103119
```
104120

121+
Can do multiple repos at once with `xargs`:
122+
123+
``` bash
124+
spaid_gh_repo_list seedcase-project | xargs spaid_gh_set_repo_settings
125+
```
126+
105127
```{bash}
106128
#| output: asis
107129
spaid_gh_create_repo_from_local -h

bin/spaid_current_functions

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

bin/spaid_gh_repo_list

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ help_text="
44
Usage: `basename $0` [-h]
55
66
Run this script to get a list of repositories within a specific organization.
7+
Outputs a repo spec in the format of 'org/repo' for each repository.
78
89
Example:
910
@@ -25,4 +26,4 @@ if [[ "$1" == "" ]] ; then
2526
fi
2627
org="$1"
2728

28-
gh repo list "$org" --json name -q ".[] | .name"
29+
gh repo list "$org" --json name -q ".[] | .name" | xargs -I {} echo "$org/{}" | sort
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/zsh
2+
3+
help_text="
4+
Usage: `basename $0` [-h]
5+
6+
Lists all open PRs in an organization. It provides basic protection of
7+
the main (default) branch. Organisation and repository admin can
8+
bypass the rules. Specifically:
9+
10+
- Stop force pushes
11+
- Can't delete
12+
- Can't create
13+
14+
Examples:
15+
16+
spaid_gh_ruleset_basic_protect_main seedcase-project/team
17+
18+
Positional argument:
19+
20+
- repo spec: The GitHub repository spec, in the format of 'owner/repo'.
21+
"
22+
23+
if [[ "$1" == "-h" ]] ; then
24+
echo $help_text
25+
exit 0
26+
fi
27+
28+
repo_spec="$1"
29+
30+
if [[ "$repo_spec" == "" ]] ; then
31+
echo "Error: No repo spec has been given. Please provide one."
32+
exit 1
33+
fi
34+
35+
basic_protect_main() {
36+
curl -L \
37+
-X POST \
38+
-H "Accept: application/vnd.github+json" \
39+
-H "Authorization: Bearer $(gh auth token)" \
40+
-H "X-GitHub-Api-Version: 2022-11-28" \
41+
https://api.github.com/repos/$1/rulesets \
42+
-d '{
43+
"name": "basic protection of default branch",
44+
"target": "branch",
45+
"enforcement": "active",
46+
"bypass_actors": [
47+
{
48+
"actor_id": 1,
49+
"actor_type": "OrganizationAdmin",
50+
"bypass_mode": "always"
51+
},
52+
{
53+
"actor_id": 5,
54+
"actor_type": "RepositoryRole",
55+
"bypass_mode": "always"
56+
}
57+
],
58+
"conditions": {
59+
"ref_name": {
60+
"include": [
61+
"~DEFAULT_BRANCH"
62+
],
63+
"exclude": []
64+
}
65+
},
66+
"rules": [
67+
{
68+
"type": "creation"
69+
},
70+
{
71+
"type": "non_fast_forward"
72+
},
73+
{
74+
"type": "deletion"
75+
}
76+
]
77+
}'
78+
}
79+
80+
basic_protect_main $repo_spec

0 commit comments

Comments
 (0)