-
Notifications
You must be signed in to change notification settings - Fork 2
ci: 新增对dependabot的阿里云依赖检查 #2126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d33c9f3
ci: 新增对dependabot的阿里云依赖检查
unknowIfGuestInDream 6fa79f0
ci: 新增对dependabot的阿里云依赖检查
unknowIfGuestInDream f5c1fcf
ci: 新增对dependabot的阿里云依赖检查
unknowIfGuestInDream 1db86fe
ci: 新增对dependabot的阿里云依赖检查
unknowIfGuestInDream c4f92b5
ci: 新增对dependabot的阿里云依赖检查
unknowIfGuestInDream File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
name: Check Aliyun Maven Dependencies | ||
|
||
on: | ||
pull_request_target: | ||
types: [ opened, synchronize ] | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
jobs: | ||
check-dependencies: | ||
# 只在dependabot的PR上运行 | ||
if: github.actor == 'dependabot[bot]' | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.event.pull_request.head.ref }} | ||
|
||
- name: Set up Java | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: '17' | ||
|
||
- name: Get build file changes | ||
id: get-changes | ||
unknowIfGuestInDream marked this conversation as resolved.
Show resolved
Hide resolved
|
||
run: | | ||
# 检测项目类型并获取变更内容 | ||
if [ -f "pom.xml" ]; then | ||
echo "build_type=maven" >> $GITHUB_OUTPUT | ||
# PR事件获取当前PR的pom.xml变更 | ||
git diff origin/${{ github.event.pull_request.base.ref }} -- pom.xml > changes.diff || true | ||
elif [ -f "build.gradle" ]; then | ||
echo "build_type=gradle" >> $GITHUB_OUTPUT | ||
git diff origin/${{ github.event.pull_request.base.ref }} -- build.gradle > changes.diff || true | ||
else | ||
echo "No supported build file found" | ||
exit 0 | ||
fi | ||
echo "changes<<EOF" >> $GITHUB_OUTPUT | ||
cat changes.diff >> $GITHUB_OUTPUT | ||
echo "EOF" >> $GITHUB_OUTPUT | ||
|
||
- name: Parse dependencies | ||
id: parse-deps | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const core = require('@actions/core'); | ||
|
||
try { | ||
const buildType = '${{ steps.detect-build.outputs.build_type }}'; | ||
const changes = '${{ steps.detect-build.outputs.changes }}'; | ||
unknowIfGuestInDream marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const dependencies = []; | ||
|
||
if (buildType === 'gradle') { | ||
// Gradle 依赖解析 | ||
const regex = /([+-])\s*(implementation|api|compile|testImplementation|runtimeOnly)\s*['"]([^'"]+)['"]/g; | ||
let match; | ||
|
||
while ((match = regex.exec(changes)) !== null) { | ||
if (match[1] === '+') { | ||
const depParts = match[3].split(':'); | ||
if (depParts.length === 3) { | ||
dependencies.push({ | ||
group: depParts[0], | ||
artifact: depParts[1], | ||
version: depParts[2] | ||
}); | ||
} | ||
} | ||
} | ||
} else if (buildType === 'maven') { | ||
// Maven 依赖解析 | ||
const diffLines = changes.split('\n'); | ||
let currentDep = null; | ||
let inDependency = false; | ||
let isNewDep = false; | ||
|
||
for (const line of diffLines) { | ||
if (line.startsWith('+ <dependency>')) { | ||
inDependency = true; | ||
isNewDep = true; | ||
currentDep = {}; | ||
} else if (line.startsWith('+ </dependency>') && isNewDep) { | ||
inDependency = false; | ||
if (currentDep.groupId && currentDep.artifactId && currentDep.version) { | ||
dependencies.push({ | ||
group: currentDep.groupId, | ||
artifact: currentDep.artifactId, | ||
version: currentDep.version | ||
}); | ||
} | ||
} else if (inDependency && isNewDep) { | ||
if (line.startsWith('+ <groupId>')) { | ||
currentDep.groupId = line.replace(/^\+ <groupId>|<\/groupId>$/g, '').trim(); | ||
} else if (line.startsWith('+ <artifactId>')) { | ||
currentDep.artifactId = line.replace(/^\+ <artifactId>|<\/artifactId>$/g, '').trim(); | ||
} else if (line.startsWith('+ <version>')) { | ||
currentDep.version = line.replace(/^\+ <version>|<\/version>$/g, '').trim(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
core.setOutput('dependencies', JSON.stringify(dependencies)); | ||
return dependencies.length; | ||
} catch (error) { | ||
core.setFailed(`Failed to parse dependencies: ${error}`); | ||
} | ||
|
||
- name: Check Aliyun Maven availability | ||
if: steps.parse-deps.outputs.dependencies != '[]' | ||
id: check-aliyun | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const { execSync } = require('child_process'); | ||
unknowIfGuestInDream marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const core = require('@actions/core'); | ||
const github = require('@actions/github'); | ||
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const dependencies = JSON.parse('${{ steps.parse-deps.outputs.dependencies }}'); | ||
const aliMavenUrl = 'https://maven.aliyun.com/repository/public'; | ||
const results = []; | ||
|
||
for (const dep of dependencies) { | ||
const artifactPath = dep.group.replace(/\./g, '/') + '/' + dep.artifact + '/' + dep.version; | ||
const pomUrl = `${aliMavenUrl}/${artifactPath}/${dep.artifact}-${dep.version}.pom`; | ||
|
||
try { | ||
execSync(`curl -I -s -o /dev/null -w "%{http_code}" ${pomUrl} | grep 200`); | ||
unknowIfGuestInDream marked this conversation as resolved.
Show resolved
Hide resolved
|
||
results.push(`✅ ${dep.group}:${dep.artifact}:${dep.version} - 可用`); | ||
} catch (e) { | ||
results.push(`❌ ${dep.group}:${dep.artifact}:${dep.version} - 不可用`); | ||
} | ||
} | ||
|
||
if (results.length > 0) { | ||
const commentBody = `### 阿里云 Maven 依赖检查结果\n\n${results.join('\n')}`; | ||
|
||
const octokit = github.getOctokit(process.env.GITHUB_TOKEN); | ||
await octokit.rest.issues.createComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: ${{ github.event.pull_request.number }}, | ||
body: commentBody | ||
}); | ||
|
||
core.setOutput('result', commentBody); | ||
} | ||
|
||
return 'Dependency check completed'; | ||
} catch (error) { | ||
core.setFailed(`Action failed with error: ${error}`); | ||
} | ||
|
||
- name: Output result | ||
if: steps.parse-deps.outputs.dependencies != '[]' | ||
run: echo "${{ steps.check-aliyun.outputs.result }}" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.