Skip to content

Commit

Permalink
Merge pull request #12 from marocchino/detect
Browse files Browse the repository at this point in the history
✨ Add detect action
  • Loading branch information
marocchino authored Mar 26, 2023
2 parents 9ac62d8 + 607bfe0 commit 03a4751
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 9 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/detect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: 'detect-test'
on:
pull_request:
types:
- edited
jobs:
detect:
runs-on: ubuntu-latest
outputs:
checked: ${{ steps.detect.outputs.checked }}
steps:
- uses: actions/checkout@v3
- uses: ./
id: detect
with:
action: 'detect'
test:
needs: detect
if: ${{ contains(fromJSON(needs.detect.outputs.checked), 'trigger test') }}
runs-on: ubuntu-latest
steps:
- run: echo TEST TRIGGERED
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @actions/actions-runtime
* @marocchino
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors
Copyright marocchino and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +19,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ I think the most common use is to visualize the results of CI execution, and her
error: 'warn'
```
Here's an example of checking to see if a particular checkbox has been checked recently.
Unchecking works the same way.
> NOTE: I don't think detect will work again on events other than edit.
```yaml
on:
pull_request:
types:
- edited
jobs:
detect:
runs-on: ubuntu-latest
outputs:
checked: ${{ steps.detect.outputs.checked }}
steps:
- uses: actions/checkout@v3
- uses: marocchino/checkbox-action@v1
id: detect
with:
action: 'detect'
test:
needs: detect
if: ${{ contains(fromJSON(needs.detect.outputs.checked), 'trigger test') }}
runs-on: ubuntu-latest
steps:
- run: echo Add your CI here
```
## Inputs
### `list`
Expand All @@ -48,7 +76,7 @@ Regular expression for the checkbox to modify.

### `action`

**Optional** check, uncheck. This default to `'check'`
**Optional** check, uncheck or detect. This default to `'check'`

### `error`

Expand All @@ -60,4 +88,12 @@ Regular expression for the checkbox to modify.

## Outputs

No output
Only available on `detect` action.

### `checked`

Returns a list of checked items from the previous modification as json in []string.

### `unchecked`

Returns a list of checked items from the previous modification as json in []string.
42 changes: 42 additions & 0 deletions __tests__/detect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {expect, describe, test} from '@jest/globals'
import {getDiff} from '../src/detect'

describe('getDiff', () => {
test('should return checked and unchecked items', () => {
const previousBody = `
# Title line
normal line
abnormal line - [ ] item 3
abnormal line - [x] item 4
- [x] item 0 unchanged
- [x] item 1 unchecked
- [ ] item 2 unchanged
- [ ] item 2-1 checked
- [ ] item 2-2 unchanged
- [ ] item 2-3 checked
- [x] item 2-4 unchecked
removed line
`
const currentBody = `
# Title line
normal line
abnormal line - [ ] item 3
abnormal line - [x] item 4
added line
- [x] item 0 unchanged
- [ ] item 1 unchecked
- [ ] item 2 unchanged
- [x] item 2-1 checked
- [ ] item 2-2 unchanged
- [x] item 2-3 checked
- [ ] item 2-4 unchecked
`

const {checked, unchecked} = getDiff(previousBody, currentBody)
expect(checked).toEqual(['item 2-1 checked', 'item 2-3 checked'])
expect(unchecked).toEqual(['item 1 unchecked', 'item 2-4 unchecked'])
})
})
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ inputs:
default: ''
action:
required: false
description: 'check, uncheck. This default to check'
description: 'check, uncheck or detect. This default to check'
default: 'check'
error:
required: false
Expand All @@ -31,6 +31,11 @@ inputs:
description: "The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to update the body. This defaults to {{ github.token }}."
default: "${{ github.token }}"
required: false
outputs:
checked:
description: 'Returns a list of checked items from the previous modification as json in []string.'
unchecked:
description: 'Returns a list of unchecked items from the previous modification as json in []string.'
runs:
using: 'node16'
main: 'dist/index.js'
80 changes: 80 additions & 0 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.

2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'

export type Action = 'check' | 'uncheck'
export type Action = 'check' | 'uncheck' | 'detect'
export type ErrorLevel = 'error' | 'warn' | 'ignore'

export const list = core.getMultilineInput('list', {required: false})
Expand Down
43 changes: 43 additions & 0 deletions src/detect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as github from '@actions/github'

export function getPreviousBody(): string {
if (!github.context.payload.pull_request) {
throw new Error('This action only supports pull_request events')
}
return github.context.payload.changes.body.from
}

export function getCurrentBody(): string {
if (!github.context.payload.pull_request) {
throw new Error('This action only supports pull_request events')
}

return github.context.payload.pull_request.body || ''
}

export function getDiff(
previousBody: string,
currentBody: string
): {checked: string[]; unchecked: string[]} {
const previousLines = previousBody.split('\n')
const currentLines = currentBody.split('\n')

const checkRegexp = /^\s*- \[x\] /
const uncheckRegexp = /^\s*- \[ \] /
const prevChecked: string[] = previousLines
.filter(line => checkRegexp.test(line))
.map(line => line.replace(checkRegexp, ''))
const prevUnchecked: string[] = previousLines
.filter(line => uncheckRegexp.test(line))
.map(line => line.replace(uncheckRegexp, ''))
const currChecked: string[] = currentLines
.filter(line => checkRegexp.test(line))
.map(line => line.replace(checkRegexp, ''))
const currUnchecked: string[] = currentLines
.filter(line => uncheckRegexp.test(line))
.map(line => line.replace(uncheckRegexp, ''))
const checked = currChecked.filter(line => prevUnchecked.includes(line))
const unchecked = currUnchecked.filter(line => prevChecked.includes(line))

return {checked, unchecked}
}
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as core from '@actions/core'
import {error} from './config'
import {action, error} from './config'
import {getBody, contains, update, mutate} from './check'
import {getPreviousBody, getCurrentBody, getDiff} from './detect'

async function run(): Promise<void> {
try {
if (action === 'detect') {
const previousBody = getPreviousBody()
const currentBody = getCurrentBody()
const {checked, unchecked} = getDiff(previousBody, currentBody)
core.setOutput('checked', JSON.stringify(checked))
core.setOutput('unchecked', JSON.stringify(unchecked))
return
}
let body = await getBody()
if (!contains(body)) {
switch (error) {
Expand Down

0 comments on commit 03a4751

Please sign in to comment.