Skip to content

Commit 718b72b

Browse files
committed
Optimize spring-website-project-version-update logic
* Remove `VERSION_TO_REMOVE` from the `VERSIONS_TO_UPDATE`, so we won't even try POST for an existing version * Extract common `curl` parts into variables * Quote all the variables * Make code more readable
1 parent ae3f547 commit 718b72b

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

  • .github/actions/spring-website-project-version-update

.github/actions/spring-website-project-version-update/action.yml

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,34 @@ runs:
2323
PROJECT=${{ github.event.repository.name }}
2424
NEW_VERSION=${{ inputs.newVersion }}
2525
GITHUB_USER=$(gh api /user --jq '.login')
26+
BASE_URL="https://api.spring.io/projects/$PROJECT"
27+
declare -a CURL_OPTS=(-s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json")
2628
2729
# Check if project exists on Spring website
28-
HTTP_CODE=$(curl -s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json" -o /dev/null -w '%{http_code}' "https://api.spring.io/projects/$PROJECT")
30+
HTTP_CODE=$(curl "${CURL_OPTS[@]}" -o /dev/null -w '%{http_code}' "$BASE_URL")
2931
30-
if [ $HTTP_CODE != 200 ]
32+
if [ "$HTTP_CODE" != "200" ]
3133
then
32-
echo "::notice title=Nothing to update on Spring website::No versions update for $PROJECT project which is not listed on Spring website."
34+
echo "::notice title=Nothing to update on Spring website:: No versions update for $PROJECT project which is not listed on Spring website."
3335
exit 0
3436
fi
3537
36-
MAJOR_MINOR=$(echo $NEW_VERSION | cut -d '.' -f1-2)
38+
MAJOR_MINOR=$(echo "$NEW_VERSION" | cut -d '.' -f1-2)
3739
3840
# Fetch OSS support end date
39-
OSS_SUPPORT_END_DATE=$(curl -s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json" "https://api.spring.io/projects/$PROJECT/generations/${MAJOR_MINOR}.x" | jq -r '.ossSupportEndDate // empty')
41+
OSS_SUPPORT_END_DATE=$(curl "${CURL_OPTS[@]}" "$BASE_URL/generations/${MAJOR_MINOR}.x" \
42+
| jq -r '.ossSupportEndDate // empty')
4043
4144
VERSIONS_TO_UPDATE=$NEW_VERSION
4245
4346
# Add next SNAPSHOT version if OSS support is still active
4447
if [[ -n "$OSS_SUPPORT_END_DATE" && "$OSS_SUPPORT_END_DATE" > "$(date '+%F')" ]]
4548
then
46-
if [[ $NEW_VERSION == *"-"* ]]
49+
if [[ "$NEW_VERSION" == *"-"* ]]
4750
then
4851
NEXT_SNAPSHOT=${NEW_VERSION/-*}
4952
else
50-
PATCH=$(echo $NEW_VERSION | cut -d '.' -f3)
53+
PATCH=$(echo "$NEW_VERSION" | cut -d '.' -f3)
5154
PATCH=$((PATCH+1))
5255
NEXT_SNAPSHOT=$MAJOR_MINOR.$PATCH
5356
fi
@@ -57,37 +60,35 @@ runs:
5760
fi
5861
5962
# Fetch versions to remove
60-
VERSIONS_TO_REMOVE=$(curl -s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json" "https://api.spring.io/projects/$PROJECT/releases" | jq -r "._embedded.releases[].version | select(startswith(\"$MAJOR_MINOR\"))")
63+
VERSIONS_TO_REMOVE=$(curl "${CURL_OPTS[@]}" "$BASE_URL/releases" \
64+
| jq -r --arg major_minor "$MAJOR_MINOR" '._embedded.releases[].version | select(startswith($major_minor))')
6165
62-
# Remove old versions if they are not in the list of versions to update
66+
# Delete old versions and filter out existing from VERSIONS_TO_UPDATE
6367
for VERSION_TO_REMOVE in $VERSIONS_TO_REMOVE
6468
do
65-
if ! echo "$VERSIONS_TO_UPDATE" | grep -qw "$VERSION_TO_REMOVE"
69+
if echo "$VERSIONS_TO_UPDATE" | grep -qw "$VERSION_TO_REMOVE"
6670
then
67-
curl -s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json" -X DELETE --fail --show-error "https://api.spring.io/projects/$PROJECT/releases/$VERSION_TO_REMOVE"
71+
# Version exists in update list, remove it from update list
72+
VERSIONS_TO_UPDATE=$(echo ${VERSIONS_TO_UPDATE//$VERSION_TO_REMOVE/})
73+
else
74+
# Version not in update list, delete it
75+
curl "${CURL_OPTS[@]}" -X DELETE --fail --show-error "$BASE_URL/releases/$VERSION_TO_REMOVE"
6876
fi
6977
done
7078
71-
# Add new versions
72-
for VERSION in $VERSIONS_TO_UPDATE
73-
do
74-
SKIP_VERSION=false
75-
for VERSION_TO_REMOVE in $VERSIONS_TO_REMOVE
79+
# Add new versions (only the ones remaining in VERSIONS_TO_UPDATE)
80+
if [ -n "$VERSIONS_TO_UPDATE" ]
81+
then
82+
for VERSION in $VERSIONS_TO_UPDATE
7683
do
77-
if [ $VERSION_TO_REMOVE = $VERSION ]
78-
then
79-
SKIP_VERSION=true
80-
break
81-
fi
82-
done
83-
84-
if [ $SKIP_VERSION = false ]
85-
then
8684
VERSION_JSON=$(jq -n \
8785
--arg version "$VERSION" \
8886
--arg project "$PROJECT" \
89-
'{version: $version, referenceDocUrl: "https://docs.spring.io/\($project)/reference/{version}", apiDocUrl: "https://docs.spring.io/\($project)/docs/\($version)/api", isAntora: true}')
87+
'{version: $version,
88+
referenceDocUrl: "https://docs.spring.io/\($project)/reference/{version}",
89+
apiDocUrl: "https://docs.spring.io/\($project)/docs/\($version)/api",
90+
isAntora: true}')
9091
91-
curl -s -u "$GITHUB_USER:$GH_TOKEN" -H "Content-Type: application/json" -d "$VERSION_JSON" --fail --show-error "https://api.spring.io/projects/$PROJECT/releases"
92-
fi
93-
done
92+
curl "${CURL_OPTS[@]}" -d "$VERSION_JSON" --fail --show-error "$BASE_URL/releases"
93+
done
94+
fi

0 commit comments

Comments
 (0)