Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.

Commit 586033f

Browse files
committed
リファクタ
1 parent 3eaf8ef commit 586033f

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ inputs:
66
required: false
77
description: 'The version of Qiita CLI to install.'
88
default: 'latest'
9+
github-token:
10+
required: false
11+
description: 'Used to pull Qiita CLI from Github'
12+
default: ${{ github.token }}
913

1014
runs:
1115
using: 'node16'

src/main.js

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,69 @@
11
const core = require("@actions/core");
2+
const github = require("@actions/github");
23
const tc = require("@actions/tool-cache");
34

5+
const owner = "koki-develop";
6+
const repo = "qiita-cli";
7+
48
(async () => {
5-
const version = (() => {
6-
const version = core.getInput("version");
7-
if (!version.startsWith("v")) {
8-
return `v${version}`;
9+
const version = await _getVersion(core.getInput("version"));
10+
const cliPath = await _download(version);
11+
await _install(cliPath);
12+
})().catch((err) => {
13+
core.setFailed(err.message);
14+
});
15+
16+
/**
17+
* @param {String} path
18+
* @returns {Promise<String>}
19+
*/
20+
const _install = async (path) => {
21+
core.info("Installing...");
22+
23+
const extractedPath = await (async () => {
24+
switch (process.platform) {
25+
case "win32":
26+
return tc.extractZip(path);
27+
case "tar.gz":
28+
return tc.extractTar(path);
929
}
1030
})();
31+
const binPath = await tc.cacheDir(extractedPath, "qiita", version);
32+
core.addPath(binPath);
33+
34+
core.info(`Installed to ${binPath}`);
35+
return binPath;
36+
};
37+
38+
/**
39+
* @param {String} version
40+
* @returns {Promise<String>}
41+
*/
42+
const _getVersion = async (version) => {
43+
const token = core.getInput("github-token");
44+
const octo = github.getOctokit(token);
45+
46+
let release;
47+
if (version === "latest") {
48+
release = await octo.repos.getLatestRelease({ owner, repo });
49+
} else {
50+
if (!version.startsWith("v")) {
51+
version = `v${version}`;
52+
}
53+
release = await octo.repos.getReleaseByTag({ owner, repo, tag: version });
54+
}
55+
56+
const version = release.data.tag_name;
1157
core.info(`version: ${version}`);
1258

59+
return version;
60+
};
61+
62+
/**
63+
* @param {String} version
64+
* @returns {Promise<String>}
65+
*/
66+
const _download = async (version) => {
1367
core.info(`platform: ${process.platform}`);
1468
core.info(`arch: ${process.arch}`);
1569

@@ -40,31 +94,19 @@ const tc = require("@actions/tool-cache");
4094
})();
4195

4296
const ext = (() => {
43-
switch (platform) {
44-
case "Windows":
97+
switch (process.platform) {
98+
case "win32":
4599
return "zip";
46100
default:
47101
return "tar.gz";
48102
}
49103
})();
50104

51-
const url = `https://github.com/koki-develop/qiita-cli/releases/download/${version}/qiita_${platform}_${arch}.${ext}`;
105+
const url = `https://github.com/${owner}/${repo}/releases/download/${version}/qiita_${platform}_${arch}.${ext}`;
106+
52107
core.info(`Downloading... ${url}`);
53108
const cliPath = await tc.downloadTool(url);
54109
core.info(`Downloaded to ${cliPath}`);
55110

56-
core.info("Installing...");
57-
const extractedPath = await (async () => {
58-
switch (ext) {
59-
case "zip":
60-
return tc.extractZip(cliPath);
61-
case "tar.gz":
62-
return tc.extractTar(cliPath);
63-
}
64-
})();
65-
const binPath = await tc.cacheDir(extractedPath, "qiita", version);
66-
core.addPath(binPath);
67-
core.info(`Installed to ${binPath}`);
68-
})().catch((err) => {
69-
core.setFailed(err.message);
70-
});
111+
return cliPath;
112+
};

0 commit comments

Comments
 (0)