Refactor: Get the latest version from Homebrew (#28)

Refactoring

* feat: Add reject to Promise
* feat: Add printing inputs
* feat: Add error handling to getLatestVersion()
* feat: show version of Hugo, Go, and Git (Close #23)
* refactor: Get the latest version from Homebrew (Close #26)
* refactor: enhance error message of promise reject

Enhancement for workflow

* gha: Add deps (test-prod job needs test job)
* gha: Fix matrix testing
* gha: comment out Build production step
* gha: remove Dump step

Update README

* docs: update readme
This commit is contained in:
Shohei Ueda 2019-09-18 04:11:47 +09:00 committed by GitHub
parent 85171ece4e
commit d46d0aa12e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 86 deletions

View file

@ -18,19 +18,19 @@ jobs:
# - name: Test script
# run: npm test
- name: Build production
run: npm run build
# - name: Build production
# run: npm run build
test-prod:
runs-on: ubuntu-18.04
needs: test
strategy:
matrix:
hugo-version: [null]
# hugo-version: [null, 'latest', '0.58.2']
extended: [null]
# extended: [null, true, false]
# hugo-version: ['latest']
# extended: [true]
hugo-version: ['latest', '0.58.2']
extended: [true, false]
steps:
- uses: actions/checkout@v1
@ -40,9 +40,3 @@ jobs:
with:
hugo-version: ${{ matrix.hugo-version }}
extended: ${{ matrix.extended }}
- name: Dump
run: |
hugo version
go version
git --version

View file

@ -83,9 +83,7 @@ jobs:
hugo-version: 'latest'
```
**Note**: This action gets a Hugo latest version by GitHub API. Please be aware of [GitHub API Rate limiting]
[GitHub API Rate limiting]: https://developer.github.com/v3/#rate-limiting
This action fetches the latest version of Hugo by [hugo | Homebrew Formulae](https://formulae.brew.sh/formula/hugo)

View file

@ -1,22 +1,18 @@
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function getLatestVersion() {
// return new Promise((resolve, reject) => {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const url = "https://api.github.com/repos/gohugoio/hugo/releases/latest";
const url = "https://formulae.brew.sh/api/formula/hugo.json";
xhr.open("GET", url);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const result = JSON.parse(xhr.responseText);
const latestURL = result["assets"][0].browser_download_url;
const latestVersion = latestURL.match(/(\d+).(\d+).(\d+)/g)[0];
const latestVersion = result.versions.stable;
resolve(latestVersion);
// } else {
// reject(`ERROR: got status ${xhr.status}`);
} else if (xhr.readyState === 4 && xhr.status !== 200) {
reject(`ERROR: got status ${xhr.status} of ${url}`);
}
};
});

View file

@ -1,51 +1,54 @@
const core = require("@actions/core");
const tc = require("@actions/tool-cache");
const io = require("@actions/io");
const exec = require("@actions/exec");
const getLatestVersion = require("./get-latest-version");
// most @actions toolkit packages have async methods
async function run() {
try {
getLatestVersion().then(async function(latestVersion) {
let hugoVersion = core.getInput("hugo-version");
if (!hugoVersion || hugoVersion === "latest") {
hugoVersion = latestVersion;
getLatestVersion().then(
async function(latestVersion) {
let hugoVersion = core.getInput("hugo-version");
if (!hugoVersion || hugoVersion === "latest") {
hugoVersion = latestVersion;
}
console.log(`Hugo version: ${hugoVersion}`);
const extended = core.getInput("extended");
console.log(`Hugo extended: ${extended}`);
let extendedStr = "";
if (extended === "true") {
extendedStr = "extended_";
}
console.log(`Operating System: ${process.platform}`);
const hugoName = `hugo_${extendedStr}${hugoVersion}_Linux-64bit`;
core.debug(`hugoName: ${hugoName}`);
const hugoURL = `https://github.com/gohugoio/hugo/releases/download/v${hugoVersion}/${hugoName}.tar.gz`;
core.debug(`hugoURL: ${hugoURL}`);
const hugoPath = `${process.env.HOME}/bin`;
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
// Download and extract Hugo binary
const hugoTarball = await tc.downloadTool(hugoURL);
const hugoExtractedFolder = await tc.extractTar(hugoTarball, "/tmp");
core.debug("hugoExtractedFolder:", hugoExtractedFolder);
await io.mv(`${hugoExtractedFolder}/hugo`, hugoPath);
// Show version
await exec.exec('hugo version');
await exec.exec('go version');
await exec.exec('git --version');
},
function(error) {
core.setFailed(error);
}
core.debug(`Hugo version: ${hugoVersion}`);
let extended = core.getInput("extended");
core.debug(`Hugo extended: ${extended}`);
let extendedStr = "";
if (extended === "true") {
extendedStr = "extended_";
}
console.log(`Operating System: ${process.platform}`);
const hugoName = `hugo_${extendedStr}${hugoVersion}_Linux-64bit`;
core.debug(`hugoName: ${hugoName}`);
const hugoURL = `https://github.com/gohugoio/hugo/releases/download/v${hugoVersion}/${hugoName}.tar.gz`;
core.debug(`hugoURL: ${hugoURL}`);
const hugoPath = `${process.env.HOME}/bin`;
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
// Download and extract Hugo binary
const hugoTarball = await tc.downloadTool(hugoURL);
const hugoExtractedFolder = await tc.extractTar(hugoTarball, "/tmp");
core.debug("hugoExtractedFolder:", hugoExtractedFolder);
await io.mv(`${hugoExtractedFolder}/hugo`, hugoPath);
// },
// function(error) {
// console.error(error);
// console.log(
// "HINT: GitHub API Rate limiting",
// "https://developer.github.com/v3/#rate-limiting"
// );
});
);
} catch (error) {
core.setFailed(error.message);
}

