Skip to content

Commit

Permalink
Add git-author option
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecRust committed Apr 16, 2024
1 parent 0fd68a1 commit 147caa6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
41 changes: 15 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

> Automate "Tested up to" version updates in your WordPress projects.
[GitHub Action](https://github.com/features/actions) that fetches the latest
This [GitHub Action](https://github.com/features/actions) fetches the latest
WordPress version then updates the "Tested up to" version of your WordPress
plugin or theme if it's out of date.

Run it based on a cron or trigger it manually. Have the action create a pull
request, or allow it to commit directly to the default branch.

Never forget to update the "Tested up to" version of your WordPress plugin or
theme again!
The action can create a pull request for the change, or commit directly to the
default branch. Never forget to update the "Tested up to" version of your
WordPress plugin or theme again!

## Example

Expand All @@ -25,7 +23,7 @@ Here's a minimal example of running the action based on a cron schedule.
```yaml
on:
schedule:
- cron: '0 0 * * 0' # Every Sunday at midnight
- cron: '0 0 * * 0'
workflow_dispatch:

permissions:
Expand All @@ -49,30 +47,21 @@ jobs:
create and approve pull requests" repository option are required for the action
to create pull requests.

Optionally specify paths to update which will override the default `readme.txt`
file:
## Usage

See [action.yml](action.yml) for detailed information on the action's inputs.

```yaml
- name: Update "Tested up to" version
uses: AlecRust/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: AlecRust/[email protected]
with:
# Paths to update (optional, default: readme.txt)
file-paths: |
readme.txt
my-other-file.php
```

Optionally the action can commit directly to your default branch by setting
`create-pr` to `false`:

```yaml
- name: Update "Tested up to" version
uses: AlecRust/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
create-pr: false
src/other-file.php
# Create a pull request, or commit directly if disabled (optional, default: true)
create-pr: true
# Git author (optional, default: github-actions <[email protected]>)
git-author: 'Joe Bloggs <[email protected]>'
```

Please remember to thoroughly test your plugin/theme with the new version of
Expand Down
4 changes: 4 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('action', () => {
it('completes after updating files and creates PR when create-pr is true', async () => {
mocked(core.getInput).mockReturnValueOnce('true') // create-pr is true
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
mocked(core.getInput).mockReturnValueOnce('Test Name <[email protected]>')
mocked(getLatestWpVersion).mockResolvedValueOnce('5.9')
mocked(updateFiles).mockResolvedValueOnce(true)

Expand All @@ -60,6 +61,7 @@ describe('action', () => {
it('completes after updating files and pushes to main branch when create-pr is false', async () => {
mocked(core.getInput).mockReturnValueOnce('false') // create-pr is false
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
mocked(core.getInput).mockReturnValueOnce('Test Name <[email protected]>')
mocked(getLatestWpVersion).mockResolvedValueOnce('5.9')
mocked(updateFiles).mockResolvedValueOnce(true)

Expand All @@ -81,6 +83,7 @@ describe('action', () => {
it('completes with no updates needed', async () => {
mocked(core.getInput).mockReturnValueOnce('true')
mocked(core.getInput).mockReturnValueOnce('readme.txt\nanother-file.txt')
mocked(core.getInput).mockReturnValueOnce('Test Name <[email protected]>')
mocked(getLatestWpVersion).mockResolvedValueOnce('5.9')
mocked(updateFiles).mockResolvedValueOnce(false)

Expand All @@ -96,6 +99,7 @@ describe('action', () => {
const error = new Error('An unexpected error')
mocked(core.getInput).mockReturnValueOnce('true')
mocked(core.getInput).mockReturnValueOnce('readme.txt')
mocked(core.getInput).mockReturnValueOnce('Test Name <[email protected]>')
mocked(getLatestWpVersion).mockRejectedValueOnce(error)

await run()
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description:
'Flag setting if a pull request should be created for the changes.'
default: 'true'
git-author:
description:
'The name and email of the Git commit author in the format "name <email>".'
default: 'github-actions <[email protected]>'

outputs:
updated:
Expand Down
9 changes: 7 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export async function run(): Promise<void> {
.split(/\r?\n/)
.filter(path => path.trim() !== '')

const authorInput = core.getInput('git-author')
const [authorName, authorEmail] = authorInput.match(/^(.+) <(.+)>$/) || []
if (!authorName || !authorEmail) {
throw new Error(
'Invalid git-author input format. Expected format: "name <email>".'
)
}

const wpVersion = await getLatestWpVersion()
const filesUpdated = await updateFiles(workspace, filePaths, wpVersion)
if (!filesUpdated) {
Expand All @@ -27,8 +35,8 @@ export async function run(): Promise<void> {

console.log(`Updated to WordPress ${wpVersion}, committing changes...`)
const commitMessage = `Update WordPress 'Tested up to' version to ${wpVersion}`
await git.addConfig('user.email', '[email protected]')
await git.addConfig('user.name', 'GitHub Action')
await git.addConfig('user.email', authorEmail)
await git.addConfig('user.name', authorName)
if (createPR) {
const branchName = `tested-up-to-${wpVersion.replace(/\./g, '-')}`
await git.checkoutLocalBranch(branchName)
Expand Down

0 comments on commit 147caa6

Please sign in to comment.