From 9d58699da5f87cc6e0e66c6a003f325a4f96783d Mon Sep 17 00:00:00 2001
From: Peter Evans <peter-evans@users.noreply.github.com>
Date: Thu, 13 Feb 2020 16:26:04 +0900
Subject: [PATCH 1/2] Skip python setup when running in a container

---
 dist/index.js     | 57 ++++++++++++++++++++++++++++++++++-------------
 index.js          | 20 +++++------------
 package-lock.json |  5 +++++
 package.json      |  3 ++-
 4 files changed, 54 insertions(+), 31 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index c309c57..5bd8357 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1001,34 +1001,24 @@ module.exports = require("os");
 /***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
 
 const { inspect } = __webpack_require__(669);
+const isDocker = __webpack_require__(160);
 const fs = __webpack_require__(747);
 const core = __webpack_require__(470);
 const exec = __webpack_require__(986);
 const setupPython = __webpack_require__(139);
 
-function fileExists(path) {
-  try {
-    return fs.statSync(path).isFile();
-  } catch (e) {
-    core.debug(`e: ${inspect(e)}`);
-    return false;
-  }
-}
-
 async function run() {
   try {
     // Allows ncc to find assets to be included in the distribution
     const src = __webpack_require__.ab + "src";
     core.debug(`src: ${src}`);
 
-    // Check if the platfrom is Alpine Linux
-    const alpineLinux = fileExists("/etc/alpine-release");
-    core.debug(`alpineLinux: ${alpineLinux}`);
-
-    // Skip Python setup if the platform is Alpine Linux
-    if (!alpineLinux)
+    if (isDocker()) {
+      core.info('Running inside a Docker container');
+    } else {
       // Setup Python from the tool cache
       setupPython("3.8.x", "x64");
+    }
 
     // Install requirements
     await exec.exec("pip", [
@@ -1411,6 +1401,43 @@ if (process.env.NODE_DEBUG && /\btunnel\b/.test(process.env.NODE_DEBUG)) {
 exports.debug = debug; // for test
 
 
+/***/ }),
+
+/***/ 160:
+/***/ (function(module, __unusedexports, __webpack_require__) {
+
+"use strict";
+
+const fs = __webpack_require__(747);
+
+let isDocker;
+
+function hasDockerEnv() {
+	try {
+		fs.statSync('/.dockerenv');
+		return true;
+	} catch (_) {
+		return false;
+	}
+}
+
+function hasDockerCGroup() {
+	try {
+		return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
+	} catch (_) {
+		return false;
+	}
+}
+
+module.exports = () => {
+	if (isDocker === undefined) {
+		isDocker = hasDockerEnv() || hasDockerCGroup();
+	}
+
+	return isDocker;
+};
+
+
 /***/ }),
 
 /***/ 211:
diff --git a/index.js b/index.js
index 2f362c6..c4575ea 100644
--- a/index.js
+++ b/index.js
@@ -1,32 +1,22 @@
 const { inspect } = require("util");
+const isDocker = require('is-docker');
 const fs = require("fs");
 const core = require("@actions/core");
 const exec = require("@actions/exec");
 const setupPython = require("./src/setup-python");
 
