-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (43 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import fetch from "node-fetch";
import { getInput, setFailed } from "@actions/core";
import { context } from "@actions/github";
import { Octokit } from "@octokit/core";
async function run() {
try {
const githubToken = getInput("github-token");
const octokit = new Octokit({ auth: githubToken });
fetch("https://db.ygoprodeck.com/api/v7/cardinfo.php?num=1&offset=0&sort=random&cachebust")
.then((response) => {
if(!response.ok){
console.error('response.ok:', response.ok);
console.error('esponse.status:', response.status);
console.error('esponse.statusText:', response.statusText);
throw new Error("Failed to fetch random card");
}
return response.json();
})
.then((data) => {
if (!(('data' in data) && Array.isArray(data["data"]) && data["data"].length > 0)) {
throw new Error("Failed to get card");
}
const card = data["data"][0];
const cardName = card.name;
if (!(('card_images' in card) && Array.isArray(card.card_images) && card.card_images.length > 0)) {
throw new Error("Failed to get card images");
}
const imageUrl = card.card_images[0].image_url;
octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
{
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `_**Draw "${cardName}" !**_\n\n![${cardName}](${imageUrl})`,
}
);
});
} catch (error) {
setFailed(error.message);
}
}
run();