-
Notifications
You must be signed in to change notification settings - Fork 0
210 lines (184 loc) · 7.94 KB
/
check_version.yml
File metadata and controls
210 lines (184 loc) · 7.94 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
name: Check new version
on:
workflow_call:
inputs:
repository:
description: repository url
required: true
type: string
library:
description: library name
required: true
type: string
pr_body:
description: custom pull request body (optional)
required: false
type: string
tag_filter:
description: include filter for tag names (regex supported, optional)
required: false
default: ''
type: string
tag_exclude:
description: exclude filter for tag names (regex supported, optional)
required: false
default: ''
type: string
jobs:
check_version:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Normalize repository input
shell: bash
run: |
repo_input='${{ inputs.repository }}'
if [[ "$repo_input" =~ ^https?:// ]]; then
repo_url="$repo_input"
repo_url="${repo_url%/}"
else
repo_url="https://github.com/$repo_input"
fi
echo "repository url: $repo_url"
echo "REPOSITORY_URL=$repo_url" >> $GITHUB_ENV
- name: Checkout latest code
shell: bash
run: |
git clone --no-single-branch "$REPOSITORY_URL" latest_code || {
echo "Failed to clone repository: $REPOSITORY_URL"
exit 1
}
git -C latest_code fetch --tags --force
- name: Resolve latest version and update file
uses: actions/github-script@v7
with:
script: |
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const tagFilter = `${{ inputs.tag_filter }}`
const tagExclude = `${{ inputs.tag_exclude }}`
const library = `${{ inputs.library }}`
const escapeRegex = (text) => text.replace(/[\\^$.*+?()[\]{}|-]/g, '\\$&')
const hasRegexMeta = (text) => /[.*+?^$[\](){}|\\]/.test(text)
const parseVersion = (ver) => {
// Keep parity with previous Python implementation.
if (!ver || ver.includes('-')) return 0
const digits = ver.replace(/[^0-9]+/g, '')
return digits ? Number.parseInt(digits, 10) : 0
}
const safeRegex = (pattern) => {
try {
return new RegExp(pattern)
} catch {
return null
}
}
const appendEnv = (key, value) => {
fs.appendFileSync(process.env.GITHUB_ENV, `${key}=${value}\n`)
}
const runGit = (args) =>
cp.execFileSync('git', args, { cwd: './latest_code', encoding: 'utf8' }).trim()
const latestCodePath = path.resolve('./latest_code')
try {
let tags = runGit(['tag', '--sort=-v:refname'])
.split('\n')
.map((tag) => tag.trim())
.filter(Boolean)
if (tags.length === 0) {
core.setFailed('No tags found in source repository. Please verify repository and tags.')
return
}
let emptyReason = ''
if (tagFilter) {
if (hasRegexMeta(tagFilter)) {
const includeRegex = safeRegex(tagFilter)
tags = includeRegex ? tags.filter((tag) => includeRegex.test(tag)) : []
} else {
tags = tags.filter((tag) => tag.includes(tagFilter))
}
if (tags.length === 0) emptyReason = 'include'
}
if (tagExclude) {
if (hasRegexMeta(tagExclude)) {
const excludeRegex = safeRegex(tagExclude)
tags = excludeRegex ? tags.filter((tag) => !excludeRegex.test(tag)) : []
} else {
tags = tags.filter((tag) => !tag.includes(tagExclude))
}
if (tags.length === 0 && !emptyReason) emptyReason = 'exclude'
} else if (!tagFilter) {
tags = tags.filter((tag) => !/[-_]/.test(tag))
if (tags.length === 0 && !emptyReason) emptyReason = 'default_exclude'
}
const latestTag = tags[0] || ''
if (!latestTag) {
if (emptyReason === 'include') {
core.setFailed(`No tags matched include filter. tag_filter='${tagFilter}'. Please verify patterns and run 'git tag --sort=-v:refname' in the source repository.`)
} else if (emptyReason === 'exclude') {
core.setFailed(`All tags were excluded by tag_exclude. tag_exclude='${tagExclude}'. Please verify patterns and run 'git tag --sort=-v:refname' in the source repository.`)
} else if (emptyReason === 'default_exclude') {
core.setFailed("All tags were excluded by default rule '[-_]'. Set tag_exclude to customize behavior, or check available tags with 'git tag --sort=-v:refname'.")
} else {
core.setFailed("No matching tag found after include-then-exclude filtering. Please verify patterns and run 'git tag --sort=-v:refname' in the source repository.")
}
return
}
core.info(`latest tag: ${latestTag}`)
appendEnv('NEW_VERSION', latestTag)
const filePath = './Sources/BuildScripts/XCFrameworkBuild/main.swift'
const content = fs.readFileSync(filePath, 'utf8')
let versionPattern
try {
versionPattern = new RegExp(`(case \\.${escapeRegex(library)}[^"]+?)"(.+?)"`)
} catch {
core.setFailed(`Invalid library pattern for '${library}'.`)
return
}
const matched = content.match(versionPattern)
if (!matched) {
core.setFailed(`Cannot find version entry for library '${library}' in ${filePath}.`)
return
}
const oldVersion = matched[2]
core.info(`old version: ${oldVersion}`)
core.info(`new version: ${latestTag}`)
appendEnv('OLD_VERSION', oldVersion)
if (parseVersion(latestTag) > parseVersion(oldVersion)) {
const updated = content.replace(versionPattern, `$1"${latestTag}"`)
fs.writeFileSync(filePath, updated, 'utf8')
appendEnv('FOUND_NEW_VERSION', '1')
}
} finally {
fs.rmSync(latestCodePath, { recursive: true, force: true })
}
- name: Prepare PR body
uses: jannekem/run-python-script-action@v1
with:
script: |
import os
pr_input = '''${{ inputs.pr_body }}'''
repo_url = '${{ env.REPOSITORY_URL }}'
new = '${{ env.NEW_VERSION }}'
old = '${{ env.OLD_VERSION }}'
if pr_input and pr_input.strip():
# Replace placeholders with actual values
pr_body = pr_input.replace('{REPOSITORY_URL}', repo_url)
pr_body = pr_body.replace('{NEW_VERSION}', new)
pr_body = pr_body.replace('{OLD_VERSION}', old)
else:
pr_body = f"{repo_url}/releases/tag/{new}\n\n{repo_url}/compare/{old}...{new}"
with open('/tmp/PR_BODY.txt', 'w', encoding='utf-8') as f:
f.write(pr_body)
- name: Create Pull Request
if: env.FOUND_NEW_VERSION
uses: peter-evans/create-pull-request@v6
with:
add-paths: |
./Sources/BuildScripts/XCFrameworkBuild/main.swift
title: "bump version to ${{ env.NEW_VERSION }}"
body-path: /tmp/PR_BODY.txt
commit-message: "chore: bump version to ${{ env.NEW_VERSION }}"