Skip to content

Commit c1e6a13

Browse files
fix(ci): Fix Groovy migration PR check (#10894)
(cherry picked from commit 4b1527a)
1 parent 6e6fcfa commit c1e6a13

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
name: Enforce Groovy Migration
2+
on:
3+
pull_request:
4+
types: [opened, edited, ready_for_review, labeled, unlabeled, synchronize]
5+
branches:
6+
- master
7+
- 'release/v*'
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
enforce_groovy_migration:
15+
name: Enforce Groovy migration
16+
permissions:
17+
issues: write # Required to create a comment on the pull request
18+
pull-requests: write # Required to create a comment on the pull request
19+
contents: read # Required to read migrated modules file
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Check for Groovy regressions
23+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # 8.0.0
24+
with:
25+
github-token: ${{ secrets.GITHUB_TOKEN }}
26+
script: |
27+
// Skip draft pull requests
28+
if (context.payload.pull_request.draft) {
29+
return
30+
}
31+
32+
// Check for override label — skip all checks if label present
33+
const labels = context.payload.pull_request.labels.map(l => l.name)
34+
if (labels.includes('tag: override-groovy-enforcement')) {
35+
console.log('tag: override-groovy-enforcement label detected — skipping all checks.')
36+
return
37+
}
38+
39+
// Read migrated modules list from master
40+
const migratedMods = await github.rest.repos.getContent({
41+
owner: context.repo.owner,
42+
repo: context.repo.repo,
43+
path: '.github/g2j-migrated-modules.txt',
44+
ref: 'master'
45+
})
46+
const migratedPrefixes = Buffer.from(migratedMods.data.content, 'base64')
47+
.toString()
48+
.split('\n')
49+
.map(l => l.trim())
50+
.filter(l => l && !l.startsWith('#'))
51+
52+
// Get all files changed in this PR
53+
const allFiles = await github.paginate(github.rest.pulls.listFiles, {
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
pull_number: context.payload.pull_request.number
57+
})
58+
59+
// Filter these changed files to newly added Groovy files in any test source set
60+
const addedGroovy = allFiles.filter(f =>
61+
f.status === 'added' &&
62+
/\/src\/[^/]*[tT]est[^/]*\/groovy\/.*\.groovy$/.test(f.filename)
63+
)
64+
65+
// Extract module prefix from file path (everything before /src/(test|testFixtures)/groovy/)
66+
const moduleOf = path => {
67+
const m = path.match(/^(.*?)\/src\/(test|testFixtures)\/groovy\//)
68+
return m ? m[1] : null
69+
}
70+
71+
// Classify each added Groovy file
72+
const regressions = []
73+
const warnings = []
74+
for (const file of addedGroovy) {
75+
const path = file.filename
76+
const mod = moduleOf(path)
77+
if (migratedPrefixes.some(prefix => path.startsWith(prefix + '/'))) {
78+
regressions.push({ path, mod })
79+
} else if (
80+
path.startsWith('dd-java-agent/instrumentation/') ||
81+
path.startsWith('dd-smoke-tests/')
82+
) {
83+
// ignore Groovy file additions to instrumentations and smoke-tests for now
84+
} else {
85+
warnings.push({ path, mod })
86+
}
87+
}
88+
89+
// Fetch existing comments once
90+
const comments = await github.rest.issues.listComments({
91+
issue_number: context.payload.pull_request.number,
92+
owner: context.repo.owner,
93+
repo: context.repo.repo
94+
})
95+
96+
const regressionMarker = '<!-- dd-trace-java-groovy-regression -->'
97+
const warningMarker = '<!-- dd-trace-java-groovy-warning -->'
98+
const existingRegressionComment = comments.data.find(c => c.body.includes(regressionMarker))
99+
const existingWarningComment = comments.data.find(c => c.body.includes(warningMarker))
100+
101+
// Handle regression comment
102+
if (regressions.length > 0) {
103+
const fileList = regressions
104+
.map(({ path, mod }) => `- \`${path}\` (module: \`${mod}\`)`)
105+
.join('\n')
106+
const body = `**❌ Groovy Test Regression Detected**\n\n` +
107+
`The following files add Groovy tests to modules that have been fully migrated to Java / JUnit 5:\n\n` +
108+
`${fileList}\n\n` +
109+
`These modules no longer accept Groovy test files. Please rewrite the test in Java / JUnit 5 instead.\n\n` +
110+
regressionMarker
111+
if (existingRegressionComment) {
112+
await github.rest.issues.updateComment({
113+
comment_id: existingRegressionComment.id,
114+
owner: context.repo.owner,
115+
repo: context.repo.repo,
116+
body
117+
})
118+
} else {
119+
await github.rest.issues.createComment({
120+
issue_number: context.payload.pull_request.number,
121+
owner: context.repo.owner,
122+
repo: context.repo.repo,
123+
body
124+
})
125+
}
126+
} else if (existingRegressionComment) {
127+
await github.rest.issues.deleteComment({
128+
comment_id: existingRegressionComment.id,
129+
owner: context.repo.owner,
130+
repo: context.repo.repo
131+
})
132+
}
133+
134+
// Handle warning comment
135+
if (warnings.length > 0) {
136+
const fileList = warnings
137+
.map(({ path, mod }) => `- \`${path}\` (module: \`${mod}\`)`)
138+
.join('\n')
139+
const body = `**⚠️ New Groovy Test Files Added**\n\n` +
140+
`The following files add Groovy tests to modules that are candidates for migration to Java / JUnit 5:\n\n` +
141+
`${fileList}\n\n` +
142+
`Consider writing these tests in Java / JUnit 5 instead to help with the ongoing migration effort.\n\n` +
143+
warningMarker
144+
if (existingWarningComment) {
145+
await github.rest.issues.updateComment({
146+
comment_id: existingWarningComment.id,
147+
owner: context.repo.owner,
148+
repo: context.repo.repo,
149+
body
150+
})
151+
} else {
152+
await github.rest.issues.createComment({
153+
issue_number: context.payload.pull_request.number,
154+
owner: context.repo.owner,
155+
repo: context.repo.repo,
156+
body
157+
})
158+
}
159+
} else if (existingWarningComment) {
160+
await github.rest.issues.deleteComment({
161+
comment_id: existingWarningComment.id,
162+
owner: context.repo.owner,
163+
repo: context.repo.repo
164+
})
165+
}
166+
167+
// Fail the check if there are regressions
168+
if (regressions.length > 0) {
169+
core.setFailed(`${regressions.length} Groovy regression(s) detected in migrated module(s). See PR comment for details. To skip this check entirely, add the 'tag: override-groovy-enforcement' label.`)
170+
}

0 commit comments

Comments
 (0)