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

Various action updates #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
- run: npm ci
- run: npm run all

- uses: actions/setup-python@v4
- run: pip install markdown-to-presentation
- run: markdown-to-presentation push --pages-branch release README.md LICENSE action.yml dist/index.js
env:
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: 'Continuous Integration'

on:
push:
branches:
- master
pull_request:

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v3

- name: Setup Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: '16.x'

- name: Install
run: npm clean-install

- name: Verify
if: runner.os == 'Linux'
run: |
npm run prepare
# Fail if "npm run build" generated new changes in dist
git update-index --refresh dist/* && git diff-index --quiet HEAD dist

- name: Test
run: npm test
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Setup Terraform docs'
description: 'Install and setup Terraform docs executable'
author: 'Labyrinth Labs'
runs:
using: 'node12'
using: 'node16'
main: 'dist/index.js'
inputs:
terraform_docs_version:
Expand Down
1 change: 1 addition & 0 deletions dist/index.js

Large diffs are not rendered by default.

160 changes: 93 additions & 67 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,129 @@
const fs = require('fs')
const os = require('os')
const path = require('path')
const process = require('process')
const os = require("os");

const core = require('@actions/core')
const tc = require('@actions/tool-cache')
const { Octokit } = require('@octokit/rest')
const core = require("@actions/core");
const tc = require("@actions/tool-cache");
const { Octokit } = require("@octokit/rest");

async function run() {
if (isSupportedPlatform(process.platform)) {
const version = await getTerraformDocsVersion()
const url = getDownloadUrl(version, process.platform)

const targetPath = getTargetPath()
core.debug(`Downloading Terraform docs version [${version}] for platform [${process.platform}] from [${url}] to destination [${targetPath}]`)
const terraformDocsPath = await tc.downloadTool(url, targetPath)

makeFileExecutable(terraformDocsPath)

core.debug(`Adding [${getTargetDirectory()}] into PATH`)
core.addPath(getTargetDirectory())
// Gather OS details
const osPlatform = os.platform();
const osArch = os.arch();

if (isSupportedPlatform(osPlatform)) {
try {
const version = await getTerraformDocsVersion();

const platform = mapOS(osPlatform);
const arch = mapArch(osArch);
const url = getDownloadUrl(version, platform, arch);
core.debug(
`Downloading Terraform docs version [${version}] for platform [${platform}-${arch}] from [${url}]`
);

const terraformDocsDownload = await tc.downloadTool(url);

if (!terraformDocsDownload) {
throw new Error(`Unable to download Terraform-docs from ${url}`);
}

let terraformDocsPath = "";

if (platform == "windows") {
terraformDocsPath = await tc.extractZip(terraformDocsDownload);
} else {
terraformDocsPath = await tc.extractTar(terraformDocsDownload);
}

if (!terraformDocsPath) {
throw new Error(`Unable to extract Terraform-docs archive`);
}

core.debug(`Adding [${terraformDocsPath}] into PATH`);
core.addPath(terraformDocsPath);
} catch (error) {
core.error(error);
throw error;
}
}
}

function getTargetDirectory() {
return path.join(os.homedir(), 'terraform_docs', 'bin')
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
function mapArch(arch) {
const mappings = {
x32: "386",
x64: "amd64",
};
return mappings[arch] || arch;
}

function getTargetPath() {
return path.join(getTargetDirectory(), 'terraform-docs')
// os in [darwin, linux, win32...] (https://nodejs.org/api/os.html#os_os_platform)
// return value in [darwin, linux, windows]
function mapOS(os) {
const mappings = {
win32: "windows",
};
return mappings[os] || os;
}

function mapOSExtension(os) {
const mappings = {
windows: "exe",
linux: "tar.gz",
darwin: "tar.gz",
};
return mappings[os];
}

function isSupportedPlatform(platform) {
const supportedPlatforms = ['win32', 'linux', 'darwin']
const supportedPlatforms = ["win32", "linux", "darwin"];
if (supportedPlatforms.includes(platform)) {
return true;
} else {
throw new Error(
`Your platform (${platform}) is not supported by the action.
Supported platforms: ${supportedPlatforms}`
)
);
}
}

function isWindows() {
return process.platform == 'win32'
}

function getOctokit() {
const options = {}
const token = core.getInput("token")
const options = {};
const token = core.getInput("token");
if (token) {
core.debug("Using token authentication for Octokit")
options.auth = token
core.debug("Using token authentication for Octokit");
options.auth = token;
}
return new Octokit(options)
return new Octokit(options);
}

async function getTerraformDocsVersion() {
const inputVersion = core.getInput("terraform_docs_version", {required: true})
const inputVersion = core.getInput("terraform_docs_version", {
required: true,
});
if (inputVersion == "latest") {
core.debug("Requesting for [latest] version ...")
const octokit = getOctokit()
core.debug("Requesting for [latest] version ...");
const octokit = getOctokit();
const response = await octokit.repos.getLatestRelease({
owner: 'terraform-docs',
repo: 'terraform-docs'
})
core.debug(`... version resolved to [${response.data.name}`)
return response.data.name
owner: "terraform-docs",
repo: "terraform-docs",
});
core.debug(`... version resolved to [${response.data.name}`);
return response.data.name;
} else {
return inputVersion
return inputVersion;
}
}

function getDownloadUrl(version, platform) {
const baseUrl = "https://github.com/terraform-docs/terraform-docs/releases/download"
const fileName = getDownloadFilename(version, platform)
return `${baseUrl}/${version}/${fileName}`
function getDownloadUrl(version, platform, arch) {
const baseUrl =
"https://github.com/terraform-docs/terraform-docs/releases/download";
const extension = mapOSExtension(platform);
const fileName = `terraform-docs-${version}-${platform}-${arch}.${extension}`;
return `${baseUrl}/${version}/${fileName}`;
}

function getDownloadFilename(version, platform) {
const fileNamePlatformMatrix = {
win32: 'windows-amd64.exe',
darwin: 'darwin-amd64',
linux: 'linux-amd64'
}
return `terraform-docs-${version}-${fileNamePlatformMatrix[platform]}`
}

function makeFileExecutable(filename) {
if (!isWindows()) {
const chmod = '755'
core.debug(`Setting chmod [${filename}] to [${chmod}]`)
fs.chmodSync(`${filename}`, chmod)
}
}


// RUN THE ACTION
run().catch((error) => core.setFailed(error.message))
run().catch((error) => core.setFailed(error.message));

module.exports = { run, makeFileExecutable }
module.exports = { run };
82 changes: 61 additions & 21 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
const core = require('@actions/core')
const tc = require('@actions/tool-cache')
const os = require("os");

const fs = require('fs')
const core = require("@actions/core");
const io = require("@actions/io");
const tc = require("@actions/tool-cache");

const index = require('./index')
const index = require("./index");

jest.mock('@actions/core')
jest.mock('@actions/tool-cache')
fs.chmodSync = jest.fn()
jest.mock("@actions/core");
jest.mock("@actions/tool-cache");
jest.mock("os");

tc.downloadTool.mockResolvedValue("terraform-docs")
fs.chmodSync.mockReturnValue(null)
describe("Mock tests", () => {
const HOME = process.env.HOME;
const APPDATA = process.env.APPDATA;

describe('Mock tests', () => {
beforeEach(() => {
jest.clearAllMocks()
})
process.env.HOME = "/tmp/asdf";
process.env.APPDATA = "/tmp/asdf";
jest.clearAllMocks();
});

test('download should be called', async () => {
await index.run()
afterEach(async () => {
await io.rmRF(process.env.HOME);
process.env.HOME = HOME;
process.env.APPDATA = APPDATA;
});

expect(tc.downloadTool).toBeCalledTimes(1)
})
test("gets specified version on linux amd64", async () => {
const version = "v0.16.0";

test('add path should be called', async () => {
await index.run()
core.getInput = jest.fn().mockReturnValueOnce(version);

expect(core.addPath).toBeCalledTimes(1)
})
})
tc.downloadTool = jest.fn().mockReturnValueOnce("file.tar.gz");

tc.extractTar = jest.fn().mockReturnValueOnce("file");

os.platform = jest.fn().mockReturnValue("linux");

os.arch = jest.fn().mockReturnValue("amd64");

await index.run();

expect(tc.downloadTool.mock.calls[0][0]).toBe(
"https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-linux-amd64.tar.gz"
);
expect(tc.downloadTool).toBeCalledTimes(1);
expect(core.addPath).toBeCalledTimes(1);
});

test("gets specified version on windows 386", async () => {
const version = "v0.16.0";

core.getInput = jest.fn().mockReturnValueOnce(version);

tc.downloadTool = jest.fn().mockReturnValueOnce("file.zip");

tc.extractZip = jest.fn().mockReturnValueOnce("file");

os.platform = jest.fn().mockReturnValue("win32");

os.arch = jest.fn().mockReturnValue("386");

await index.run();

expect(tc.downloadTool.mock.calls[0][0]).toBe(
"https://github.com/terraform-docs/terraform-docs/releases/download/v0.16.0/terraform-docs-v0.16.0-windows-386.exe"
);
expect(tc.downloadTool).toBeCalledTimes(1);
expect(core.addPath).toBeCalledTimes(1);
});
});
Loading