Skip to content

Commit dc8bcb3

Browse files
authored
chore(ci): remove svace/analyze label after build (#1130)
Signed-off-by: Maksim Fedotov <maksim.fedotov@flant.com>
1 parent 52d01f2 commit dc8bcb3

2 files changed

Lines changed: 48 additions & 33 deletions

File tree

.github/scripts/js/ci.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
//@ts-check
1313

14-
/*
15-
* this file contains only 3 functions from the original ci.js:
14+
/*
15+
* this file contains only 3 functions from the original ci.js:
1616
* extractCommandFromComment, reactToComment, startWorkflow.
1717
* original ci.js file can be found here:
1818
* https://github.com/deckhouse/deckhouse/blob/main/.github/scripts/js/ci.js
@@ -32,18 +32,18 @@ const extractCommandFromComment = (comment) => {
3232
if (lines.length < 1) {
3333
return {'err': 'first line is not a slash command'}
3434
}
35-
35+
3636
// Search for user command in the first line of the comment.
3737
// User command is a command and a tag name.
3838
const argv = lines[0].split(/\s+/);
39-
39+
4040
if ( ! /^\/[a-z\d_\-\/.,]+$/.test(argv[0])) {
4141
return {'err': 'not a slash command in the first line'};
4242
}
43-
43+
4444
return {argv, lines}
4545
};
46-
46+
4747
module.exports.extractCommandFromComment = extractCommandFromComment;
4848

4949
/**
@@ -88,7 +88,7 @@ const getClusterUser = async ({context, core, userClusterLabels}) => {
8888
return userClusterLabels[userLabelsInPR].id
8989
};
9090
module.exports.getClusterUser = getClusterUser;
91-
91+
9292
/**
9393
* Start workflow using workflow_dispatch event.
9494
*
@@ -103,7 +103,7 @@ module.exports.getClusterUser = getClusterUser;
103103
*/
104104
const startWorkflow = async ({ github, context, core, workflow_id, ref, inputs }) => {
105105
core.info(`Start workflow '${workflow_id}' using ref '${ref}' and inputs ${JSON.stringify(inputs)}.`);
106-
106+
107107
let response = null
108108
try {
109109
response = await github.rest.actions.createWorkflowDispatch({
@@ -116,13 +116,38 @@ const startWorkflow = async ({ github, context, core, workflow_id, ref, inputs }
116116
} catch(error) {
117117
return core.setFailed(`Error triggering workflow_dispatch event: ${dumpError(error)}`)
118118
}
119-
119+
120120
core.debug(`status: ${response.status}`);
121121
core.debug(`workflow dispatch response: ${JSON.stringify(response)}`);
122-
122+
123123
if (response.status !== 204) {
124124
return core.setFailed(`Error triggering workflow_dispatch event for '${workflow_id}'. createWorkflowDispatch response: ${JSON.stringify(response)}`);
125125
}
126126
return core.info(`Workflow '${workflow_id}' started successfully`);
127127
};
128-
module.exports.startWorkflow = startWorkflow;
128+
module.exports.startWorkflow = startWorkflow;
129+
130+
/**
131+
* Removes a specified label from a GitHub issue or pull request.
132+
* @param {{ github: any, context: any, labels: any[], labelToRemove: string }} params - The parameters for the function.
133+
* @returns {Promise<boolean>} A promise that resolves to true if the label was removed, false if it was not found.
134+
*/
135+
const removeLabel = async ({ github, context, labels, labelToRemove }) => {
136+
const issueNumber = context.issue.number;
137+
const owner = context.repo.owner;
138+
const repo = context.repo.repo;
139+
if (labels.some(label => label.name === labelToRemove)) {
140+
await github.rest.issues.removeLabel({
141+
owner,
142+
repo,
143+
issue_number: issueNumber,
144+
name: labelToRemove,
145+
});
146+
console.log(`Removed label '${labelToRemove}' from PR #${issueNumber}`);
147+
return true;
148+
} else {
149+
console.log(`Label '${labelToRemove}' not found on PR #${issueNumber}`);
150+
return false;
151+
}
152+
}
153+
module.exports.removeLabel = removeLabel;

.github/workflows/dev_module_build.yml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -652,33 +652,32 @@ jobs:
652652
- set_vars
653653
- run_e2e
654654
- update_comment_on_finish
655+
- analyze_build
655656
steps:
656-
- name: Remove label and add comment
657+
- uses: actions/checkout@v4
658+
- name: Remove labels and add comment
657659
id: remove-label
658660
uses: actions/github-script@v6
659661
with:
660662
github-token: ${{secrets.RELEASE_PLEASE_TOKEN}}
661663
script: |
662-
const issueNumber = context.issue.number; const owner = context.repo.owner;
664+
const ci = require('./.github/scripts/js/ci');
665+
const issueNumber = context.issue.number;
666+
const owner = context.repo.owner;
663667
const repo = context.repo.repo;
664-
const labelToRemove = 'e2e/run';
668+
669+
const labelToRemoveE2E = 'e2e/run';
670+
const labelToRemoveSvace = 'analyze/svace';
665671
666672
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
667673
owner,
668674
repo,
669675
issue_number: issueNumber,
670676
});
671677
672-
if (labels.some(label => label.name === labelToRemove)) {
673-
await github.rest.issues.removeLabel({
674-
owner,
675-
repo,
676-
issue_number: issueNumber,
677-
name: labelToRemove,
678-
});
679-
console.log(`Removed label '${labelToRemove}' from PR #${issueNumber}`);
678+
if (ci.removeLabel({github, context, labels, labelToRemove: labelToRemoveE2E})) {
680679
const commentIdStr = '${{ needs.run_e2e.outputs.comment_id }}';
681-
const commentId = parseInt(commentIdStr, 10); // Cast to integer
680+
const commentId = parseInt(commentIdStr, 10);
682681
const stepOutcome = '${{ needs.run_e2e.result }}';
683682
const body = `Workflow has started.
684683
Follow the progress here: [Workflow Run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})
@@ -692,15 +691,6 @@ jobs:
692691
comment_id: commentId, // Now an integer
693692
body: body
694693
});
695-
} else {
696-
// Invalid or missing, create a new comment
697-
await github.rest.issues.createComment({
698-
owner: context.repo.owner,
699-
repo: context.repo.repo,
700-
issue_number: context.payload.pull_request.number,
701-
body: body
702-
});
703694
}
704-
} else {
705-
console.log(`Label '${labelToRemove}' not found on PR #${issueNumber}`);
706695
}
696+
ci.removeLabel({github, context, labels, labelToRemove: labelToRemoveSvace});

0 commit comments

Comments
 (0)