Skip to content

Commit 65ad699

Browse files
committed
Improve
1 parent bc3dcde commit 65ad699

File tree

9 files changed

+82
-97
lines changed

9 files changed

+82
-97
lines changed

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/action.yml

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,15 @@
1-
name: create
2-
description: |
3-
Create a reports group directory under provided `path`, with a metada file and a copy of all `files`
4-
Reports are expected to share the same format !
1+
name: TODO
2+
description: TODO
53
inputs:
64
name:
75
description: Status check name
86
required: true
9-
# commit-sha:
10-
# description: Commit SHA to attach to the check
11-
# required: true
127
github-token:
138
description: Github Authentication token used to create the check through GitHub API
149
required: true
1510
job-status:
1611
description: Job check status
1712
required: true
18-
# external-id:
19-
# description: Status check external ID
20-
# required: false
21-
# started-at:
22-
# description: Status check "started at"
23-
# required: false
24-
# completed-at:
25-
# description: Status check "completed at"
26-
# required: false
27-
# details-url:
28-
# description: Status check details url
29-
# required: false
30-
# output:
31-
# description: Status check output
32-
# required: false
33-
# output-summary:
34-
# description: Status check output summary
35-
# required: false
3613

3714
outputs:
3815
check-run-id:

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
},
1111
"dependencies": {
1212
"@actions/core": "^1.10.1",
13-
"@actions/github": "^6.0.0",
14-
"@octokit/webhooks-definitions": "^3.67.3"
13+
"@actions/github": "^6.0.0"
1514
},
1615
"devDependencies": {
1716
"@vercel/ncc": "^0.38.1"

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/src/cleanup.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@ const core = require('@actions/core');
44
const {GITHUB_REPOSITORY} = process.env;
55

66
async function run() {
7-
if (core.getState('check-run-is-concluded').length) {
7+
if (core.getState('check-run-already-concluded').length > 0) {
88
core.info('Check run already concluded, skipping check run update');
99
}
10-
11-
const [repoOwner, repoName] = GITHUB_REPOSITORY.split('/');
12-
/** INPUTS **/
13-
const jobStatus = core.getInput('job-status', {required: true});
1410
const checkRunId = core.getState('check-run-id');
1511
if (checkRunId.length === 0) {
1612
throw new Error('Unable to retrieve check run id !');
1713
}
14+
15+
/** INPUTS **/
16+
const jobStatus = core.getInput('job-status', {required: true});
1817
const githubToken = core.getInput('github-token', {required: true});
1918

2019
const requestParams = await core.group(
2120
'Build API params',
2221
async () => {
22+
const [repoOwner, repoName] = GITHUB_REPOSITORY.split('/');
23+
2324
return {
2425
conclusion: jobStatus,
26+
// Url path parameters
2527
owner: repoOwner,
2628
repo: repoName,
2729
check_run_id: checkRunId
@@ -30,17 +32,13 @@ async function run() {
3032
);
3133
core.debug('API params=' + JSON.stringify(requestParams));
3234

33-
const apiResponse = await core.group('Retrieve ', async () => {
35+
const apiResponse = await core.group('Conclude check-run ', async () => {
3436
const octokit = github.getOctokit(githubToken);
3537

3638
// @TODO Move back to `octokit.rest.checks.update()`
37-
const res = await octokit.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', requestParams);
38-
39-
core.info('TMP DEBUG0 ' + JSON.stringify(res));
40-
41-
return res;
39+
return octokit.request('PATCH /repos/{owner}/{repo}/check-runs/{check_run_id}', requestParams);
4240
});
43-
core.info('TMP DEBUG' + JSON.stringify(apiResponse));
41+
core.debug('API call to ' +apiResponse.url + ' => HTTP ' + apiResponse.status);
4442
}
4543

4644
run();
Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,123 @@
11
const github = require('@actions/github'); // @TODO move to 'imports from' when moved to TS !
22
const core = require('@actions/core');
33

4-
const {GITHUB_REPOSITORY, GITHUB_SERVER_URL, GITHUB_RUN_ID, GITHUB_JOB} = process.env;
4+
const {GITHUB_REPOSITORY} = process.env;
55

66
function guessPrNumber() {
7+
if ('pull_request' === github.context.eventName) {
8+
return github.context.payload.number
9+
}
710
if ('workflow_run' === github.context.eventName) {
8-
return 'pull_request' === github.context.payload.event && github.context.payload.pull_requests[0]?.number
11+
return 'pull_request' === github.context.payload.workflow_run.event
912
? github.context.payload.pull_requests[0]?.number
1013
: undefined
1114
;
1215
}
1316

14-
return 'pull_request' === github.context.eventName ? github.context.payload.number : undefined;
17+
return undefined;
1518
}
1619

1720
function guessCommitSha() {
18-
// "${{ ('workflow_run' == github.event_name && ('pull_request' == github.event.workflow_run.event || 'push' == github.event.workflow_run.event) && github.event.workflow_run.head_sha) || ('pull_request' == github.event_name && github.event.pull_request.head.sha) || ('push' == github.event_name && github.sha) || null }}"
21+
if ('pull_request' === github.context.eventName) {
22+
return github.context.payload.pull_request.head.sha;
23+
}
24+
if ('push' === github.context.eventName) {
25+
return github.context.payload.after;
26+
}
1927
if ('workflow_run' === github.context.eventName) {
20-
return 'pull_request' === github.context.payload.event || 'push' === github.context.payload.event
28+
return ['pull_request', 'push'].includes(github.context.payload.workflow_run.event)
2129
? github.context.payload.head_sha
2230
: undefined
2331
;
2432
}
2533

26-
return 'pull_request' === github.context.eventName ? github.context.payload.number : undefined;
34+
throw new Error('Unable to guess the commit SHA !');
35+
}
36+
37+
function guessTriggeringWorkflowName() {
38+
if ('workflow_run' === github.context.eventName) {
39+
return github.context.payload.workflow.name;
40+
}
41+
42+
return github.context.workflow;
43+
}
44+
45+
function guessRunId() {
46+
if ('workflow_run' === github.context.eventName) {
47+
return github.context.payload.id;
48+
}
49+
50+
return github.context.runId;
51+
}
52+
53+
async function getWorkflowJobsForRunId(octokit, owner, repo, runId) {
54+
return octokit.paginate(
55+
'GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs',
56+
{
57+
//filter: 'latest',
58+
// Url path parameters
59+
owner: owner,
60+
repo: repo,
61+
run_id: runId
62+
}
63+
);
2764
}
2865

2966
async function run() {
30-
core.info('TMP DEBUG ' + JSON.stringify(github.context));throw 'plop';
3167
/** INPUTS **/
3268
const checkName = core.getInput('name', {required: true});
3369
const githubToken = core.getInput('github-token', {required: true});
3470
const jobStatus = core.getInput('job-status', {required: true});
3571

3672
const isSuccessfulJobAsOfNow = 'success' === jobStatus;
73+
const octokit = github.getOctokit(githubToken);
3774

3875
const requestParams = await core.group(
3976
'Build API params',
4077
async () => {
41-
const [repoOwner, repoName] = GITHUB_REPOSITORY.split('/');
42-
const externalId = 'workflow_run' === github.context.eventName ? github.context.payload.id : GITHUB_RUN_ID;
78+
const repoInfo = github.context.repo;
79+
const triggeringWorkflowRunId = guessRunId();
80+
core.info('TMP DEBUG context=' + JSON.stringify(github.context));
81+
const jobsForCurrentWorkflow = await getWorkflowJobsForRunId(octokit, repoInfo.owner, repoInfo.repo, github.context.runId);
82+
core.info('TMP DEBUG jobsForCurrentWorkflow=' + JSON.stringify(jobsForCurrentWorkflow.map(v => {v.steps = '-_-'; return v;})));
83+
const jobsForTriggeringWorkflow = await getWorkflowJobsForRunId(octokit, repoInfo.owner, repoInfo.repo, triggeringWorkflowRunId);
84+
core.info('TMP DEBUG jobsForTriggeringWorkflow=' + JSON.stringify(jobsForTriggeringWorkflow.map(v => {v.steps = '-_-'; return v;})));
85+
core.info('TMP DEBUG job name=' + process.env.GITHUB_JOB);
4386
const commitSha = guessCommitSha();
4487
const startedAt = (new Date()).toISOString();
4588
const prNumber = guessPrNumber();
46-
const detailsUrl = GITHUB_SERVER_URL + '/' + GITHUB_REPOSITORY + '/actions/runs/' + GITHUB_RUN_ID + '/job/' + GITHUB_JOB + (undefined !== prNumber ? '?pr=' + prNumber : '');
47-
const outputTitle = 'My title';
48-
const outputSummary = 'My summary';
49-
const outputText = 'My text';
89+
const originalWorkflowName = guessTriggeringWorkflowName();
90+
const outputTitle = '🔔 ' + github.context.workflow; // Current workflow name !
91+
const originalWorkflowUrl = github.context.serverUrl + '/' + GITHUB_REPOSITORY + '/actions/runs/' + triggeringWorkflowRunId + (undefined !== prNumber ? '?pr=' + prNumber : '');
92+
const outputSummary = '🪢 Triggered by <a href="' + originalWorkflowUrl + '" target="blank">**' + originalWorkflowName + '** workflow</a>';
5093

5194
return {
5295
name: checkName,
5396
head_sha: commitSha,
54-
details_url: detailsUrl,
55-
external_id: externalId,
97+
//details_url: detailsUrl,
98+
external_id: triggeringWorkflowRunId,
5699
status: isSuccessfulJobAsOfNow ? 'in_progress' : 'completed',
57100
output: {
58101
title: outputTitle,
59102
summary: outputSummary,
60-
text: outputText,
61103
},
62104
// Conclusion
63105
conclusion: isSuccessfulJobAsOfNow ? undefined : jobStatus,
64106
started_at: startedAt,
65107
completed_at: isSuccessfulJobAsOfNow ? undefined : startedAt,
66108
// Url path parameters
67-
owner: repoOwner,
68-
repo: repoName
109+
owner: repoInfo.owner,
110+
repo: repoInfo.repo
69111
};
70112
}
71113
);
72114
core.debug('API params=' + JSON.stringify(requestParams));
73115

74-
const apiResponse = await core.group('Call API', async () => {
75-
const octokit = github.getOctokit(githubToken);
76-
116+
const apiResponse = await core.group('Create check-run', async () => {
77117
// @TODO Move back to `octokit.rest.checks.create()`
78-
const res = await octokit.request('POST /repos/{owner}/{repo}/check-runs', requestParams);
79-
80-
core.info('TMP DEBUG0 ' + JSON.stringify(res));
81-
82-
return res;
118+
return octokit.request('POST /repos/{owner}/{repo}/check-runs', requestParams);
83119
});
84-
core.info('TMP DEBUG' + JSON.stringify(apiResponse));
120+
core.debug('API call to ' +apiResponse.url + ' => HTTP ' + apiResponse.status);
85121

86122
core.setOutput('check-run-id', apiResponse.data.id);
87123
core.saveState('check-run-id', apiResponse.data.id); // In order to use it during POST hook
@@ -90,21 +126,4 @@ async function run() {
90126
}
91127
}
92128

93-
/**
94-
* @param {string} val
95-
*
96-
* @returns {string|undefined}
97-
*/
98-
function undefinedIfEmpty(val) {
99-
return !isEmpty(val) ? val : undefined
100-
}
101-
/**
102-
* @param {string} val
103-
*
104-
* @returns {boolean}
105-
*/
106-
function isEmpty(val) {
107-
return val.trim().length === 0;
108-
}
109-
110129
run();

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@
113113
dependencies:
114114
"@octokit/openapi-types" "^20.0.0"
115115

116-
"@octokit/webhooks-definitions@^3.67.3":
117-
version "3.67.3"
118-
resolved "https://registry.yarnpkg.com/@octokit/webhooks-definitions/-/webhooks-definitions-3.67.3.tgz#d2a905a90b04af8111982d0c13658a49fc4eecb9"
119-
integrity sha512-do4Z1r2OVhuI0ihJhQ8Hg+yPWnBYEBNuFNCrvtPKoYT1w81jD7pBXgGe86lYuuNirkDHb0Nxt+zt4O5GiFJfgA==
120-
121116
"@vercel/ncc@^0.38.1":
122117
version "0.38.1"
123118
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.38.1.tgz#13f08738111e1d9e8a22fd6141f3590e54d9a60e"

.github/workflows/codacy-upload-from-artifacts.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ jobs:
4747

4848
- uses: ./custom-action-repo/.github/actions/reports-group/attach-check-run-to-triggering-workflow-action
4949
with:
50-
name: "${{ github.workflow }} (${{ github.event.workflow.name}}) / Codacy / Upload all reports"
51-
commit-sha: "${{ ('workflow_run' == github.event_name && ('pull_request' == github.event.workflow_run.event || 'push' == github.event.workflow_run.event) && github.event.workflow_run.head_sha) || ('pull_request' == github.event_name && github.event.pull_request.head.sha) || ('push' == github.event_name && github.sha) || null }}"
50+
name: "Codacy / Upload all reports"
5251
github-token: ${{ github.token }}
5352
job-status: ${{ job.status }}
5453

.github/workflows/codecov-upload-from-artifacts.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ jobs:
3939

4040
- uses: ./custom-action-repo/.github/actions/reports-group/attach-check-run-to-triggering-workflow-action
4141
with:
42-
name: "${{ github.workflow }} (${{ github.event.workflow.name}}) / Codecov / Prepare"
43-
commit-sha: "${{ ('workflow_run' == github.event_name && ('pull_request' == github.event.workflow_run.event || 'push' == github.event.workflow_run.event) && github.event.workflow_run.head_sha) || ('pull_request' == github.event_name && github.event.pull_request.head.sha) || ('push' == github.event_name && github.sha) || null }}"
42+
name: "Codecov / 📮 🗃️ Prepare uploads"
4443
github-token: ${{ github.token }}
4544
job-status: ${{ job.status }}
4645

@@ -100,7 +99,7 @@ jobs:
10099
run: echo '${{ toJson(steps.build-matrix.outputs) }}'
101100

102101
uploads:
103-
name: ${{ matrix.artifact}} - ${{ matrix.path }}
102+
name: "${{ matrix.artifact }} - ${{ matrix.path }}"
104103
runs-on: ubuntu-latest
105104
needs: [ prepare ]
106105
strategy:
@@ -126,8 +125,7 @@ jobs:
126125

127126
- uses: ./custom-action-repo/.github/actions/reports-group/attach-check-run-to-triggering-workflow-action
128127
with:
129-
name: "${{ github.workflow }} (${{ github.event.workflow.name}}) / Codecov / ${{ matrix.artifact}} - ${{ matrix.path }}"
130-
commit-sha: "${{ ('workflow_run' == github.event_name && ('pull_request' == github.event.workflow_run.event || 'push' == github.event.workflow_run.event) && github.event.workflow_run.head_sha) || ('pull_request' == github.event_name && github.event.pull_request.head.sha) || ('push' == github.event_name && github.sha) || null }}"
128+
name: "Codecov / ${{ matrix.artifact }} - ${{ matrix.path }}"
131129
github-token: ${{ github.token }}
132130
job-status: ${{ job.status }}
133131

0 commit comments

Comments
 (0)