-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (40 loc) · 1.67 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
48
49
const fetch = require('node-fetch');
const core = require('@actions/core');
const octocore = require("@octokit/core")
const repo = (repoName) => {
if (repoName.includes('/')) {
return repoName.split('/')[1]
}
else return repoName;
};
const pokemon = core.getInput('POKEMON');
const repo_owner = core.getInput('REPOSITORY_OWNER')
const gh_token = core.getInput('GH_TOKEN');
const commit_message = core.getInput('COMMIT_MESSAGE');
const octocore_client = new octocore.Octokit({auth: gh_token})
const repo_name = repo(core.getInput('REPOSITORY'));
console.log(pokemon)
function getReadme(){
return octocore_client.request(`GET /repos/${repo_owner}/${repo_name}/contents/README.md`)
}
function updateReadme(spriteMarkdown){
getReadme().then(({ data }) => {
const rawContent = Buffer.from(data.content, data.encoding).toString();
const startIndex = rawContent.indexOf("<!--Pokemon Sprite-->")
const updatedContent = `${startIndex === -1 ? rawContent : rawContent.slice(0, startIndex)}\n${spriteMarkdown}`
octocore_client.request(`PUT /repos/${repo_owner}/${repo_name}/contents/README.md`, {
message: commit_message,
content: Buffer.from(updatedContent, "utf-8").toString(data.encoding),
path: "README.md",
sha: data.sha,
})
})
}
fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`)
.then((response) => response.json())
.then((data) => {
const spriteUrl = data.sprites.front_default;
const spriteMarkdown = `<!--Pokemon Sprite-->\n![image](${spriteUrl})`;
console.log(spriteMarkdown);
updateReadme(spriteMarkdown);
});