diff --git a/.github/workflows/update_post.yml b/.github/workflows/update_post.yml new file mode 100644 index 00000000..1cf42b2d --- /dev/null +++ b/.github/workflows/update_post.yml @@ -0,0 +1,80 @@ +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="wildfly-dev@lists.jboss.org" + + FIX_BRANCH_NAME='' + PR_TITLE="" + 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))" + PR_TITLE="Update date of PR: $(gh pr view 1 --repo "${REPO_ID}" --json title --jq '.title')" + else + echo "Using branch main" + FIX_BRANCH_NAME="fix_pr_main" + + SOURCE_LINK="[main]($(gh browse -b main -n --repo "${REPO_ID}"))" + PR_TITLE="Update date of post: ${OLD_POST}" + fi + + if [ ! -f "_posts/${OLD_POST}" ]; then + echo "Blog post ${OLD_POST} not found" + exit 1 + 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 "Creating a new branch: " + git checkout -b "${FIX_BRANCH_NAME}" + + echo "Committing changes and creating update PR" + git config --global user.name "${COMMIT_USERNAME}" + git config --global user.email "${COMMIT_EMAIL}" + + git commit -am "Update ${OLD_POST} blog date" + git push --set-upstream origin "${FIX_BRANCH_NAME}" + + gh pr create --title "${PR_TITLE}"\ + --body "Date update for ${OLD_POST} from ${SOURCE_LINK}"\ + --repo "${REPO_ID}"