-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rulesets): add script #19
Draft
sgoudham
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-rulesets-script
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
deno.lock | ||
.vscode |
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,120 @@ | ||
#!/usr/bin/env -S deno run --allow-net | ||
|
||
import { Octokit } from "https://esm.sh/@octokit/[email protected]"; | ||
import { paginateGraphQL } from "https://esm.sh/@octokit/[email protected]"; | ||
import { z } from "https://esm.sh/[email protected]"; | ||
|
||
if (Deno.args.length < 1) { | ||
console.error( | ||
"Please provide your GitHub personal access token as an argument." | ||
); | ||
Deno.exit(1); | ||
} | ||
|
||
// Unsure how to properly code-generate the types and have it play nice with | ||
// @ocotokit/plugin-paginate-graphql so zod should be fine for a simple script | ||
// like this. | ||
const ruleSchema = z.object({ | ||
type: z.string(), | ||
}); | ||
|
||
const rulesetSchema = z.object({ | ||
name: z.string(), | ||
rules: z.object({ | ||
nodes: z.array(ruleSchema), | ||
}), | ||
}); | ||
|
||
const repositorySchema = z | ||
.object({ | ||
name: z.string(), | ||
isPrivate: z.boolean(), | ||
isArchived: z.boolean(), | ||
rulesets: z.object({ | ||
nodes: z.array(rulesetSchema), | ||
}), | ||
}) | ||
.strict(); | ||
type Repository = z.infer<typeof repositorySchema>; | ||
|
||
const repositoriesSchema = z.object({ | ||
nodes: z.array(repositorySchema), | ||
}); | ||
|
||
const organisationSchema = z.object({ | ||
repositories: repositoriesSchema, | ||
}); | ||
|
||
const responseSchema = z.object({ | ||
organization: organisationSchema, | ||
}); | ||
|
||
const PaginatedOctokit = Octokit.plugin(paginateGraphQL); | ||
const octokit = new PaginatedOctokit({ | ||
auth: Deno.args[0], | ||
}); | ||
|
||
const iterator = octokit.graphql.paginate.iterator( | ||
`query($cursor: String) { | ||
organization(login: "catppuccin") { | ||
repositories(first: 100, after: $cursor) { | ||
nodes { | ||
name | ||
isPrivate | ||
isArchived | ||
rulesets(first: 3) { | ||
nodes { | ||
name | ||
rules(first: 3) { | ||
nodes { | ||
type | ||
} | ||
} | ||
} | ||
} | ||
} | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
} | ||
} | ||
}` | ||
); | ||
|
||
async function* yieldRepositoryRulesets() { | ||
for await (const response of iterator) { | ||
const responseData = await responseSchema.parseAsync(response); | ||
yield responseData.organization.repositories.nodes; | ||
} | ||
} | ||
|
||
const fetchRepositoryRulesets = async (): Promise<Repository[]> => { | ||
return (await Array.fromAsync(await yieldRepositoryRulesets())) | ||
.flat() | ||
.filter((repo) => !repo.isPrivate && !repo.isArchived); | ||
}; | ||
|
||
// const displayRepositoryRulesets = (repos: Repository[]) => { | ||
// for (const repo of repos) { | ||
// const repoName = repo.name; | ||
// const rulesets = repo.rulesets.nodes; | ||
|
||
// console.log(`Rulesets for repository "${repoName}":`); | ||
// rulesets.forEach((ruleset) => { | ||
// console.log( | ||
// ` - Ruleset: ${ruleset.name} (${ruleset.rules.nodes | ||
// .map((rule) => rule.type) | ||
// .join(", ")})` | ||
// ); | ||
// }); | ||
// console.log("\n "); | ||
// } | ||
// }; | ||
|
||
try { | ||
const repositoryRulesets = await fetchRepositoryRulesets(); | ||
console.log(JSON.stringify(repositoryRulesets, null, 2)); | ||
} catch (error) { | ||
console.error(`Error: ${error}`); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be read via https://docs.deno.com/runtime/manual/basics/env_variables/ instead of as an arg. This is how it is done in userstyles Octokit stuff as well FYI.