|
| 1 | +name: Check Aliyun Maven Dependencies |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [ opened, synchronize, reopened ] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + check-dependencies: |
| 14 | + # 只在dependabot的PR上运行 |
| 15 | + if: contains(github.event.pull_request.user.login, 'dependabot') |
| 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: Set up Node.js |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: '20' |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: | |
| 37 | + npm install xml2js |
| 38 | + npm install @actions/github |
| 39 | +
|
| 40 | + - name: Get build file changes |
| 41 | + id: get-changes |
| 42 | + run: | |
| 43 | + # 检测项目类型并获取变更内容 |
| 44 | + if [ -f "pom.xml" ]; then |
| 45 | + echo "build_type=maven" >> $GITHUB_OUTPUT |
| 46 | + # PR事件获取当前PR的pom.xml变更 |
| 47 | + git diff origin/${{ github.event.pull_request.base.ref }} -- pom.xml > changes.diff || true |
| 48 | + elif [ -f "build.gradle" ]; then |
| 49 | + echo "build_type=gradle" >> $GITHUB_OUTPUT |
| 50 | + git diff origin/${{ github.event.pull_request.base.ref }} -- build.gradle > changes.diff || true |
| 51 | + else |
| 52 | + echo "No supported build file found" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | + echo "changes<<EOF" >> $GITHUB_OUTPUT |
| 56 | + cat changes.diff >> $GITHUB_OUTPUT |
| 57 | + echo "EOF" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | + - name: Parse dependencies |
| 60 | + id: parse-deps |
| 61 | + uses: actions/github-script@v7 |
| 62 | + with: |
| 63 | + script: | |
| 64 | + const fs = require('fs'); |
| 65 | + const xml2js = require('xml2js'); |
| 66 | + try { |
| 67 | + const buildType = '${{ steps.get-changes.outputs.build_type }}'; |
| 68 | + const changes = '${{ steps.get-changes.outputs.changes }}'; |
| 69 | + const dependencies = []; |
| 70 | +
|
| 71 | + if (buildType === 'gradle') { |
| 72 | + // Gradle 依赖解析 |
| 73 | + const regex = /([+-])\s*(implementation|api|compile|testImplementation|runtimeOnly)\s*['"]([^'"]+)['"]/g; |
| 74 | + let match; |
| 75 | +
|
| 76 | + while ((match = regex.exec(changes)) !== null) { |
| 77 | + if (match[1] === '+') { |
| 78 | + const depParts = match[3].split(':'); |
| 79 | + if (depParts.length === 3) { |
| 80 | + dependencies.push({ |
| 81 | + group: depParts[0], |
| 82 | + artifact: depParts[1], |
| 83 | + version: depParts[2] |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } else if (buildType === 'maven') { |
| 89 | + // Maven 依赖解析 |
| 90 | + // 1. 读取并解析整个pom.xml |
| 91 | + const pomContent = fs.readFileSync('pom.xml', 'utf-8'); |
| 92 | +
|
| 93 | + // 使用xml2js解析pom.xml |
| 94 | + const parser = new xml2js.Parser({ explicitArray: false }); |
| 95 | + const result = await parser.parseStringPromise(pomContent); |
| 96 | +
|
| 97 | + // 2. 分析变更 |
| 98 | + const diffLines = changes.split('\n'); |
| 99 | + const changedProperties = {}; |
| 100 | + // 提取properties |
| 101 | + const properties = result.project?.properties || {}; |
| 102 | + // 找出变更的属性 |
| 103 | + const propertyRegex = /^([+-])\s*<([^>]+\.version)>\s*([^<]+)\s*<\/[^>]+>$/; |
| 104 | + for (const line of diffLines) { |
| 105 | + const match = line.match(propertyRegex); |
| 106 | + if (match && match[1] === '+') { |
| 107 | + const propName = match[2]; // 获取属性名 |
| 108 | + const propValue = match[3].trim(); // 获取新值 |
| 109 | + changedProperties[propName] = propValue; |
| 110 | + } |
| 111 | + } |
| 112 | + // 3. 合并变更后的属性 |
| 113 | + const effectiveProperties = { ...properties, ...changedProperties }; |
| 114 | +
|
| 115 | + console.log("=====changedProperties====="); |
| 116 | + console.log(changedProperties); |
| 117 | +
|
| 118 | + // 递归提取依赖的函数 |
| 119 | + const extractDeps = (deps, scope) => { |
| 120 | + if (!deps?.dependency) return; |
| 121 | + const depList = Array.isArray(deps.dependency) ? deps.dependency : [deps.dependency]; |
| 122 | +
|
| 123 | + depList.forEach(dep => { |
| 124 | + if (!dep.groupId || !dep.artifactId) return; |
| 125 | + // 处理版本号(可能是直接值或属性引用) |
| 126 | + let version = dep.version || ''; |
| 127 | + let fromProperty = ''; |
| 128 | +
|
| 129 | + if (version.startsWith('${') && version.endsWith('}')) { |
| 130 | + const propName = version.slice(2, -1); |
| 131 | + version = effectiveProperties[propName] || version; |
| 132 | + fromProperty = propName; |
| 133 | + } |
| 134 | +
|
| 135 | + // 只有当属性有变更或版本直接变更时才包含 |
| 136 | + if (fromProperty in changedProperties || !version.startsWith('${')) { |
| 137 | + dependencies.push({ |
| 138 | + group: dep.groupId, |
| 139 | + artifact: dep.artifactId, |
| 140 | + version: version, |
| 141 | + scope: scope || dep.scope || 'compile', |
| 142 | + fromProperty: fromProperty || undefined, |
| 143 | + isManaged: scope === 'management' |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | + }; |
| 148 | +
|
| 149 | + // 1. 提取dependencyManagement中的依赖 |
| 150 | + if (result.project?.dependencyManagement?.dependencies) { |
| 151 | + extractDeps(result.project.dependencyManagement.dependencies, 'management'); |
| 152 | + } |
| 153 | +
|
| 154 | + // 2. 提取普通dependencies |
| 155 | + if (result.project?.dependencies) { |
| 156 | + extractDeps(result.project.dependencies); |
| 157 | + } |
| 158 | + } |
| 159 | +
|
| 160 | + core.setOutput('dependencies', JSON.stringify(dependencies)); |
| 161 | + console.log("=====dependencies.length====="); |
| 162 | + console.log(dependencies.length); |
| 163 | + return dependencies.length; |
| 164 | + } catch (error) { |
| 165 | + core.setFailed(`Failed to parse dependencies: ${error}`); |
| 166 | + } |
| 167 | +
|
| 168 | + - name: Check Aliyun Maven availability |
| 169 | + if: steps.parse-deps.outputs.dependencies != '[]' |
| 170 | + id: check-aliyun |
| 171 | + uses: actions/github-script@v7 |
| 172 | + with: |
| 173 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 174 | + script: | |
| 175 | + const { execSync } = require('child_process'); |
| 176 | +
|
| 177 | + try { |
| 178 | + const dependencies = JSON.parse('${{ steps.parse-deps.outputs.dependencies }}'); |
| 179 | + const aliMavenUrl = 'https://maven.aliyun.com/repository/public'; |
| 180 | + const results = []; |
| 181 | +
|
| 182 | + for (const dep of dependencies) { |
| 183 | + if (dep.artifact.startsWith("javafxTool-")) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + const artifactPath = dep.group.replace(/\./g, '/') + '/' + dep.artifact + '/' + dep.version; |
| 187 | + const pomUrl = `${aliMavenUrl}/${artifactPath}/${dep.artifact}-${dep.version}.pom`; |
| 188 | + console.log(pomUrl); |
| 189 | +
|
| 190 | + try { |
| 191 | + execSync(`curl -I -s -o /dev/null -w "%{http_code}" ${pomUrl} | grep 200`); |
| 192 | + results.push(`✅ ${dep.group}:${dep.artifact}:${dep.version} - 可用`); |
| 193 | + } catch (e) { |
| 194 | + results.push(`❌ ${dep.group}:${dep.artifact}:${dep.version} - 不可用`); |
| 195 | + } |
| 196 | + } |
| 197 | +
|
| 198 | + if (results.length > 0) { |
| 199 | + const { data: comments } = await github.rest.issues.listComments({ |
| 200 | + owner: context.repo.owner, |
| 201 | + repo: context.repo.repo, |
| 202 | + issue_number: context.issue.number, |
| 203 | + }); |
| 204 | +
|
| 205 | + // 查找我们之前发布的评论(通过特定标识) |
| 206 | + const botComment = comments.find(comment => |
| 207 | + comment.user.login === 'github-actions[bot]' && |
| 208 | + comment.body.startsWith('### 阿里云 Maven 依赖检查结果') |
| 209 | + ); |
| 210 | +
|
| 211 | + const commentBody = `### 阿里云 Maven 依赖检查结果\n\n${results.join('\n')}`; |
| 212 | + if (botComment) { |
| 213 | + // 更新现有评论 |
| 214 | + await github.rest.issues.updateComment({ |
| 215 | + owner: context.repo.owner, |
| 216 | + repo: context.repo.repo, |
| 217 | + comment_id: botComment.id, |
| 218 | + body: commentBody |
| 219 | + }); |
| 220 | + } else { |
| 221 | + // 创建新评论 |
| 222 | + github.rest.issues.createComment({ |
| 223 | + owner: context.repo.owner, |
| 224 | + repo: context.repo.repo, |
| 225 | + issue_number: context.issue.number, |
| 226 | + body: commentBody |
| 227 | + }); |
| 228 | + } |
| 229 | +
|
| 230 | + core.setOutput('result', commentBody); |
| 231 | + } |
| 232 | +
|
| 233 | + return 'Dependency check completed'; |
| 234 | + } catch (error) { |
| 235 | + core.setFailed(`Action failed with error: ${error}`); |
| 236 | + } |
| 237 | +
|
| 238 | + - name: Output result |
| 239 | + if: steps.parse-deps.outputs.dependencies != '[]' |
| 240 | + run: echo "${{ steps.check-aliyun.outputs.result }}" |
0 commit comments