Skip to content

Commit 6709b66

Browse files
waldnerbrokenpip3
andauthored
allow disabling the github token (#66)
Co-authored-by: Luigi Operoso <40476330+brokenpip3@users.noreply.github.com>
1 parent 6371195 commit 6709b66

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ For example, if you want to install `bats-support` in the `./test/bats-support`
6363
[...]
6464
```
6565

66+
## Authenticated requests
67+
68+
The action performs a number of calls against github API URLs to discover bats release numbers and download assets. To help against github rate limits, you can pass a valid github token so that those requests are authenticated. Example:
69+
70+
``` yaml
71+
[...]
72+
- name: Setup Bats and Bats libs
73+
id: setup-bats
74+
uses: bats-core/bats-action@3.0.1
75+
with:
76+
github-token: ${{ secrets.GITHUB_TOKEN }}
77+
[...]
78+
```
79+
80+
If you don't have a token (for example, because you're using this action in a non-github pipeline), you can omit the token and things shoud work just fine, however you'll be slightly more likely to run into github rate limits, so keep this in mind if you encounter problems.
81+
82+
6683
## About Caching
6784

6885
The caching mechanism for the `bats binary` is always available. However, the caching for the `bats libraries` is dependent on the location of each library path. If a library is located within the $HOME directory, caching is supported. Conversely, if a library is located outside the $HOME directory (which is the default location per each library), caching is not supported. This is due to a known limitation with sudo and the cache action, as detailed in this GitHub issue: https://github.com/actions/toolkit/issues/946.

action.yaml

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ inputs:
8484
github-token:
8585
description: "GitHub token to use to download the releases"
8686
required: false
87-
default: ${{ github.token }}
87+
default: ""
8888
outputs:
8989
bats-installed:
9090
description: "True/False if bats has been installed"
@@ -138,18 +138,23 @@ runs:
138138
VERSION=${{ inputs.bats-version }}
139139
DESTDIR="$HOME/.local/share/bats"
140140
TEMPDIR="/tmp/bats"
141-
URL="https://github.com/bats-core/bats-core/"
141+
URL="https://api.github.com"
142+
143+
declare -a AUTH_ARGS=()
144+
if [ "$GITHUB_TOKEN" != "" ]; then
145+
AUTH_ARGS=( -H "Authorization: token $GITHUB_TOKEN" )
146+
fi
142147
143148
# From https://github.com/fluxcd/flux2/blob/44d69d6fc0c353e79c1bad021a4aca135033bce8/action/action.yml#L35
144149
if [[ -z "$VERSION" ]] || [[ "$VERSION" = "latest" ]]; then
145-
VERSION=$(curl -fsSL --retry 4 --retry-connrefused -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/bats-core/bats-core/releases/latest | grep tag_name | cut -d '"' -f 4)
150+
VERSION=$(curl -fsSL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${URL}/repos/bats-core/bats-core/releases/latest" | grep tag_name | cut -d '"' -f 4)
146151
fi
147152
[[ $VERSION == v* ]] && VERSION="${VERSION:1}"
148153
149154
mkdir -p ${TEMPDIR}
150155
mkdir -p ${DESTDIR}
151156
152-
curl -sL --retry 4 --retry-connrefused -H "Authorization: token $GITHUB_TOKEN" ${URL}/archive/refs/tags/v${VERSION}.tar.gz | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
157+
curl -sL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${URL}/repos/bats-core/bats-core/tarball/v${VERSION}" | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
153158
154159
./install.sh ${DESTDIR}
155160
echo "Bats v${VERSION} installed in ${DESTDIR}"
@@ -209,13 +214,18 @@ runs:
209214
shell: bash
210215
run: |
211216
VERSION=${{ inputs.support-version }}
212-
url="https://github.com/bats-core/bats-support/archive/refs/tags/v${VERSION}.tar.gz"
217+
url="https://api.github.com/repos/bats-core/bats-support/tarball/v${VERSION}"
213218
TEMPDIR="${TEMPDIR}/bats-support"
214219
DESTDIR=${SUPPORT_DESTDIR}
215220
221+
declare -a AUTH_ARGS=()
222+
if [ "$GITHUB_TOKEN" != "" ]; then
223+
AUTH_ARGS=( -H "Authorization: token $GITHUB_TOKEN" )
224+
fi
225+
216226
mkdir -p ${TEMPDIR}
217227
${CMD} mkdir -p ${DESTDIR}/src/
218-
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
228+
curl -sL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${url}" | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
219229
# Archlinux style, except that we are not in a fakeroot env
220230
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
221231
for fn in src/*.bash; do
@@ -225,6 +235,8 @@ runs:
225235
echo "Bats Support v$VERSION installed in $DESTDIR"
226236
echo "support-installed=true" >> $GITHUB_OUTPUT
227237
[[ "${{ inputs.support-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
238+
env:
239+
GITHUB_TOKEN: ${{ inputs.github-token }}
228240

229241
- name: "Calculate DESTDIR for Bats-assert"
230242
if: inputs.assert-install == 'true'
@@ -251,13 +263,18 @@ runs:
251263
shell: bash
252264
run: |
253265
VERSION=${{ inputs.assert-version }}
254-
url="https://github.com/bats-core/bats-assert/archive/refs/tags/v${VERSION}.tar.gz"
266+
url="https://api.github.com/repos/bats-core/bats-assert/tarball/v${VERSION}"
255267
TEMPDIR="${TEMPDIR}/bats-assert"
256268
DESTDIR=${ASSERT_DESTDIR}
257269
270+
declare -a AUTH_ARGS=()
271+
if [ "$GITHUB_TOKEN" != "" ]; then
272+
AUTH_ARGS=( -H "Authorization: token $GITHUB_TOKEN" )
273+
fi
274+
258275
mkdir -p ${TEMPDIR}
259276
${CMD} mkdir -p ${DESTDIR}/src/
260-
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
277+
curl -sL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${url}" | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
261278
# Archlinux style, except that we are not in a fakeroot env
262279
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
263280
for fn in src/*.bash; do
@@ -267,6 +284,8 @@ runs:
267284
echo "Bats Assert v$VERSION installed in $DESTDIR"
268285
echo "assert-installed=true" >> "$GITHUB_OUTPUT"
269286
[[ "${{ inputs.assert-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
287+
env:
288+
GITHUB_TOKEN: ${{ inputs.github-token }}
270289

271290
- name: "Calculate DESTDIR for Bats-detik"
272291
if: inputs.detik-install == 'true'
@@ -293,13 +312,18 @@ runs:
293312
shell: bash
294313
run: |
295314
VERSION=${{ inputs.detik-version }}
296-
url="https://github.com/bats-core/bats-detik/archive/refs/tags/v${VERSION}.tar.gz"
315+
url="https://api.github.com/repos/bats-core/bats-detik/tarball/v${VERSION}"
297316
TEMPDIR="${TEMPDIR}/bats-detik"
298317
DESTDIR="${DETIK_DESTDIR}"
299318
319+
declare -a AUTH_ARGS=()
320+
if [ "$GITHUB_TOKEN" != "" ]; then
321+
AUTH_ARGS=( -H "Authorization: token $GITHUB_TOKEN" )
322+
fi
323+
300324
mkdir -p ${TEMPDIR}
301325
${CMD} mkdir -p ${DESTDIR}/src/
302-
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
326+
curl -sL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${url}" | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
303327
# Archlinux style, except that we are not in a fakeroot env so we need to use sudo
304328
for fn in lib/*.bash; do
305329
${CMD} install -m755 $fn \
@@ -308,6 +332,8 @@ runs:
308332
echo "Bats Detik v$VERSION installed in $DESTDIR"
309333
echo "detik-installed=true" >> $GITHUB_OUTPUT
310334
[[ "${{ inputs.detik-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
335+
env:
336+
GITHUB_TOKEN: ${{ inputs.github-token }}
311337

312338
- name: "Calculate DESTDIR for Bats-file"
313339
if: inputs.file-install == 'true'
@@ -334,13 +360,18 @@ runs:
334360
shell: bash
335361
run: |
336362
VERSION=${{ inputs.file-version }}
337-
url="https://github.com/bats-core/bats-file/archive/refs/tags/v${VERSION}.tar.gz"
363+
url="https://api.github.com/repos/bats-core/bats-file/tarball/v${VERSION}"
338364
TEMPDIR="${TEMPDIR}/bats-file"
339365
DESTDIR="${FILE_DESTDIR}"
340366
367+
declare -a AUTH_ARGS=()
368+
if [ "$GITHUB_TOKEN" != "" ]; then
369+
AUTH_ARGS=( -H "Authorization: token $GITHUB_TOKEN" )
370+
fi
371+
341372
mkdir -p ${TEMPDIR}
342373
${CMD} mkdir -p ${DESTDIR}/src/
343-
curl -sL --retry 4 --retry-connrefused ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
374+
curl -sL --retry 4 --retry-connrefused "${AUTH_ARGS[@]}" "${url}" | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR}
344375
# Archlinux style, except that we are not in a fakeroot env
345376
${CMD} install -m755 load.bash ${DESTDIR}/load.bash
346377
for fn in src/*.bash; do
@@ -350,6 +381,8 @@ runs:
350381
echo "Bats File v$VERSION installed in $DESTDIR"
351382
echo "file-installed=true" >> $GITHUB_OUTPUT
352383
[[ "${{ inputs.file-clean }}" == "true" ]] && rm -rf ${TEMPDIR} || exit 0
384+
env:
385+
GITHUB_TOKEN: ${{ inputs.github-token }}
353386

354387
- name: "build BATS_LIB_PATH"
355388
id: libpath

0 commit comments

Comments
 (0)