Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit c5d6949

Browse files
Merge pull request #623 from github-for-unity/fixes/octorun-user-agent
Providing Octorun JS with a user agent
2 parents 6789164 + c124acd commit c5d6949

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

octorun/.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
OCTOKIT_CLIENT_ID=
22
OCTOKIT_CLIENT_SECRET=
3+
OCTOKIT_USER_AGENT=
34
OCTORUN_USER=
45
OCTORUN_TOKEN=

octorun/src/api.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ function ApiWrapper() {
55
this.octokit = octokitWrapper.createOctokit();
66

77
if (!config.user || !config.token) {
8-
throw "User and/or Token missing";
8+
throw "user and/or token missing";
9+
}
10+
11+
if (!config.appName) {
12+
throw "appName missing";
913
}
1014

1115
this.octokit.authenticate({

octorun/src/authentication.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ var twoFactorRegex = new RegExp("must specify two-factor authentication otp code
99
var badCredentialsRegex = new RegExp("bad credentials", "gi");
1010

1111
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) {
12+
if (!config.clientId || !config.clientSecret) {
13+
throw "clientId and/or clientSecret missing";
14+
}
15+
16+
if (!config.appName) {
17+
throw "appName missing";
18+
}
19+
1220
var octokit = octokitWrapper.createOctokit();
1321

1422
octokit.authenticate({
@@ -44,6 +52,14 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
4452
}
4553

4654
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) {
55+
if (!config.clientId || !config.clientSecret) {
56+
throw "clientId and/or clientSecret missing";
57+
}
58+
59+
if (!config.appName) {
60+
throw "appName missing";
61+
}
62+
4763
var octokit = octokitWrapper.createOctokit();
4864

4965
octokit.authenticate({
@@ -54,11 +70,11 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
5470

5571
octokit.authorization.create({
5672
scopes: scopes,
57-
note: config.appName,
5873
client_id: config.clientId,
5974
client_secret: config.clientSecret,
6075
headers: {
61-
"X-GitHub-OTP": twoFactor
76+
"X-GitHub-OTP": twoFactor,
77+
"user-agent": config.appName
6278
}
6379
}, function (err, res) {
6480
if (err) {

octorun/src/bin/app-login.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ if (commander.twoFactor) {
3131

3232
var twoFactor = readlineSync.question('Two Factor: ');
3333

34-
handleTwoFactorAuthentication(username, password, twoFactor);
34+
try {
35+
handleTwoFactorAuthentication(username, password, twoFactor);
36+
}
37+
catch (error) {
38+
output.error(error);
39+
process.exit();
40+
}
3541
}
3642
else {
3743
var data = '';
@@ -49,7 +55,13 @@ if (commander.twoFactor) {
4955
.split(/\r?\n/)
5056
.filter(function (item) { return item; });
5157

52-
handleTwoFactorAuthentication(items[0], items[1], items[2]);
58+
try {
59+
handleTwoFactorAuthentication(items[0], items[1], items[2]);
60+
}
61+
catch (error) {
62+
output.error(error);
63+
process.exit();
64+
}
5365
});
5466
}
5567
}
@@ -76,7 +88,13 @@ else {
7688
hideEchoBack: true
7789
});
7890

79-
handleBasicAuthentication(username, password);
91+
try {
92+
handleBasicAuthentication(username, password);
93+
}
94+
catch (error) {
95+
output.error(error);
96+
process.exit();
97+
}
8098
}
8199
else {
82100
var data = '';
@@ -94,7 +112,13 @@ else {
94112
.split(/\r?\n/)
95113
.filter(function (item) { return item; });
96114

97-
handleBasicAuthentication(items[0], items[1]);
115+
try {
116+
handleBasicAuthentication(items[0], items[1]);
117+
}
118+
catch (error) {
119+
output.error(error);
120+
process.exit();
121+
}
98122
});
99123
}
100124
}

octorun/src/configuration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require("dotenv").config({silent: true});
22

33
var clientId = process.env.OCTOKIT_CLIENT_ID;
44
var clientSecret = process.env.OCTOKIT_CLIENT_SECRET;
5-
var appName = process.env.OCTORUN_APP_NAME | "octorun";
5+
var appName = process.env.OCTOKIT_USER_AGENT;
66
var user = process.env.OCTORUN_USER;
77
var token = process.env.OCTORUN_TOKEN;
88

src/GitHub.Api/Application/ApplicationInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static partial class ApplicationInfo
1010
public const string ApplicationName = "GitHubUnity";
1111
public const string ApplicationProvider = "GitHub";
1212
#endif
13-
public const string ApplicationSafeName = "unity-internal-test";
13+
public const string ApplicationSafeName = "GitHubUnity";
1414
public const string ApplicationDescription = "GitHub for Unity";
1515

1616
internal static string ClientId { get; private set; } = "";

src/GitHub.Api/Tasks/OctorunTask.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Reflection;
45
using System.Threading;
56
using GitHub.Logging;
67

@@ -57,6 +58,7 @@ class OctorunTask : ProcessTask<OctorunResult>
5758
private readonly string userToken;
5859

5960
private readonly NPath pathToNodeJs;
61+
private readonly NPath pathToOctorunJs;
6062
private readonly string arguments;
6163

6264
public OctorunTask(CancellationToken token, NPath pathToNodeJs, NPath pathToOctorunJs, string arguments,
@@ -72,13 +74,18 @@ public OctorunTask(CancellationToken token, NPath pathToNodeJs, NPath pathToOcto
7274
this.user = user;
7375
this.userToken = userToken;
7476
this.pathToNodeJs = pathToNodeJs;
77+
this.pathToOctorunJs = pathToOctorunJs;
7578
this.arguments = $"{pathToOctorunJs} {arguments}";
7679
}
7780

7881
public override void Configure(ProcessStartInfo psi)
7982
{
8083
base.Configure(psi);
8184

85+
psi.WorkingDirectory = pathToOctorunJs.Parent.Parent.Parent;
86+
87+
psi.EnvironmentVariables.Add("OCTOKIT_USER_AGENT", $"{ApplicationInfo.ApplicationSafeName}/{ApplicationInfo.Version}");
88+
8289
if (clientId != null)
8390
{
8491
psi.EnvironmentVariables.Add("OCTOKIT_CLIENT_ID", clientId);

0 commit comments

Comments
 (0)