Repository navigation
142 lines (127 loc) · 5.43 KB
/
Copy pathcomment-plugin-validation.yml
File metadata and controls
142 lines (127 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Comment Plugin Validation
on:
workflow_run:
workflows:
- Validate Plugin TOML
types:
- completed
permissions:
actions: read
contents: read
issues: write
pull-requests: write
jobs:
comment:
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Download validation report
continue-on-error: true
uses: actions/download-artifact@v8
with:
name: plugin-toml-validation
path: validation
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Post validation comment
uses: actions/github-script@v9
env:
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion }}
with:
script: |
const fs = require('fs');
const path = require('path');
const escapeMarkdownLinkText = value => value
.replace(/\\/g, '\\\\')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n/g, ' ');
const sanitizeReportMarkdown = value => value
.replace(/\r\n?/g, '\n')
.replace(/\\/g, '\\\\')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\[/g, '\\[')
.replace(/\]/g, '\\]')
.replace(/@/g, '@<!-- -->')
.replace(/\b(https?|ftp):\/\//gi, '$1\\://')
.replace(/\bwww\./gi, 'www\\.')
.replace(/^ {0,3}(`{3,}|~{3,})/gm, fence => fence.replace(/[`~]/g, '\\$&'));
const marker = '<!-- plugin-toml-validation -->';
const reportPath = path.join(process.env.GITHUB_WORKSPACE, 'validation', 'validation-report.md');
let pullRequestNumber = context.payload.workflow_run.pull_requests?.[0]?.number;
const headOwner = context.payload.workflow_run.head_repository?.owner?.login;
const headBranch = context.payload.workflow_run.head_branch;
if (!pullRequestNumber && headOwner && headBranch) {
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${headOwner}:${headBranch}`,
per_page: 100,
});
pullRequestNumber = pullRequests.find(
pullRequest => pullRequest.head.sha === context.payload.workflow_run.head_sha
)?.number;
}
if (!pullRequestNumber) {
core.warning('No pull request is attached to this workflow run');
return;
}
const workflowUrl = context.payload.workflow_run.html_url;
const status = process.env.WORKFLOW_CONCLUSION === 'success'
? '✅ Passed'
: process.env.WORKFLOW_CONCLUSION === 'failure'
? '❌ Failed'
: `⚠️ ${process.env.WORKFLOW_CONCLUSION}`;
let body;
if (fs.existsSync(reportPath)) {
const report = fs.readFileSync(reportPath, 'utf8')
.replace(/\r\n?/g, '\n')
.trim();
const maxReportLength = 60000;
const truncated = report.length > maxReportLength;
const displayedReport = truncated
? `${report.slice(0, maxReportLength)}\n\n> Report truncated`
: report;
const renderedReport = sanitizeReportMarkdown(displayedReport);
body = marker + '\n' + renderedReport + '\n\n' +
`[View workflow logs](${escapeMarkdownLinkText(workflowUrl)})`;
} else if (process.env.WORKFLOW_CONCLUSION === 'failure') {
body = marker + '\n## Plugin TOML Validation Report\n\n' +
`**Status**: ${status}\n\n> Validation workflow failed before generating a report\n\n` +
`[View workflow logs](${escapeMarkdownLinkText(workflowUrl)})`;
} else {
body = marker + '\n## Plugin TOML Validation Report\n\n' +
`**Status**: ${status}\n\n> No validation report was available. See the workflow logs for details.\n\n` +
`[View workflow logs](${escapeMarkdownLinkText(workflowUrl)})`;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequestNumber,
per_page: 100,
});
const existing = comments.find(comment =>
comment.user?.login === 'github-actions[bot]' &&
comment.body?.includes(marker)
);
const commentBody = body.slice(0, 65536);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: commentBody,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequestNumber,
body: commentBody,
});
}