Skip to content

Commit 39a084a

Browse files
committed
adding user-agent
1 parent 0dae1f1 commit 39a084a

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

lib/main.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
1717
};
1818
Object.defineProperty(exports, "__esModule", { value: true });
1919
const core = __importStar(require("@actions/core"));
20+
const crypto = __importStar(require("crypto"));
2021
const exec = __importStar(require("@actions/exec"));
2122
const io = __importStar(require("@actions/io"));
2223
const actions_secret_parser_1 = require("actions-secret-parser");
2324
var azPath;
25+
var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : "";
2426
function main() {
2527
return __awaiter(this, void 0, void 0, function* () {
2628
try {
29+
// Set user agent varable
30+
let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
31+
let actionName = 'AzureLogin';
32+
let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS_${actionName}_${usrAgentRepo}`;
33+
core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString);
2734
azPath = yield io.which("az", true);
2835
yield executeAzCliCommand("--version");
2936
let creds = core.getInput('creds', { required: true });
@@ -33,16 +40,20 @@ function main() {
3340
let tenantId = secrets.getSecret("$.tenantId", false);
3441
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
3542
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
36-
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows");
43+
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
3744
}
3845
yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`);
3946
yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`);
4047
console.log("Login successful.");
4148
}
4249
catch (error) {
43-
console.log("Login failed. Please check the credentials.");
50+
core.error("Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows");
4451
core.setFailed(error);
4552
}
53+
finally {
54+
// Reset AZURE_HTTP_USER_AGENT
55+
core.exportVariable('AZURE_HTTP_USER_AGENT', prefix);
56+
}
4657
});
4758
}
4859
function executeAzCliCommand(command) {

src/main.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import * as core from '@actions/core';
2+
import * as crypto from "crypto";
23
import * as exec from '@actions/exec';
34
import * as io from '@actions/io';
45

56
import { FormatType, SecretParser } from 'actions-secret-parser';
67

78
var azPath: string;
9+
var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : "";
810

911
async function main() {
1012
try{
13+
// Set user agent varable
14+
let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
15+
let actionName = 'AzureLogin';
16+
let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS_${actionName}_${usrAgentRepo}`;
17+
core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString);
18+
1119
azPath = await io.which("az", true);
1220
await executeAzCliCommand("--version");
1321

@@ -18,15 +26,18 @@ async function main() {
1826
let tenantId = secrets.getSecret("$.tenantId", false);
1927
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
2028
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
21-
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows");
29+
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
2230
}
2331

2432
await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`);
2533
await executeAzCliCommand(`account set --subscription "${subscriptionId}"`);
2634
console.log("Login successful.");
2735
} catch (error) {
28-
console.log("Login failed. Please check the credentials.");
36+
core.error("Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows");
2937
core.setFailed(error);
38+
} finally {
39+
// Reset AZURE_HTTP_USER_AGENT
40+
core.exportVariable('AZURE_HTTP_USER_AGENT', prefix);
3041
}
3142
}
3243

0 commit comments

Comments
 (0)