-function fileExists(path) {
-  try {
-    return fs.statSync(path).isFile();
-  } catch (e) {
-    core.debug(`e: ${inspect(e)}`);
-    return false;
-  }
-}
-
 async function run() {
   try {
     // Allows ncc to find assets to be included in the distribution
     const src = __dirname + "/src";
     core.debug(`src: ${src}`);
 
-    // Check if the platfrom is Alpine Linux
-    const alpineLinux = fileExists("/etc/alpine-release");
-    core.debug(`alpineLinux: ${alpineLinux}`);
-
-    // Skip Python setup if the platform is Alpine Linux
-    if (!alpineLinux)
+    if (isDocker()) {
+      core.info('Running inside a Docker container');
+    } else {
       // Setup Python from the tool cache
       setupPython("3.8.x", "x64");
+    }
 
     // Install requirements
     await exec.exec("pip", [
diff --git a/package-lock.json b/package-lock.json
index 18e22c3..2c569ba 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -41,6 +41,11 @@
       "integrity": "sha512-M9WzgquSOt2nsjRkYM9LRylBLmmlwNCwYbm3Up3PDEshfvdmIfqpFNSK8EJvR18NwZjGHE5z2avlDtYQx2JQnw==",
       "dev": true
     },
+    "is-docker": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.0.0.tgz",
+      "integrity": "sha512-pJEdRugimx4fBMra5z2/5iRdZ63OhYV0vr0Dwm5+xtW4D1FvRkB8hamMIhnWfyJeDdyr/aa7BDyNbtG38VxgoQ=="
+    },
     "qs": {
       "version": "6.9.1",
       "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.1.tgz",
diff --git a/package.json b/package.json
index f0778cf..3453a37 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,8 @@
   "dependencies": {
     "@actions/core": "^1.1.1",
     "@actions/exec": "^1.0.1",
-    "@actions/tool-cache": "^1.1.2"
+    "@actions/tool-cache": "^1.1.2",
+    "is-docker": "^2.0.0"
   },
   "devDependencies": {
     "@zeit/ncc": "0.21.1"

From 4beea725d361a3a2bdf0aa2938f46ce8c7dae36e Mon Sep 17 00:00:00 2001
From: Peter Evans <peter-evans@users.noreply.github.com>
Date: Thu, 13 Feb 2020 17:36:57 +0900
Subject: [PATCH 2/2] Call python3 when running in a container

---
 dist/index.js | 31 +++++++++++++++++++++----------
 index.js      | 33 ++++++++++++++++++++++-----------
 2 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index 5bd8357..4fcff7f 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1002,7 +1002,6 @@ module.exports = require("os");
 
 const { inspect } = __webpack_require__(669);
 const isDocker = __webpack_require__(160);
-const fs = __webpack_require__(747);
 const core = __webpack_require__(470);
 const exec = __webpack_require__(986);
 const setupPython = __webpack_require__(139);
@@ -1013,15 +1012,27 @@ async function run() {
     const src = __webpack_require__.ab + "src";
     core.debug(`src: ${src}`);
 
-    if (isDocker()) {
-      core.info('Running inside a Docker container');
-    } else {
-      // Setup Python from the tool cache
-      setupPython("3.8.x", "x64");
-    }
+    // Determine how to access python and pip
+    const { pip, python } = (function() {
+      if (isDocker()) {
+        core.info("Running inside a Docker container");
+        // Python 3 assumed to be installed and on the PATH
+        return {
+          pip: "pip3",
+          python: "python3"
+        };
+      } else {
+        // Setup Python from the tool cache
+        setupPython("3.x", "x64");
+        return {
+          pip: "pip",
+          python: "python"
+        };
+      }
+    })();
 
     // Install requirements
-    await exec.exec("pip", [
+    await exec.exec(pip, [
       "install",
       "--requirement",
       `${src}/requirements.txt`,
@@ -1047,7 +1058,7 @@ async function run() {
       projectColumn: core.getInput("project-column"),
       branch: core.getInput("branch"),
       base: core.getInput("base"),
-      branchSuffix: core.getInput("branch-suffix"),
+      branchSuffix: core.getInput("branch-suffix")
     };
     core.debug(`Inputs: ${inspect(inputs)}`);
 
@@ -1071,7 +1082,7 @@ async function run() {
     if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix;
 
     // Execute python script
-    await exec.exec("python", [`${src}/create_pull_request.py`]);
+    await exec.exec(python, [`${src}/create_pull_request.py`]);
   } catch (error) {
     core.setFailed(error.message);
   }
diff --git a/index.js b/index.js
index c4575ea..3dce607 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,5 @@
 const { inspect } = require("util");
-const isDocker = require('is-docker');
-const fs = require("fs");
+const isDocker = require("is-docker");
 const core = require("@actions/core");
 const exec = require("@actions/exec");
 const setupPython = require("./src/setup-python");
@@ -11,15 +10,27 @@ async function run() {
     const src = __dirname + "/src";
     core.debug(`src: ${src}`);
 
-    if (isDocker()) {
-      core.info('Running inside a Docker container');
-    } else {
-      // Setup Python from the tool cache
-      setupPython("3.8.x", "x64");
-    }
+    // Determine how to access python and pip
+    const { pip, python } = (function() {
+      if (isDocker()) {
+        core.info("Running inside a Docker container");
+        // Python 3 assumed to be installed and on the PATH
+        return {
+          pip: "pip3",
+          python: "python3"
+        };
+      } else {
+        // Setup Python from the tool cache
+        setupPython("3.x", "x64");
+        return {
+          pip: "pip",
+          python: "python"
+        };
+      }
+    })();
 
     // Install requirements
-    await exec.exec("pip", [
+    await exec.exec(pip, [
       "install",
       "--requirement",
       `${src}/requirements.txt`,
@@ -45,7 +56,7 @@ async function run() {
       projectColumn: core.getInput("project-column"),
       branch: core.getInput("branch"),
       base: core.getInput("base"),
-      branchSuffix: core.getInput("branch-suffix"),
+      branchSuffix: core.getInput("branch-suffix")
     };
     core.debug(`Inputs: ${inspect(inputs)}`);
 
@@ -69,7 +80,7 @@ async function run() {
     if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix;
 
     // Execute python script
-    await exec.exec("python", [`${src}/create_pull_request.py`]);
+    await exec.exec(python, [`${src}/create_pull_request.py`]);
   } catch (error) {
     core.setFailed(error.message);
   }