forked from leohenon/opencode-vim
-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (151 loc) · 7.7 KB
/
sync-upstream.yml
File metadata and controls
168 lines (151 loc) · 7.7 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
name: Sync with upstream
on:
workflow_dispatch:
permissions:
contents: read
issues: write
concurrency:
group: sync-upstream
cancel-in-progress: false
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ocv
fetch-depth: 0
token: ${{ secrets.SYNC_PAT }}
- name: Merge upstream/dev
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git remote add upstream https://github.com/sst/opencode.git
git fetch upstream dev
git merge --no-edit upstream/dev
- name: Bump ocv track for ocv PRs
env:
GH_TOKEN: ${{ secrets.SYNC_PAT }}
run: |
previous=$(gh release list --repo ${{ github.repository }} --limit 200 --json tagName -q 'map(.tagName | select(test("^v[0-9]+\\.[0-9]+\\.[0-9]+-ocv\\.[0-9]+\\.[0-9]+$"))) | .[0] // ""')
if [ -z "$previous" ]; then
echo "No previous ocv release found, skipping track bump"
exit 0
fi
git rev-parse --verify --quiet "$previous^{commit}" >/dev/null || git fetch origin "refs/tags/$previous:refs/tags/$previous"
track=$(tr -d '[:space:]' < .github/ocv-track)
if ! printf '%s' "$track" | grep -Eq '^[0-9]+\.[0-9]+$'; then
echo "Invalid ocv track: '$track'"
exit 1
fi
previous_track="${previous##*-ocv.}"
if [ "$track" != "$previous_track" ]; then
echo "ocv track is already bumped from $previous_track to $track"
exit 0
fi
prs=$(mktemp)
git fetch origin ocv:refs/remotes/origin/ocv
scan_ref=$(git rev-parse --verify --quiet origin/ocv >/dev/null && printf origin/ocv || printf HEAD)
git log --first-parent --reverse --format='%H' "$previous..$scan_ref" | while read -r sha; do
if pull_requests=$(gh api "repos/${{ github.repository }}/commits/$sha/pulls" -q '.[] | select(.base.ref == "ocv") | "#\(.number) \(.title)"' 2>/dev/null); then
if [ -n "$pull_requests" ]; then
printf '%s\n' "$pull_requests"
fi
fi
done | awk '!seen[$1]++' > "$prs"
if [ ! -s "$prs" ]; then
echo "No ocv PRs merged since $previous"
exit 0
fi
next="${track%.*}.$((10#${track#*.} + 1))"
printf '%s\n' "$next" > .github/ocv-track
git add .github/ocv-track
git commit -m "release: bump ocv-track"
echo "Bumped ocv track from $track to $next for ocv PRs merged since $previous:"
cat "$prs"
- name: Push
run: git push origin ocv
- name: Disable upstream workflows
env:
GH_TOKEN: ${{ secrets.SYNC_PAT }}
run: |
keep="ci.yml publish-ocv.yml sync-upstream.yml"
gh api repos/${{ github.repository }}/actions/workflows --paginate -q '.workflows[] | "\(.id) \(.path)"' | while read id path; do
name=$(basename "$path")
if echo "$keep" | grep -qw "$name"; then
continue
fi
gh api -X PUT "repos/${{ github.repository }}/actions/workflows/$id/disable" 2>/dev/null || true
done
- name: Check for new upstream version
id: version
env:
GH_TOKEN: ${{ secrets.SYNC_PAT }}
run: |
upstream=$(gh release list --repo sst/opencode --exclude-drafts --exclude-pre-releases --limit 200 --json tagName -q 'map(.tagName | select(test("^v[0-9]+\\.[0-9]+\\.[0-9]+$"))) | .[0] // ""' | sed 's/^v//')
current=$(gh release list --repo ${{ github.repository }} --limit 200 --json tagName -q 'map(.tagName | sub("^v"; "")) | map(select(test("^[0-9]+\\.[0-9]+\\.[0-9]+-ocv\\.[0-9]+\\.[0-9]+$"))) | .[0] // ""')
track=$(tr -d '[:space:]' < .github/ocv-track)
upstream_base=$(echo "$upstream" | sed -E 's/-(vim|ocv)\..*$//')
current_base=$(echo "$current" | sed -E 's/-(vim|ocv)\..*$//')
if ! printf '%s' "$track" | grep -Eq '^[0-9]+\.[0-9]+$'; then
echo "Invalid ocv track: '$track'"
exit 1
fi
release="${upstream_base}-ocv.${track}"
exists=$(gh release view "v$release" --repo ${{ github.repository }} >/dev/null 2>&1 && printf true || printf false)
echo "upstream=$upstream"
echo "current=$current"
echo "track=$track"
echo "upstream_base=$upstream_base"
echo "current_base=$current_base"
echo "release=$release"
echo "exists=$exists"
echo "upstream=$upstream" >> "$GITHUB_OUTPUT"
echo "current=$current" >> "$GITHUB_OUTPUT"
echo "track=$track" >> "$GITHUB_OUTPUT"
echo "upstream_base=$upstream_base" >> "$GITHUB_OUTPUT"
echo "current_base=$current_base" >> "$GITHUB_OUTPUT"
echo "release=$release" >> "$GITHUB_OUTPUT"
echo "exists=$exists" >> "$GITHUB_OUTPUT"
- name: Trigger release
if: steps.version.outputs.upstream_base != '' && steps.version.outputs.release != steps.version.outputs.current && steps.version.outputs.exists != 'true'
env:
GH_TOKEN: ${{ secrets.SYNC_PAT }}
run: gh workflow run publish-ocv.yml --repo ${{ github.repository }} --ref ocv -f version=${{ steps.version.outputs.release }}
- name: Close failure issue on success
if: success()
env:
GH_TOKEN: ${{ github.token }}
run: |
issues=$(gh issue list --repo ${{ github.repository }} --state open --limit 1000 --json number,title -q '.[] | select(.title == "[ci]: sync upstream failed" or .title == "[actions]: sync upstream failed") | .number')
if [ -z "$issues" ]; then
exit 0
fi
printf '%s\n' "$issues" | while read -r number; do
gh issue close "$number" --repo ${{ github.repository }} --comment "Closed automatically after a successful sync run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
done
- name: Notify on failure
if: failure()
env:
GH_TOKEN: ${{ github.token }}
run: |
gh label create ci --repo ${{ github.repository }} --color D73A4A --description "Continuous integration" 2>/dev/null || true
issues=$(gh issue list --repo ${{ github.repository }} --state open --limit 1000 --json number,title -q '.[] | select(.title == "[ci]: sync upstream failed" or .title == "[actions]: sync upstream failed") | [.number, .title] | @tsv')
tracker=$(printf '%s\n' "$issues" | awk -F '\t' '$2 == "[ci]: sync upstream failed" { print $1; exit }')
if [ -z "$tracker" ]; then
tracker=$(printf '%s\n' "$issues" | awk -F '\t' 'NF { print $1; exit }')
if [ -n "$tracker" ]; then
gh issue edit "$tracker" --repo ${{ github.repository }} --title "[ci]: sync upstream failed" --add-label ci
fi
fi
if [ -z "$tracker" ]; then
gh issue create --repo ${{ github.repository }} \
--title "[ci]: sync upstream failed" \
--label ci \
--body "Sync workflow failed. Likely a merge conflict. [View run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})"
exit 0
fi
gh issue edit "$tracker" --repo ${{ github.repository }} --add-label ci
printf '%s\n' "$issues" | awk -F '\t' -v tracker="$tracker" 'NF && $1 != tracker { print $1 }' | while read -r number; do
gh issue close "$number" --repo ${{ github.repository }} --comment "Closing duplicate; tracking this failure in #$tracker."
done