forked from Kong/insomnia
-
Notifications
You must be signed in to change notification settings - Fork 0
276 lines (226 loc) · 10.3 KB
/
Copy pathtest.yml
File metadata and controls
276 lines (226 loc) · 10.3 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
name: Test App
on:
merge_group:
workflow_dispatch:
push:
branches:
- develop
pull_request:
types:
- opened
- synchronize
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions: {}
jobs:
Test:
timeout-minutes: 20
runs-on: ubuntu-24.04
permissions:
contents: read
pull-requests: write
packages: read
steps:
- name: Checkout branch
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Setup Node
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: .nvmrc
cache: npm
cache-dependency-path: package-lock.json
registry-url: 'https://npm.pkg.github.com'
scope: '@kong'
- name: Install packages
run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'
- name: Lint
run: npm run lint
- name: Type checks
run: npm run type-check
- name: Unit Tests
run: npm test
- name: Check autocomplete snippets are up to date
run: |
npm run generate:autocomplete -w insomnia-scripting-environment
if ! git diff --exit-code packages/insomnia-scripting-environment/src/autocomplete-snippets.json; then
echo "::error::autocomplete-snippets.json is out of date. Run 'npm run generate:autocomplete -w insomnia-scripting-environment' and commit the result."
exit 1
fi
- name: Checkout base branch (cycle comparison)
if: github.event_name == 'pull_request' && always()
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ github.event.pull_request.base.ref }}
path: insomnia-base
- name: Setup Node (base branch tree)
if: github.event_name == 'pull_request' && always()
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version-file: insomnia-base/.nvmrc
cache: npm
cache-dependency-path: insomnia-base/package-lock.json
registry-url: 'https://npm.pkg.github.com'
scope: '@kong'
- name: Install packages (base branch tree)
if: github.event_name == 'pull_request' && always()
working-directory: insomnia-base
run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'
- name: Check Circular References
if: github.event_name == 'pull_request' && always()
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const path = require('path');
const workspaceRoot = process.env.GITHUB_WORKSPACE;
const prPackagesDir = path.join(workspaceRoot, 'packages');
const basePackagesDir = path.join(workspaceRoot, 'insomnia-base', 'packages');
async function analyzeCircularReferences(rootDir) {
const madge = require('madge');
const res = await madge(rootDir, {
fileExtensions: ['ts', 'tsx'],
});
return res.circular();
}
// Function to generate markdown report
function generateMarkdownReport(prCircularRefs, baseCircularRefs, prCount, baseCount, baseBranch) {
const timestamp = new Date().toISOString();
const diff = prCount - baseCount;
const diffPercent = baseCount > 0 ? ((diff / baseCount) * 100).toFixed(2) : '0.00';
let status = '✅ PASSED';
let statusEmoji = '✅';
if (diff > 0) {
status = '⚠️ WARNING';
statusEmoji = '⚠️';
} else if (diff === 0) {
status = '✅ NO CHANGE';
statusEmoji = '✅';
} else {
status = '✨ IMPROVED';
statusEmoji = '✨';
}
// Find new and removed circular references
const prCycleStrings = new Set(prCircularRefs.map(cycle => cycle.join(' -> ')));
const baseCycleStrings = new Set(baseCircularRefs.map(cycle => cycle.join(' -> ')));
const newCycles = Array.from(prCycleStrings).filter(cycle => !baseCycleStrings.has(cycle)).sort();
const removedCycles = Array.from(baseCycleStrings).filter(cycle => !prCycleStrings.has(cycle)).sort();
return `# ${statusEmoji} Circular References Report
**Generated at:** ${timestamp}
**Status:** ${status}
## Summary
| Metric | Base (\`${baseBranch}\`) | PR | Change |
|--------|----------------|-----|---------|
| Total Circular References | ${baseCount} | ${prCount} | ${diff > 0 ? '+' : ''}${diff} (${diffPercent > 0 ? '+' : ''}${diffPercent}%) |
${newCycles.length > 0 ? `
## ⚠️ New Circular References Added (${newCycles.length})
<details open>
<summary>Click to expand/collapse</summary>
\`\`\`
${newCycles.join('\n')}
\`\`\`
</details>
` : ''}
${removedCycles.length > 0 ? `
## ✨ Circular References Removed (${removedCycles.length})
<details open>
<summary>Click to expand/collapse</summary>
\`\`\`
${removedCycles.join('\n')}
\`\`\`
</details>
` : ''}
<details>
<summary>Click to view all circular references in PR (${prCount})</summary>
\`\`\`
${prCircularRefs.length > 0 ? prCircularRefs.sort().map(cycle => cycle.join(' -> ')).join('\n') : 'No circular references found'}
\`\`\`
</details>
<details>
<summary>Click to view all circular references in base branch (${baseCount})</summary>
\`\`\`
${baseCircularRefs.length > 0 ? baseCircularRefs.sort().map(cycle => cycle.join(' -> ')).join('\n') : 'No circular references found'}
\`\`\`
</details>
## Analysis
${
diff > 0
? `⚠️ **Warning:** This PR introduces ${diff} new circular ${diff === 1 ? 'reference' : 'references'}. Consider refactoring to avoid adding circular dependencies.`
: diff < 0
? `✨ **Great Job!** This PR removes ${Math.abs(diff)} circular ${Math.abs(diff) === 1 ? 'reference' : 'references'}. Keep up the good work!`
: `✅ **No Change:** This PR does not introduce or remove any circular references.`
}
---
*This report was generated automatically by comparing against the \`${baseBranch}\` branch.*
`;
}
try {
const baseBranch = context.payload.pull_request.base.ref;
console.log(`Base branch (PR target): ${baseBranch}`);
console.log(`PR packages dir: ${prPackagesDir}`);
console.log(`Base packages dir: ${basePackagesDir}`);
console.log('Analyzing circular references in PR (merge) tree...');
const prCircularRefs = await analyzeCircularReferences(prPackagesDir);
const prCount = prCircularRefs.length;
console.log(`PR: Found ${prCount} circular references`);
console.log(`Analyzing circular references in ${baseBranch} tree...`);
const baseCircularRefs = await analyzeCircularReferences(basePackagesDir);
const baseCount = baseCircularRefs.length;
console.log(`Base: Found ${baseCount} circular references`);
// Generate report
const diff = prCount - baseCount;
const markdownContent = generateMarkdownReport(prCircularRefs, baseCircularRefs, prCount, baseCount, baseBranch);
// Post PR comment
if (context.eventName === 'pull_request') {
try {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user?.type === 'Bot' &&
comment.body?.includes('# ') &&
comment.body?.includes('Circular References Report')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: markdownContent
});
console.log('Updated existing PR comment');
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: markdownContent
});
console.log('Created new PR comment');
}
} catch (error) {
console.error('Error posting PR comment:', error);
}
}
// Log results but don't fail the build
if (diff > 0) {
console.log(`⚠️ Warning: This PR introduces ${diff} new circular ${diff === 1 ? 'reference' : 'references'}. Base: ${baseCount}, PR: ${prCount}`);
} else if (diff < 0) {
console.log(`✨ Great! This PR removes ${Math.abs(diff)} circular ${Math.abs(diff) === 1 ? 'reference' : 'references'}!`);
} else {
console.log(`✅ No change in circular references (${prCount})`);
}
} catch (error) {
console.error('Error analyzing circular references:', error);
core.setFailed('Failed to analyze circular references');
}