Compare commits

..

4 Commits

11 changed files with 479 additions and 16 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
/node_modules
/dist
config.toml

3
REAME.md Normal file
View File

@@ -0,0 +1,3 @@
github fine-grained permissions required: `metadata`, `content`

View File

@@ -1,9 +1,8 @@
{
"name": "issue-scheduler",
"description": "Schedule issues in Gitea or Github from issue templates",
"description": "Schedule issues in Gitea or GitHub using issue templates",
"version": "0.0.0",
"main": "dist/index.js",
"author": "Luís Figueiredo (TxFig)",
"homepage": "https://gitea.alluna.pt/TxFig/issue-scheduler",
"repository": {
@@ -12,15 +11,18 @@
},
"license": "ISC",
"keywords": [],
"scripts": {
"test": "vitest --run",
"build": "tsc",
"start": "node dist/main.js"
},
"devDependencies": {
"@types/node": "^24.9.1",
"typescript": "^5.9.3",
"vitest": "^3.2.4"
},
"type": "module"
"type": "module",
"dependencies": {
"toml": "^3.0.0"
}
}

47
pnpm-lock.yaml generated
View File

@@ -7,13 +7,20 @@ settings:
importers:
.:
dependencies:
toml:
specifier: ^3.0.0
version: 3.0.0
devDependencies:
'@types/node':
specifier: ^24.9.1
version: 24.9.1
typescript:
specifier: ^5.9.3
version: 5.9.3
vitest:
specifier: ^3.2.4
version: 3.2.4
version: 3.2.4(@types/node@24.9.1)
packages:
@@ -295,6 +302,9 @@ packages:
'@types/estree@1.0.8':
resolution: {integrity: sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==}
'@types/node@24.9.1':
resolution: {integrity: sha512-QoiaXANRkSXK6p0Duvt56W208du4P9Uye9hWLWgGMDTEoKPhuenzNcC4vGUmrNkiOKTlIrBoyNQYNpSwfEZXSg==}
'@vitest/expect@3.2.4':
resolution: {integrity: sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==}
@@ -460,11 +470,17 @@ packages:
resolution: {integrity: sha512-azl+t0z7pw/z958Gy9svOTuzqIk6xq+NSheJzn5MMWtWTFywIacg2wUlzKFGtt3cthx0r2SxMK0yzJOR0IES7Q==}
engines: {node: '>=14.0.0'}
toml@3.0.0:
resolution: {integrity: sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==}
typescript@5.9.3:
resolution: {integrity: sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==}
engines: {node: '>=14.17'}
hasBin: true
undici-types@7.16.0:
resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}
vite-node@3.2.4:
resolution: {integrity: sha512-EbKSKh+bh1E1IFxeO0pg1n4dvoOTt0UDiXMd/qn++r98+jPO1xtJilvXldeuQ8giIB5IkpjCgMleHMNEsGH6pg==}
engines: {node: ^18.0.0 || ^20.0.0 || >=22.0.0}
@@ -699,6 +715,10 @@ snapshots:
'@types/estree@1.0.8': {}
'@types/node@24.9.1':
dependencies:
undici-types: 7.16.0
'@vitest/expect@3.2.4':
dependencies:
'@types/chai': 5.2.2
@@ -707,13 +727,13 @@ snapshots:
chai: 5.3.3
tinyrainbow: 2.0.0
'@vitest/mocker@3.2.4(vite@7.1.9)':
'@vitest/mocker@3.2.4(vite@7.1.9(@types/node@24.9.1))':
dependencies:
'@vitest/spy': 3.2.4
estree-walker: 3.0.3
magic-string: 0.30.19
optionalDependencies:
vite: 7.1.9
vite: 7.1.9(@types/node@24.9.1)
'@vitest/pretty-format@3.2.4':
dependencies:
@@ -886,15 +906,19 @@ snapshots:
tinyspy@4.0.4: {}
toml@3.0.0: {}
typescript@5.9.3: {}
vite-node@3.2.4:
undici-types@7.16.0: {}
vite-node@3.2.4(@types/node@24.9.1):
dependencies:
cac: 6.7.14
debug: 4.4.3
es-module-lexer: 1.7.0
pathe: 2.0.3
vite: 7.1.9
vite: 7.1.9(@types/node@24.9.1)
transitivePeerDependencies:
- '@types/node'
- jiti
@@ -909,7 +933,7 @@ snapshots:
- tsx
- yaml
vite@7.1.9:
vite@7.1.9(@types/node@24.9.1):
dependencies:
esbuild: 0.25.10
fdir: 6.5.0(picomatch@4.0.3)
@@ -918,13 +942,14 @@ snapshots:
rollup: 4.52.4
tinyglobby: 0.2.15
optionalDependencies:
'@types/node': 24.9.1
fsevents: 2.3.3
vitest@3.2.4:
vitest@3.2.4(@types/node@24.9.1):
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.4
'@vitest/mocker': 3.2.4(vite@7.1.9)
'@vitest/mocker': 3.2.4(vite@7.1.9(@types/node@24.9.1))
'@vitest/pretty-format': 3.2.4
'@vitest/runner': 3.2.4
'@vitest/snapshot': 3.2.4
@@ -942,9 +967,11 @@ snapshots:
tinyglobby: 0.2.15
tinypool: 1.1.1
tinyrainbow: 2.0.0
vite: 7.1.9
vite-node: 3.2.4
vite: 7.1.9(@types/node@24.9.1)
vite-node: 3.2.4(@types/node@24.9.1)
why-is-node-running: 2.3.0
optionalDependencies:
'@types/node': 24.9.1
transitivePeerDependencies:
- jiti
- less

