-
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.
/probot/stats and check repo endpoints
- Loading branch information
Showing
4 changed files
with
101 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,23 @@ | ||
import type { Request, Response } from "express"; | ||
import type { Probot } from "probot"; | ||
import express from "express"; | ||
import getProbotStats from "@/src/router/probot-stats.ts"; | ||
import getRepoHandlers from "@/src/router/repo-handler.ts"; | ||
|
||
const createRouter = (app: Probot) => { | ||
const router = express.Router(); | ||
|
||
router.get("/ping", (_req: Request, res: Response) => { | ||
res.json({ status: "pong" }); | ||
}); | ||
|
||
router.get("/probot/stats", getProbotStats); | ||
|
||
const { checkHandler } = getRepoHandlers(app); | ||
router.get("/check/:owner/:repo", checkHandler); | ||
// TODO Add process route | ||
|
||
return router; | ||
}; | ||
|
||
export default createRouter; |
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,11 @@ | ||
import type { Request, Response } from "express"; | ||
|
||
async function getProbotStats(_req: Request, res: Response) { | ||
const response = await fetch( | ||
"https://raw.githack.com/pull-app/stats/master/stats.json", | ||
); | ||
const data = await response.json(); | ||
res.json(data); | ||
} | ||
|
||
export default getProbotStats; |
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,65 @@ | ||
import type { Request, Response } from "express"; | ||
import type { Probot } from "probot"; | ||
import { appConfig } from "@/src/configs/app-config.ts"; | ||
import { getPullConfig } from "@/src/utils/get-pull-config.ts"; | ||
import { JobPriority, RepositoryModel } from "@wei/probot-scheduler"; | ||
|
||
function getRepoHandlers(app: Probot) { | ||
async function checkHandler(req: Request, res: Response) { | ||
const full_name = `${req.params.owner}/${req.params.repo}`; | ||
app.log.info(`[${full_name}] Checking ${appConfig.configFilename}`); | ||
|
||
try { | ||
// Get Octokit | ||
const repoRecord = await RepositoryModel.findOne({ full_name }); | ||
|
||
if (!repoRecord) { | ||
app.log.error({ full_name }, `❌ Repo record not found`); | ||
throw new Error(`❌ Repo record not found`); | ||
} | ||
|
||
const { | ||
installation_id, | ||
id: repository_id, | ||
owner: { login: owner }, | ||
name: repo, | ||
} = repoRecord; | ||
|
||
const octokit = await app.auth(installation_id); | ||
const config = await getPullConfig(octokit, app.log, { | ||
installation_id, | ||
owner, | ||
repo, | ||
repository_id, | ||
metadata: { | ||
cron: "", | ||
job_priority: JobPriority.Normal, | ||
repository_id, | ||
}, | ||
}); | ||
|
||
if (!config) { | ||
return res.status(404).json({ | ||
status: "error", | ||
message: `Configuration file '${appConfig.configFilename}' not found`, | ||
}); | ||
} | ||
|
||
res.json(config); | ||
} catch (error) { | ||
app.log.error(error); | ||
res.status(500).json({ | ||
status: "error", | ||
message: error instanceof Error | ||
? error.message | ||
: "Unknown error occurred", | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
checkHandler, | ||
}; | ||
} | ||
|
||
export default getRepoHandlers; |