Skip to content

Commit

Permalink
色々と変更 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi authored Nov 6, 2024
1 parent 3c9235f commit 5d791c4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 66 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"devDependencies": {
"@octokit/plugin-paginate-rest": "11.3.5",
"@octokit/plugin-throttling": "9.3.2",
"@octokit/types": "13.6.1",
"@types/node": "22.9.0",
"@types/unzip-stream": "0.3.4",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 60 additions & 55 deletions scripts/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { pipeline } from "node:stream/promises";
import { App, Octokit } from "octokit";
import { config } from "dotenv";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { throttling } from "@octokit/plugin-throttling";
import { Semaphore } from "@core/asyncutil";
import * as logtape from "@logtape/logtape";
import unzip from "unzip-stream";
Expand All @@ -20,7 +21,7 @@ const guestRepo = "voicevox/voicevox";
const commentMarker = "<!-- voiccevox preview-pages info -->";
// ダウンロードしたファイルを展開するディレクトリ
const destinationDir = `${import.meta.dirname}/../public/preview`;
// ビルドチェックの名前
// ビルドチェックのJobの名前
const pagesBuildCheckName = "build_preview_pages";
// ダウンロードするアーティファクトの名前
const artifactName = "preview-pages";
Expand Down Expand Up @@ -72,7 +73,7 @@ const app = new App({
clientId: getEnv("CLIENT_ID"),
clientSecret: getEnv("CLIENT_SECRET"),
},
Octokit: Octokit.plugin(paginateRest),
Octokit: Octokit.plugin(paginateRest, throttling),
});

const appInfo = await app.octokit.request("GET /app");
Expand Down Expand Up @@ -253,59 +254,6 @@ const downloadTargets = await Promise.all(
);
log.info("Done.");

if (source.type === "pullRequest") {
log.info("Fetching comments...");
const comments = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: guestRepoOwner,
repo: guestRepoName,
issue_number: source.pullRequest.number,
},
);
const deployInfoMessage = [
commentMarker,
":rocket: プレビュー用ページを作成しました :rocket:",
"",
`- [:pencil: エディタ](${pagesUrl}/vv-preview-demo-bot/${dirname}/editor)`,
`- [:book: Storybook](${pagesUrl}/vv-preview-demo-bot/${dirname}/storybook)`,
"",
`更新時点でのコミットハッシュ:[\`${source.pullRequest.head.sha.slice(0, 7)}\`](https://github.com/${
source.pullRequest.head.repo.full_name
}/commit/${source.pullRequest.head.sha})`,
].join("\n");
const maybePreviousDeployInfo = comments.find(
(comment) =>
comment.user &&
appInfo.data &&
comment.user.login === `${appInfo.data.slug}[bot]` &&
comment.body?.startsWith(commentMarker),
);
if (!maybePreviousDeployInfo) {
log.info("Adding deploy info...");
await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: guestRepoOwner,
repo: guestRepoName,
issue_number: source.pullRequest.number,
body: deployInfoMessage,
},
);
} else {
log.info("Updating deploy info...");
await octokit.request(
"PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}",
{
owner: guestRepoOwner,
repo: guestRepoName,
comment_id: maybePreviousDeployInfo.id,
body: deployInfoMessage,
},
);
}
}

return { source, dirname };
} catch (e) {
log.error`Failed to process: ${e}`;
Expand All @@ -319,6 +267,63 @@ if (successfulDownloads.length === 0) {
throw new Error("No successful downloads");
}

for (const { dirname, source } of successfulDownloads) {
if (source.type === "branch") {
continue;
}
const log = rootLogger.getChild(`PR #${source.pullRequest.number}`);
log.info("Fetching comments...");
const comments = await octokit.paginate(
"GET /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: guestRepoOwner,
repo: guestRepoName,
issue_number: source.pullRequest.number,
},
);
const deployInfoMessage = [
":rocket: プレビュー用ページを作成しました :rocket:",
"",
`- [:pencil: エディタ](${pagesUrl}/preview/${dirname}/editor)`,
`- [:book: Storybook](${pagesUrl}/preview/${dirname}/storybook)`,
"",
`更新時点でのコミットハッシュ:[\`${source.pullRequest.head.sha.slice(0, 7)}\`](https://github.com/${
source.pullRequest.head.repo.full_name
}/commit/${source.pullRequest.head.sha})`,
commentMarker,
].join("\n");
const maybePreviousDeployInfo = comments.find(
(comment) =>
comment.user &&
appInfo.data &&
comment.user.login === `${appInfo.data.slug}[bot]` &&
comment.body?.endsWith(commentMarker),
);
if (!maybePreviousDeployInfo) {
log.info("Adding deploy info...");
await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
owner: guestRepoOwner,
repo: guestRepoName,
issue_number: source.pullRequest.number,
body: deployInfoMessage,
},
);
} else {
log.info("Updating deploy info...");
await octokit.request(
"PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}",
{
owner: guestRepoOwner,
repo: guestRepoName,
comment_id: maybePreviousDeployInfo.id,
body: deployInfoMessage,
},
);
}
}

await fs.writeFile(
`${destinationDir}/downloads.json`,
JSON.stringify(successfulDownloads, null, 2),
Expand Down
5 changes: 1 addition & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ import { ElButton, ElCard, ElLoading, ElTag } from "element-plus";
import { useDownloadData } from "./composables/useDownloadData.ts";
const downloads = useDownloadData();
const baseUrl = import.meta.env.BASE_URL;
const joinUrl = (path: string) => {
return `${baseUrl}/preview/${path}`.replace(/\/+/g, "/");
};
const joinUrl = (path: string) => `${import.meta.env.BASE_URL}preview/${path}`;
</script>

<style scoped lang="scss">
Expand Down
16 changes: 9 additions & 7 deletions src/composables/useDownloadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export type DownloadData = {

const downloadDataRef = ref<DownloadData[] | null>(null);

void fetch("/preview/downloads.json").then(async (response) => {
if (!response.ok) {
throw new Error(`Failed to fetch downloads.json: ${response.statusText}`);
}
const downloadData = (await response.json()) as DownloadData[];
downloadDataRef.value = downloadData;
});
void fetch(`${import.meta.env.BASE_URL}preview/downloads.json`).then(
async (response) => {
if (!response.ok) {
throw new Error(`Failed to fetch downloads.json: ${response.statusText}`);
}
const downloadData = (await response.json()) as DownloadData[];
downloadDataRef.value = downloadData;
},
);

export function useDownloadData() {
return downloadDataRef;
Expand Down

0 comments on commit 5d791c4

Please sign in to comment.