69
src/apis/gitea.ts Normal file
View File

@@ -0,0 +1,69 @@
import { URL } from "url"
import { BaseApi } from "./index.js"
import type { Result } from "../result.js";
import * as result from "../result.js"
// /api/swagger
const Prefix = "/api/v1"
const urls = {
IssueTemplatesUrl: (user: string, repo: string) =>
`${Prefix}/repos/${user}/${repo}/issue_templates`,
FileContentUrl: (user: string, repo: string, filepath: string) =>
`${Prefix}/repos/${user}/${repo}/contents/${filepath}`
}
namespace GiteaApi {
export type IssueTemplatesResponse = {
name: string,
title: string,
about: string,
labels: string[] | null,
assignees: string[] | null,
ref: string,
content: string,
body: unknown | null,
file_name: string
}[]
}
class Gitea extends BaseApi {
headers: Record<string, string>
constructor(...options: ConstructorParameters<typeof BaseApi>) {
super(...options)
this.headers = {
"Authorization": `token ${this.token}`
}
}
async getIssueTemplates(): Promise<Result<string[], string>> {
const url = new URL(
urls.IssueTemplatesUrl(this.user, this.repo),
this.host)
const response = await fetch(url, { headers: this.headers })
switch (response.status) {
case 200: break
case 404: return result.error("Issue templates not found")
default: return result.error("Unknown status code")
}
const json: GiteaApi.IssueTemplatesResponse = await response.json()
return result.value(
json.map(template => template.file_name)
)
}
async getFileContent(filepath: string): Promise<Result<string[], string>> {
const url = new URL(
urls.FileContentUrl(this.user, this.repo, filepath),
this.host)
}
}
export default Gitea

124
src/apis/github.ts Normal file
View File

