-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.qmd
More file actions
83 lines (63 loc) 路 2.22 KB
/
Copy pathREADME.qmd
File metadata and controls
83 lines (63 loc) 路 2.22 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
---
title: copy-ruleset
format: gfm
execute:
echo: false
output: asis
---
```{python}
import ccbr_actions.docs
action = ccbr_actions.docs.parse_action_yaml('action.yml')
print(ccbr_actions.docs.action_markdown_desc(action))
```
Copy a named ruleset from one GitHub repository to another.
This action fetches the full ruleset definition from the source repository via the GitHub API and creates an identical ruleset in the target repository.
It is useful for standardising branch protection rules across multiple repositories in an organisation.
## Usage
### Basic example
[copy-ruleset.yml](/examples/copy-ruleset.yml)
```{python}
yml_file = '../examples/copy-ruleset.yml'
print('```yaml')
with open(yml_file, 'r') as infile:
print(infile.read())
print('```\n')
```
### Using a PAT for cross-repository access
`github.token` is scoped to the repository running the workflow.
To copy rulesets to a repository outside the current one, supply a personal access token (PAT) or a GitHub App token with `repo` scope on both repositories:
```yaml
steps:
- uses: CCBR/actions/copy-ruleset@main
with:
source-repo: CCBR/actions
target-repo: CCBR/other-repo
ruleset-name: "Require PR reviews"
token: ${{ secrets.ORG_PAT }}
```
## Running locally
Both operations can also be performed directly in the terminal using the [`gh` CLI](https://cli.github.com/) and `jq`, without installing any additional packages.
**List rulesets** in a repository:
```bash
gh api repos/{owner}/{repo}/rulesets --jq '.[] | [.id, .enforcement, .name] | @tsv'
```
**Copy a ruleset** from one repository to another:
```bash
gh api repos/{source_owner}/{source_repo}/rulesets \
--jq '.[] | select(.name == "Require PR reviews") | .id' \
| xargs -I{} gh api repos/{source_owner}/{source_repo}/rulesets/{} \
| jq 'del(.id, .node_id, .created_at, .updated_at, ._links)' \
| gh api repos/{target_owner}/{target_repo}/rulesets \
--method POST \
--input -
```
Alternatively, using the `ccbr_actions` Python package:
```bash
# list rulesets
ccbr_actions list-rulesets owner/repo
# copy a ruleset
ccbr_actions copy-ruleset CCBR/actions CCBR/other-repo "Require PR reviews"
```
```{python}
print(ccbr_actions.docs.action_markdown_io(action))
```