Skip to content

Commit

Permalink
Merge pull request #35 from jgillis/regex
Browse files Browse the repository at this point in the history
Adding support for regex file
  • Loading branch information
dsaltares authored Mar 14, 2022
2 parents 1e60cfe + b5fecbf commit f471e12
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ The release version to fetch from in the form `tags/<tag_name>` or `<release_id>

### `target`

Target file path. Only supports paths to subdirectories of the GitHub Actions workspace directory
Target file path. Only supports paths to subdirectories of the GitHub Actions workspace directory.

### `regex`

Boolean indicating if `file` is to be interpreted as regular expression. Defaults to `false`.

## Outputs

Expand All @@ -40,6 +44,17 @@ The body (description) of a release.

## Example usage

Save a single asset as a new file `<workspace>/plague-linux.zip`:
```yaml
uses: dsaltares/fetch-gh-release-asset@master
with:
repo: "dsaltares/godot-wild-jam-18"
version: "tags/v0.1.18"
file: "plague-linux.zip"
token: ${{ secrets.GITHUB_TOKEN }}
```
Save a single asset as a new file `<workspace>/subdir/plague-linux.zip`:
```yaml
uses: dsaltares/fetch-gh-release-asset@master
with:
Expand All @@ -50,6 +65,29 @@ with:
token: ${{ secrets.GITHUB_TOKEN }}
```

Save a range of assets as new files in directory `<workspace>`:
```yaml
uses: dsaltares/fetch-gh-release-asset@master
with:
repo: "dsaltares/godot-wild-jam-18"
version: "tags/v0.1.18"
regex: true
file: "plague-.*\\.zip"
token: ${{ secrets.GITHUB_TOKEN }}
```

Save a range of assets as new files in directory `<workspace>/subdir`:
```yaml
uses: dsaltares/fetch-gh-release-asset@master
with:
repo: "dsaltares/godot-wild-jam-18"
version: "tags/v0.1.18"
regex: true
file: "plague-.*\\.zip"
target: "subdir/"
token: ${{ secrets.GITHUB_TOKEN }}
```

## Support

This action only supports Linux runners as this is a [docker container](https://docs.github.com/en/actions/creating-actions/about-actions#types-of-actions) action. If you encounter `Error: Container action is only supported on Linux` then you are using non-linux runner.
6 changes: 5 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ inputs:
description: 'The GitHub token. Typically this will be secrets.GITHUB_TOKEN'
required: false
default: ${{ github.token }}

regex:
description: 'Use regex for file input'
type: boolean
required: false
runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -30,6 +33,7 @@ runs:
- ${{ inputs.file }}
- ${{ inputs.target }}
- ${{ inputs.token }}
- ${{ inputs.regex }}

outputs:
version:
Expand Down
49 changes: 39 additions & 10 deletions fetch_github_asset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ if [[ -n ${INPUT_REPO} ]]; then
REPO=$INPUT_REPO
fi

echo "INPUT_REGEX:$INPUT_REGEX"

# Optional target file path
TARGET=$INPUT_FILE
if [[ "$INPUT_REGEX" == "true" ]]; then
TARGET=""
else
TARGET=$INPUT_FILE
fi
if [[ -n ${INPUT_TARGET} ]]; then
TARGET=$INPUT_TARGET
fi
Expand All @@ -28,6 +34,7 @@ if [[ -n ${INPUT_TOKEN} ]]; then
fi

API_URL="https://api.github.com/repos/$REPO"
echo "$API_URL/releases/${INPUT_VERSION}"
RELEASE_DATA=$(curl ${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \
"$API_URL/releases/${INPUT_VERSION}")
MESSAGE=$(echo "$RELEASE_DATA" | jq -r ".message")
Expand All @@ -44,8 +51,15 @@ if [[ "$MESSAGE" == "Not Found" ]]; then
fi

echo "MESSAGE: '$RELEASE_DATA'"
if [[ "$INPUT_REGEX" == "true" ]]; then
echo $INPUT_FILE
INPUT_FILE_ESCAPE=$(echo $INPUT_FILE | sed -e "s/\\\\/\\\\\\\/g")
echo $INPUT_FILE_ESCAPE
ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name|test(\"${INPUT_FILE_ESCAPE}\")) | .id")
else
ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name == \"${INPUT_FILE}\") | .id")
fi

ASSET_ID=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.name == \"${INPUT_FILE}\") | .id")
TAG_VERSION=$(echo "$RELEASE_DATA" | jq -r ".tag_name" | sed -e "s/^v//" | sed -e "s/^v.//")
RELEASE_NAME=$(echo "$RELEASE_DATA" | jq -r ".name")
RELEASE_BODY=$(echo "$RELEASE_DATA" | jq -r ".body")
Expand All @@ -55,14 +69,29 @@ if [[ -z "$ASSET_ID" ]]; then
exit 1
fi

curl \
-J \
-L \
-H "Accept: application/octet-stream" \
${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \
"$API_URL/releases/assets/$ASSET_ID" \
--create-dirs \
-o "${TARGET}"
if [[ "$INPUT_REGEX" == "true" ]]; then
for SINGLE_ASSET_ID in $ASSET_ID
do
NAME=$(echo "$RELEASE_DATA" | jq -r ".assets | .[] | select(.id == ${SINGLE_ASSET_ID}) | .name")
curl \
-J \
-L \
-H "Accept: application/octet-stream" \
${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \
"$API_URL/releases/assets/$SINGLE_ASSET_ID" \
--create-dirs \
-o "${TARGET}${NAME}"
done
else
curl \
-J \
-L \
-H "Accept: application/octet-stream" \
${TOKEN:+"-H"} ${TOKEN:+"Authorization: token ${TOKEN}"} \
"$API_URL/releases/assets/$ASSET_ID" \
--create-dirs \
-o "${TARGET}"
fi

echo "::set-output name=version::$TAG_VERSION"
echo "::set-output name=name::$RELEASE_NAME"
Expand Down

0 comments on commit f471e12

Please sign in to comment.