generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from marocchino/detect
✨ Add detect action
- Loading branch information
Showing
11 changed files
with
246 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
* @actions/actions-runtime | ||
* @marocchino |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters