Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deno.lock
.vscode
120 changes: 120 additions & 0 deletions rulesets/main.ts
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);
}
Comment on lines +7 to +12
Copy link
Member

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.


// 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}`);
}