-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcodeclimate.js
More file actions
76 lines (65 loc) · 2.23 KB
/
codeclimate.js
File metadata and controls
76 lines (65 loc) · 2.23 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const io = require('@actions/io');
const context = require('@actions/github');
const os = require('os');
const path = require('path');
const { exec } = require('child_process');
const tool = 'cc-test-reporter';
module.exports.download = async function (options = {}) {
options.version = options.version || 'latest';
let toolPath = tc.find(tool, options.version);
if (!toolPath) {
const url = getUrl(options);
const downloadPath = await tc.downloadTool(url);
toolPath = path.join(path.dirname(downloadPath), tool);
await io.mv(downloadPath, toolPath);
await new Promise((resolve, reject) => {
exec(`chmod +x ${toolPath}`, {}, (err) => {
err ? reject(err) : resolve(0);
});
});
toolPath = await tc.cacheDir(path.dirname(toolPath), tool, options.version);
}
core.addPath(toolPath);
};
function getUrl(options) {
if (options.url) {
return options.url;
}
return `https://codeclimate.com/downloads/test-reporter/test-reporter-${options.version}-${os.platform()}-amd64`;
}
/**
* The following code has been copied from github.com/paambaati/codeclimate-action
* and is copyrighted by GP under the terms of the MIT license.
*
* See:
* https://github.com/paambaati/codeclimate-action/blob/master/LICENSE
* https://github.com/paambaati/codeclimate-action/blob/master/src/main.ts#L42-L59
*/
function getEnv() {
const env = process.env;
if (process.env.GITHUB_SHA !== undefined) {
env.GIT_COMMIT_SHA = process.env.GITHUB_SHA;
}
if (process.env.GITHUB_REF !== undefined) {
env.GIT_BRANCH = process.env.GITHUB_REF;
}
if (env.GIT_BRANCH)
env.GIT_BRANCH = env.GIT_BRANCH.replace(/^refs\/heads\//, '');
if (process.env.GITHUB_EVENT_NAME === 'pull_request') {
env.GIT_BRANCH = process.env.GITHUB_HEAD_REF || env.GIT_BRANCH;
env.GIT_COMMIT_SHA = context.payload.pull_request['head']['sha'];
}
return env;
}
module.exports.command = async function (args) {
return await new Promise((resolve, reject) => {
exec(`${tool} ${args}`, { env: getEnv() }, (err) => {
err ? reject(err) : resolve(0);
});
});
};
module.exports.find = function (version) {
return tc.find(tool, version);
};