-
Notifications
You must be signed in to change notification settings - Fork 1
289 lines (247 loc) · 11.2 KB
/
Copy pathwinget.yml
File metadata and controls
289 lines (247 loc) · 11.2 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
name: Sync Winget
on:
push:
paths:
- 'bucket/*.json'
workflow_dispatch:
inputs:
manifests:
description: 'Comma or whitespace separated manifest names to process (without bucket/ and .json)'
required: false
type: string
jobs:
detect:
name: Detect manifest version changes
runs-on: ubuntu-latest
if: github.event_name != 'push' || github.actor != 'github-actions[bot]'
outputs:
has_updates: ${{ steps.collect.outputs.has_updates }}
packages: ${{ steps.collect.outputs.packages }}
steps:
- name: Checkout bucket
uses: actions/checkout@main
with:
fetch-depth: 0
- name: Collect Winget updates
id: collect
shell: bash
run: |
set -euo pipefail
before="${{ github.event.before }}"
head_sha="${{ github.sha }}"
map_file="winget/package-map.json"
event_name="${{ github.event_name }}"
dispatch_manifests="${{ github.event.inputs.manifests || '' }}"
emit_empty() {
echo "has_updates=false" >> "$GITHUB_OUTPUT"
echo "packages=[]" >> "$GITHUB_OUTPUT"
}
if [ "$event_name" != "workflow_dispatch" ] && { [ -z "$before" ] || [ "$before" = "0000000000000000000000000000000000000000" ]; }; then
before="$(git rev-list --max-count=1 HEAD^ 2>/dev/null || true)"
fi
if [ ! -f "$map_file" ]; then
emit_empty
exit 0
fi
if [ "$event_name" = "workflow_dispatch" ]; then
if [ -n "$dispatch_manifests" ]; then
mapfile -t changed_files < <(
printf '%s\n' "$dispatch_manifests" \
| tr ',\t ' '\n\n\n' \
| sed -E 's/\r$//' \
| sed -E 's#^bucket/##; s#\.json$##' \
| sed '/^$/d' \
| sort -u \
| while IFS= read -r manifest_name; do
manifest_path="bucket/${manifest_name}.json"
[ -f "$manifest_path" ] && printf '%s\n' "$manifest_path"
done
)
else
mapfile -t changed_files < <(find bucket -maxdepth 1 -type f -name '*.json' | sort)
fi
else
mapfile -t changed_files < <(
[ -n "$before" ] && git diff --name-only "$before" "$head_sha" -- 'bucket/*.json' | sort -u || true
)
fi
if [ "${#changed_files[@]}" -eq 0 ]; then
emit_empty
exit 0
fi
updates_json='[]'
for manifest_path in "${changed_files[@]}"; do
manifest_name="$(basename "$manifest_path" .json)"
winget_id=$(jq -r --arg name "$manifest_name" '.[$name] // empty' "$map_file")
if [ -z "$winget_id" ]; then
continue
fi
new_version=$(jq -r '.version // empty' "$manifest_path")
if [ -z "$new_version" ]; then
if [ "$event_name" = "workflow_dispatch" ]; then
echo "::error::Manifest '$manifest_path' is missing a version field"
exit 1
fi
continue
fi
old_version=""
if [ "$event_name" != "workflow_dispatch" ] && git cat-file -e "$before:$manifest_path" 2>/dev/null; then
old_version=$(git show "$before:$manifest_path" | jq -r '.version // empty' || true)
fi
if [ "$event_name" != "workflow_dispatch" ]; then
if [ -z "$old_version" ] || [ "$old_version" = "$new_version" ] || [ "$(printf '%s\n%s\n' "$old_version" "$new_version" | sort -V | tail -n1)" != "$new_version" ]; then
echo "Skipping $manifest_path: version did not increase ($old_version -> $new_version)"
continue
fi
fi
winget_first_char="$(printf '%s' "$winget_id" | cut -c1 | tr '[:upper:]' '[:lower:]')"
winget_manifest_base="manifests/${winget_first_char}/${winget_id//./\/}"
versions_json=$(curl -fsSL "https://api.github.com/repos/microsoft/winget-pkgs/contents/${winget_manifest_base}") || {
echo "Skipping $manifest_path: cannot read Winget package path for $winget_id"
continue
}
winget_version=$(jq -r '[.[] | select(.type == "dir" and (.name | test("^[0-9]"))) | .name] | sort_by(split(".") | map(tonumber? // .)) | last // empty' <<< "$versions_json")
if [ -z "$winget_version" ]; then
echo "Skipping $manifest_path: no Winget versions found for $winget_id"
continue
fi
if [ "$winget_version" = "$new_version" ] || [ "$(printf '%s\n%s\n' "$winget_version" "$new_version" | sort -V | tail -n1)" != "$new_version" ]; then
echo "Skipping $manifest_path: Scoop version is not newer than Winget ($new_version <= $winget_version)"
continue
fi
installer_manifest_path="${winget_manifest_base}/${winget_version}/${winget_id}.installer.yaml"
installer_manifest=$(curl -fsSL "https://raw.githubusercontent.com/microsoft/winget-pkgs/master/${installer_manifest_path}") || {
echo "Skipping $manifest_path: cannot read $installer_manifest_path"
continue
}
urls_json=$(printf '%s\n' "$installer_manifest" \
| sed -nE 's/^[[:space:]]*(-[[:space:]]*)?InstallerUrl:[[:space:]]*(.+)$/\2/p' \
| sed -E 's/[[:space:]]+$//' \
| jq -R -s 'split("\n") | map(select(length > 0))')
if [ "$(jq length <<< "$urls_json")" -eq 0 ]; then
echo "Skipping $manifest_path: no InstallerUrl entries found in Winget manifest"
continue
fi
urls_json=$(jq -c --arg old "$winget_version" --arg new "$new_version" '
map(if contains($old) then split($old) | join($new) else . end)
' <<< "$urls_json")
updates_json=$(jq -c --arg id "$winget_id" --arg version "$new_version" --argjson urls "$urls_json" \
'. + [{identifier: $id, version: $version, urls: $urls}]' <<< "$updates_json")
done
if [ "$(jq length <<< "$updates_json")" -eq 0 ]; then
emit_empty
exit 0
fi
echo "has_updates=true" >> "$GITHUB_OUTPUT"
{
echo "packages<<EOF"
echo "$updates_json"
echo "EOF"
} >> "$GITHUB_OUTPUT"
submit:
name: Submit Winget updates
runs-on: ubuntu-latest
needs: detect
if: needs.detect.outputs.has_updates == 'true'
strategy:
fail-fast: false
matrix:
package: ${{ fromJson(needs.detect.outputs.packages) }}
steps:
- name: Resolve Komac token
id: token
env:
WINGET_TOKEN: ${{ secrets.WINGET_TOKEN }}
shell: bash
run: |
set -euo pipefail
token="${WINGET_TOKEN:-}"
if [ -z "$token" ]; then
echo "has_token=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "::add-mask::$token"
echo "has_token=true" >> "$GITHUB_OUTPUT"
echo "token=$token" >> "$GITHUB_OUTPUT"
- name: Skip Komac update when token is missing
if: steps.token.outputs.has_token != 'true'
run: echo "::warning::Skipping Komac submission because WINGET_TOKEN is not set"
- name: Validate Komac token permissions
if: steps.token.outputs.has_token == 'true'
id: validate
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
shell: bash
run: |
set -euo pipefail
api() {
curl -fsSL \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"$1"
}
viewer=$(api "https://api.github.com/user")
login=$(jq -r '.login // empty' <<< "$viewer")
if [ -z "$login" ]; then
echo "::error::WINGET_TOKEN can authenticate, but GitHub did not return an account login."
exit 1
fi
echo "login=$login" >> "$GITHUB_OUTPUT"
fork_repo=$(api "https://api.github.com/repos/${login}/winget-pkgs" 2>/dev/null || true)
if [ -z "$fork_repo" ]; then
echo "::error::WINGET_TOKEN belongs to '${login}', but https://github.com/${login}/winget-pkgs is missing or not accessible."
echo "::error::Create a fork of microsoft/winget-pkgs under '${login}' and grant the token repository contents write access."
exit 1
fi
default_branch=$(jq -r '.default_branch // empty' <<< "$fork_repo")
if [ -z "$default_branch" ]; then
echo "::error::GitHub did not report a default branch for ${login}/winget-pkgs."
exit 1
fi
echo "default_branch=$default_branch" >> "$GITHUB_OUTPUT"
if [ "$(jq -r '.fork // false' <<< "$fork_repo")" != "true" ]; then
echo "::error::${login}/winget-pkgs exists, but GitHub does not report it as a fork."
echo "::error::Komac submits through a fork of microsoft/winget-pkgs; recreate the fork before rerunning."
exit 1
fi
if [ "$(jq -r '.permissions.push // false' <<< "$fork_repo")" != "true" ]; then
echo "::error::WINGET_TOKEN for '${login}' can read ${login}/winget-pkgs but cannot push branches to it."
echo "::error::Grant repository contents write access to the token, or replace it with a token that can create refs in the fork."
exit 1
fi
- name: Sync winget-pkgs fork
if: steps.token.outputs.has_token == 'true'
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
FORK_OWNER: ${{ steps.validate.outputs.login }}
FORK_BRANCH: ${{ steps.validate.outputs.default_branch }}
shell: bash
run: |
set -euo pipefail
response_file="$(mktemp)"
status_code=$(curl -sSL -o "$response_file" -w '%{http_code}' -X POST \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${FORK_OWNER}/winget-pkgs/merge-upstream" \
-d "{\"branch\":\"${FORK_BRANCH}\"}")
response="$(cat "$response_file")"
if [ "$status_code" != "200" ]; then
message=$(jq -r '.message // empty' <<< "$response")
echo "::error::Failed to sync ${FORK_OWNER}/winget-pkgs:${FORK_BRANCH} before running Komac (HTTP ${status_code}${message:+: ${message}})."
exit 1
fi
message=$(jq -r '.message // empty' <<< "$response")
if [ -n "$message" ]; then
echo "::notice::$message"
fi
- name: Run Komac update
if: steps.token.outputs.has_token == 'true'
uses: michidk/run-komac@v2
with:
args: >-
update ${{ matrix.package.identifier }}
--version ${{ matrix.package.version }}
--urls ${{ join(matrix.package.urls, ' ') }}
--submit
--token ${{ steps.token.outputs.token }}