forked from actions/create-github-app-token
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-permission-inputs.js
More file actions
65 lines (53 loc) · 2.41 KB
/
update-permission-inputs.js
File metadata and controls
65 lines (53 loc) · 2.41 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
import { readFile, writeFile } from "node:fs/promises";
import OctokitOpenapi from "@octokit/openapi";
const appPermissionsSchema =
OctokitOpenapi.schemas["api.github.com"].components.schemas[
"app-permissions"
];
await writeFile(
`scripts/generated/app-permissions.json`,
JSON.stringify(appPermissionsSchema, null, 2) + "\n",
"utf8"
);
const permissionsInputs = Object.entries(appPermissionsSchema.properties)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((result, [key, value]) => {
const formatter = new Intl.ListFormat("en", {
style: "long",
type: "disjunction",
});
const permissionAccessValues = formatter.format(
value.enum.map((p) => `'${p}'`)
);
const description = `${value.description} Can be set to ${permissionAccessValues}.`;
return `${result}
permission-${key.replace(/_/g, "-")}:
description: "${description}"`;
}, "");
const actionYamlContent = await readFile("action.yml", "utf8");
// In the action.yml file, replace the content between the `<START GENERATED PERMISSIONS INPUTS>` and `<END GENERATED PERMISSIONS INPUTS>` comments with the new content
const updatedActionYamlContent = actionYamlContent.replace(
/(?<=# <START GENERATED PERMISSIONS INPUTS>)(.|\n)*(?=# <END GENERATED PERMISSIONS INPUTS>)/,
permissionsInputs + "\n "
);
await writeFile("action.yml", updatedActionYamlContent, "utf8");
console.log("Updated action.yml with new permissions inputs");
const permissionsTypes = Object.entries(appPermissionsSchema.properties)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((result, [key, value]) => {
const permissionAccessValues = value.enum.map((p) => ` - "${p}"`).reduce((result, p) => `${result}\n${p}`);
return `${result}
permission-${key.replace(/_/g, "-")}:
type: enum
allowed-values:
${permissionAccessValues}
`;
}, "");
const actionTypesYamlContent = await readFile("action-types.yml", "utf8");
// In the action-types.yml file, replace the content between the `<START GENERATED PERMISSIONS TYPES>` and `<END GENERATED PERMISSIONS TYPES>` comments with the new content
const updatedActionTypesYamlContent = actionTypesYamlContent.replace(
/(?<=# <START GENERATED PERMISSIONS TYPES>)(.|\n)*(?=# <END GENERATED PERMISSIONS TYPES>)/,
permissionsTypes + "\n "
);
await writeFile("action-types.yml", updatedActionTypesYamlContent, "utf8");
console.log("Updated action-types.yml with new permissions types");