Skip to content

Commit

Permalink
fix: Add override OS and linux default
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrockhu-codecov committed Aug 20, 2021
1 parent 53cd6a7 commit b924225
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest, khadas-vim3]
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ inputs:
override_tag:
description: 'Specify the git tag'
required: false
os:
description: 'Override the assumed OS. Options are alpine | linux | macos | windows.'
required: false
root_dir:
description: 'Used when not in git/hg project to identify project root directory'
required: false
Expand Down
69 changes: 39 additions & 30 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({

/***/ 7351:
/***/ 5241:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";
Expand Down Expand Up @@ -135,7 +135,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getState = exports.saveState = exports.group = exports.endGroup = exports.startGroup = exports.info = exports.warning = exports.error = exports.debug = exports.isDebug = exports.setFailed = exports.setCommandEcho = exports.setOutput = exports.getBooleanInput = exports.getMultilineInput = exports.getInput = exports.addPath = exports.setSecret = exports.exportVariable = exports.ExitCode = void 0;
const command_1 = __nccwpck_require__(7351);
const command_1 = __nccwpck_require__(5241);
const file_command_1 = __nccwpck_require__(717);
const utils_1 = __nccwpck_require__(5278);
const os = __importStar(__nccwpck_require__(2087));
Expand Down Expand Up @@ -626,7 +626,7 @@ const os = __importStar(__nccwpck_require__(2087));
const events = __importStar(__nccwpck_require__(8614));
const child = __importStar(__nccwpck_require__(3129));
const path = __importStar(__nccwpck_require__(5622));
const io = __importStar(__nccwpck_require__(7436));
const io = __importStar(__nccwpck_require__(7351));
const ioUtil = __importStar(__nccwpck_require__(1962));
const timers_1 = __nccwpck_require__(8213);
/* eslint-disable @typescript-eslint/unbound-method */
Expand Down Expand Up @@ -2221,7 +2221,7 @@ exports.getCmdPath = getCmdPath;

/***/ }),

