-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP script to update
action.yml
with permissions based on https://g…
…ithub.com/octokit/app-permissions/blob/main/generated/api.github.com.json Co-Authored-By: Parker Brown <[email protected]>
- Loading branch information
1 parent
9ccc6db
commit 580a7f4
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { readFile, writeFile } from "node:fs/promises"; | ||
|
||
import { request } from "@octokit/request"; | ||
|
||
const { data: permissionsSchemaString } = await request( | ||
"GET /repos/{owner}/{repo}/contents/{path}", | ||
{ | ||
owner: "octokit", | ||
repo: "app-permissions", | ||
path: "generated/api.github.com.json", | ||
mediaType: { | ||
format: "raw", | ||
}, | ||
headers: { | ||
authorization: `token ${process.env.GITHUB_TOKEN}`, | ||
}, | ||
}, | ||
); | ||
|
||
const permissionsSchema = JSON.parse(permissionsSchemaString); | ||
|
||
const permissionsInputs = Object.entries(permissionsSchema.permissions).reduce( | ||
(result, [key, value]) => { | ||
const supportsWrite = value.write.length > 0; | ||
const description = supportsWrite | ||
? `Can be set to 'read' or 'write'. Learn more at ${value.url}` | ||
: `Can be set to 'read'. Learn more at ${value.url}`; | ||
return `${result} | ||
permission-${key.replace(/_/g, "-")}: | ||
description: "${description}"`; | ||
}, | ||
"", | ||
); | ||
|
||
const actionsYamlContent = 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 updatedActionsYamlContent = actionsYamlContent.replace( | ||
/(?<=# <START GENERATED PERMISSIONS INPUTS>)(.|\n)*(?=# <END GENERATED PERMISSIONS INPUTS>)/, | ||
permissionsInputs + "\n ", | ||
); | ||
|
||
await writeFile("action.yml", updatedActionsYamlContent, "utf8"); | ||
console.log("Updated action.yml with new permissions inputs"); |