Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
uses: actions/checkout@v3
- name: Shellcheck
uses: ludeeus/action-shellcheck@master
- name: Behavior tests
run: ./tests/check-upgrade.sh
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ jobs:
The action assumes that a `package-lock.json` file exists in the root of the
repository to run `npm ci`.

When one or more open pull requests created by this action already exist, the
action updates the newest pull request's branch, title, and checklist instead
of opening another pull request. It also comments on the pull request with the
previous and new WordPress versions. Closed and merged pull requests are not
reused.

### Inputs

- `plugin-file` - The name of the plugin's main file. Defaults to `plugin.php`.
Expand Down
79 changes: 63 additions & 16 deletions check-upgrade.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
#!/bin/bash

set -o pipefail

# Automatically upgrade a plugin to match the latest WordPress version.

function normalize_version() {
echo "$1" | awk -F. '{printf("%d.%d.%d\n", $1,$2,$3)}'
}

function update_pull_request() {
local comment_marker="<!-- action-update-wordpress-plugin:$WP_NORMALIZED_VERSION -->"
local comments
local update_note="Updated this pull request from WordPress \`$PREVIOUS_VERSION\` to \`$WP_VERSION\` instead of opening a new pull request. $comment_marker"

gh pr edit "$PR_NUMBER" --title "Upgrade plugin to WordPress $WP_VERSION" --body "- [ ] Test plugin against WordPress \`$WP_VERSION\`"
comments=$(gh api --paginate "repos/{owner}/{repo}/issues/$PR_NUMBER/comments" --jq '.[].body')

if ! grep -Fq "$comment_marker" <<< "$comments"; then
gh pr comment "$PR_NUMBER" --body "$update_note"
fi
}

# Ensure PLUGIN_FILE is set.
if [ -z "$PLUGIN_FILE" ]; then
echo "[action-update-wordpress-plugin] PLUGIN_FILE is not set."
Expand All @@ -18,7 +37,7 @@ fi
LATEST_VERSION=$(grep "Tested up to:" "$PLUGIN_FILE" | awk '{print $NF}')

# Ensure latest version is always in x.x.x format (e.g. 5.2.1 vs 5.2).
LATEST_VERSION=$(echo "$LATEST_VERSION" | awk -F. '{printf("%d.%d.%d\n", $1,$2,$3)}')
LATEST_VERSION=$(normalize_version "$LATEST_VERSION")

# Check if the latest version is set and not empty.
if [ -z "$LATEST_VERSION" ]; then
Expand All @@ -28,9 +47,10 @@ fi

# Fetch the latest WordPress version from api.wordpress.org.
WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r '.offers[0].version')
WP_NORMALIZED_VERSION=$(normalize_version "$WP_VERSION")

# Early exist if they're the same version.
if [ "$WP_VERSION" == "$LATEST_VERSION" ]; then
# Early exit if they're the same version.
if [ "$WP_NORMALIZED_VERSION" == "$LATEST_VERSION" ]; then
echo "[action-update-wordpress-plugin] Latest WordPress version and plugin-supported version are the same, no upgrade needed."
exit 0
fi
Expand All @@ -43,26 +63,49 @@ function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
}

if version_gt "$LATEST_VERSION" "$WP_VERSION"; then
if version_gt "$LATEST_VERSION" "$WP_NORMALIZED_VERSION"; then
echo "[action-update-wordpress-plugin] Latest WordPress version is greater than the latest plugin supported version, no upgrade needed."
exit 0
else
echo "[action-update-wordpress-plugin] Latest WordPress version is less than the latest plugin supported version, upgrade needed."
fi

# Check if a pull request already exists with the gh cli.
if [ "$(gh pr list --search "Upgrade plugin to WordPress $WP_VERSION" --state "all" | wc -l)" -gt 0 ]; then
echo "[action-update-wordpress-plugin] Pull request already exists, no upgrade needed."
exit 0
fi

echo "[action-update-wordpress-plugin] Upgrading plugin to $WP_VERSION ..."

set -e

# Checkout a new branch including the current time
BRANCH_NAME="action/upgrade-to-$WP_VERSION-$(date +%s)"
git checkout -b "$BRANCH_NAME"
OPEN_PR=$(gh pr list --app github-actions --state open --limit 1000 --json number,headRefName,title,isCrossRepository | jq -r 'sort_by(.number) | reverse | map(select(.isCrossRepository == false and (.headRefName | startswith("action/upgrade-to-")))) | first | if . then [.number, .headRefName, .title] | @tsv else empty end')

if [ -n "$OPEN_PR" ]; then
IFS=$'\t' read -r PR_NUMBER BRANCH_NAME PR_TITLE <<< "$OPEN_PR"
echo "[action-update-wordpress-plugin] Reusing pull request #$PR_NUMBER: $PR_TITLE"
git fetch origin "$BRANCH_NAME"
git checkout -B "$BRANCH_NAME" "origin/$BRANCH_NAME"

PREVIOUS_VERSION=$(grep "Tested up to:" "$PLUGIN_FILE" | awk '{print $NF}')
PREVIOUS_VERSION=$(normalize_version "$PREVIOUS_VERSION")

if [ "$WP_NORMALIZED_VERSION" == "$PREVIOUS_VERSION" ]; then
BRANCH_VERSION=${BRANCH_NAME#action/upgrade-to-}
BRANCH_VERSION=$(normalize_version "${BRANCH_VERSION%-*}")

if [ "$BRANCH_VERSION" != "$WP_NORMALIZED_VERSION" ]; then
PREVIOUS_VERSION=$(git show HEAD^:"${PLUGIN_FILE#./}" 2>/dev/null | grep "Tested up to:" | awk '{print $NF}')
PREVIOUS_VERSION=$(normalize_version "${PREVIOUS_VERSION:-$BRANCH_VERSION}")
update_pull_request
echo "[action-update-wordpress-plugin] Pull request #$PR_NUMBER metadata updated"
else
echo "[action-update-wordpress-plugin] Pull request #$PR_NUMBER already targets WordPress $WP_VERSION, no update needed."
fi

exit 0
fi
else
PR_NUMBER=""
PREVIOUS_VERSION="$LATEST_VERSION"
BRANCH_NAME="action/upgrade-to-$WP_VERSION-$(date +%s)"
git checkout -b "$BRANCH_NAME"
fi

# npm ci if UPGRADE_DEPENDENCIES is not equal to "false".
if [ "$UPGRADE_DEPENDENCIES" != "false" ]; then
Expand Down Expand Up @@ -90,8 +133,12 @@ git config --global user.name "$GITHUB_ACTOR"
git add -A && git commit -m "Upgrade plugin to $WP_VERSION"
git push origin "$BRANCH_NAME"

# Create a pull request.
gh pr create --title "Upgrade plugin to WordPress $WP_VERSION" --body "- [ ] Test plugin against WordPress \`$WP_VERSION\`" --head "$BRANCH_NAME"
if [ -n "$PR_NUMBER" ]; then
update_pull_request
echo "[action-update-wordpress-plugin] Pull request #$PR_NUMBER updated"
else
gh pr create --title "Upgrade plugin to WordPress $WP_VERSION" --body "- [ ] Test plugin against WordPress \`$WP_VERSION\`" --head "$BRANCH_NAME"
echo "[action-update-wordpress-plugin] Pull request created"
fi

echo "[action-update-wordpress-plugin] Pull request created"
exit 0
Loading
Loading