From 4a87176a98fe1d813454daa915a18a395bb4b953 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 9 Mar 2018 10:16:38 -0500 Subject: [PATCH 1/4] Octorun user-agent changes --- octorun/.env.template | 1 + octorun/src/authentication.js | 4 ++-- octorun/src/configuration.js | 2 +- src/GitHub.Api/Tasks/OctorunTask.cs | 3 +++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/octorun/.env.template b/octorun/.env.template index 2c1f93479..a42e21c4a 100644 --- a/octorun/.env.template +++ b/octorun/.env.template @@ -1,4 +1,5 @@ OCTOKIT_CLIENT_ID= OCTOKIT_CLIENT_SECRET= +OCTOKIT_USER_AGENT= OCTORUN_USER= OCTORUN_TOKEN= \ No newline at end of file diff --git a/octorun/src/authentication.js b/octorun/src/authentication.js index 7f7b45685..c085361f9 100644 --- a/octorun/src/authentication.js +++ b/octorun/src/authentication.js @@ -54,11 +54,11 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS octokit.authorization.create({ scopes: scopes, - note: config.appName, client_id: config.clientId, client_secret: config.clientSecret, headers: { - "X-GitHub-OTP": twoFactor + "X-GitHub-OTP": twoFactor, + "user-agent": config.appName } }, function (err, res) { if (err) { diff --git a/octorun/src/configuration.js b/octorun/src/configuration.js index ba5ad4447..2e90db630 100644 --- a/octorun/src/configuration.js +++ b/octorun/src/configuration.js @@ -2,7 +2,7 @@ require("dotenv").config({silent: true}); var clientId = process.env.OCTOKIT_CLIENT_ID; var clientSecret = process.env.OCTOKIT_CLIENT_SECRET; -var appName = process.env.OCTORUN_APP_NAME | "octorun"; +var appName = process.env.OCTOKIT_USER_AGENT | "octorun"; var user = process.env.OCTORUN_USER; var token = process.env.OCTORUN_TOKEN; diff --git a/src/GitHub.Api/Tasks/OctorunTask.cs b/src/GitHub.Api/Tasks/OctorunTask.cs index a8e17bda3..374a882bd 100644 --- a/src/GitHub.Api/Tasks/OctorunTask.cs +++ b/src/GitHub.Api/Tasks/OctorunTask.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Diagnostics; +using System.Reflection; using System.Threading; namespace GitHub.Unity @@ -29,6 +30,8 @@ public override void Configure(ProcessStartInfo psi) { base.Configure(psi); + psi.EnvironmentVariables.Add("OCTOKIT_USER_AGENT", ApplicationInfo.ApplicationSafeName+ AssemblyName.Version.ToString()); + if (clientId != null) { psi.EnvironmentVariables.Add("OCTOKIT_CLIENT_ID", clientId); From c3b63707eac806f70d85327742b91b8e9763c5c6 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 9 Mar 2018 14:58:37 -0500 Subject: [PATCH 2/4] Proving the user agent to octokit --- octorun/src/api.js | 6 +++- octorun/src/authentication.js | 16 ++++++++++ octorun/src/bin/app-login.js | 32 ++++++++++++++++--- octorun/src/configuration.js | 2 +- src/GitHub.Api/Application/ApplicationInfo.cs | 2 +- src/GitHub.Api/Tasks/OctorunTask.cs | 2 +- 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/octorun/src/api.js b/octorun/src/api.js index b447f564e..32322ef6a 100644 --- a/octorun/src/api.js +++ b/octorun/src/api.js @@ -5,7 +5,11 @@ function ApiWrapper() { this.octokit = octokitWrapper.createOctokit(); if (!config.user || !config.token) { - throw "User and/or Token missing"; + throw "user and/or token missing"; + } + + if (!config.appName) { + throw "appName missing"; } this.octokit.authenticate({ diff --git a/octorun/src/authentication.js b/octorun/src/authentication.js index c085361f9..e2d082100 100644 --- a/octorun/src/authentication.js +++ b/octorun/src/authentication.js @@ -9,6 +9,14 @@ var twoFactorRegex = new RegExp("must specify two-factor authentication otp code var badCredentialsRegex = new RegExp("bad credentials", "gi"); var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) { + if (!config.clientId || !config.clientSecret) { + throw "clientId and/or clientSecret missing"; + } + + if (!config.appName) { + throw "appName missing"; + } + var octokit = octokitWrapper.createOctokit(); octokit.authenticate({ @@ -44,6 +52,14 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi } var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) { + if (!config.clientId || !config.clientSecret) { + throw "clientId and/or clientSecret missing"; + } + + if (!config.appName) { + throw "appName missing"; + } + var octokit = octokitWrapper.createOctokit(); octokit.authenticate({ diff --git a/octorun/src/bin/app-login.js b/octorun/src/bin/app-login.js index 0e450bfd4..eec473f4d 100644 --- a/octorun/src/bin/app-login.js +++ b/octorun/src/bin/app-login.js @@ -31,7 +31,13 @@ if (commander.twoFactor) { var twoFactor = readlineSync.question('Two Factor: '); - handleTwoFactorAuthentication(username, password, twoFactor); + try { + handleTwoFactorAuthentication(username, password, twoFactor); + } + catch (error) { + output.error(error); + process.exit(); + } } else { var data = ''; @@ -49,7 +55,13 @@ if (commander.twoFactor) { .split(/\r?\n/) .filter(function (item) { return item; }); - handleTwoFactorAuthentication(items[0], items[1], items[2]); + try { + handleTwoFactorAuthentication(items[0], items[1], items[2]); + } + catch (error) { + output.error(error); + process.exit(); + } }); } } @@ -76,7 +88,13 @@ else { hideEchoBack: true }); - handleBasicAuthentication(username, password); + try { + handleBasicAuthentication(username, password); + } + catch (error) { + output.error(error); + process.exit(); + } } else { var data = ''; @@ -94,7 +112,13 @@ else { .split(/\r?\n/) .filter(function (item) { return item; }); - handleBasicAuthentication(items[0], items[1]); + try { + handleBasicAuthentication(items[0], items[1]); + } + catch (error) { + output.error(error); + process.exit(); + } }); } } \ No newline at end of file diff --git a/octorun/src/configuration.js b/octorun/src/configuration.js index 2e90db630..f9462acde 100644 --- a/octorun/src/configuration.js +++ b/octorun/src/configuration.js @@ -2,7 +2,7 @@ require("dotenv").config({silent: true}); var clientId = process.env.OCTOKIT_CLIENT_ID; var clientSecret = process.env.OCTOKIT_CLIENT_SECRET; -var appName = process.env.OCTOKIT_USER_AGENT | "octorun"; +var appName = process.env.OCTOKIT_USER_AGENT; var user = process.env.OCTORUN_USER; var token = process.env.OCTORUN_TOKEN; diff --git a/src/GitHub.Api/Application/ApplicationInfo.cs b/src/GitHub.Api/Application/ApplicationInfo.cs index 83b2387cd..4b766f75a 100644 --- a/src/GitHub.Api/Application/ApplicationInfo.cs +++ b/src/GitHub.Api/Application/ApplicationInfo.cs @@ -10,7 +10,7 @@ static partial class ApplicationInfo public const string ApplicationName = "GitHubUnity"; public const string ApplicationProvider = "GitHub"; #endif - public const string ApplicationSafeName = "unity-internal-test"; + public const string ApplicationSafeName = "GitHubUnity"; public const string ApplicationDescription = "GitHub for Unity"; internal static string ClientId { get; private set; } = ""; diff --git a/src/GitHub.Api/Tasks/OctorunTask.cs b/src/GitHub.Api/Tasks/OctorunTask.cs index d6ade22ac..9358e2851 100644 --- a/src/GitHub.Api/Tasks/OctorunTask.cs +++ b/src/GitHub.Api/Tasks/OctorunTask.cs @@ -80,7 +80,7 @@ public override void Configure(ProcessStartInfo psi) { base.Configure(psi); - psi.EnvironmentVariables.Add("OCTOKIT_USER_AGENT", ApplicationInfo.ApplicationSafeName+ AssemblyName.Version.ToString()); + psi.EnvironmentVariables.Add("OCTOKIT_USER_AGENT", $"{ApplicationInfo.ApplicationSafeName}/{ApplicationInfo.Version}"); if (clientId != null) { From a2c4b5cb6a644b8941a976bac99989d0bee4c701 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 9 Mar 2018 15:00:40 -0500 Subject: [PATCH 3/4] Setting the working directory of octorun --- src/GitHub.Api/Tasks/OctorunTask.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/GitHub.Api/Tasks/OctorunTask.cs b/src/GitHub.Api/Tasks/OctorunTask.cs index b435b1996..20119b400 100644 --- a/src/GitHub.Api/Tasks/OctorunTask.cs +++ b/src/GitHub.Api/Tasks/OctorunTask.cs @@ -57,6 +57,7 @@ class OctorunTask : ProcessTask private readonly string userToken; private readonly NPath pathToNodeJs; + private readonly NPath pathToOctorunJs; private readonly string arguments; public OctorunTask(CancellationToken token, NPath pathToNodeJs, NPath pathToOctorunJs, string arguments, @@ -72,6 +73,7 @@ public OctorunTask(CancellationToken token, NPath pathToNodeJs, NPath pathToOcto this.user = user; this.userToken = userToken; this.pathToNodeJs = pathToNodeJs; + this.pathToOctorunJs = pathToOctorunJs; this.arguments = $"{pathToOctorunJs} {arguments}"; } @@ -79,6 +81,8 @@ public override void Configure(ProcessStartInfo psi) { base.Configure(psi); + psi.WorkingDirectory = pathToOctorunJs.Parent.Parent; + if (clientId != null) { psi.EnvironmentVariables.Add("OCTOKIT_CLIENT_ID", clientId); From bc30989accf167d64fb4a8c1d1bf1aefc0d2c2a2 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 9 Mar 2018 15:08:30 -0500 Subject: [PATCH 4/4] Correcting the path --- src/GitHub.Api/Tasks/OctorunTask.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub.Api/Tasks/OctorunTask.cs b/src/GitHub.Api/Tasks/OctorunTask.cs index 20119b400..5c9f1e8ff 100644 --- a/src/GitHub.Api/Tasks/OctorunTask.cs +++ b/src/GitHub.Api/Tasks/OctorunTask.cs @@ -81,7 +81,7 @@ public override void Configure(ProcessStartInfo psi) { base.Configure(psi); - psi.WorkingDirectory = pathToOctorunJs.Parent.Parent; + psi.WorkingDirectory = pathToOctorunJs.Parent.Parent.Parent; if (clientId != null) {