Repository navigation
Use user GitHub OAuth token for repochecker API calls #203
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2e2a8be
Initial plan
Copilot dfbff16
fix: forward github auth to repochecker endpoint
Copilot 24977d9
fix: run repochecker with request token
Copilot 0c3dec3
fix: sanitize repochecker auth handling
Copilot afd7f98
fix: hide repochecker backend errors
Copilot 7796961
fix: use repochecker 5.21.0 githubToken parameter
Copilot c2e63a2
fix: prevent overlapping repochecker token use
Copilot 07f5920
Merge remote-tracking branch 'origin/main' into copilot/fix-403-error…
Copilot a277f3b
Update comment for Axios instance usage
Apollon77 07d15a1
Read repochecker GitHub token from session cookie and allow parallel …
claude faa53ab
Bound the repochecker queue and reject excess checks with 429
claude b64210d
Require a logged-in user for repochecker requests
claude 8673c1a
Run one repochecker check at a time until upstream isolates credentials
claude 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,25 +1,84 @@ | ||
| import { handler } from "@iobroker/repochecker"; | ||
| import { CheckRequest, CheckResult, handler } from "@iobroker/repochecker"; | ||
| import { Router } from "express"; | ||
| import Cookies from "universal-cookie"; | ||
| import cookiesMiddleware from "universal-cookie-express"; | ||
| import { COOKIE_NAME_PORTAL_TOKEN } from "../auth"; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get("/api/repochecker/", async function (req, res) { | ||
| handler( | ||
| { | ||
| queryStringParameters: { | ||
| url: req.query.url as string, | ||
| branch: req.query.branch as string | undefined, | ||
| }, | ||
| }, | ||
| null, | ||
| (error, result) => { | ||
| // Each check performs many HTTP requests, so only a few may run at once; | ||
| // additional checks wait in a queue until a slot becomes free. | ||
| const MAX_PARALLEL_CHECKS = 3; | ||
| let runningChecks = 0; | ||
| const waitingChecks: (() => void)[] = []; | ||
|
|
||
| // Only one check per user (GitHub token) may be running or queued at a time. | ||
| const activeTokens = new Set<string>(); | ||
|
|
||
| async function withCheckSlot<T>(fn: () => Promise<T>): Promise<T> { | ||
| if (runningChecks >= MAX_PARALLEL_CHECKS) { | ||
| await new Promise<void>((resolve) => waitingChecks.push(resolve)); | ||
|
Apollon77 marked this conversation as resolved.
|
||
| } else { | ||
| runningChecks++; | ||
| } | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| const next = waitingChecks.shift(); | ||
| if (next) { | ||
| // hand the slot over directly to the next waiting check | ||
| next(); | ||
| } else { | ||
| runningChecks--; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function runRepochecker(request: CheckRequest): Promise<CheckResult> { | ||
| return new Promise((resolve, reject) => { | ||
| handler(request, null, (error, result) => { | ||
|
Comment on lines
+50
to
+52
|
||
| if (error || !result) { | ||
| res.status(500).send(error); | ||
| reject(error || new Error("Repochecker returned no result")); | ||
| } else { | ||
| res.status(result.statusCode).send(result.body); | ||
| resolve(result); | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| router.get("/api/repochecker/", cookiesMiddleware(), async function (req, res) { | ||
| const cookies = (req as any)["universalCookies"] as Cookies; | ||
| const ghToken: string | undefined = | ||
| cookies.get(COOKIE_NAME_PORTAL_TOKEN) || undefined; | ||
|
|
||
| if (ghToken) { | ||
| if (activeTokens.has(ghToken)) { | ||
| res.status(429).send( | ||
| "A repository check is already running for your account; please try again later", | ||
| ); | ||
| return; | ||
| } | ||
| activeTokens.add(ghToken); | ||
| } | ||
| try { | ||
| const result = await withCheckSlot(() => | ||
| runRepochecker({ | ||
| queryStringParameters: { | ||
| url: req.query.url as string, | ||
| branch: req.query.branch as string | undefined, | ||
| githubToken: ghToken, | ||
| }, | ||
| }), | ||
| ); | ||
| res.status(result.statusCode).send(result.body); | ||
| } catch { | ||
| console.error("Repochecker request failed"); | ||
| res.status(500).send("Repochecker request failed"); | ||
| } finally { | ||
| if (ghToken) { | ||
| activeTokens.delete(ghToken); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| export default router; | ||
This file contains hidden or 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,136 @@ | ||
| const assert = require("node:assert/strict"); | ||
| const { test } = require("node:test"); | ||
| const repochecker = require("@iobroker/repochecker"); | ||
| const { COOKIE_NAME_PORTAL_TOKEN } = require("../src/auth"); | ||
| const router = require("../src/api/repochecker").default; | ||
| const routeStack = router.stack[0].route.stack; | ||
| const route = routeStack[routeStack.length - 1].handle; | ||
|
|
||
| const url = "https://github.com/example/ioBroker.test"; | ||
|
|
||
| function request(token, query = { url }) { | ||
| return { | ||
| query, | ||
| universalCookies: { | ||
| get: (name) => (name === COOKIE_NAME_PORTAL_TOKEN ? token : undefined), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function response() { | ||
| return { | ||
| status(code) { | ||
| this.statusCode = code; | ||
| return this; | ||
| }, | ||
| send(body) { | ||
| this.body = body; | ||
| return this; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const tick = () => new Promise((resolve) => setImmediate(resolve)); | ||
|
|
||
| test("passes the cookie token to repochecker without changing the environment", async (t) => { | ||
| const environment = { ...process.env }; | ||
| let parameters; | ||
| t.mock.method(repochecker, "handler", (request, ctx, callback) => { | ||
| parameters = request.queryStringParameters; | ||
| assert.equal(ctx, null); | ||
| callback(null, { statusCode: 201, body: "checked" }); | ||
| }); | ||
|
|
||
| for (const token of ["test-token", undefined, ""]) { | ||
| const res = response(); | ||
| await route(request(token, { url, branch: "main" }), res); | ||
| assert.deepEqual(parameters, { | ||
| url, | ||
| branch: "main", | ||
| githubToken: token || undefined, | ||
| }); | ||
| assert.equal(res.statusCode, 201); | ||
| assert.equal(res.body, "checked"); | ||
| } | ||
| assert.deepEqual({ ...process.env }, environment); | ||
| }); | ||
|
|
||
| test("allows only one check per user at a time", async (t) => { | ||
| const pending = []; | ||
| t.mock.method(repochecker, "handler", (request, ctx, callback) => { | ||
| pending.push({ request, callback }); | ||
| }); | ||
| const first = response(); | ||
| const firstCheck = route(request("user-token"), first); | ||
| await tick(); | ||
|
|
||
| const second = response(); | ||
| await route(request("user-token"), second); | ||
| assert.equal(second.statusCode, 429); | ||
| assert.equal(pending.length, 1); | ||
|
|
||
| const other = response(); | ||
| const otherCheck = route(request("other-token"), other); | ||
| await tick(); | ||
| assert.equal(pending.length, 2); | ||
| assert.equal(pending[1].request.queryStringParameters.githubToken, "other-token"); | ||
|
|
||
| pending[0].callback(null, { statusCode: 200, body: "first" }); | ||
| pending[1].callback(null, { statusCode: 200, body: "other" }); | ||
| await Promise.all([firstCheck, otherCheck]); | ||
| assert.equal(first.body, "first"); | ||
| assert.equal(other.body, "other"); | ||
|
|
||
| const retry = response(); | ||
| const retryCheck = route(request("user-token"), retry); | ||
| await tick(); | ||
| pending[2].callback(null, { statusCode: 200, body: "retry" }); | ||
| await retryCheck; | ||
| assert.equal(retry.statusCode, 200); | ||
| }); | ||
|
|
||
| test("queues checks beyond the parallel limit", async (t) => { | ||
| const pending = []; | ||
| t.mock.method(repochecker, "handler", (request, ctx, callback) => { | ||
| pending.push({ request, callback }); | ||
| }); | ||
| const tokens = ["a", "b", "c", "d"]; | ||
| const responses = tokens.map(() => response()); | ||
| const checks = tokens.map((token, i) => route(request(token), responses[i])); | ||
| await tick(); | ||
| assert.equal(pending.length, 3); | ||
|
|
||
| pending[0].callback(null, { statusCode: 200, body: "a" }); | ||
| await tick(); | ||
| assert.equal(pending.length, 4); | ||
| assert.equal(pending[3].request.queryStringParameters.githubToken, "d"); | ||
|
|
||
| for (const p of pending.slice(1)) { | ||
| p.callback(null, { statusCode: 200, body: p.request.queryStringParameters.githubToken }); | ||
| } | ||
| await Promise.all(checks); | ||
| assert.deepEqual(responses.map((r) => r.body), tokens); | ||
| }); | ||
|
|
||
| test("does not expose errors or tokens in responses or logs", async (t) => { | ||
| const log = t.mock.method(console, "error", () => {}); | ||
| const error = new Error("test-token"); | ||
| const handler = t.mock.method(repochecker, "handler"); | ||
| for (const implementation of [ | ||
| (request, ctx, callback) => callback(error), | ||
| (request, ctx, callback) => callback(null), | ||
| () => { | ||
| throw error; | ||
| }, | ||
| ]) { | ||
| handler.mock.mockImplementation(implementation); | ||
| const res = response(); | ||
| await route(request("test-token"), res); | ||
| assert.equal(res.statusCode, 500); | ||
| assert.equal(res.body, "Repochecker request failed"); | ||
| } | ||
| assert.equal(log.mock.calls.length, 3); | ||
| for (const call of log.mock.calls) { | ||
| assert.deepEqual(call.arguments, ["Repochecker request failed"]); | ||
| } | ||
| }); |
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.