From a99a39ad8abf0da6e74e22ef75988ec168041845 Mon Sep 17 00:00:00 2001 From: Geoff Lamrock Date: Thu, 24 Aug 2023 08:54:59 +1000 Subject: [PATCH] feat: Adds support for authenticating using an access token (#23) --- __tests__/integration/integration.test.ts | 4 +- dist/index.js | 63 +++++++++++++++++------ package-lock.json | 14 ++--- package.json | 4 +- src/index.ts | 1 + src/input-parameters.ts | 18 ++++--- 6 files changed, 70 insertions(+), 34 deletions(-) diff --git a/__tests__/integration/integration.test.ts b/__tests__/integration/integration.test.ts index 92a71fc4..90d9a705 100644 --- a/__tests__/integration/integration.test.ts +++ b/__tests__/integration/integration.test.ts @@ -226,9 +226,7 @@ describe('integration tests', () => { 60000, (serverTask: ServerTask): void => { // eslint-disable-next-line no-console - console.log( - `Waiting for task ${serverTask.Id}. Current status: ${serverTask.State}` - ) + console.log(`Waiting for task ${serverTask.Id}. Current status: ${serverTask.State}`) } ) }) diff --git a/dist/index.js b/dist/index.js index 3f5ba720..475e4637 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1829,11 +1829,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.AxiosAdapter = void 0; var axios_1 = __importDefault(__nccwpck_require__(8757)); var adapter_1 = __nccwpck_require__(937); +var createRequestHeaders_1 = __nccwpck_require__(7843); var AxiosAdapter = /** @class */ (function () { function AxiosAdapter() { } AxiosAdapter.prototype.execute = function (options) { - var _a, _b; + var _a; return __awaiter(this, void 0, void 0, function () { function formatError(response) { if (!response.data) { @@ -1841,6 +1842,7 @@ var AxiosAdapter = /** @class */ (function () { } var message = response.data.ErrorMessage; if (response.data.Errors) { + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions var errors = response.data.Errors; for (var i = 0; i < errors.length; i++) { message += "\n".concat(errors[i]); @@ -1849,21 +1851,19 @@ var AxiosAdapter = /** @class */ (function () { return message; } var config, userAgent, response, error_1; - return __generator(this, function (_c) { - switch (_c.label) { + return __generator(this, function (_b) { + switch (_b.label) { case 0: - _c.trys.push([0, 2, , 3]); + _b.trys.push([0, 2, , 3]); config = { httpsAgent: options.configuration.httpsAgent, url: options.url, maxContentLength: Infinity, maxBodyLength: Infinity, + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions method: options.method, data: options.requestBody, - headers: { - "Accept-Encoding": "gzip,deflate,compress", - "X-Octopus-ApiKey": (_a = options.configuration.apiKey) !== null && _a !== void 0 ? _a : "", - }, + headers: (0, createRequestHeaders_1.createRequestHeaders)(options.configuration), responseType: "json", }; if (typeof XMLHttpRequest === "undefined") { @@ -1877,15 +1877,15 @@ var AxiosAdapter = /** @class */ (function () { } return [4 /*yield*/, axios_1.default.request(config)]; case 1: - response = _c.sent(); + response = _b.sent(); return [2 /*return*/, { data: response.data, statusCode: response.status, }]; case 2: - error_1 = _c.sent(); + error_1 = _b.sent(); if (axios_1.default.isAxiosError(error_1) && error_1.response) { - throw new adapter_1.AdapterError(error_1.response.status, (_b = formatError(error_1.response)) !== null && _b !== void 0 ? _b : error_1.message); + throw new adapter_1.AdapterError(error_1.response.status, (_a = formatError(error_1.response)) !== null && _a !== void 0 ? _a : error_1.message); } else { throw error_1; @@ -1901,6 +1901,34 @@ var AxiosAdapter = /** @class */ (function () { exports.AxiosAdapter = AxiosAdapter; +/***/ }), + +/***/ 7843: +/***/ ((__unused_webpack_module, exports) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.createRequestHeaders = void 0; +function createRequestHeaders(configuration) { + var headers = { + "Accept-Encoding": "gzip,deflate,compress", // HACK: required for https://github.com/axios/axios/issues/5346 -- this line can be removed once this bug has been fixed + }; + if (configuration.apiKey) { + headers["X-Octopus-ApiKey"] = configuration.apiKey; + } + if (configuration.accessToken) { + headers["Authorization"] = "Bearer ".concat(configuration.accessToken); + } + if (!configuration.accessToken && !configuration.apiKey) { + // Backward compatibility: Add the api key header in with a blank value + headers["X-Octopus-ApiKey"] = ""; + } + return headers; +} +exports.createRequestHeaders = createRequestHeaders; + + /***/ }), /***/ 1542: @@ -42344,6 +42372,7 @@ const api_wrapper_1 = __nccwpck_require__(4636); userAgentApp: 'GitHubActions (release;deploy;v3)', instanceURL: parameters.server, apiKey: parameters.apiKey, + accessToken: parameters.accessToken, logging: logger }; const client = yield api_client_1.Client.create(config); @@ -42385,6 +42414,7 @@ const core_1 = __nccwpck_require__(2186); const EnvironmentVariables = { URL: 'OCTOPUS_URL', ApiKey: 'OCTOPUS_API_KEY', + AccessToken: 'OCTOPUS_ACCESS_TOKEN', Space: 'OCTOPUS_SPACE' }; function getInputParameters() { @@ -42399,7 +42429,8 @@ function getInputParameters() { } const parameters = { server: (0, core_1.getInput)('server') || process.env[EnvironmentVariables.URL] || '', - apiKey: (0, core_1.getInput)('api_key') || process.env[EnvironmentVariables.ApiKey] || '', + apiKey: (0, core_1.getInput)('api_key') || process.env[EnvironmentVariables.ApiKey], + accessToken: process.env[EnvironmentVariables.AccessToken], space: (0, core_1.getInput)('space') || process.env[EnvironmentVariables.Space] || '', project: (0, core_1.getInput)('project', { required: true }), releaseNumber: (0, core_1.getInput)('release_number', { required: true }), @@ -42409,13 +42440,13 @@ function getInputParameters() { }; const errors = []; if (!parameters.server) { - errors.push("The Octopus instance URL is required, please specify explictly through the 'server' input or set the OCTOPUS_URL environment variable."); + errors.push("The Octopus instance URL is required, please specify explicitly through the 'server' input or set the OCTOPUS_URL environment variable."); } - if (!parameters.apiKey) { - errors.push("The Octopus API Key is required, please specify explictly through the 'api_key' input or set the OCTOPUS_API_KEY environment variable."); + if (!parameters.apiKey && !parameters.accessToken) { + errors.push("The Octopus API Key is required, please specify explicitly through the 'api_key' input or set the OCTOPUS_API_KEY environment variable."); } if (!parameters.space) { - errors.push("The Octopus space name is required, please specify explictly through the 'space' input or set the OCTOPUS_SPACE environment variable."); + errors.push("The Octopus space name is required, please specify explicitly through the 'space' input or set the OCTOPUS_SPACE environment variable."); } if (errors.length > 0) { throw new Error(errors.join('\n')); diff --git a/package-lock.json b/package-lock.json index fd523418..22b99bf5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@actions/core": "^1.10.0", - "@octopusdeploy/api-client": "^3.0.7" + "@octopusdeploy/api-client": "^3.1.0" }, "devDependencies": { "@octopusdeploy/runtime-inputs": "^0.16.0", @@ -1226,9 +1226,9 @@ } }, "node_modules/@octopusdeploy/api-client": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/@octopusdeploy/api-client/-/api-client-3.0.7.tgz", - "integrity": "sha512-6+6/+ARTqldNVwTZGfIvPMX0ZvELo2HRSbm97VaFxFZPu8PWJOzS9yCwfugta0MWRNpPNOVjjC5wd0joSTgC+Q==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@octopusdeploy/api-client/-/api-client-3.1.0.tgz", + "integrity": "sha512-n/HtwCExJdTYOHdFb47+y2bN7MOPIDayNAXZZtWqKMTxHmXN2sqYNq5NS/SVHvFYsbxFi/hpNlrwpLufg8mOWQ==", "dependencies": { "adm-zip": "^0.5.9", "axios": "^1.2.1", @@ -7427,9 +7427,9 @@ } }, "@octopusdeploy/api-client": { - "version": "3.0.7", - "resolved": "https://registry.npmjs.org/@octopusdeploy/api-client/-/api-client-3.0.7.tgz", - "integrity": "sha512-6+6/+ARTqldNVwTZGfIvPMX0ZvELo2HRSbm97VaFxFZPu8PWJOzS9yCwfugta0MWRNpPNOVjjC5wd0joSTgC+Q==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@octopusdeploy/api-client/-/api-client-3.1.0.tgz", + "integrity": "sha512-n/HtwCExJdTYOHdFb47+y2bN7MOPIDayNAXZZtWqKMTxHmXN2sqYNq5NS/SVHvFYsbxFi/hpNlrwpLufg8mOWQ==", "requires": { "adm-zip": "^0.5.9", "axios": "^1.2.1", diff --git a/package.json b/package.json index 323099f2..a0f0ff17 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ }, "dependencies": { "@actions/core": "^1.10.0", - "@octopusdeploy/api-client": "^3.0.7" + "@octopusdeploy/api-client": "^3.1.0" }, "description": "GitHub Action to Create a Release in Octopus Deploy", "devDependencies": { @@ -85,4 +85,4 @@ "test:integration": "jest --ci --reporters=default --reporters=jest-junit --testPathPattern=__tests__/integration" }, "version": "3.0.4" -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 0e4dd70d..2819e09d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ import { createDeploymentFromInputs } from './api-wrapper' userAgentApp: 'GitHubActions (release;deploy;v3)', instanceURL: parameters.server, apiKey: parameters.apiKey, + accessToken: parameters.accessToken, logging: logger } diff --git a/src/input-parameters.ts b/src/input-parameters.ts index e5b08c62..87e4f133 100644 --- a/src/input-parameters.ts +++ b/src/input-parameters.ts @@ -4,6 +4,7 @@ import { PromptedVariableValues } from '@octopusdeploy/api-client' const EnvironmentVariables = { URL: 'OCTOPUS_URL', ApiKey: 'OCTOPUS_API_KEY', + AccessToken: 'OCTOPUS_ACCESS_TOKEN', Space: 'OCTOPUS_SPACE' } as const @@ -11,7 +12,9 @@ export interface InputParameters { // Optional: A server is required, but you should use the OCTOPUS_URL env server: string // Optional: An API key is required, but you should use the OCTOPUS_API_KEY environment variable instead of this. - apiKey: string + apiKey?: string + // Optional: Access token can only be obtained from the OCTOPUS_ACCESS_TOKEN environment variable. + accessToken?: string // Optional: You should prefer the OCTOPUS_SPACE environment variable space: string // Required @@ -37,7 +40,8 @@ export function getInputParameters(): InputParameters { const parameters: InputParameters = { server: getInput('server') || process.env[EnvironmentVariables.URL] || '', - apiKey: getInput('api_key') || process.env[EnvironmentVariables.ApiKey] || '', + apiKey: getInput('api_key') || process.env[EnvironmentVariables.ApiKey], + accessToken: process.env[EnvironmentVariables.AccessToken], space: getInput('space') || process.env[EnvironmentVariables.Space] || '', project: getInput('project', { required: true }), releaseNumber: getInput('release_number', { required: true }), @@ -49,17 +53,19 @@ export function getInputParameters(): InputParameters { const errors: string[] = [] if (!parameters.server) { errors.push( - "The Octopus instance URL is required, please specify explictly through the 'server' input or set the OCTOPUS_URL environment variable." + "The Octopus instance URL is required, please specify explicitly through the 'server' input or set the OCTOPUS_URL environment variable." ) } - if (!parameters.apiKey) { + + if (!parameters.apiKey && !parameters.accessToken) { errors.push( - "The Octopus API Key is required, please specify explictly through the 'api_key' input or set the OCTOPUS_API_KEY environment variable." + "The Octopus API Key is required, please specify explicitly through the 'api_key' input or set the OCTOPUS_API_KEY environment variable." ) } + if (!parameters.space) { errors.push( - "The Octopus space name is required, please specify explictly through the 'space' input or set the OCTOPUS_SPACE environment variable." + "The Octopus space name is required, please specify explicitly through the 'space' input or set the OCTOPUS_SPACE environment variable." ) }