github-settings-as-code 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +11 -0
- package/package.json +105 -0
- package/src/github/constants.mts +19 -0
- package/src/github/index.mts +5 -0
- package/src/github/octokit.mts +5 -0
- package/src/github/repository/api/get-repository.mts +27 -0
- package/src/github/repository/api/index.mts +2 -0
- package/src/github/repository/api/update-repository.mts +71 -0
- package/src/github/repository/apply.mts +92 -0
- package/src/github/repository/backup.mts +35 -0
- package/src/github/repository/constants.mts +67 -0
- package/src/github/repository/index.mts +3 -0
- package/src/github/ruleset/api/create-ruleset.mts +34 -0
- package/src/github/ruleset/api/get-all-rulesets.mts +73 -0
- package/src/github/ruleset/api/get-ruleset.mts +29 -0
- package/src/github/ruleset/api/index.mts +4 -0
- package/src/github/ruleset/api/update-ruleset.mts +37 -0
- package/src/github/ruleset/apply.mts +77 -0
- package/src/github/ruleset/backup.mts +30 -0
- package/src/github/ruleset/constants.mts +26 -0
- package/src/github/ruleset/index.mts +4 -0
- package/src/github/ruleset/read-rule-set-contents.mts +45 -0
- package/src/github/variables/api/create-repo-variable.mts +24 -0
- package/src/github/variables/api/index.mts +3 -0
- package/src/github/variables/api/list-repo-variables.mts +25 -0
- package/src/github/variables/api/update-repo-variable.mts +24 -0
- package/src/github/variables/apply.mts +28 -0
- package/src/github/variables/index.mts +1 -0
- package/src/globals.d.mts +1 -0
- package/src/index.mts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "github-settings-as-code",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"keywords": [
|
|
6
|
+
"github",
|
|
7
|
+
"octokit"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/noshiro-pf/github-settings-as-code.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "noshiro-pf <noshiro.pf@gmail.com>",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"type": "module",
|
|
17
|
+
"bin": {
|
|
18
|
+
"gh:apply-all": "run-s gh:apply-variables gh:apply-rulesets gh:apply-repository-settings",
|
|
19
|
+
"gh:apply-repository-settings": "tsx --env-file=.env src/github/repository/apply.mts",
|
|
20
|
+
"gh:apply-rulesets": "tsx --env-file=.env src/github/ruleset/apply.mts",
|
|
21
|
+
"gh:apply-variables": "tsx --env-file=.env src/github/variables/apply.mts",
|
|
22
|
+
"gh:backup-all": "run-s gh:backup-rulesets gh:backup-repository-settings",
|
|
23
|
+
"gh:backup-repository-settings": "tsx --env-file=.env src/github/repository/backup.mts",
|
|
24
|
+
"gh:backup-rulesets": "tsx --env-file=.env src/github/ruleset/backup.mts"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"check-all": "tsx ./scripts/cmd/check-all.mts",
|
|
33
|
+
"check:ext": "tsx ./scripts/cmd/check-ext.mts",
|
|
34
|
+
"cspell": "cspell \"**\" --gitignore --gitignore-root ./ --no-progress",
|
|
35
|
+
"fmt": "format-uncommitted",
|
|
36
|
+
"fmt:diff": "format-diff-from origin/main",
|
|
37
|
+
"fmt:full": "prettier --write .",
|
|
38
|
+
"gh:apply-all": "run-s gh:apply-variables gh:apply-rulesets gh:apply-repository-settings",
|
|
39
|
+
"gh:apply-repository-settings": "tsx --env-file=.env src/github/repository/apply.mts",
|
|
40
|
+
"gh:apply-rulesets": "tsx --env-file=.env src/github/ruleset/apply.mts",
|
|
41
|
+
"gh:apply-variables": "tsx --env-file=.env src/github/variables/apply.mts",
|
|
42
|
+
"gh:backup-all": "run-s gh:backup-rulesets gh:backup-repository-settings",
|
|
43
|
+
"gh:backup-repository-settings": "tsx --env-file=.env src/github/repository/backup.mts",
|
|
44
|
+
"gh:backup-rulesets": "tsx --env-file=.env src/github/ruleset/backup.mts",
|
|
45
|
+
"gi": "run-s gi:scripts gi:src fmt",
|
|
46
|
+
"gi:scripts": "gen-index-ts ./src/github --index-ext .mts --export-ext .mjs --target-ext .mts --exclude cmd --exclude apply.mts --exclude octokit.mts",
|
|
47
|
+
"gi:src": "gen-index-ts ./src --index-ext .mts --export-ext .mjs --target-ext .mts --target-ext .tsx --exclude apply.mts",
|
|
48
|
+
"lint": "eslint .",
|
|
49
|
+
"lint:fix": "eslint . --fix",
|
|
50
|
+
"md": "markdownlint-cli2",
|
|
51
|
+
"test": "pnpm run z:vitest run",
|
|
52
|
+
"test:cov": "pnpm run z:vitest run --coverage",
|
|
53
|
+
"test:cov:ui": "vite preview --outDir ./coverage",
|
|
54
|
+
"test:ui": "pnpm run z:vitest --ui",
|
|
55
|
+
"testw": "pnpm run z:vitest watch",
|
|
56
|
+
"tsc": "tsc --noEmit",
|
|
57
|
+
"tscw": "tsc --noEmit --watch -p ./tsconfig.json",
|
|
58
|
+
"type-check": "tsc --noEmit",
|
|
59
|
+
"update-packages": "pnpm update --latest",
|
|
60
|
+
"z:vitest": "vitest --config ./configs/vitest.config.ts"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"npm-run-all2": "^8.0.4",
|
|
64
|
+
"tsx": "^4.20.6"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@octokit/core": "7.0.6",
|
|
68
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
69
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
70
|
+
"@semantic-release/exec": "^7.1.0",
|
|
71
|
+
"@semantic-release/git": "^10.0.1",
|
|
72
|
+
"@semantic-release/github": "^12.0.2",
|
|
73
|
+
"@semantic-release/npm": "^13.1.1",
|
|
74
|
+
"@semantic-release/release-notes-generator": "^14.1.0",
|
|
75
|
+
"@types/node": "^24.10.0",
|
|
76
|
+
"@vitest/coverage-v8": "^4.0.8",
|
|
77
|
+
"@vitest/ui": "^4.0.8",
|
|
78
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
79
|
+
"cspell": "^9.3.0",
|
|
80
|
+
"eslint": "9.39.1",
|
|
81
|
+
"eslint-config-typed": "^3.5.1",
|
|
82
|
+
"fast-glob": "^3.3.3",
|
|
83
|
+
"jiti": "^2.6.1",
|
|
84
|
+
"markdownlint": "^0.39.0",
|
|
85
|
+
"markdownlint-cli2": "^0.18.1",
|
|
86
|
+
"octokit-safe-types": "^1.2.0",
|
|
87
|
+
"prettier": "^3.6.2",
|
|
88
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
89
|
+
"prettier-plugin-packagejson": "^2.5.19",
|
|
90
|
+
"semantic-release": "^25.0.2",
|
|
91
|
+
"ts-data-forge": "^5.0.0",
|
|
92
|
+
"ts-fortress": "^6.0.0",
|
|
93
|
+
"ts-repo-utils": "^7.8.1",
|
|
94
|
+
"ts-type-forge": "^2.3.0",
|
|
95
|
+
"tslib": "^2.8.1",
|
|
96
|
+
"typedoc": "^0.28.14",
|
|
97
|
+
"typedoc-plugin-markdown": "^4.9.0",
|
|
98
|
+
"typescript": "^5.9.3",
|
|
99
|
+
"vitest": "^4.0.8"
|
|
100
|
+
},
|
|
101
|
+
"volta": {
|
|
102
|
+
"node": "25.0.0",
|
|
103
|
+
"npm": "11.6.2"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import packageJson from '../../package.json' with { type: 'json' };
|
|
2
|
+
|
|
3
|
+
export const OWNER = 'noshiro-pf';
|
|
4
|
+
export const REPO = packageJson.name;
|
|
5
|
+
|
|
6
|
+
const githubDir = path.resolve(process.cwd(), './github');
|
|
7
|
+
|
|
8
|
+
export const repositorySettingsDir = path.resolve(
|
|
9
|
+
githubDir,
|
|
10
|
+
'./repository-settings',
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
export const repositorySettingsJsonName = 'settings.json';
|
|
14
|
+
|
|
15
|
+
export const rulesetsDir = path.resolve(githubDir, './rulesets');
|
|
16
|
+
|
|
17
|
+
export const octokitHeaders = {
|
|
18
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
19
|
+
} as const;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { GetRepositoryResponse, type EndpointKeys } from 'octokit-safe-types';
|
|
2
|
+
import { validationErrorsToMessages } from 'ts-fortress';
|
|
3
|
+
import { Result } from 'ts-repo-utils';
|
|
4
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
5
|
+
import { octokit } from '../../octokit.mjs';
|
|
6
|
+
|
|
7
|
+
export const getRepositorySettings =
|
|
8
|
+
async (): Promise<GetRepositoryResponse> => {
|
|
9
|
+
// https://docs.github.com/ja/rest/repos/repos?apiVersion=2022-11-28#get-a-repository
|
|
10
|
+
const getRepositoryResult = await octokit.request(
|
|
11
|
+
'GET /repos/{owner}/{repo}' satisfies EndpointKeys,
|
|
12
|
+
{
|
|
13
|
+
owner: OWNER,
|
|
14
|
+
repo: REPO,
|
|
15
|
+
headers: octokitHeaders,
|
|
16
|
+
},
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
const res = GetRepositoryResponse.validate(getRepositoryResult.data);
|
|
21
|
+
if (Result.isErr(res)) {
|
|
22
|
+
console.warn(validationErrorsToMessages(res.value));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return GetRepositoryResponse.fill(getRepositoryResult.data);
|
|
27
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EndpointKeys,
|
|
3
|
+
type UpdateRepositoryRequest,
|
|
4
|
+
} from 'octokit-safe-types';
|
|
5
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
6
|
+
import { octokit } from '../../octokit.mjs';
|
|
7
|
+
|
|
8
|
+
export const updateRepository = async ({
|
|
9
|
+
payload: {
|
|
10
|
+
name,
|
|
11
|
+
description,
|
|
12
|
+
homepage,
|
|
13
|
+
private: private_,
|
|
14
|
+
visibility,
|
|
15
|
+
security_and_analysis,
|
|
16
|
+
has_issues,
|
|
17
|
+
has_projects,
|
|
18
|
+
has_wiki,
|
|
19
|
+
is_template,
|
|
20
|
+
default_branch,
|
|
21
|
+
allow_squash_merge,
|
|
22
|
+
allow_merge_commit,
|
|
23
|
+
allow_rebase_merge,
|
|
24
|
+
allow_auto_merge,
|
|
25
|
+
delete_branch_on_merge,
|
|
26
|
+
allow_update_branch,
|
|
27
|
+
use_squash_pr_title_as_default,
|
|
28
|
+
squash_merge_commit_title,
|
|
29
|
+
squash_merge_commit_message,
|
|
30
|
+
merge_commit_title,
|
|
31
|
+
merge_commit_message,
|
|
32
|
+
archived,
|
|
33
|
+
allow_forking,
|
|
34
|
+
web_commit_signoff_required,
|
|
35
|
+
},
|
|
36
|
+
}: Readonly<{
|
|
37
|
+
payload: UpdateRepositoryRequest;
|
|
38
|
+
}>): Promise<void> => {
|
|
39
|
+
// https://docs.github.com/ja/rest/repos/repos?apiVersion=2022-11-28#update-a-repository
|
|
40
|
+
await octokit.request('PATCH /repos/{owner}/{repo}' satisfies EndpointKeys, {
|
|
41
|
+
owner: OWNER,
|
|
42
|
+
repo: REPO,
|
|
43
|
+
headers: octokitHeaders,
|
|
44
|
+
|
|
45
|
+
name,
|
|
46
|
+
description,
|
|
47
|
+
homepage,
|
|
48
|
+
private: private_,
|
|
49
|
+
visibility,
|
|
50
|
+
security_and_analysis,
|
|
51
|
+
has_issues,
|
|
52
|
+
has_projects,
|
|
53
|
+
has_wiki,
|
|
54
|
+
is_template,
|
|
55
|
+
default_branch,
|
|
56
|
+
allow_squash_merge,
|
|
57
|
+
allow_merge_commit,
|
|
58
|
+
allow_rebase_merge,
|
|
59
|
+
allow_auto_merge,
|
|
60
|
+
delete_branch_on_merge,
|
|
61
|
+
allow_update_branch,
|
|
62
|
+
use_squash_pr_title_as_default,
|
|
63
|
+
squash_merge_commit_title,
|
|
64
|
+
squash_merge_commit_message,
|
|
65
|
+
merge_commit_title,
|
|
66
|
+
merge_commit_message,
|
|
67
|
+
archived,
|
|
68
|
+
allow_forking,
|
|
69
|
+
web_commit_signoff_required,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { type UpdateRepositoryRequest } from 'octokit-safe-types';
|
|
2
|
+
import { Obj } from 'ts-data-forge';
|
|
3
|
+
import { validationErrorsToMessages } from 'ts-fortress';
|
|
4
|
+
import { Result } from 'ts-repo-utils';
|
|
5
|
+
import {
|
|
6
|
+
repositorySettingsDir,
|
|
7
|
+
repositorySettingsJsonName,
|
|
8
|
+
} from '../constants.mjs';
|
|
9
|
+
import { getRepositorySettings, updateRepository } from './api/index.mjs';
|
|
10
|
+
import { backupRepositorySettings } from './backup.mjs';
|
|
11
|
+
import {
|
|
12
|
+
type RepositoryKeysToPick,
|
|
13
|
+
repositoryKeysToPick,
|
|
14
|
+
RepositoryPicked,
|
|
15
|
+
} from './constants.mjs';
|
|
16
|
+
|
|
17
|
+
const readSettings = async (): Promise<RepositoryPicked> => {
|
|
18
|
+
const settingsText = await fs.readFile(
|
|
19
|
+
path.resolve(repositorySettingsDir, repositorySettingsJsonName),
|
|
20
|
+
{
|
|
21
|
+
encoding: 'utf8',
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const validationResult = RepositoryPicked.validate(JSON.parse(settingsText));
|
|
26
|
+
|
|
27
|
+
if (Result.isErr(validationResult)) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
validationErrorsToMessages(validationResult.value).join('\n'),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return validationResult.value;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
await backupRepositorySettings(false);
|
|
37
|
+
|
|
38
|
+
const settings = await readSettings();
|
|
39
|
+
|
|
40
|
+
await updateRepository({
|
|
41
|
+
payload: {
|
|
42
|
+
// name: settings.name,
|
|
43
|
+
// description: settings.description ?? undefined,
|
|
44
|
+
// homepage: settings.homepage ?? undefined,
|
|
45
|
+
// private: settings.private,
|
|
46
|
+
// visibility: settings.visibility === 'public' ? 'public' : 'private',
|
|
47
|
+
security_and_analysis: settings.security_and_analysis,
|
|
48
|
+
has_issues: settings.has_issues,
|
|
49
|
+
has_projects: settings.has_projects,
|
|
50
|
+
has_wiki: settings.has_wiki,
|
|
51
|
+
// is_template: settings.is_template,
|
|
52
|
+
// default_branch: settings.default_branch,
|
|
53
|
+
allow_squash_merge: settings.allow_squash_merge,
|
|
54
|
+
allow_merge_commit: settings.allow_merge_commit,
|
|
55
|
+
allow_rebase_merge: settings.allow_rebase_merge,
|
|
56
|
+
allow_auto_merge: settings.allow_auto_merge,
|
|
57
|
+
delete_branch_on_merge: settings.delete_branch_on_merge,
|
|
58
|
+
allow_update_branch: settings.allow_update_branch,
|
|
59
|
+
use_squash_pr_title_as_default: settings.use_squash_pr_title_as_default,
|
|
60
|
+
squash_merge_commit_title: settings.squash_merge_commit_title,
|
|
61
|
+
squash_merge_commit_message: settings.squash_merge_commit_message,
|
|
62
|
+
merge_commit_title: settings.merge_commit_title,
|
|
63
|
+
merge_commit_message: settings.merge_commit_message,
|
|
64
|
+
// archived: settings.archived,
|
|
65
|
+
// NOTE: "RequestError [HttpError]: Allow forks can only be changed on org-owned repositories"
|
|
66
|
+
// allow_forking: settings.allow_forking,
|
|
67
|
+
web_commit_signoff_required: settings.web_commit_signoff_required,
|
|
68
|
+
} satisfies Pick<UpdateRepositoryRequest, RepositoryKeysToPick>,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// update local setting file
|
|
72
|
+
{
|
|
73
|
+
const repositorySettings = await getRepositorySettings();
|
|
74
|
+
|
|
75
|
+
const str = JSON.stringify(
|
|
76
|
+
Obj.pick(repositorySettings, repositoryKeysToPick),
|
|
77
|
+
undefined,
|
|
78
|
+
2,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
await fs.writeFile(
|
|
82
|
+
path.resolve(repositorySettingsDir, repositorySettingsJsonName),
|
|
83
|
+
str,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
await fs.writeFile(
|
|
87
|
+
path.resolve(repositorySettingsDir, 'bk', repositorySettingsJsonName),
|
|
88
|
+
str,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
await $('pnpm run fmt');
|
|
92
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import { makeEmptyDir } from 'ts-repo-utils';
|
|
3
|
+
import {
|
|
4
|
+
repositorySettingsDir,
|
|
5
|
+
repositorySettingsJsonName,
|
|
6
|
+
} from '../constants.mjs';
|
|
7
|
+
import { getRepositorySettings } from './api/index.mjs';
|
|
8
|
+
import { repositoryKeysToPick } from './constants.mjs';
|
|
9
|
+
|
|
10
|
+
const backupDir = path.resolve(repositorySettingsDir, './bk');
|
|
11
|
+
|
|
12
|
+
export const backupRepositorySettings = async (
|
|
13
|
+
fmt: boolean = true,
|
|
14
|
+
): Promise<void> => {
|
|
15
|
+
await makeEmptyDir(backupDir);
|
|
16
|
+
|
|
17
|
+
const repositorySettings = await getRepositorySettings();
|
|
18
|
+
|
|
19
|
+
await fs.writeFile(
|
|
20
|
+
path.resolve(backupDir, repositorySettingsJsonName),
|
|
21
|
+
JSON.stringify(
|
|
22
|
+
Obj.pick(repositorySettings, repositoryKeysToPick),
|
|
23
|
+
undefined,
|
|
24
|
+
2,
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
if (fmt) {
|
|
29
|
+
await $('pnpm run fmt');
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
if (isDirectlyExecuted(import.meta.url)) {
|
|
34
|
+
await backupRepositorySettings();
|
|
35
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FullRepository,
|
|
3
|
+
type UpdateRepositoryRequest,
|
|
4
|
+
} from 'octokit-safe-types';
|
|
5
|
+
import { expectType } from 'ts-data-forge';
|
|
6
|
+
import * as t from 'ts-fortress';
|
|
7
|
+
|
|
8
|
+
const keysToPickBase = [
|
|
9
|
+
'allow_auto_merge',
|
|
10
|
+
'allow_forking',
|
|
11
|
+
'allow_merge_commit',
|
|
12
|
+
'allow_rebase_merge',
|
|
13
|
+
'allow_squash_merge',
|
|
14
|
+
'allow_update_branch',
|
|
15
|
+
'archived',
|
|
16
|
+
'default_branch',
|
|
17
|
+
'delete_branch_on_merge',
|
|
18
|
+
'description',
|
|
19
|
+
'has_issues',
|
|
20
|
+
'has_projects',
|
|
21
|
+
'has_wiki',
|
|
22
|
+
'homepage',
|
|
23
|
+
'is_template',
|
|
24
|
+
'merge_commit_message',
|
|
25
|
+
'merge_commit_title',
|
|
26
|
+
'name',
|
|
27
|
+
'private',
|
|
28
|
+
'security_and_analysis',
|
|
29
|
+
'squash_merge_commit_message',
|
|
30
|
+
'squash_merge_commit_title',
|
|
31
|
+
'use_squash_pr_title_as_default',
|
|
32
|
+
'visibility',
|
|
33
|
+
'web_commit_signoff_required',
|
|
34
|
+
] as const satisfies readonly (keyof FullRepository)[];
|
|
35
|
+
|
|
36
|
+
const keysToDropList = [
|
|
37
|
+
'name',
|
|
38
|
+
'private',
|
|
39
|
+
'description',
|
|
40
|
+
'homepage',
|
|
41
|
+
'default_branch',
|
|
42
|
+
'is_template',
|
|
43
|
+
'archived',
|
|
44
|
+
'visibility',
|
|
45
|
+
|
|
46
|
+
// NOTE: "RequestError [HttpError]: Allow forks can only be changed on org-owned repositories"
|
|
47
|
+
'allow_forking',
|
|
48
|
+
] as const;
|
|
49
|
+
|
|
50
|
+
const keysToDrop: ReadonlySet<(typeof keysToPickBase)[number]> = new Set(
|
|
51
|
+
keysToDropList satisfies readonly (typeof keysToPickBase)[number][],
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
export const repositoryKeysToPick = keysToPickBase.filter(
|
|
55
|
+
(k) => !keysToDrop.has(k),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
export type RepositoryKeysToPick = StrictExclude<
|
|
59
|
+
(typeof keysToPickBase)[number],
|
|
60
|
+
(typeof keysToDropList)[number]
|
|
61
|
+
>;
|
|
62
|
+
|
|
63
|
+
expectType<keyof UpdateRepositoryRequest, RepositoryKeysToPick>('>=');
|
|
64
|
+
|
|
65
|
+
export const RepositoryPicked = t.pick(FullRepository, repositoryKeysToPick);
|
|
66
|
+
|
|
67
|
+
export type RepositoryPicked = t.TypeOf<typeof RepositoryPicked>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type components,
|
|
3
|
+
type CreateRulesetRequest,
|
|
4
|
+
type EndpointKeys,
|
|
5
|
+
} from 'octokit-safe-types';
|
|
6
|
+
import { castDeepMutable } from 'ts-data-forge';
|
|
7
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
8
|
+
import { octokit } from '../../octokit.mjs';
|
|
9
|
+
|
|
10
|
+
export const createRuleset = async ({
|
|
11
|
+
payload: { name, target, enforcement, bypass_actors, conditions, rules },
|
|
12
|
+
}: Readonly<{
|
|
13
|
+
payload: CreateRulesetRequest;
|
|
14
|
+
}>): Promise<void> => {
|
|
15
|
+
// https://docs.github.com/ja/rest/repos/rules?apiVersion=2022-11-28#create-a-repository-ruleset
|
|
16
|
+
await octokit.request(
|
|
17
|
+
'POST /repos/{owner}/{repo}/rulesets' satisfies EndpointKeys,
|
|
18
|
+
{
|
|
19
|
+
owner: OWNER,
|
|
20
|
+
repo: REPO,
|
|
21
|
+
headers: octokitHeaders,
|
|
22
|
+
|
|
23
|
+
name,
|
|
24
|
+
target,
|
|
25
|
+
enforcement,
|
|
26
|
+
bypass_actors: castDeepMutable(bypass_actors),
|
|
27
|
+
conditions: castDeepMutable(conditions),
|
|
28
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
29
|
+
rules: castDeepMutable(
|
|
30
|
+
rules,
|
|
31
|
+
) as components['schemas']['repository-rule'][],
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GetAllRulesetsResponse,
|
|
3
|
+
type components,
|
|
4
|
+
type EndpointKeys,
|
|
5
|
+
type OctokitResponse,
|
|
6
|
+
} from 'octokit-safe-types';
|
|
7
|
+
import { expectType } from 'ts-data-forge';
|
|
8
|
+
import { validationErrorsToMessages } from 'ts-fortress';
|
|
9
|
+
import { Result } from 'ts-repo-utils';
|
|
10
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
11
|
+
import { octokit } from '../../octokit.mjs';
|
|
12
|
+
|
|
13
|
+
export const getAllRulesets = async (): Promise<GetAllRulesetsResponse> => {
|
|
14
|
+
// https://docs.github.com/ja/rest/repos/rules?apiVersion=2022-11-28#get-all-repository-rulesets
|
|
15
|
+
const getAllRulesetsResponse = (await octokit.request(
|
|
16
|
+
'GET /repos/{owner}/{repo}/rulesets' satisfies EndpointKeys,
|
|
17
|
+
{
|
|
18
|
+
owner: OWNER,
|
|
19
|
+
repo: REPO,
|
|
20
|
+
headers: octokitHeaders,
|
|
21
|
+
},
|
|
22
|
+
)) satisfies OctokitResponse<GetAllRulesetsResponse, 200>;
|
|
23
|
+
|
|
24
|
+
expectType<
|
|
25
|
+
DeepReadonly<typeof getAllRulesetsResponse>,
|
|
26
|
+
DeepReadonly<
|
|
27
|
+
OctokitResponse<
|
|
28
|
+
{
|
|
29
|
+
id: number;
|
|
30
|
+
name: string;
|
|
31
|
+
target?: 'branch' | 'tag' | 'push' | 'repository';
|
|
32
|
+
source_type?: 'Repository' | 'Organization' | 'Enterprise';
|
|
33
|
+
source: string;
|
|
34
|
+
enforcement: components['schemas']['repository-rule-enforcement'];
|
|
35
|
+
bypass_actors?: components['schemas']['repository-ruleset-bypass-actor'][];
|
|
36
|
+
current_user_can_bypass?:
|
|
37
|
+
| 'always'
|
|
38
|
+
| 'pull_requests_only'
|
|
39
|
+
| 'never'
|
|
40
|
+
| 'exempt';
|
|
41
|
+
node_id?: string;
|
|
42
|
+
_links?: {
|
|
43
|
+
self?: {
|
|
44
|
+
href?: string;
|
|
45
|
+
};
|
|
46
|
+
html?: {
|
|
47
|
+
href?: string;
|
|
48
|
+
} | null;
|
|
49
|
+
};
|
|
50
|
+
conditions?:
|
|
51
|
+
| (
|
|
52
|
+
| components['schemas']['repository-ruleset-conditions']
|
|
53
|
+
| components['schemas']['org-ruleset-conditions']
|
|
54
|
+
)
|
|
55
|
+
| null;
|
|
56
|
+
rules?: components['schemas']['repository-rule'][];
|
|
57
|
+
created_at?: string;
|
|
58
|
+
updated_at?: string;
|
|
59
|
+
}[],
|
|
60
|
+
200
|
|
61
|
+
>
|
|
62
|
+
>
|
|
63
|
+
>('=');
|
|
64
|
+
|
|
65
|
+
{
|
|
66
|
+
const res = GetAllRulesetsResponse.validate(getAllRulesetsResponse.data);
|
|
67
|
+
if (Result.isErr(res)) {
|
|
68
|
+
console.warn(validationErrorsToMessages(res.value));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return GetAllRulesetsResponse.fill(getAllRulesetsResponse.data);
|
|
73
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { GetRulesetResponse, type EndpointKeys } from 'octokit-safe-types';
|
|
2
|
+
import { validationErrorsToMessages } from 'ts-fortress';
|
|
3
|
+
import { Result } from 'ts-repo-utils';
|
|
4
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
5
|
+
import { octokit } from '../../octokit.mjs';
|
|
6
|
+
|
|
7
|
+
export const getRuleset = async (
|
|
8
|
+
rulesetId: number,
|
|
9
|
+
): Promise<GetRulesetResponse> => {
|
|
10
|
+
// https://docs.github.com/ja/rest/repos/rules?apiVersion=2022-11-28#get-a-repository-ruleset
|
|
11
|
+
const getRulesetResult = await octokit.request(
|
|
12
|
+
'GET /repos/{owner}/{repo}/rulesets/{ruleset_id}' satisfies EndpointKeys,
|
|
13
|
+
{
|
|
14
|
+
owner: OWNER,
|
|
15
|
+
repo: REPO,
|
|
16
|
+
ruleset_id: rulesetId,
|
|
17
|
+
headers: octokitHeaders,
|
|
18
|
+
},
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
{
|
|
22
|
+
const res = GetRulesetResponse.validate(getRulesetResult.data);
|
|
23
|
+
if (Result.isErr(res)) {
|
|
24
|
+
console.warn(validationErrorsToMessages(res.value));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return GetRulesetResponse.fill(getRulesetResult.data);
|
|
29
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type components,
|
|
3
|
+
type EndpointKeys,
|
|
4
|
+
type UpdateRulesetRequest,
|
|
5
|
+
} from 'octokit-safe-types';
|
|
6
|
+
import { castDeepMutable } from 'ts-data-forge';
|
|
7
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
8
|
+
import { octokit } from '../../octokit.mjs';
|
|
9
|
+
|
|
10
|
+
export const updateRuleset = async ({
|
|
11
|
+
rulesetId,
|
|
12
|
+
payload: { name, target, enforcement, bypass_actors, conditions, rules },
|
|
13
|
+
}: Readonly<{
|
|
14
|
+
rulesetId: number;
|
|
15
|
+
payload: UpdateRulesetRequest;
|
|
16
|
+
}>): Promise<void> => {
|
|
17
|
+
// https://docs.github.com/ja/rest/repos/rules?apiVersion=2022-11-28#update-a-repository-ruleset
|
|
18
|
+
await octokit.request(
|
|
19
|
+
'PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}' satisfies EndpointKeys,
|
|
20
|
+
{
|
|
21
|
+
owner: OWNER,
|
|
22
|
+
repo: REPO,
|
|
23
|
+
ruleset_id: rulesetId,
|
|
24
|
+
headers: octokitHeaders,
|
|
25
|
+
|
|
26
|
+
name,
|
|
27
|
+
target,
|
|
28
|
+
enforcement,
|
|
29
|
+
bypass_actors: castDeepMutable(bypass_actors),
|
|
30
|
+
conditions: castDeepMutable(conditions),
|
|
31
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
32
|
+
rules: castDeepMutable(
|
|
33
|
+
rules,
|
|
34
|
+
) as components['schemas']['repository-rule'][],
|
|
35
|
+
},
|
|
36
|
+
);
|
|
37
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import 'ts-repo-utils';
|
|
3
|
+
import { rulesetsDir } from '../constants.mjs';
|
|
4
|
+
import {
|
|
5
|
+
createRuleset,
|
|
6
|
+
getAllRulesets,
|
|
7
|
+
getRuleset,
|
|
8
|
+
updateRuleset,
|
|
9
|
+
} from './api/index.mjs';
|
|
10
|
+
import { backupRulesets } from './backup.mjs';
|
|
11
|
+
import { rulesetKeysToPick } from './constants.mjs';
|
|
12
|
+
import {
|
|
13
|
+
readRulesetBackupFiles,
|
|
14
|
+
readRulesetFiles,
|
|
15
|
+
} from './read-rule-set-contents.mjs';
|
|
16
|
+
|
|
17
|
+
await backupRulesets(false);
|
|
18
|
+
|
|
19
|
+
const rulesets = await readRulesetFiles();
|
|
20
|
+
|
|
21
|
+
const backupIds: ReadonlySet<number> = await readRulesetBackupFiles().then(
|
|
22
|
+
(rs) => new Set(rs.map((r) => r.id)),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const rulesetsToUpdate = rulesets.filter((r) => backupIds.has(r.id));
|
|
26
|
+
const rulesetsToCreate = rulesets.filter((r) => !backupIds.has(r.id));
|
|
27
|
+
|
|
28
|
+
for (const ruleset of rulesetsToUpdate) {
|
|
29
|
+
await updateRuleset({
|
|
30
|
+
rulesetId: ruleset.id,
|
|
31
|
+
payload: {
|
|
32
|
+
bypass_actors: ruleset.bypass_actors,
|
|
33
|
+
conditions: ruleset.conditions ?? undefined,
|
|
34
|
+
enforcement: ruleset.enforcement,
|
|
35
|
+
name: ruleset.name,
|
|
36
|
+
rules: ruleset.rules,
|
|
37
|
+
target: ruleset.target === 'repository' ? undefined : ruleset.target,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const ruleset of rulesetsToCreate) {
|
|
43
|
+
await createRuleset({
|
|
44
|
+
payload: {
|
|
45
|
+
bypass_actors: ruleset.bypass_actors,
|
|
46
|
+
conditions: ruleset.conditions ?? undefined,
|
|
47
|
+
enforcement: ruleset.enforcement,
|
|
48
|
+
name: ruleset.name,
|
|
49
|
+
rules: ruleset.rules,
|
|
50
|
+
target: ruleset.target === 'repository' ? undefined : ruleset.target,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// update local ruleset files
|
|
56
|
+
{
|
|
57
|
+
const rulesetsResult = await getAllRulesets();
|
|
58
|
+
|
|
59
|
+
for (const rule of rulesetsResult) {
|
|
60
|
+
const content = await getRuleset(rule.id);
|
|
61
|
+
|
|
62
|
+
const str = JSON.stringify(
|
|
63
|
+
Obj.pick(content, rulesetKeysToPick),
|
|
64
|
+
undefined,
|
|
65
|
+
2,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
await fs.writeFile(path.resolve(rulesetsDir, `${rule.name}.json`), str);
|
|
69
|
+
|
|
70
|
+
await fs.writeFile(
|
|
71
|
+
path.resolve(rulesetsDir, `./bk/${rule.name}.json`),
|
|
72
|
+
str,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await $('pnpm run fmt');
|
|
77
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Obj } from 'ts-data-forge';
|
|
2
|
+
import { makeEmptyDir } from 'ts-repo-utils';
|
|
3
|
+
import { rulesetsDir } from '../constants.mjs';
|
|
4
|
+
import { getAllRulesets, getRuleset } from './api/index.mjs';
|
|
5
|
+
import { rulesetKeysToPick } from './constants.mjs';
|
|
6
|
+
|
|
7
|
+
const backupDir = path.resolve(rulesetsDir, './bk');
|
|
8
|
+
|
|
9
|
+
export const backupRulesets = async (fmt: boolean = true): Promise<void> => {
|
|
10
|
+
await makeEmptyDir(backupDir);
|
|
11
|
+
|
|
12
|
+
const rulesetsResult = await getAllRulesets();
|
|
13
|
+
|
|
14
|
+
for (const rule of rulesetsResult) {
|
|
15
|
+
const content = await getRuleset(rule.id);
|
|
16
|
+
|
|
17
|
+
await fs.writeFile(
|
|
18
|
+
path.resolve(backupDir, `${rule.name}.json`),
|
|
19
|
+
JSON.stringify(Obj.pick(content, rulesetKeysToPick), undefined, 2),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (fmt) {
|
|
24
|
+
await $('pnpm run fmt');
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (isDirectlyExecuted(import.meta.url)) {
|
|
29
|
+
await backupRulesets();
|
|
30
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RepositoryRuleset,
|
|
3
|
+
type CreateRulesetRequest,
|
|
4
|
+
type UpdateRulesetRequest,
|
|
5
|
+
} from 'octokit-safe-types';
|
|
6
|
+
import { expectType } from 'ts-data-forge';
|
|
7
|
+
import * as t from 'ts-fortress';
|
|
8
|
+
|
|
9
|
+
export const rulesetKeysToPick = [
|
|
10
|
+
'id',
|
|
11
|
+
'name',
|
|
12
|
+
'target',
|
|
13
|
+
'enforcement',
|
|
14
|
+
'bypass_actors',
|
|
15
|
+
'conditions',
|
|
16
|
+
'rules',
|
|
17
|
+
] as const satisfies readonly (keyof RepositoryRuleset)[];
|
|
18
|
+
|
|
19
|
+
type KeysToPick = (typeof rulesetKeysToPick)[number];
|
|
20
|
+
|
|
21
|
+
expectType<keyof UpdateRulesetRequest | 'id', KeysToPick>('=');
|
|
22
|
+
expectType<keyof CreateRulesetRequest, keyof UpdateRulesetRequest>('=');
|
|
23
|
+
|
|
24
|
+
export const RulesetPicked = t.pick(RepositoryRuleset, rulesetKeysToPick);
|
|
25
|
+
|
|
26
|
+
export type RulesetPicked = t.TypeOf<typeof RulesetPicked>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Dirent } from 'node:fs';
|
|
2
|
+
import * as t from 'ts-fortress';
|
|
3
|
+
import { Result } from 'ts-repo-utils';
|
|
4
|
+
import { rulesetsDir } from '../constants.mjs';
|
|
5
|
+
import { RulesetPicked } from './constants.mjs';
|
|
6
|
+
|
|
7
|
+
export const readRulesetFiles = async (): Promise<readonly RulesetPicked[]> =>
|
|
8
|
+
readFilesIn(rulesetsDir);
|
|
9
|
+
|
|
10
|
+
export const readRulesetBackupFiles = async (): Promise<
|
|
11
|
+
readonly RulesetPicked[]
|
|
12
|
+
> => readFilesIn(path.resolve(rulesetsDir, './bk'));
|
|
13
|
+
|
|
14
|
+
const readFilesIn = async (dir: string): Promise<readonly RulesetPicked[]> => {
|
|
15
|
+
const files: readonly Dirent<string>[] = await fs.readdir(dir, {
|
|
16
|
+
withFileTypes: true,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const rulesetFileContents: readonly string[] = await Promise.all(
|
|
20
|
+
files
|
|
21
|
+
.filter((d) => d.isFile())
|
|
22
|
+
.map((d) =>
|
|
23
|
+
fs.readFile(path.resolve(dir, d.name), {
|
|
24
|
+
encoding: 'utf8',
|
|
25
|
+
}),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const validationResults = rulesetFileContents.map((rule) =>
|
|
30
|
+
RulesetPicked.validate(JSON.parse(rule)),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (validationResults.every(Result.isOk)) {
|
|
34
|
+
return validationResults.map((r) => r.value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
38
|
+
const error = validationResults.find(Result.isErr)!;
|
|
39
|
+
|
|
40
|
+
throw new Error(t.validationErrorsToMessages(error.value).join('\n'));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (isDirectlyExecuted(import.meta.url)) {
|
|
44
|
+
await readRulesetFiles();
|
|
45
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type EndpointKeys } from 'octokit-safe-types';
|
|
2
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
3
|
+
import { octokit } from '../../octokit.mjs';
|
|
4
|
+
|
|
5
|
+
export const createRepoVariable = async ({
|
|
6
|
+
name,
|
|
7
|
+
value,
|
|
8
|
+
}: Readonly<{
|
|
9
|
+
name: string;
|
|
10
|
+
value: string;
|
|
11
|
+
}>): Promise<void> => {
|
|
12
|
+
// https://docs.github.com/ja/rest/actions/variables?apiVersion=2022-11-28#create-a-repository-variable
|
|
13
|
+
await octokit.request(
|
|
14
|
+
'POST /repos/{owner}/{repo}/actions/variables' satisfies EndpointKeys,
|
|
15
|
+
{
|
|
16
|
+
owner: OWNER,
|
|
17
|
+
repo: REPO,
|
|
18
|
+
headers: octokitHeaders,
|
|
19
|
+
|
|
20
|
+
name,
|
|
21
|
+
value,
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type EndpointKeys } from 'octokit-safe-types';
|
|
2
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
3
|
+
import { octokit } from '../../octokit.mjs';
|
|
4
|
+
|
|
5
|
+
export const listRepoVariables = async (): Promise<
|
|
6
|
+
DeepReadonly<
|
|
7
|
+
{
|
|
8
|
+
name: string;
|
|
9
|
+
value: string;
|
|
10
|
+
created_at: string;
|
|
11
|
+
updated_at: string;
|
|
12
|
+
}[]
|
|
13
|
+
>
|
|
14
|
+
> => {
|
|
15
|
+
// https://docs.github.com/ja/rest/actions/variables?apiVersion=2022-11-28#list-repository-variables
|
|
16
|
+
const result = await octokit.request(
|
|
17
|
+
'GET /repos/{owner}/{repo}/actions/variables' satisfies EndpointKeys,
|
|
18
|
+
{
|
|
19
|
+
owner: OWNER,
|
|
20
|
+
repo: REPO,
|
|
21
|
+
headers: octokitHeaders,
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
return result.data.variables;
|
|
25
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type EndpointKeys } from 'octokit-safe-types';
|
|
2
|
+
import { octokitHeaders, OWNER, REPO } from '../../constants.mjs';
|
|
3
|
+
import { octokit } from '../../octokit.mjs';
|
|
4
|
+
|
|
5
|
+
export const updateRepoVariable = async ({
|
|
6
|
+
name,
|
|
7
|
+
value,
|
|
8
|
+
}: Readonly<{
|
|
9
|
+
name: string;
|
|
10
|
+
value: string;
|
|
11
|
+
}>): Promise<void> => {
|
|
12
|
+
// https://docs.github.com/en/rest/actions/variables#update-a-repository-variable
|
|
13
|
+
await octokit.request(
|
|
14
|
+
'PATCH /repos/{owner}/{repo}/actions/variables/{name}' satisfies EndpointKeys,
|
|
15
|
+
{
|
|
16
|
+
owner: OWNER,
|
|
17
|
+
repo: REPO,
|
|
18
|
+
headers: octokitHeaders,
|
|
19
|
+
|
|
20
|
+
name,
|
|
21
|
+
value,
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import 'ts-repo-utils';
|
|
2
|
+
import {
|
|
3
|
+
createRepoVariable,
|
|
4
|
+
listRepoVariables,
|
|
5
|
+
updateRepoVariable,
|
|
6
|
+
} from './api/index.mjs';
|
|
7
|
+
|
|
8
|
+
const variables = [
|
|
9
|
+
{
|
|
10
|
+
name: 'DEPENDABOT_AUTO_MERGE_BOT_APP_ID',
|
|
11
|
+
value: '1442916',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: 'SEMANTIC_RELEASE_GIT_PERMISSION_BOT_APP_ID',
|
|
15
|
+
value: '1442563',
|
|
16
|
+
},
|
|
17
|
+
] as const;
|
|
18
|
+
|
|
19
|
+
const variableListSaved = await listRepoVariables();
|
|
20
|
+
const variablesSaved: ReadonlySet<string> = new Set(
|
|
21
|
+
variableListSaved.map((v) => v.name),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
for (const variable of variables) {
|
|
25
|
+
const found = variablesSaved.has(variable.name);
|
|
26
|
+
|
|
27
|
+
await (found ? updateRepoVariable(variable) : createRepoVariable(variable));
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './api/index.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="ts-type-forge" />
|
package/src/index.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './github/index.mjs';
|