diff --git a/src/blob-path.ts b/src/blob-path.ts new file mode 100644 index 0000000..2a64d03 --- /dev/null +++ b/src/blob-path.ts @@ -0,0 +1,51 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023-2024 Tracehub.git + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Path. + */ +export class BlobPath implements Scalar { + + /** + * Ctor. + * @param body Body + */ + constructor(private readonly body: string | undefined) { + } + + value(): string | undefined { + const pattern = /https:\/\/github\.com\/[^/]+\/[^/]+\/blob\/[^/]+\/(.+)/; + const match = this.body?.match(pattern); + let path; + if (match) { + const full = match[1]; + path = full.split("#")[0]; + } else { + throw new Error( + `Asset body ${this.body} does not contain puzzle blob, regex: ${pattern}` + ); + } + return path; + } +} diff --git a/src/blob.ts b/src/blob.ts index ee63e8d..cd95b85 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -33,12 +33,12 @@ export class Blob implements Scalar> { * Ctor. * @param github GitHub * @param issue Issue - * @param body Report body + * @param path Path */ constructor( private readonly github: Octokit, private readonly issue: Issue, - private readonly body: string | undefined + private readonly path: Scalar ) { } @@ -46,30 +46,20 @@ export class Blob implements Scalar> { * As text. */ async value(): Promise { - const {data} = await this.github.repos.get( - { - owner: this.issue.owner, - repo: this.issue.repo - } - ); - const pattern = /https:\/\/github\.com\/[^/]+\/[^/]+\/blob\/[^/]+\/(.+)/; - const match = this.body?.match(pattern); - if (match) { - const full = match[1]; - const path = full.split("#")[0]; - - + const {data} = await this.github.repos.get( + { + owner: this.issue.owner, + repo: this.issue.repo + } + ); const response = await this.github.repos.getContent({ owner: this.issue.owner, repo: this.issue.repo, ref: data.default_branch, - path: path + path: String(this.path.value()) }); const encoded = JSON.parse(JSON.stringify(response.data)).content; const decoded = Base64.decode(encoded); return decoded.split('\n'); - } else { - throw new Error(`Asset ${this.body} does not contain puzzle blob, regex ${pattern}`); - } } } diff --git a/src/main.ts b/src/main.ts index 472a1a6..ff9446d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -100,6 +100,7 @@ async function run() { throw new Error(reason); } if (new Puzzled(body).value()) { + console.log(`Puzzle found: ${body}`) await new Pdd(octokit, issue, body).run(); } const openai = core.getInput("openai_token"); diff --git a/src/pdd.ts b/src/pdd.ts index ca2c03d..fd039e4 100644 --- a/src/pdd.ts +++ b/src/pdd.ts @@ -24,7 +24,7 @@ import {Ranged} from "./ranged"; import {Blob} from "./blob"; import {Octokit} from "@octokit/rest"; -import {blob} from "node:stream/consumers"; +import {BlobPath} from "./blob-path"; /** * PDD routine. @@ -49,9 +49,8 @@ export class Pdd { */ async run() { const puzzle = await new Ranged( - new Blob(this.github, this.issue, this.body), - "1-5" // from parsed link - // /https://github.com/tracehubpm/tracehub/blob/8d2aca048e33a5c9d83a49af4246c9ad7fde9998/src/main/java/git/tracehub/tk/TkGitHub.java#L150-L156 + new Blob(this.github, this.issue, new BlobPath(this.body)), + "1-5" ).value(); console.log(puzzle); }