Skip to content

Commit 84ae0da

Browse files
authored
add support for user-defined filenames (#8)
1 parent 83cb73a commit 84ae0da

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ The path to the plugin directory to validate. If not specified, the action will
3232
Only used in self-testing. If passed, this will not actually create a PR against the repository.
3333

3434
#### `gh-token`
35-
The GitHub token to use for creating a PR. If not specified, the action will use the default GitHub token.
35+
The GitHub token to use for creating a PR. If not specified, the action will use the default GitHub token.
36+
37+
#### `filenames`
38+
A comma-separated list of filenames to check for the "Tested Up To" version. If not specified, the action will use `readme.txt` and `README.md`.

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ inputs:
1717
description: The GitHub token to use for creating a pull request.
1818
required: false
1919
default: ${{ github.token }}
20+
filenames:
21+
description: The filenames to check for the tested up to version. Default is 'readme.txt,README.md'.
22+
required: false
23+
default: 'readme.txt,README.md'
2024
runs:
2125
using: composite
2226
steps:
@@ -35,5 +39,6 @@ runs:
3539
DRY_RUN: ${{ inputs.dry-run }}
3640
WORKFLOW_PATH: ${{ github.workspace }}
3741
GH_TOKEN: ${{ inputs.gh-token }}
42+
FILENAMES: ${{ inputs.filenames }}
3843
run: bash ${{ github.action_path }}/bin/validate-plugin-version.sh
3944

bin/validate-plugin-version.sh

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,49 @@ main() {
2424
CURRENT_WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r '.offers[0].current')
2525
echo "Current WordPress Version: ${CURRENT_WP_VERSION}"
2626

27-
# Get "Tested up to" version from readme.txt
28-
if [[ -f "${PLUGIN_PATH}/readme.txt" ]]; then
29-
TESTED_UP_TO=$(grep -i "Tested up to:" "${PLUGIN_PATH}/readme.txt" | tr -d '\r\n' | awk -F ': ' '{ print $2 }')
30-
else
31-
echo "readme.txt not found."
32-
exit 1
33-
fi
27+
# Split FILENAMES into an array
28+
IFS=',' read -ra FILENAMES_ARRAY <<< "$FILENAMES"
29+
30+
local TESTED_UP_TO=""
31+
for filename in "${FILENAMES_ARRAY[@]}"; do
32+
trimmed_filename=$(echo "$filename" | xargs) # Trim whitespace
33+
full_path="${PLUGIN_PATH}/${trimmed_filename}"
34+
35+
# Get "Tested up to" version if file exists
36+
if [[ -f "$full_path" ]]; then
37+
TESTED_UP_TO=$(grep -i "Tested up to:" "$full_path" | tr -d '\r\n' | awk -F ': ' '{ print $2 }')
38+
echo "Found 'Tested up to' version in $trimmed_filename: $TESTED_UP_TO"
39+
break
40+
fi
41+
done
3442

3543
if [[ -z "$TESTED_UP_TO" ]]; then
36-
echo "Tested up to version not found in readme.txt."
44+
echo "'Tested up to' version not found in any of the specified files: ${FILENAMES_ARRAY[*]}"
3745
exit 1
3846
fi
3947

4048
# Compare versions using PHP
4149
if php -r "exit(version_compare('$TESTED_UP_TO', '$CURRENT_WP_VERSION', '>=') ? 0 : 1);"; then
42-
echo "Tested up to version matches or is greater than the current WordPress version. Check passed."
50+
echo "Tested up to version matches or is greater than the current WordPress version. Nothing to do here."
4351
exit
4452
fi
4553
echo "Tested up to version ($TESTED_UP_TO) is less than current WordPress version ($CURRENT_WP_VERSION)."
46-
echo "Updating readme.txt with new Tested up to version."
47-
48-
# Check if the script is running on macOS or Linux, and use the appropriate sed syntax
49-
if [[ "$OSTYPE" == "darwin"* ]]; then
50-
sed -i '' -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "${PLUGIN_PATH}/readme.txt"
51-
else
52-
sed -i -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "${PLUGIN_PATH}/readme.txt"
53-
fi
54+
echo "Updating files with new Tested up to version."
55+
56+
# Update each specified filename if it exists
57+
for filename in "${FILENAMES_ARRAY[@]}"; do
58+
trimmed_filename=$(echo "$filename" | xargs) # Trim whitespace
59+
full_path="${PLUGIN_PATH}/${trimmed_filename}"
5460

55-
# Update README.md if it exists
56-
if [[ -f "${PLUGIN_PATH}/README.md" ]]; then
57-
if [[ "$OSTYPE" == "darwin"* ]]; then
58-
sed -i '' -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "${PLUGIN_PATH}/README.md"
59-
else
60-
sed -i -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "${PLUGIN_PATH}/README.md"
61+
if [[ -f "$full_path" ]]; then
62+
echo "Updating 'Tested up to' version in $full_path"
63+
if [[ "$OSTYPE" == "darwin"* ]]; then
64+
sed -i '' -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "$full_path"
65+
else
66+
sed -i -E "s/(Tested up to: ).*/\1$CURRENT_WP_VERSION/" "$full_path"
67+
fi
6168
fi
62-
echo "README.md updated with new Tested up to version."
63-
fi
69+
done
6470

6571
# Create a pull request with a dynamic branch name
6672
BRANCH_PREFIX="update-tested-up-to-version-"
@@ -76,14 +82,23 @@ main() {
7682
git config user.name "github-actions"
7783
git config user.email "[email protected]"
7884
git checkout -b "$BRANCH_NAME"
79-
git add "${PLUGIN_PATH}"/{readme,README}.* || true
85+
86+
# Add updated files to git
87+
for filename in "${FILENAMES_ARRAY[@]}"; do
88+
trimmed_filename=$(echo "$filename" | xargs) # Trim whitespace
89+
full_path="${PLUGIN_PATH}/${trimmed_filename}"
90+
if [[ -f "$full_path" ]]; then
91+
git add "$full_path"
92+
fi
93+
done
8094

8195
# Bail before committing anything if we're dry-running.
8296
if [[ "${DRY_RUN}" == "true" ]]; then
8397
echo "Dry run enabled. Happy testing."
8498
exit 0
8599
fi
86100

101+
# Check if there are any staged changes
87102
if [[ -z $(git status --porcelain) ]]; then
88103
echo "No changes to commit. Exiting."
89104
exit 1
@@ -93,7 +108,7 @@ main() {
93108
git commit -m "Update Tested Up To version to $CURRENT_WP_VERSION"
94109
git push origin "$BRANCH_NAME"
95110

96-
gh pr create --title "Update Tested Up To version to $CURRENT_WP_VERSION" --body "This pull request updates the \"Tested up to\" version in readme.txt (and README.md if applicable) to match the current WordPress version $CURRENT_WP_VERSION."
111+
gh pr create --title "Update Tested Up To version to $CURRENT_WP_VERSION" --body "This pull request updates the \"Tested up to\" version in specified files (${FILENAMES}) to match the current WordPress version $CURRENT_WP_VERSION."
97112
}
98113

99114
main

0 commit comments

Comments
 (0)