Skip to content

Commit 2c14af2

Browse files
authored
chore: replace curl/jq with gh CLI for release resolution and downloads (#16)
Signed-off-by: Yordis Prieto <yordis.prieto@gmail.com>
1 parent 224d068 commit 2c14af2

1 file changed

Lines changed: 25 additions & 32 deletions

File tree

.github/workflows/autobump.yml

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
permissions:
1818
contents: write
1919
pull-requests: write
20+
env:
21+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2022
strategy:
2123
fail-fast: false
2224
matrix:
@@ -70,20 +72,17 @@ jobs:
7072
- name: Get latest upstream version
7173
id: upstream
7274
run: |
73-
RESPONSE=$(curl -fsSL \
74-
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
75-
-H "Accept: application/vnd.github+json" \
76-
-H "X-GitHub-Api-Version: 2022-11-28" \
77-
"https://api.github.com/repos/${{ matrix.upstream_repo }}/releases?per_page=100")
78-
79-
if ! echo "$RESPONSE" | jq -e 'type == "array"' > /dev/null 2>&1; then
80-
echo "::error::GitHub API did not return an array for ${{ matrix.upstream_repo }}/releases. Response: $(echo "$RESPONSE" | jq -c .)"
75+
TAG=$(gh release list \
76+
--repo "${{ matrix.upstream_repo }}" \
77+
--limit 100 \
78+
--json tagName \
79+
--jq '[.[] | select(.tagName | startswith("${{ matrix.tag_prefix }}") and (contains("/") | not))] | first | .tagName | ltrimstr("${{ matrix.tag_prefix }}")')
80+
81+
if [ -z "$TAG" ] || [ "$TAG" = "null" ]; then
82+
echo "::error::Could not resolve upstream version for ${{ matrix.formula }} (tag_prefix: ${{ matrix.tag_prefix }})"
8183
exit 1
8284
fi
8385
84-
TAG=$(echo "$RESPONSE" \
85-
| jq -r --arg prefix "${{ matrix.tag_prefix }}" \
86-
'[.[] | select(.tag_name | startswith($prefix) and (contains("/") | not))] | first | .tag_name | ltrimstr($prefix)')
8786
echo "version=$TAG" >> $GITHUB_OUTPUT
8887
8988
- name: Get current formula version
@@ -97,11 +96,7 @@ jobs:
9796
run: |
9897
UPSTREAM="${{ steps.upstream.outputs.version }}"
9998
CURRENT="${{ steps.current.outputs.version }}"
100-
if [ -z "$UPSTREAM" ] || [ "$UPSTREAM" = "null" ]; then
101-
echo "needed=false" >> $GITHUB_OUTPUT
102-
echo "::error::${{ matrix.formula }}: could not resolve upstream version (got '$UPSTREAM'). Check tag_prefix and upstream_repo."
103-
exit 1
104-
elif [ "$UPSTREAM" = "$CURRENT" ]; then
99+
if [ "$UPSTREAM" = "$CURRENT" ]; then
105100
echo "needed=false" >> $GITHUB_OUTPUT
106101
echo "${{ matrix.formula }}: already at $CURRENT, skipping."
107102
else
@@ -116,9 +111,8 @@ jobs:
116111
VERSION="${{ steps.upstream.outputs.version }}"
117112
FORMULA="${{ matrix.formula }}"
118113
TAG_PREFIX="${{ matrix.tag_prefix }}"
119-
BASE="https://github.com/${{ matrix.upstream_repo }}/releases/download/${TAG_PREFIX}${VERSION}"
120114
STYLE="${{ matrix.url_style }}"
121-
RESULT=""
115+
TMPDIR=$(mktemp -d)
122116
123117
for ARCH in ${{ matrix.archs }}; do
124118
if [ "$STYLE" = "rust" ]; then
@@ -129,23 +123,28 @@ jobs:
129123
FILE="otelcol-contrib_${VERSION}_${ARCH}.tar.gz"
130124
fi
131125
132-
URL="${BASE}/${FILE}"
133-
SHA=$(curl -fsSL "$URL" | sha256sum | awk '{print $1}')
126+
gh release download "${TAG_PREFIX}${VERSION}" \
127+
--repo "${{ matrix.upstream_repo }}" \
128+
--pattern "$FILE" \
129+
--dir "$TMPDIR"
130+
131+
SHA=$(sha256sum "$TMPDIR/$FILE" | awk '{print $1}')
134132
if ! echo "$SHA" | grep -qE '^[a-f0-9]{64}$'; then
135-
echo "::error::Failed to fetch or hash $URL (got: '$SHA')"
133+
echo "::error::Invalid SHA256 for $FILE (got: '$SHA')"
136134
exit 1
137135
fi
138-
RESULT="${RESULT}${ARCH}=${SHA}\n"
136+
139137
echo "sha_${ARCH//[-.]/_}=$SHA" >> $GITHUB_OUTPUT
138+
rm "$TMPDIR/$FILE"
140139
done
141140
142-
printf "$RESULT"
141+
rmdir "$TMPDIR"
143142
144143
- name: Update formula
145144
if: steps.check.outputs.needed == 'true'
146145
run: |
147146
python3 - <<'PYEOF'
148-
import re, os, subprocess
147+
import re, os
149148
150149
formula_file = os.environ["FORMULA_FILE"]
151150
version = os.environ["NEW_VERSION"]
@@ -165,27 +164,21 @@ jobs:
165164
for arch in archs_raw:
166165
if style == "rust":
167166
new_url = f"{base}/{formula}-{version}-{arch}.tar.gz"
168-
arch_pat = arch.replace("-", r"\-")
169167
content = re.sub(
170-
rf'(url ")[^"]+{arch_pat}\.tar\.gz"',
168+
rf'(url ")[^"]+{re.escape(arch)}\.tar\.gz"',
171169
f'url "{new_url}"',
172170
content
173171
)
174172
elif style == "goreleaser":
175173
new_url = f"{base}/{formula}@v{version}_{arch}.tar.gz"
176-
arch_pat = arch.replace("_", r"\_")
177174
content = re.sub(
178175
rf'(url ")[^"]+{re.escape(arch)}\.tar\.gz"',
179176
f'url "{new_url}"',
180177
content
181178
)
182179
elif style == "otelcol":
183180
new_url = f"{base}/otelcol-contrib_{version}_{arch}.tar.gz"
184-
content = re.sub(
185-
rf'(url ")[^"]+"',
186-
f'url "{new_url}"',
187-
content
188-
)
181+
content = re.sub(r'(url ")[^"]+"', f'url "{new_url}"', content)
189182
190183
env_key = "sha_" + arch.replace("-", "_").replace(".", "_")
191184
sha = os.environ.get(env_key, "")

0 commit comments

Comments
 (0)