Skip to content

Commit

Permalink
feat(#49): blob path
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Mar 19, 2024
1 parent 1e6b007 commit 7cf009a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
51 changes: 51 additions & 0 deletions src/blob-path.ts
Original file line number Diff line number Diff line change
@@ -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<string | undefined> {

/**
* 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;
}
}
28 changes: 9 additions & 19 deletions src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,33 @@ export class Blob implements Scalar<Promise<string[]>> {
* 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<string | undefined>
) {
}

/**
* As text.
*/
async value(): Promise<string[]> {
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}`);
}
}
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
7 changes: 3 additions & 4 deletions src/pdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down

0 comments on commit 7cf009a

Please sign in to comment.