View file

@ -1,15 +1,39 @@
{
"name": "@actions/exec",
"version": "1.0.1",
"description": "Actions exec lib",
"keywords": [
"github",
"actions",
"exec"
"_from": "@actions/exec",
"_id": "@actions/exec@1.0.1",
"_inBundle": false,
"_integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ==",
"_location": "/@actions/exec",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "@actions/exec",
"name": "@actions/exec",
"escapedName": "@actions%2fexec",
"scope": "@actions",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/",
"/@actions/tool-cache"
],
"homepage": "https://github.com/actions/toolkit/tree/master/packages/exec",
"license": "MIT",
"main": "lib/exec.js",
"_resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz",
"_shasum": "1624b541165697e7008d7c87bc1f69f191263c6c",
"_spec": "@actions/exec",
"_where": "/Users/iris/Documents/repos/github.com/peaceiris/actions-hugo",
"bugs": {
"url": "https://github.com/actions/toolkit/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Actions exec lib",
"devDependencies": {
"@actions/io": "^1.0.1"
},
"directories": {
"lib": "lib",
"test": "__tests__"
@ -17,6 +41,16 @@
"files": [
"lib"
],
"gitHead": "a2ab4bcf78e4f7080f0d45856e6eeba16f0bbc52",
"homepage": "https://github.com/actions/toolkit/tree/master/packages/exec",
"keywords": [
"github",
"actions",
"exec"
],
"license": "MIT",
"main": "lib/exec.js",
"name": "@actions/exec",
"publishConfig": {
"access": "public"
},
@ -28,15 +62,5 @@
"test": "echo \"Error: run tests from root\" && exit 1",
"tsc": "tsc"
},
"bugs": {
"url": "https://github.com/actions/toolkit/issues"
},
"devDependencies": {
"@actions/io": "^1.0.1"
},
"gitHead": "a2ab4bcf78e4f7080f0d45856e6eeba16f0bbc52"
,"_resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.0.1.tgz"
,"_integrity": "sha512-nvFkxwiicvpzNiCBF4wFBDfnBvi7xp/as7LE1hBxBxKG2L29+gkIPBiLKMVORL+Hg3JNf07AKRfl0V5djoypjQ=="
,"_from": "@actions/exec@1.0.1"
}
"version": "1.0.1"
}

View file

@ -26,6 +26,7 @@
"homepage": "https://github.com/peaceiris/actions-hugo#readme",
"dependencies": {
"@actions/core": "^1.1.0",
"@actions/exec": "^1.0.1",
"@actions/io": "^1.0.1",
"@actions/tool-cache": "^1.1.1",
"xmlhttprequest": "^1.8.0"