Skip to content

Commit 77e504f

Browse files
ci: 新增对dependabot的阿里云依赖检查 (#2126)
* ci: 新增对dependabot的阿里云依赖检查 Close #2125 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * ci: 新增对dependabot的阿里云依赖检查 Close #2125 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * ci: 新增对dependabot的阿里云依赖检查 Close #2125 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * ci: 新增对dependabot的阿里云依赖检查 Close #2125 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> * ci: 新增对dependabot的阿里云依赖检查 Close #2125 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com> --------- Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com>
1 parent 7365957 commit 77e504f

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
name: Check Aliyun Maven Dependencies
2+
3+
on:
4+
pull_request_target:
5+
types: [ opened, synchronize ]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
12+
jobs:
13+
check-dependencies:
14+
# 只在dependabot的PR上运行
15+
if: github.actor == 'dependabot[bot]'
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
with:
22+
ref: ${{ github.event.pull_request.head.ref }}
23+
24+
- name: Set up Java
25+
uses: actions/setup-java@v4
26+
with:
27+
distribution: 'temurin'
28+
java-version: '17'
29+
30+
- name: Get build file changes
31+
id: get-changes
32+
run: |
33+
# 检测项目类型并获取变更内容
34+
if [ -f "pom.xml" ]; then
35+
echo "build_type=maven" >> $GITHUB_OUTPUT
36+
# PR事件获取当前PR的pom.xml变更
37+
git diff origin/${{ github.event.pull_request.base.ref }} -- pom.xml > changes.diff || true
38+
elif [ -f "build.gradle" ]; then
39+
echo "build_type=gradle" >> $GITHUB_OUTPUT
40+
git diff origin/${{ github.event.pull_request.base.ref }} -- build.gradle > changes.diff || true
41+
else
42+
echo "No supported build file found"
43+
exit 0
44+
fi
45+
echo "changes<<EOF" >> $GITHUB_OUTPUT
46+
cat changes.diff >> $GITHUB_OUTPUT
47+
echo "EOF" >> $GITHUB_OUTPUT
48+
49+
- name: Parse dependencies
50+
id: parse-deps
51+
uses: actions/github-script@v7
52+
with:
53+
script: |
54+
const core = require('@actions/core');
55+
56+
try {
57+
const buildType = '${{ steps.get-changes.outputs.build_type }}';
58+
const changes = '${{ steps.get-changes.outputs.changes }}';
59+
const dependencies = [];
60+
61+
if (buildType === 'gradle') {
62+
// Gradle 依赖解析
63+
const regex = /([+-])\s*(implementation|api|compile|testImplementation|runtimeOnly)\s*['"]([^'"]+)['"]/g;
64+
let match;
65+
66+
while ((match = regex.exec(changes)) !== null) {
67+
if (match[1] === '+') {
68+
const depParts = match[3].split(':');
69+
if (depParts.length === 3) {
70+
dependencies.push({
71+
group: depParts[0],
72+
artifact: depParts[1],
73+
version: depParts[2]
74+
});
75+
}
76+
}
77+
}
78+
} else if (buildType === 'maven') {
79+
// Maven 依赖解析
80+
const diffLines = changes.split('\n');
81+
let currentDep = null;
82+
let inDependency = false;
83+
let isNewDep = false;
84+
85+
for (const line of diffLines) {
86+
if (line.startsWith('+ <dependency>')) {
87+
inDependency = true;
88+
isNewDep = true;
89+
currentDep = {};
90+
} else if (line.startsWith('+ </dependency>') && isNewDep) {
91+
inDependency = false;
92+
if (currentDep.groupId && currentDep.artifactId && currentDep.version) {
93+
dependencies.push({
94+
group: currentDep.groupId,
95+
artifact: currentDep.artifactId,
96+
version: currentDep.version
97+
});
98+
}
99+
} else if (inDependency && isNewDep) {
100+
if (line.startsWith('+ <groupId>')) {
101+
currentDep.groupId = line.replace(/^\+ <groupId>|<\/groupId>$/g, '').trim();
102+
} else if (line.startsWith('+ <artifactId>')) {
103+
currentDep.artifactId = line.replace(/^\+ <artifactId>|<\/artifactId>$/g, '').trim();
104+
} else if (line.startsWith('+ <version>')) {
105+
currentDep.version = line.replace(/^\+ <version>|<\/version>$/g, '').trim();
106+
}
107+
}
108+
}
109+
}
110+
111+
core.setOutput('dependencies', JSON.stringify(dependencies));
112+
return dependencies.length;
113+
} catch (error) {
114+
core.setFailed(`Failed to parse dependencies: ${error}`);
115+
}
116+
117+
- name: Check Aliyun Maven availability
118+
if: steps.parse-deps.outputs.dependencies != '[]'
119+
id: check-aliyun
120+
uses: actions/github-script@v7
121+
with:
122+
script: |
123+
const { execSync } = require('child_process');
124+
const core = require('@actions/core');
125+
const { context, getOctokit } = require('@actions/github');
126+
127+
try {
128+
const dependencies = JSON.parse('${{ steps.parse-deps.outputs.dependencies }}');
129+
const aliMavenUrl = 'https://maven.aliyun.com/repository/public';
130+
const results = [];
131+
132+
for (const dep of dependencies) {
133+
const artifactPath = dep.group.replace(/\./g, '/') + '/' + dep.artifact + '/' + dep.version;
134+
const pomUrl = `${aliMavenUrl}/${artifactPath}/${dep.artifact}-${dep.version}.pom`;
135+
136+
try {
137+
execSync(`curl -I -s -o /dev/null -w "%{http_code}" ${pomUrl} | grep 200`);
138+
results.push(`✅ ${dep.group}:${dep.artifact}:${dep.version} - 可用`);
139+
} catch (e) {
140+
results.push(`❌ ${dep.group}:${dep.artifact}:${dep.version} - 不可用`);
141+
}
142+
}
143+
144+
if (results.length > 0) {
145+
const commentBody = `### 阿里云 Maven 依赖检查结果\n\n${results.join('\n')}`;
146+
147+
const octokit = getOctokit(process.env.GITHUB_TOKEN);
148+
await octokit.rest.issues.createComment({
149+
owner: context.repo.owner,
150+
repo: context.repo.repo,
151+
issue_number: ${{ github.event.pull_request.number }},
152+
body: commentBody
153+
});
154+
155+
core.setOutput('result', commentBody);
156+
}
157+
158+
return 'Dependency check completed';
159+
} catch (error) {
160+
core.setFailed(`Action failed with error: ${error}`);
161+
}
162+
163+
- name: Output result
164+
if: steps.parse-deps.outputs.dependencies != '[]'
165+
run: echo "${{ steps.check-aliyun.outputs.result }}"

0 commit comments

Comments
 (0)