Skip to content

Update a blog post's date #2

Update a blog post's date

Update a blog post's date #2

name: "Update the blog post's date"
on:
workflow_dispatch:
inputs:
PR_NUM:
description: "PR ID to amend. Leave empty to perform changes on the main branch."
required: false
post_name:
description: "Blog post name to amend"
required: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Clone git repository
uses: actions/checkout@v4
with:
ref: "main"
- name: Update the date of ${{ github.event.inputs.post_name }}
env:
GH_TOKEN: ${{ github.token }}
run: |
set -e
readonly OLD_POST="${{ github.event.inputs.post_name }}"
readonly ORIGINAL_PR="${{ github.event.inputs.PR_NUM }}"
readonly NEW_DATE="$(date '+%Y-%m-%d')"
readonly REPO_ID="wildfly/wildfly.org"
readonly COMMIT_USERNAME="Wildfly.org CI"
readonly COMMIT_EMAIL="[email protected]"
FIX_BRANCH_NAME=''
SOURCE_LINK=""
if [ "${ORIGINAL_PR}" != '' ]; then
echo "Checking out PR${ORIGINAL_PR}"
gh pr checkout "${ORIGINAL_PR}" --repo "${REPO_ID}"
FIX_BRANCH_NAME="fix_pr_${ORIGINAL_PR}"
SOURCE_LINK="[PR${ORIGINAL_PR}]($(gh browse ${ORIGINAL_PR} --repo "${REPO_ID}" -n))"
else
echo "Using branch main"
FIX_BRANCH_NAME="fix_pr_main"
SOURCE_LINK="[main]($(gh browse -b main -n --repo "${REPO_ID}"))"
fi
if [ ! -f "_posts/${OLD_POST}" ]; then
echo "Blog post ${OLD_POST} not found"
exit 1
fi
echo "Creating a new branch: "
git checkout -b "${FIX_BRANCH_NAME}"
if [ "${ORIGINAL_PR}" != '' ]; then
# Issue_549 Merge main into FIX_BRANCH_NAME to avoid permission issues if main
# includes .github/workflows changes that are not in the PR branch
MERGE_MSG="Merge 'main' into '${FIX_BRANCH_NAME}'"
echo "${MERGE_MSG}"
git merge main -m "${MERGE_MSG}"
fi
readonly OLD_DATE=$(echo ${OLD_POST} | sed -E "s/([0-9]{4}\-[0-9]{2}\-[0-9]{2}).*/\1/")
echo "Replacing ${OLD_DATE} in the blog post"
sed -E -i "s/date:( *)${OLD_DATE}/date:\1${NEW_DATE}/" "_posts/${OLD_POST}"
readonly NEW_POST="${NEW_DATE}"`echo "${OLD_POST}" | sed -E "s/[0-9]{4}\-[0-9]{2}\-[0-9]{2}//"`
echo "Renaming the ${OLD_POST} to ${NEW_POST}"
git mv _posts/${OLD_POST} _posts/${NEW_POST}
echo "Committing changes and creating update PR"
git config --global user.name "${COMMIT_USERNAME}"
git config --global user.email "${COMMIT_EMAIL}"
readonly COMMIT_MESSAGE="Update ${OLD_POST} blog date"
git commit -am "${COMMIT_MESSAGE}"
git push --set-upstream origin "${FIX_BRANCH_NAME}"
gh pr create --title "${COMMIT_MESSAGE}"\
--body "Date update for ${OLD_POST} from ${SOURCE_LINK}"\
--repo "${REPO_ID}"