From 8ddda09c145dc2a260658d97cb29242e3c25a332 Mon Sep 17 00:00:00 2001 From: Thomas Hu Date: Fri, 17 Jul 2020 11:57:18 -0400 Subject: [PATCH] Wrap request promise in try catch --- LICENSE | 2 +- dist/index.js | 200 ++++++++++++++++++++++++---------------------- index.js | 198 +++++++++++++++++++++++---------------------- package-lock.json | 2 +- package.json | 2 +- 5 files changed, 208 insertions(+), 196 deletions(-) diff --git a/LICENSE b/LICENSE index 408c827fd..d284c1fb9 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Codecov +Copyright (c) 2019-2020 Codecov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/dist/index.js b/dist/index.js index 86ec17418..0f2b943f5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2236,121 +2236,127 @@ try { url: "https://codecov.io/bash", json: false }, (error, response, body) => { - if (error && fail_ci) { - throw error; - } else if (error) { - core.warning(`Codecov warning: ${error.message}`); - } + try { + if (error && fail_ci) { + throw error; + } else if (error) { + core.warning(`Codecov warning: ${error.message}`); + } + + fs.writeFile("codecov.sh", body, err => { + if (err && fail_ci) { + throw err; + } else if (err) { + core.warning(`Codecov warning: ${err.message}`); + } + + let output = ""; + let execError = ""; + const options = {}; + options.listeners = { + stdout: data => { + output += data.toString(); + }, + stderr: data => { + execError += data.toString(); + } + }; - fs.writeFile("codecov.sh", body, err => { - if (err && fail_ci) { - throw err; - } else if (err) { - core.warning(`Codecov warning: ${err.message}`); - } + options.env = Object.assign(process.env, { + GITHUB_ACTION: process.env.GITHUB_ACTION, + GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, + GITHUB_REF: process.env.GITHUB_REF, + GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, + GITHUB_SHA: process.env.GITHUB_SHA, + GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' + }); - let output = ""; - let execError = ""; - const options = {}; - options.listeners = { - stdout: data => { - output += data.toString(); - }, - stderr: data => { - execError += data.toString(); + if(token){ + options.env.CODECOV_TOKEN = token } - }; - options.env = Object.assign(process.env, { - GITHUB_ACTION: process.env.GITHUB_ACTION, - GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, - GITHUB_REF: process.env.GITHUB_REF, - GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, - GITHUB_SHA: process.env.GITHUB_SHA, - GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' - }); - - if(token){ - options.env.CODECOV_TOKEN = token - } + const env_vars_arg = [] + for (let env_var of env_vars.split(",")) { + let env_var_clean = env_var.trim(); + if (env_var_clean) { + options.env[env_var_clean] = process.env[env_var_clean]; + env_vars_arg.push(env_var_clean) + } + } - const env_vars_arg = [] - for (let env_var of env_vars.split(",")) { - let env_var_clean = env_var.trim(); - if (env_var_clean) { - options.env[env_var_clean] = process.env[env_var_clean]; - env_vars_arg.push(env_var_clean) + const execArgs = ["codecov.sh"]; + if (file) { + execArgs.push( + "-f", `${file}` + ); } - } - const execArgs = ["codecov.sh"]; - if (file) { - execArgs.push( - "-f", `${file}` - ); - } + if (files) { + files.split(',').forEach(f => { + execArgs.push( + "-f", `${f}` + ); + }); + } - if (files) { - files.split(',').forEach(f => { + if (dir) { execArgs.push( - "-f", `${f}` + "-s", `${dir}` ); - }); - } + } - if (dir) { execArgs.push( - "-s", `${dir}` + "-n", `${name}`, + "-F", `${flags}` ); - } - - execArgs.push( - "-n", `${name}`, - "-F", `${flags}` - ); - if (fail_ci) { - execArgs.push( - "-Z" - ); - } + if (fail_ci) { + execArgs.push( + "-Z" + ); + } - if (env_vars_arg.length) { - execArgs.push( - "-e", env_vars_arg.join(",") - ); - } + if (env_vars_arg.length) { + execArgs.push( + "-e", env_vars_arg.join(",") + ); + } - if (write_path) { - execArgs.push( - "-q", `${write_path}` - ); - } + if (write_path) { + execArgs.push( + "-q", `${write_path}` + ); + } - exec.exec("bash", execArgs, options) - .catch(err => { - if (fail_ci) { - core.setFailed( - `Codecov failed with the following error: ${err.message}` - ); - } else { - core.warning(`Codecov warning: ${err.message}`); - } - }) - .then(() => { - unlinkFile(); - }); + exec.exec("bash", execArgs, options) + .catch(err => { + if (fail_ci) { + core.setFailed( + `Codecov failed with the following error: ${err.message}` + ); + } else { + core.warning(`Codecov warning: ${err.message}`); + } + }) + .then(() => { + unlinkFile(); + }); - const unlinkFile = () => { - fs.unlink("codecov.sh", err => { - if (err && fail_ci) { - throw err; - } else if (err) { - core.warning(`Codecov warning: ${err.message}`); - } - }); - }; - }); + const unlinkFile = () => { + fs.unlink("codecov.sh", err => { + if (err && fail_ci) { + throw err; + } else if (err) { + core.warning(`Codecov warning: ${err.message}`); + } + }); + }; + }); + } catch (error) { + core.setFailed( + `Codecov failed with the following error: ${error.message}` + ); + } }); } catch (error) { if (fail_ci) { diff --git a/index.js b/index.js index dfdf105f6..7cf979768 100644 --- a/index.js +++ b/index.js @@ -32,121 +32,127 @@ try { url: "https://codecov.io/bash", json: false }, (error, response, body) => { - if (error && fail_ci) { - throw error; - } else if (error) { - core.warning(`Codecov warning: ${error.message}`); - } - - fs.writeFile("codecov.sh", body, err => { - if (err && fail_ci) { - throw err; - } else if (err) { - core.warning(`Codecov warning: ${err.message}`); + try { + if (error && fail_ci) { + throw error; + } else if (error) { + core.warning(`Codecov warning: ${error.message}`); } - let output = ""; - let execError = ""; - const options = {}; - options.listeners = { - stdout: data => { - output += data.toString(); - }, - stderr: data => { - execError += data.toString(); + fs.writeFile("codecov.sh", body, err => { + if (err && fail_ci) { + throw err; + } else if (err) { + core.warning(`Codecov warning: ${err.message}`); } - }; - - options.env = Object.assign(process.env, { - GITHUB_ACTION: process.env.GITHUB_ACTION, - GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, - GITHUB_REF: process.env.GITHUB_REF, - GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, - GITHUB_SHA: process.env.GITHUB_SHA, - GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' - }); - if(token){ - options.env.CODECOV_TOKEN = token - } + let output = ""; + let execError = ""; + const options = {}; + options.listeners = { + stdout: data => { + output += data.toString(); + }, + stderr: data => { + execError += data.toString(); + } + }; + + options.env = Object.assign(process.env, { + GITHUB_ACTION: process.env.GITHUB_ACTION, + GITHUB_RUN_ID: process.env.GITHUB_RUN_ID, + GITHUB_REF: process.env.GITHUB_REF, + GITHUB_REPOSITORY: process.env.GITHUB_REPOSITORY, + GITHUB_SHA: process.env.GITHUB_SHA, + GITHUB_HEAD_REF: process.env.GITHUB_HEAD_REF || '' + }); - const env_vars_arg = [] - for (let env_var of env_vars.split(",")) { - let env_var_clean = env_var.trim(); - if (env_var_clean) { - options.env[env_var_clean] = process.env[env_var_clean]; - env_vars_arg.push(env_var_clean) + if(token){ + options.env.CODECOV_TOKEN = token } - } - const execArgs = ["codecov.sh"]; - if (file) { - execArgs.push( - "-f", `${file}` - ); - } + const env_vars_arg = [] + for (let env_var of env_vars.split(",")) { + let env_var_clean = env_var.trim(); + if (env_var_clean) { + options.env[env_var_clean] = process.env[env_var_clean]; + env_vars_arg.push(env_var_clean) + } + } - if (files) { - files.split(',').forEach(f => { + const execArgs = ["codecov.sh"]; + if (file) { execArgs.push( - "-f", `${f}` + "-f", `${file}` ); - }); - } + } - if (dir) { - execArgs.push( - "-s", `${dir}` - ); - } + if (files) { + files.split(',').forEach(f => { + execArgs.push( + "-f", `${f}` + ); + }); + } - execArgs.push( - "-n", `${name}`, - "-F", `${flags}` - ); + if (dir) { + execArgs.push( + "-s", `${dir}` + ); + } - if (fail_ci) { execArgs.push( - "-Z" + "-n", `${name}`, + "-F", `${flags}` ); - } - if (env_vars_arg.length) { - execArgs.push( - "-e", env_vars_arg.join(",") - ); - } + if (fail_ci) { + execArgs.push( + "-Z" + ); + } - if (write_path) { - execArgs.push( - "-q", `${write_path}` - ); - } + if (env_vars_arg.length) { + execArgs.push( + "-e", env_vars_arg.join(",") + ); + } - exec.exec("bash", execArgs, options) - .catch(err => { - if (fail_ci) { - core.setFailed( - `Codecov failed with the following error: ${err.message}` - ); - } else { - core.warning(`Codecov warning: ${err.message}`); - } - }) - .then(() => { - unlinkFile(); - }); + if (write_path) { + execArgs.push( + "-q", `${write_path}` + ); + } - const unlinkFile = () => { - fs.unlink("codecov.sh", err => { - if (err && fail_ci) { - throw err; - } else if (err) { - core.warning(`Codecov warning: ${err.message}`); - } - }); - }; - }); + exec.exec("bash", execArgs, options) + .catch(err => { + if (fail_ci) { + core.setFailed( + `Codecov failed with the following error: ${err.message}` + ); + } else { + core.warning(`Codecov warning: ${err.message}`); + } + }) + .then(() => { + unlinkFile(); + }); + + const unlinkFile = () => { + fs.unlink("codecov.sh", err => { + if (err && fail_ci) { + throw err; + } else if (err) { + core.warning(`Codecov warning: ${err.message}`); + } + }); + }; + }); + } catch (error) { + core.setFailed( + `Codecov failed with the following error: ${error.message}` + ); + } }); } catch (error) { if (fail_ci) { diff --git a/package-lock.json b/package-lock.json index a07d03892..d3fc3f436 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "codecov-action", - "version": "1.0.11", + "version": "1.0.12", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c47a9437a..a544b5796 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codecov-action", - "version": "1.0.11", + "version": "1.0.12", "description": "Upload coverage reports to Codecov from GitHub Actions", "main": "index.js", "scripts": {