diff --git a/lib/Utils/CommonUtils.js b/lib/Utils/CommonUtils.js index 519e0835..2f3c12d7 100644 --- a/lib/Utils/CommonUtils.js +++ b/lib/Utils/CommonUtils.js @@ -79,6 +79,7 @@ exports.getDefaultTestRunName = getDefaultTestRunName; exports.getDefaultRunDescription = getDefaultRunDescription; exports.validateTestRunParamsFromPipeline = validateTestRunParamsFromPipeline; exports.getAllFileErrors = getAllFileErrors; +exports.sanitisePipelineNameHeader = sanitisePipelineNameHeader; const { v4: uuidv4 } = require('uuid'); const util_1 = require("util"); const GeneralConstants_1 = require("../Constants/GeneralConstants"); @@ -484,3 +485,26 @@ function getAllFileErrors(testObj) { } return fileErrors; } +/** + * This function returns the string with only ascii charaters, removing the non-ascii characters. + * @param pipelineName - original pipeline name + * @returns sanitised pipeline name with only ascii characters + */ +function sanitisePipelineNameHeader(pipelineName) { + if (!pipelineName) { + return pipelineName; + } + let result = ""; + for (const ch of pipelineName) { + const code = ch.codePointAt(0); + const allowed = (code >= 32 && code <= 126); // ASCII characters range, the only allowed characters in headers. + if (allowed) { + result += ch; + } + } + result = result.trim(); + if (result.length == 0) { + result = "-"; // this is what GH does when i try to give all non-ascii characters in the repo name. + } + return result; +} diff --git a/lib/Utils/FetchUtils.js b/lib/Utils/FetchUtils.js index afdc125b..eef22847 100644 --- a/lib/Utils/FetchUtils.js +++ b/lib/Utils/FetchUtils.js @@ -82,7 +82,7 @@ function httpClientRetries(urlSuffix_1, header_1, method_1) { const runId = process.env.GITHUB_RUN_ID; const pipelineName = process.env.GITHUB_WORKFLOW || "Unknown Pipeline"; const pipelineUri = `${githubBaseUrl}/${repository}/actions/runs/${runId}`; - header['x-ms-pipeline-name'] = pipelineName; // setting these for patch calls. + header['x-ms-pipeline-name'] = (0, CommonUtils_1.sanitisePipelineNameHeader)(pipelineName); // setting these for patch calls. header['x-ms-pipeline-uri'] = pipelineUri; httpResponse = yield httpClient.request(methodEnumToString[method], urlSuffix, data, header); } diff --git a/src/Utils/CommonUtils.ts b/src/Utils/CommonUtils.ts index 377d0076..68f2d5eb 100644 --- a/src/Utils/CommonUtils.ts +++ b/src/Utils/CommonUtils.ts @@ -444,3 +444,27 @@ export function getAllFileErrors(testObj:TestModel | null): { [key: string]: str return fileErrors; } + +/** + * This function returns the string with only ascii charaters, removing the non-ascii characters. + * @param pipelineName - original pipeline name + * @returns sanitised pipeline name with only ascii characters + */ +export function sanitisePipelineNameHeader(pipelineName: string | null): string | null { + if(!pipelineName) { + return pipelineName; + } + let result = ""; + for (const ch of pipelineName) { + const code = ch.codePointAt(0)!; + const allowed = (code >= 32 && code <= 126); // ASCII characters range, the only allowed characters in headers. + if(allowed) { + result += ch; + } + } + result = result.trim(); + if(result.length == 0) { + result = "-"; // this is what GH does when i try to give all non-ascii characters in the repo name. + } + return result; +} \ No newline at end of file diff --git a/src/Utils/FetchUtils.ts b/src/Utils/FetchUtils.ts index 373427f3..9342b63c 100644 --- a/src/Utils/FetchUtils.ts +++ b/src/Utils/FetchUtils.ts @@ -1,5 +1,5 @@ import { IHeaders, IHttpClientResponse } from 'typed-rest-client/Interfaces'; -import { errorCorrection, getResultObj, getUniqueId, sleep } from './CommonUtils'; +import { errorCorrection, getResultObj, getUniqueId, sanitisePipelineNameHeader, sleep } from './CommonUtils'; import { FetchCallType, correlationHeader } from './../models/UtilModels'; import * as httpc from 'typed-rest-client/HttpClient'; import { uploadFileData } from './FileUtils'; @@ -37,7 +37,7 @@ export async function httpClientRetries(urlSuffix : string, header : IHeaders, m const pipelineName = process.env.GITHUB_WORKFLOW || "Unknown Pipeline"; const pipelineUri = `${githubBaseUrl}/${repository}/actions/runs/${runId}`; - header['x-ms-pipeline-name'] = pipelineName; // setting these for patch calls. + header['x-ms-pipeline-name'] = sanitisePipelineNameHeader(pipelineName); // setting these for patch calls. header['x-ms-pipeline-uri'] = pipelineUri; httpResponse = await httpClient.request(methodEnumToString[method], urlSuffix, data, header); } diff --git a/test/CommonUtils.test.ts b/test/CommonUtils.test.ts new file mode 100644 index 00000000..e9f7bde2 --- /dev/null +++ b/test/CommonUtils.test.ts @@ -0,0 +1,48 @@ +import { sanitisePipelineNameHeader } from "../src/Utils/CommonUtils"; +describe("CommonUtils tests", () => { + it.each([ + { + input: "Pipeline@2025#Release$!", + expected: "Pipeline@2025#Release$!" + }, + { + input: "Build_Definition-01 (Test) ", + expected: "Build_Definition-01 (Test)" + }, + { + input: "Normal Name", + expected: "Normal Name" + }, + { + input: "Special*&^%$#@!Characters", + expected: "Special*&^%$#@!Characters" + }, + { + input: "", + expected: "" + }, + { + input: " ", + expected: "-" + }, + { + input: "Name_with_underscores_and-dashes", + expected: "Name_with_underscores_and-dashes" + }, + { + input: null, + expected: null + }, + { + input: "🚀 Deploy", + expected: "Deploy" + }, + { + input: "流水线-test-𰻞", + expected: "-test-" + } + ])("sanitisePipelineNameHeader removes special characters", ({ input, expected }) => { + const result = sanitisePipelineNameHeader(input); + expect(result).toBe(expected); + }); +}); \ No newline at end of file