/***/ 7436:
/***/ 7351:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";
Expand Down Expand Up @@ -12842,10 +12842,10 @@ var external_fs_ = __nccwpck_require__(5747);
var external_https_ = __nccwpck_require__(7211);
// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(5622);
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(2186);
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
var exec = __nccwpck_require__(1514);
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(2186);
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
var github = __nccwpck_require__(5438);
;// CONCATENATED MODULE: ./package.json
Expand Down Expand Up @@ -12874,6 +12874,7 @@ const buildExec = () => {
const flags = core.getInput('flags');
const functionalities = core.getInput('functionalities');
const name = core.getInput('name');
const os = core.getInput('os');
const overrideBranch = core.getInput('override_branch');
const overrideBuild = core.getInput('override_build');
const overrideCommit = core.getInput('override_commit');
Expand Down Expand Up @@ -12981,7 +12982,7 @@ const buildExec = () => {
if (workingDir) {
options.cwd = workingDir;
}
return { execArgs, options, failCi };
return { execArgs, options, failCi, os };
};
/* harmony default export */ const src_buildExec = (buildExec);

Expand All @@ -12994,24 +12995,37 @@ const setFailure = (message, failCi) => {
process.exit();
}
};
const getUploaderName = () => {
if (isWindows()) {
const getUploaderName = (platform) => {
if (isWindows(platform)) {
return 'codecov.exe';
}
else {
return 'codecov';
}
};
const isValidPlatform = () => {
return PLATFORMS.includes(getPlatform());
const isValidPlatform = (platform) => {
return PLATFORMS.includes(platform);
};
const isWindows = () => {
return getPlatform() === 'windows';
const isWindows = (platform) => {
return platform === 'windows';
};
const getPlatform = () => {
return process.env.RUNNER_OS.toLowerCase();
const getPlatform = (os) => {
var _a;
if (isValidPlatform(os)) {
core.info(`==> ${os} OS provided`);
return os;
}
const platform = (_a = process.env.RUNNER_OS) === null || _a === void 0 ? void 0 : _a.toLowerCase();
if (isValidPlatform(platform)) {
core.info(`==> ${platform} OS detected`);
return platform;
}
core.info('==> Could not detect OS or provided OS is invalid. Defaulting to linux');
return 'linux';
};
const getBaseUrl = (platform) => {
return `https://uploader.codecov.io/latest/${platform}/${getUploaderName(platform)}`;
};
const BASEURL = `https://uploader.codecov.io/latest/${getPlatform()}/${getUploaderName()}`;


// EXTERNAL MODULE: external "crypto"
Expand All @@ -13037,15 +13051,15 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argume



const verify = (filename) => __awaiter(void 0, void 0, void 0, function* () {
const verify = (filename, platform) => __awaiter(void 0, void 0, void 0, function* () {
try {
const uploaderName = getUploaderName();
const uploaderName = getUploaderName(platform);
// Read in public key
const publicKeyArmored = yield external_fs_.readFileSync(__nccwpck_require__.ab + "pgp_keys.asc", 'utf-8');
// Get SHASUM and SHASUM signature files
const shasumRes = yield lib(`${BASEURL}.SHA256SUM`);
const shasumRes = yield lib(`${getBaseUrl(platform)}.SHA256SUM`);
const shasum = yield shasumRes.text();
const shaSigRes = yield lib(`${BASEURL}.SHA256SUM.sig`);
const shaSigRes = yield lib(`${getBaseUrl(platform)}.SHA256SUM.sig`);
const shaSig = yield shaSigRes.text();
// Verify shasum
const verified = yield openpgp_min/* verify */.T({
Expand Down Expand Up @@ -13102,17 +13116,12 @@ var src_awaiter = (undefined && undefined.__awaiter) || function (thisArg, _argu




let failCi;
try {
const { execArgs, options, failCi } = src_buildExec();
const platform = getPlatform();
core.info(`==> ${platform} OS detected`);
if (!isValidPlatform()) {
setFailure(`Codecov: Encountered an unexpected platform: ${platform}`, failCi);
}
const filename = external_path_.join(__dirname, getUploaderName());
external_https_.get(BASEURL, (res) => {
const { execArgs, options, failCi, os } = src_buildExec();
const platform = getPlatform(os);
const filename = external_path_.join(__dirname, getUploaderName(platform));
external_https_.get(getBaseUrl(platform), (res) => {
// Image will be stored at this path
const filePath = external_fs_.createWriteStream(filename);
res.pipe(filePath);
Expand All @@ -13121,7 +13130,7 @@ try {
setFailure(`Codecov: Failed to write uploader binary: ${err.message}`, true);
}).on('finish', () => src_awaiter(void 0, void 0, void 0, function* () {
filePath.close();
yield validate(filename);
yield validate(filename, platform);
yield external_fs_.chmodSync(filename, '777');
const unlink = () => {
external_fs_.unlink(filename, (err) => {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/buildExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const buildExec = () => {
const flags = core.getInput('flags');
const functionalities = core.getInput('functionalities');
const name = core.getInput('name');
const os = core.getInput('os');
const overrideBranch = core.getInput('override_branch');
const overrideBuild = core.getInput('override_build');
const overrideCommit = core.getInput('override_commit');
Expand Down Expand Up @@ -146,7 +147,7 @@ const buildExec = () => {
options.cwd = workingDir;
}

return {execArgs, options, failCi};
return {execArgs, options, failCi, os};
};

export default buildExec;
61 changes: 61 additions & 0 deletions src/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
getBaseUrl,
getPlatform,
isValidPlatform,
isWindows,
PLATFORMS,
} from './helpers';

let OLDOS = process.env.RUNNER_OS;

beforeEach(() => {
jest.resetModules();
OLDOS = process.env.RUNNER_OS;
});

afterAll(() => {
process.env.RUNNER_OS = OLDOS;
});

test('getPlatform', () => {
expect(getPlatform('linux')).toBe('linux');
expect(getPlatform('windows')).toBe('windows');

const defaultPlatform =
process.env.RUNNER_OS ? process.env.RUNNER_OS.toLowerCase() : 'linux';
expect(getPlatform('fakeos')).toBe(defaultPlatform);
expect(getPlatform()).toBe(defaultPlatform);

process.env.RUNNER_OS = 'macos';
expect(getPlatform('fakeos')).toBe('macos');
expect(getPlatform()).toBe('macos');

process.env.RUNNER_OS = 'alsofakeos';
expect(getPlatform()).toBe('linux');
expect(getPlatform('fakeos')).toBe('linux');
});

test('getBaseUrl', () => {
expect(PLATFORMS.map((platform) => {
return getBaseUrl(platform);
})).toEqual([
'https://uploader.codecov.io/latest/alpine/codecov',
'https://uploader.codecov.io/latest/linux/codecov',
'https://uploader.codecov.io/latest/macos/codecov',
'https://uploader.codecov.io/latest/windows/codecov.exe',
]);
});

test('isWindows', () => {
expect(PLATFORMS.map((platform) => {
return isWindows(platform);
})).toEqual([false, false, false, true]);
});

test('isValidPlatform', () => {
expect(PLATFORMS.map((platform) => {
return isValidPlatform(platform);
})).toEqual([true, true, true, true]);

expect(isValidPlatform('fakeos')).toBeFalsy();
});
37 changes: 27 additions & 10 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,47 @@ const setFailure = (message: string, failCi: boolean): void => {
}
};

const getUploaderName = (): string => {
if (isWindows()) {
const getUploaderName = (platform: string): string => {
if (isWindows(platform)) {
return 'codecov.exe';
} else {
return 'codecov';
}
};

const isValidPlatform = (): boolean => {
return PLATFORMS.includes(getPlatform());
const isValidPlatform = (platform: string): boolean => {
return PLATFORMS.includes(platform);
};

const isWindows = (): boolean => {
return getPlatform() === 'windows';
const isWindows = (platform: string): boolean => {
return platform === 'windows';
};

const getPlatform = (): string => {
return process.env.RUNNER_OS.toLowerCase();
const getPlatform = (os?: string): string => {
if (isValidPlatform(os)) {
core.info(`==> ${os} OS provided`);
return os;
}

const platform = process.env.RUNNER_OS?.toLowerCase();
if (isValidPlatform(platform)) {
core.info(`==> ${platform} OS detected`);
return platform;
}

core.info(
'==> Could not detect OS or provided OS is invalid. Defaulting to linux',
);
return 'linux';
};

const BASEURL = `https://uploader.codecov.io/latest/${getPlatform()}/${getUploaderName()}`;
const getBaseUrl = (platform: string): string => {
return `https://uploader.codecov.io/latest/${platform}/${getUploaderName(platform)}`;
};

export {
BASEURL,
PLATFORMS,
getBaseUrl,
getPlatform,
getUploaderName,
isValidPlatform,
Expand Down
21 changes: 6 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import * as fs from 'fs';
import * as https from 'https';
import * as path from 'path';

import * as core from '@actions/core';
import * as exec from '@actions/exec';

import buildExec from './buildExec';
import {
BASEURL,
getBaseUrl,
getPlatform,
getUploaderName,
isValidPlatform,
setFailure,
} from './helpers';

Expand All @@ -19,18 +17,11 @@ import verify from './validate';
let failCi;

try {
const {execArgs, options, failCi} = buildExec();
const platform = getPlatform();
core.info(`==> ${platform} OS detected`);
const {execArgs, options, failCi, os} = buildExec();
const platform = getPlatform(os);

if (!isValidPlatform()) {
setFailure(
`Codecov: Encountered an unexpected platform: ${platform}`,
failCi,
);
}
const filename = path.join( __dirname, getUploaderName());
https.get(BASEURL, (res) => {
const filename = path.join( __dirname, getUploaderName(platform));
https.get(getBaseUrl(platform), (res) => {
// Image will be stored at this path
const filePath = fs.createWriteStream(filename);
res.pipe(filePath);
Expand All @@ -43,7 +34,7 @@ try {
}).on('finish', async () => {
filePath.close();

await verify(filename);
await verify(filename, platform);
await fs.chmodSync(filename, '777');

const unlink = () => {
Expand Down
10 changes: 5 additions & 5 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import * as openpgp from 'openpgp';
import * as fetch from 'node-fetch';

import {
BASEURL,
getBaseUrl,
getUploaderName,
setFailure,
} from './helpers';

const verify = async (filename: string) => {
const verify = async (filename: string, platform: string) => {
try {
const uploaderName = getUploaderName();
const uploaderName = getUploaderName(platform);

// Read in public key
const publicKeyArmored = await fs.readFileSync(
Expand All @@ -23,10 +23,10 @@ const verify = async (filename: string) => {
);

// Get SHASUM and SHASUM signature files
const shasumRes = await fetch( `${BASEURL}.SHA256SUM`);
const shasumRes = await fetch( `${getBaseUrl(platform)}.SHA256SUM`);
const shasum = await shasumRes.text();

const shaSigRes = await fetch( `${BASEURL}.SHA256SUM.sig`);
const shaSigRes = await fetch( `${getBaseUrl(platform)}.SHA256SUM.sig`);
const shaSig = await shaSigRes.text();

// Verify shasum
Expand Down

0 comments on commit b924225

Please sign in to comment.