-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config file for tweaking behavior (#6)
- Loading branch information
Showing
7 changed files
with
298 additions
and
121 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,80 @@ | ||
export const Config = { | ||
import { RequestError } from "octokit"; | ||
import { z } from "zod"; | ||
import { load } from "js-toml"; | ||
import type { Context } from "./context"; | ||
|
||
const ConfigSchema = z.object({ | ||
labels: z | ||
.array( | ||
z.object({ | ||
name: z.string(), | ||
color: z.string(), | ||
threshold: z.number().gte(0), | ||
}), | ||
) | ||
.refine( | ||
(labels) => { | ||
const set = new Set(labels.map((label) => label.name)); | ||
return set.size === labels.length; | ||
}, | ||
{ message: "Label name should be unique" }, | ||
) | ||
.transform((labels) => { | ||
// Make sure all labels are ordered in descending order of threshold. | ||
// Size logic depends on this property. | ||
const clone = [...labels]; | ||
clone.sort((a, b) => b.threshold - a.threshold); | ||
return clone; | ||
}), | ||
}); | ||
|
||
export type ConfigType = z.infer<typeof ConfigSchema>; | ||
|
||
const CONFIG_PATH = ".github/prsize.toml"; | ||
|
||
const DEFAULT_CONFIG: ConfigType = { | ||
labels: [ | ||
{ name: "size/x-small", color: "008000", threshold: 0 }, | ||
{ name: "size/small", color: "008000", threshold: 100 }, | ||
{ name: "size/medium", color: "FFFF00", threshold: 200 }, | ||
{ name: "size/large", color: "FF0000", threshold: 500 }, | ||
{ name: "size/x-large", color: "FF0000", threshold: 1000 }, | ||
{ name: "size/large", color: "FF0000", threshold: 500 }, | ||
{ name: "size/medium", color: "FFFF00", threshold: 200 }, | ||
{ name: "size/small", color: "008000", threshold: 100 }, | ||
{ name: "size/x-small", color: "008000", threshold: 0 }, | ||
], | ||
}; | ||
|
||
export async function readConfig(ctx: Context): Promise<ConfigType> { | ||
let content: unknown; | ||
|
||
try { | ||
const resp = await ctx.octo.request( | ||
"GET /repos/{owner}/{repo}/contents/{path}", | ||
{ | ||
owner: ctx.owner, | ||
repo: ctx.repo, | ||
path: CONFIG_PATH, | ||
ref: ctx.pr.base.ref, | ||
mediaType: { | ||
format: "raw", | ||
}, | ||
}, | ||
); | ||
|
||
content = resp.data as unknown; | ||
} catch (error) { | ||
if (error instanceof RequestError && error.status === 404) { | ||
console.info("Config not found. Using default config"); | ||
return DEFAULT_CONFIG; | ||
} | ||
|
||
throw error; | ||
} | ||
|
||
if (typeof content !== "string") { | ||
throw new Error( | ||
`Config path ${CONFIG_PATH} seems to be a directory. Cannot read config content`, | ||
); | ||
} | ||
|
||
const tom = load(content); | ||
return ConfigSchema.parse(tom); | ||
} |
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,21 @@ | ||
import type { Octokit } from "octokit"; | ||
import type { PullRequest } from "@octokit/webhooks-types"; | ||
|
||
export class Context { | ||
public constructor( | ||
public readonly octo: Octokit, | ||
public readonly pr: PullRequest, | ||
) {} | ||
|
||
public get owner() { | ||
return this.pr.base.repo.owner.login; | ||
} | ||
|
||
public get repo() { | ||
return this.pr.base.repo.name; | ||
} | ||
|
||
public get prNum() { | ||
return this.pr.number; | ||
} | ||
} |
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
Oops, something went wrong.