Skip to content

Commit 0b23e82

Browse files
committed
ci: refactoring the tests-report step into their own ECMAscript module
1 parent c288c84 commit 0b23e82

File tree

4 files changed

+53
-39
lines changed

4 files changed

+53
-39
lines changed

.github/workflows/actions-scripts-tests.yml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,8 @@ jobs:
4343
with:
4444
script: |
4545
const steps = ${{ toJSON(steps) }};
46-
const marker = 'to show where the warning was created)';
47-
const output = steps.run_tests.outputs.tests_report;
48-
const sanitized = output.split(marker);
49-
const msg = (sanitized.length > 1) ? sanitized[1] : sanitized[0];
50-
51-
const prNumber = context.payload.issue.number;
52-
53-
if (sanitized.length >= 1) {
54-
github.rest.issues.createComment({
55-
owner: context.repo.owner,
56-
repo: context.repo.repo,
57-
issue_number: prNumber,
58-
body: msg,
59-
});
60-
} else {
61-
core.setFailed('No tests report data available.')
62-
}
46+
47+
const { default: testReportOnPrComment } =
48+
await import('${{ github.workspace }}/actions_scripts/tests_report_on_pr_comment.js');
49+
50+
await testReportOnPrComment(github, context, steps);

actions_scripts/helpers.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Contains helper functions made to be reusable across the JS scripts
22
// of the notifications
33

4-
// Helper function to determine if the workflow is running locally.
54
/**
65
* Check if the current run is a local run (e.g., with ACT).
76
* @param {Object} context - GitHub Actions context object.
@@ -12,7 +11,23 @@ export function ciLocalRun(context) {
1211
return (localRun !== undefined) ? localRun : false;
1312
}
1413

15-
// Helper function to retrieve the username of the actor triggering the workflow.
14+
/**
15+
* Post a comment on the PR to notify the user.
16+
* @param {Object} github - GitHub API client.
17+
* @param {Object} context - GitHub Actions context object.
18+
* @param {number} prNumber - Pull request number.
19+
* @param {string} message - Message content to be posted.
20+
* @returns {Object} - The response object from the GitHub API.
21+
*/
22+
export async function createPrComment(github, context, prNumber, message) {
23+
return await github.rest.issues.createComment({
24+
owner: context.repo.owner,
25+
repo: context.repo.repo,
26+
issue_number: prNumber,
27+
body: message,
28+
});
29+
}
30+
1631
/**
1732
* Get the username of the actor from the context.
1833
* @param {Object} context - GitHub Actions context object.
@@ -27,7 +42,6 @@ export function extractUsername(context) {
2742
throw new Error("Unable to determine the actor (user) that triggered this deploy. Leaving...");
2843
}
2944

30-
// Helper function to retrieve the owner of the repository in which the action has been triggered.
3145
/**
3246
* Get the username of the actor from the context.
3347
* @param {Object} context - GitHub Actions context object.

actions_scripts/notify_user.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ciLocalRun, extractUsername } from "./helpers.js";
1+
import { ciLocalRun, extractUsername, createPrComment } from "./helpers.js";
22

33
/**
44
* Notify the user about the deployment action.
@@ -56,24 +56,6 @@ function generatePrCommentMsg(username, environment, project, infra) {
5656
return message;
5757
}
5858

59-
// Helper function to create a PR comment via the GitHub API.
60-
/**
61-
* Post a comment on the PR to notify the user.
62-
* @param {Object} github - GitHub API client.
63-
* @param {Object} context - GitHub Actions context object.
64-
* @param {number} prNumber - Pull request number.
65-
* @param {string} message - Message content to be posted.
66-
* @returns {Object} - The response object from the GitHub API.
67-
*/
68-
async function createPrComment(github, context, prNumber, message) {
69-
return await github.rest.issues.createComment({
70-
owner: context.repo.owner,
71-
repo: context.repo.repo,
72-
issue_number: prNumber,
73-
body: message,
74-
});
75-
}
76-
7759
// Helper function to log the PR comment message in a local environment.
7860
/**
7961
* Log the message locally for debugging when running with ACT.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { ciLocalRun, getRepoOwner } from "./helpers.js";
2+
3+
/**
4+
* Generates a new comment on the PR that triggered the workflow with the
5+
* report of the tests runned by 'JEST' over the actions scripts
6+
*/
7+
export default async (github, context, steps) => {
8+
const isLocalRun = ciLocalRun(context);
9+
10+
const marker = 'to show where the warning was created)';
11+
const output = steps.run_tests.outputs.tests_report;
12+
const sanitized = output.split(marker);
13+
const msg = (sanitized.length > 1) ? sanitized[1] : sanitized[0];
14+
15+
const prNumber = context.payload.issue.number;
16+
17+
if (!isLocalRun && sanitized.length >= 1) {
18+
github.rest.issues.createComment({
19+
owner: context.repo.owner,
20+
repo: context.repo.repo,
21+
issue_number: prNumber,
22+
body: msg,
23+
});
24+
} else {
25+
if (!isLocalRun)
26+
core.setFailed('No tests report data available.');
27+
else
28+
console.log(`PR message: ${msg}`);
29+
}
30+
};

0 commit comments

Comments
 (0)