@@ -0,0 +1,124 @@
import { BaseApi } from "./index.js"
import type { Result } from "../result.js"
import * as result from "../result.js"
// docs.github.com/en/rest
const IssueTemplateFolders = [
"ISSUE_TEMPLATE",
"issue_template",
".gitea/ISSUE_TEMPLATE",
".gitea/issue_template",
".github/ISSUE_TEMPLATE",
".github/issue_template"
]
const Prefix = "https://api.github.com"
const urls = {
// https://docs.github.com/en/rest/repos/repos#get-a-repository
RepoInfoUrl: (user: string, repo: string) =>
`${Prefix}/repos/${user}/${repo}`,
// https://docs.github.com/en/rest/git/trees#get-a-tree
TreeUrl: (user: string, repo: string, branch: string) =>
`${Prefix}/repos/${user}/${repo}/git/trees/${branch}?recursive=1`
}
namespace GitHubApi {
export interface RepoInfoResponse {
default_branch: string
// ...
}
export interface TreeResponse {
sha: string
url: string
tree: {
path: string
mode: string
type: "tree" | "blob"
sha: string
size: number
url: string
}[]
}
}
class GitHub extends BaseApi {
headers: Record<string, string>
constructor(...options: ConstructorParameters<typeof BaseApi>) {
super(...options)
this.headers = {
"Authorization": `token ${this.token}`
}
}
private async getDefaultBranch(): Promise<Result<string, string>> {
const url = urls.RepoInfoUrl(this.user, this.repo)
const response = await fetch(url, { headers: this.headers })
switch (response.status) {
case 301: return result.error("Moved permanently")
case 403: return result.error("Forbidden")
case 404: return result.error("Resource not found")
case 200: break
default: return result.error("Unknown status code")
}
const json: GitHubApi.RepoInfoResponse = await response.json()
return result.value(json.default_branch)
}
private async getRepoFiles(): Promise<Result<string[], string>> {
const branch = await this.getDefaultBranch()
if (!branch.success) return result.error(`Default branch error: ${branch.error}`)
const url = urls.TreeUrl(this.user, this.repo, branch.value)
let response: Response
try {
response = await fetch(url, { headers: this.headers })
} catch (err) {
return result.error(`Fetch Error: ${err}`)
}
switch (response.status) {
case 200: break
case 404: return result.error("Resource not found")
case 409: return result.error("Conflict")
case 422: return result.error("Validation failed, or the endpoint has been spammed")
default: return result.error("Unknown status code")
}
let json: GitHubApi.TreeResponse
try {
json = await response.json()
} catch (error) {
return result.error(`Json Error: ${error}`)
}
return result.value(json.tree
.filter(entry => entry.type == "blob")
.map(entry => entry.path)
)
}
async getIssueTemplates(): Promise<Result<string[], string>> {
const files = await this.getRepoFiles()
if (!files.success) return result.error(`Repo files error: ${files.error}`)
const issue_templates = files.value.filter(
file => IssueTemplateFolders.some(folder => file.startsWith(folder))
)
return result.value(issue_templates)
}
async getFileContent(filepath: string): Promise<Result<string[], string>> {
}
}
export default GitHub

17
src/apis/index.ts Normal file
View File

@@ -0,0 +1,17 @@
import type { Result } from "../result.js"
export abstract class BaseApi {
constructor(
readonly host: string,
readonly token: string,
readonly user: string,
readonly repo: string
) {}
/**
* @returns Array of filepath's
*/
abstract getIssueTemplates(): Promise<Result<string[], string>>
abstract getFileContent(filepath: string): Promise<Result<string, string>>
}

View File

