Skip to content

Commit 2a05678

Browse files
committed
[feat] Add error handling and rename url map type
1 parent 21d7060 commit 2a05678

File tree

5 files changed

+73
-47
lines changed

5 files changed

+73
-47
lines changed

dist/index.js

+34-21
Original file line numberDiff line numberDiff line change
@@ -34261,34 +34261,49 @@ async function createOrUpdateComment(pullRequestNumber, commentBody) {
3426134261
}
3426234262
}
3426334263
exports.createOrUpdateComment = createOrUpdateComment;
34264-
async function generatePRComment(testResultUrlsMap) {
34264+
async function generatePRComment(testRunUrlsMap) {
3426534265
/**
34266-
* This function generates the body of the action comment.
34267-
*
34268-
* @param {any} testResults - The test results
34269-
*
34270-
* @returns {string} - The body of the action comment
34266+
* This function posts/updates a comment containing the test run URLs if a pull request is present.
3427134267
*
34268+
* @param {TestRunUrlsMap} testResults - Map of test run URLs where the key is the script path
34269+
* and the value is the test run URL
3427234270
*
3427334271
* */
34272+
if (Object.keys(testRunUrlsMap).length === 0) {
34273+
core.debug('No test result URLs found, skipping comment creation');
34274+
return;
34275+
}
3427434276
core.debug('Generating PR comment');
3427534277
let testRunUrls = '';
34276-
for (const [scriptPath, testRunUrl] of Object.entries(testResultUrlsMap)) {
34278+
for (const [scriptPath, testRunUrl] of Object.entries(testRunUrlsMap)) {
3427734279
testRunUrls += `🔗 [${scriptPath}](${testRunUrl})\n`;
3427834280
}
3427934281
let comment = `# Performance Test Results 🚀
3428034282

34281-
Click on the links below to view the test results on Grafana Cloud K6:
34283+
Select a test run from below to view the test progress and results on Grafana Cloud K6:
3428234284

3428334285
${testRunUrls}
3428434286
`;
34285-
const pullRequestNumber = await getPullRequestNumber();
34287+
let pullRequestNumber;
34288+
try {
34289+
pullRequestNumber = await getPullRequestNumber();
34290+
}
34291+
catch (error) {
34292+
core.error(`Error getting pull request number`);
34293+
core.error(error);
34294+
}
3428634295
if (!pullRequestNumber) {
3428734296
core.debug('Pull request number not found skipping comment creation');
3428834297
return;
3428934298
}
34290-
await createOrUpdateComment(pullRequestNumber, comment);
34291-
core.debug('Comment created successfully');
34299+
try {
34300+
await createOrUpdateComment(pullRequestNumber, comment);
34301+
core.debug('Comment created successfully');
34302+
}
34303+
catch (error) {
34304+
core.error(`Error creating comment on pull request: ${pullRequestNumber}`);
34305+
core.error(error);
34306+
}
3429234307
}
3429334308
exports.generatePRComment = generatePRComment;
3429434309

@@ -34349,8 +34364,6 @@ async function run() {
3434934364
set: (target, key, value) => {
3435034365
target[key] = value;
3435134366
if (Object.keys(target).length === TOTAL_TEST_RUNS) {
34352-
core.debug('📊 URLs for all the tests gathered');
34353-
core.debug(`📊 Test URLs: ${target}`);
3435434367
if (isCloud) {
3435534368
// Generate PR comment with test run URLs
3435634369
(0, githubHelper_1.generatePRComment)(target);
@@ -34533,14 +34546,14 @@ const REGEX_EXPRESSIONS = {
3453334546
REGEX_EXPRESSIONS.executionProgress,
3453434547
REGEX_EXPRESSIONS.cloudRunExecution
3453534548
];
34536-
function extractTestRunUrl(data, testResultUrlsMap) {
34549+
function extractTestRunUrl(data, testRunUrlsMap) {
3453734550
/**
3453834551
* This function extracts the script path and output URL from the k6 output.
34539-
* It then adds the script path and output URL to the testResultUrlsMap which is a reference to
34552+
* It then adds the script path and output URL to the testRunUrlsMap which is a reference to
3454034553
* an object passed from the main function to store test run urls mapped to corresponding test script.
3454134554
*
3454234555
* @param {string} data - The k6 command output data as string
34543-
* @param {TestResultUrlsMap} testResultUrlsMap - The map containing the script path and output URL
34556+
* @param {TestRunUrlsMap} testRunUrlsMap - The map containing the script path and output URL
3454434557
*
3454534558
* @returns {boolean} - Returns true if the script path and output URL were successfully extracted and added to the map. Otherwise, returns false.
3454634559
*
@@ -34554,7 +34567,7 @@ function extractTestRunUrl(data, testResultUrlsMap) {
3455434567
const outputCloudUrlMatch = output ? output.match(REGEX_EXPRESSIONS.outputCloudUrl) : null;
3455534568
const outputCloudUrl = outputCloudUrlMatch ? outputCloudUrlMatch[1] : output;
3455634569
if (scriptPath && output) {
34557-
testResultUrlsMap[scriptPath] = outputCloudUrl || '';
34570+
testRunUrlsMap[scriptPath] = outputCloudUrl || '';
3455834571
return true;
3455934572
}
3456034573
else {
@@ -34609,22 +34622,22 @@ function checkIfK6ASCIIArt(data) {
3460934622
return true;
3461034623
}
3461134624
}
34612-
function parseK6Output(data, testResultUrlsMap, totalTestRuns) {
34625+
function parseK6Output(data, testRunUrlsMap, totalTestRuns) {
3461334626
/*
3461434627
* This function is responsible for parsing the output of the k6 command.
3461534628
* It filters out the progress lines and logs the rest of the output.
3461634629
* It also extracts the test run URLs from the output.
3461734630
*
3461834631
* @param {Buffer} data - The k6 command output data
34619-
* @param {TestResultUrlsMap | null} testResultUrlsMap - The map containing the script path and output URL. If null, the function will not extract test run URLs.
34632+
* @param {TestRunUrlsMap | null} testRunUrlsMap - The map containing the script path and output URL. If null, the function will not extract test run URLs.
3462034633
* @param {number} totalTestRuns - The total number of test runs. This is used to determine when all test run URLs have been extracted.
3462134634
*
3462234635
* @returns {void}
3462334636
*/
3462434637
const dataString = data.toString(), lines = dataString.split('\n');
3462534638
// Extract test run URLs
34626-
if (testResultUrlsMap && Object.keys(testResultUrlsMap).length < totalTestRuns) {
34627-
if (extractTestRunUrl(dataString, testResultUrlsMap)) {
34639+
if (testRunUrlsMap && Object.keys(testRunUrlsMap).length < totalTestRuns) {
34640+
if (extractTestRunUrl(dataString, testRunUrlsMap)) {
3462834641
// Test URL was extracted successfully and added to the map.
3462934642
// Ignore further output parsing for this data.
3463034643
return;

src/githubHelper.ts

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from '@actions/core';
22
import * as github from '@actions/github';
3+
import { TestRunUrlsMap } from './types';
34

45
const { context } = github;
56
const { eventName, payload } = context;
@@ -112,39 +113,53 @@ export async function createOrUpdateComment(pullRequestNumber: number, commentBo
112113
}
113114
}
114115

115-
export async function generatePRComment(testResultUrlsMap: any) {
116+
export async function generatePRComment(testRunUrlsMap: TestRunUrlsMap): Promise<void> {
116117
/**
117-
* This function generates the body of the action comment.
118+
* This function posts/updates a comment containing the test run URLs if a pull request is present.
118119
*
119-
* @param {any} testResults - The test results
120-
*
121-
* @returns {string} - The body of the action comment
122-
*
120+
* @param {TestRunUrlsMap} testResults - Map of test run URLs where the key is the script path
121+
* and the value is the test run URL
123122
*
124123
* */
125124

125+
if (Object.keys(testRunUrlsMap).length === 0) {
126+
core.debug('No test result URLs found, skipping comment creation');
127+
return;
128+
}
129+
126130
core.debug('Generating PR comment')
127131

128132
let testRunUrls = '';
129-
for (const [scriptPath, testRunUrl] of Object.entries(testResultUrlsMap)) {
133+
for (const [scriptPath, testRunUrl] of Object.entries(testRunUrlsMap)) {
130134
testRunUrls += `🔗 [${scriptPath}](${testRunUrl})\n`;
131135
}
132136

133137
let comment = `# Performance Test Results 🚀
134138
135-
Click on the links below to view the test results on Grafana Cloud K6:
139+
Select a test run from below to view the test progress and results on Grafana Cloud K6:
136140
137141
${testRunUrls}
138142
`;
139143

140-
const pullRequestNumber = await getPullRequestNumber();
144+
let pullRequestNumber;
145+
146+
try {
147+
pullRequestNumber = await getPullRequestNumber();
148+
} catch (error: any) {
149+
core.error(`Error getting pull request number`);
150+
core.error(error);
151+
}
141152

142153
if (!pullRequestNumber) {
143154
core.debug('Pull request number not found skipping comment creation');
144155
return;
145156
}
146157

147-
await createOrUpdateComment(pullRequestNumber, comment);
148-
149-
core.debug('Comment created successfully');
158+
try {
159+
await createOrUpdateComment(pullRequestNumber, comment);
160+
core.debug('Comment created successfully');
161+
} catch (error: any) {
162+
core.error(`Error creating comment on pull request: ${pullRequestNumber}`);
163+
core.error(error);
164+
}
150165
}

src/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { spawn } from 'child_process';
44
import * as fs from 'fs-extra';
55
import { generatePRComment } from './githubHelper';
66
import { parseK6Output } from './k6OutputParser';
7+
import { TestRunUrlsMap } from './types';
78

89
const TEST_PIDS: number[] = [];
910

@@ -26,12 +27,9 @@ export async function run(): Promise<void> {
2627
const commands = testPaths.map(testPath => generateCommand(testPath)),
2728
TOTAL_TEST_RUNS = commands.length,
2829
TEST_RESULT_URLS_MAP = new Proxy({}, {
29-
set: (target: { [key: string]: string }, key: string, value: string) => {
30+
set: (target: TestRunUrlsMap, key: string, value: string) => {
3031
target[key] = value;
3132
if (Object.keys(target).length === TOTAL_TEST_RUNS) {
32-
core.debug('📊 URLs for all the tests gathered');
33-
core.debug(`📊 Test URLs: ${target}`);
34-
3533
if (isCloud) {
3634
// Generate PR comment with test run URLs
3735
generatePRComment(target);

src/k6OutputParser.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TestResultUrlsMap } from './types';
1+
import { TestRunUrlsMap } from './types';
22

33
const REGEX_EXPRESSIONS = {
44
scriptPath: /^\s*script:\s*(.+)$/m,
@@ -23,14 +23,14 @@ const REGEX_EXPRESSIONS = {
2323
];
2424

2525

26-
function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap): boolean {
26+
function extractTestRunUrl(data: string, testRunUrlsMap: TestRunUrlsMap): boolean {
2727
/**
2828
* This function extracts the script path and output URL from the k6 output.
29-
* It then adds the script path and output URL to the testResultUrlsMap which is a reference to
29+
* It then adds the script path and output URL to the testRunUrlsMap which is a reference to
3030
* an object passed from the main function to store test run urls mapped to corresponding test script.
3131
*
3232
* @param {string} data - The k6 command output data as string
33-
* @param {TestResultUrlsMap} testResultUrlsMap - The map containing the script path and output URL
33+
* @param {TestRunUrlsMap} testRunUrlsMap - The map containing the script path and output URL
3434
*
3535
* @returns {boolean} - Returns true if the script path and output URL were successfully extracted and added to the map. Otherwise, returns false.
3636
*
@@ -47,7 +47,7 @@ function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap):
4747
const outputCloudUrl = outputCloudUrlMatch ? outputCloudUrlMatch[1] : output;
4848

4949
if (scriptPath && output) {
50-
testResultUrlsMap[scriptPath] = outputCloudUrl || '';
50+
testRunUrlsMap[scriptPath] = outputCloudUrl || '';
5151
return true;
5252
} else {
5353
return false;
@@ -110,14 +110,14 @@ function checkIfK6ASCIIArt(data: string): boolean {
110110
}
111111
}
112112

113-
export function parseK6Output(data: Buffer, testResultUrlsMap: TestResultUrlsMap | null, totalTestRuns: number): void {
113+
export function parseK6Output(data: Buffer, testRunUrlsMap: TestRunUrlsMap | null, totalTestRuns: number): void {
114114
/*
115115
* This function is responsible for parsing the output of the k6 command.
116116
* It filters out the progress lines and logs the rest of the output.
117117
* It also extracts the test run URLs from the output.
118118
*
119119
* @param {Buffer} data - The k6 command output data
120-
* @param {TestResultUrlsMap | null} testResultUrlsMap - The map containing the script path and output URL. If null, the function will not extract test run URLs.
120+
* @param {TestRunUrlsMap | null} testRunUrlsMap - The map containing the script path and output URL. If null, the function will not extract test run URLs.
121121
* @param {number} totalTestRuns - The total number of test runs. This is used to determine when all test run URLs have been extracted.
122122
*
123123
* @returns {void}
@@ -127,8 +127,8 @@ export function parseK6Output(data: Buffer, testResultUrlsMap: TestResultUrlsMap
127127
lines = dataString.split('\n');
128128

129129
// Extract test run URLs
130-
if (testResultUrlsMap && Object.keys(testResultUrlsMap).length < totalTestRuns) {
131-
if (extractTestRunUrl(dataString, testResultUrlsMap)) {
130+
if (testRunUrlsMap && Object.keys(testRunUrlsMap).length < totalTestRuns) {
131+
if (extractTestRunUrl(dataString, testRunUrlsMap)) {
132132
// Test URL was extracted successfully and added to the map.
133133
// Ignore further output parsing for this data.
134134
return;

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export type TestResultUrlsMap = {
1+
export type TestRunUrlsMap = {
22
[key: string]: string;
33
};

0 commit comments

Comments
 (0)