@@ -8,6 +8,10 @@ export interface CLIConfig {
// doesn't log to stdout/stderr (still logs to file if --log-file)
quiet: boolean
}
// TODO: Git client shouldn't be identified from the config,
// but from the host url or a custom config property
// TODO: add --list-issue-templates <user> <repo> <token> : list results from api.getIssueTemplates
// TODO: add other utility commands
export const defaults: CLIConfig = {
config_filepath: "config.toml",
@@ -97,14 +101,37 @@ export function optionalBooleanFlag(args: string[], flags: string[]): boolean {
return value === null;
}
const Usage = (program_name: string) => `\
Usage: ${program_name} [options...]
Issue Scheduler - Schedule issues in Gitea or GitHub using issue templates
Options:
-h, --help Display this message
-c, --config <filepath> Config file [default: "config.toml"]
-l, --log-file <filepath> Logs output to file
-v, --verbose Verbose logging
-q, --quiet Only logs to --log-file (if provided)
`
/**
* Parse command line arguments from a string array (process.argv)
* @param args Command line arguments
* @return {CLIConfig} Parsed flags
*/
export function parseArgs(args: string[]): CLIConfig {
if (args.length < 2) { // [node executable, program path, ...]
// TODO: log error
process.exit(1)
}
const config = defaults
const helpValue = optionalBooleanFlag(args, ["--help", "-h"])
if (helpValue) {
console.log(Usage(args[1]!))
process.exit(0)
}
const configValue = optionalStringFlag(args, ["--config", "-c"])
if (configValue) config.config_filepath = configValue

91
src/config.ts Normal file
View File

@@ -0,0 +1,91 @@
import fs from "node:fs/promises"
import * as toml from "toml"
import type { Result } from "./result.js"
import * as result from "./result.js"
export interface Repository {
url: URL
token: string
client?: "gitea" | "github"
}
function isValidUrl(url: string) {
try {
new URL(url)
return true
} catch {
return false
}
}
function validateUrl(url: unknown): Result<undefined, string> {
if (!url)
return result.error("Missing url")
if (typeof url !== "string")
return result.error("Url must be a string")
if (!isValidUrl(url))
return result.error("Invalid URL")
return result.value(undefined)
}
function validateToken(token: unknown): Result<undefined, string> {
if (!token)
return result.error("Missing token")
if (typeof token !== "string")
return result.error("Token must be a string")
return result.value(undefined)
}
function validateClient(client: unknown): Result<undefined, string> {
if (!client)
return result.value(undefined)
if (typeof client !== "string")
return result.error("Client must be a string")
if (!["gitea", "github"].includes(client)) {
return result.error("Client must be \"gitea\" or \"github\"")
}
return result.value(undefined)
}
export function validateConfig(config: Record<string, any>): Result<Repository[], string> {
if (!("repo" in config) ||
!Array.isArray(config["repo"])
) return result.error("Missing repo array of tables")
for (let i = 0; i < config["repo"].length; i++) {
const repo = config["repo"][i]
const url = validateUrl(repo["url"])
if (!url.success)
return result.error(`Invalid Config (Repo#${i + 1}): ${url.error}`)
repo["url"] = new URL(repo["url"])
const token = validateToken(repo["token"])
if (!token.success)
return result.error(`Invalid Config (Repo#${i + 1}): ${token.error}`)
const client = validateClient(repo["client"])
if (!client.success)
return result.error(`Invalid Config (Repo#${i + 1}): ${client.error}`)
}
return result.value(config["repo"])
}
export async function getConfig(config_filepath: string): Promise<Result<Repository[], string>> {
let configFile: string
try {
configFile = await fs.readFile(config_filepath, "utf8")
} catch (err) {
return result.error(`Failed to open config file: "${config_filepath}"`)
}
let config: Record<string, any>
try {
config = toml.parse(configFile)
} catch (err) {
return result.error(`Failed to parse config file. ${err}`)
}
return validateConfig(config)
}

View File

@@ -1,5 +1,90 @@
import * as cli from "./cli.js"
import { getConfig, type Repository } from "./config.js"
import type { Result } from "./result.js"
import * as result from "./result.js"
import type { BaseApi } from "./apis";
import Gitea from "./apis/gitea.js"
import GitHub from "./apis/github.js";
const config = cli.parseArgs(process.argv)
console.log(config)
function identifyGitClient(repo: Repository): "gitea" | "github" {
if (repo.client)
return repo.client
if (repo.url.host === "github.com")
return "github"
return "gitea"
}
function instantiateApi(repo: Repository): Result<InstanceType<typeof BaseApi>, string> {
const client = identifyGitClient(repo)
const host = repo.url.origin
const token = repo.token
const path_parts = repo.url.pathname
.split("/")
.filter(part => part && part != "/")
if (path_parts.length != 2)
return result.error("Invalid repo url")
const [user, repo_name] = path_parts as [string, string]
const ApiClass = client === "gitea" ? Gitea : GitHub
return result.value(
new ApiClass(host, token, user, repo_name)
)
}
async function handleTemplate(template_file: string): Promise<Result<undefined, string>> {
return result.value(undefined)
}
async function handleRepo(repo: Repository): Promise<Result<undefined, string>> {
const api = instantiateApi(repo)
if (!api.success)
return result.error(`instantiateApi: ${api.error}`)
const templates = await api.value.getIssueTemplates()
if (!templates.success)
return result.error(`getIssueTemplates: ${templates.error}`)
for (const template of templates.value) {
const result = await handleTemplate(template)
console.log(result)
}
return result.value(undefined)
}
async function main() {
const cliConfig = cli.parseArgs(process.argv)
const config = await getConfig(cliConfig.config_filepath)
if (!config.success) {
console.error(config.error)
process.exit(1)
}
const promise_results = await Promise.allSettled(
config.value.map(repo => handleRepo(repo))
)
for (const p_result of promise_results) {
if (p_result.status === "rejected") {
console.error(p_result.reason)
continue
}
if (!p_result.value.success) {
console.error(p_result.value.error)
continue
}
console.log(p_result.value.value)
}
}
await main()

16
src/result.ts Normal file
View File

@@ -0,0 +1,16 @@
export type Result<TValue, TError> = {
value: TValue,
success: true
} | {
error: TError,
success: false
}
export function value<TValue, TError>(val: TValue): Result<TValue, TError> {
return { value: val, success: true }
}
export function error<TValue, TError>(val: TError): Result<TValue, TError> {
return { error: val, success: false }
}