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.
fix: fix getCommitText; add tests (#12)
- Loading branch information
1 parent
fbb9940
commit 1032080
Showing
9 changed files
with
182 additions
and
109 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
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,27 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getCommitText matches snapshots 1`] = ` | ||
"feat: test test test | ||
KEY-42 | ||
## Примечания | ||
Тест-тест-тест" | ||
`; | ||
|
||
exports[`getCommitText matches snapshots 2`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 3`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 4`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 5`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 6`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 7`] = `"feat: test test test"`; | ||
|
||
exports[`getCommitText matches snapshots 8`] = ` | ||
"feat: test test test | ||
KEY-42" | ||
`; |
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,28 +1,63 @@ | ||
// import {wait} from '../src/wait' | ||
// import * as process from 'process' | ||
// import * as cp from 'child_process' | ||
// import * as path from 'path' | ||
import {getCommitText} from '../src/getCommitText' | ||
import {expect, test} from '@jest/globals' | ||
|
||
test('test', async () => { | ||
expect(true).toBeTruthy() | ||
}) | ||
const title = 'feat: test test test' | ||
const body1 = `Test-test-test | ||
test test | ||
- [] do smth | ||
- [x] test | ||
<!--Коммент--> | ||
*** | ||
KEY-42 | ||
<!--Коммент--> | ||
## Примечания | ||
<!--Коммент--> | ||
Тест-тест-тест` | ||
const body2 = '' | ||
const body3 = ` | ||
- [] test | ||
- [] bla-bla | ||
## Примечания | ||
` | ||
const body4 = ` | ||
bla-bla | ||
KEY-42 | ||
## Примечания | ||
Тест-тест-тест | ||
*** | ||
<!--Коммент--> | ||
<!--Коммент--> | ||
` | ||
const body5 = '\n' | ||
const body6 = '\r' | ||
const body7 = '\r\n' | ||
const body8 = ` | ||
Тест | ||
// test('wait 500 ms', async () => { | ||
// const start = new Date() | ||
// await wait(500) | ||
// const end = new Date() | ||
// var delta = Math.abs(end.getTime() - start.getTime()) | ||
// expect(delta).toBeGreaterThan(450) | ||
// }) | ||
|
||
// // shows how the runner will run a javascript action with env / stdout protocol | ||
// test('test runs', () => { | ||
// process.env['INPUT_MILLISECONDS'] = '500' | ||
// const np = process.execPath | ||
// const ip = path.join(__dirname, '..', 'lib', 'main.js') | ||
// const options: cp.ExecFileSyncOptions = { | ||
// env: process.env | ||
// } | ||
// console.log(cp.execFileSync(np, [ip], options).toString()) | ||
// }) | ||
*** | ||
KEY-42 | ||
<!--Коммент--> | ||
` | ||
|
||
test('getCommitText matches snapshots', async () => { | ||
expect(getCommitText(body1, title)).toMatchSnapshot() | ||
expect(getCommitText(body2, title)).toMatchSnapshot() | ||
expect(getCommitText(body3, title)).toMatchSnapshot() | ||
expect(getCommitText(body4, title)).toMatchSnapshot() | ||
expect(getCommitText(body5, title)).toMatchSnapshot() | ||
expect(getCommitText(body6, title)).toMatchSnapshot() | ||
expect(getCommitText(body7, title)).toMatchSnapshot() | ||
expect(getCommitText(body8, title)).toMatchSnapshot() | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Regex ожидает текст вида: | ||
* | ||
* <Описание PRа для ревью> | ||
* ... | ||
* ... | ||
* ... | ||
* *** | ||
* <Часть, которая попадает в коммит. | ||
* Включает в себя описание коммита, | ||
* примечания и ссылку на задачу в трекере> | ||
*/ | ||
const bodyRegex = /^.*\*{3}(.*)$/s | ||
const commentsPattern = /(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)/g | ||
|
||
export function getCommitText(prBody: string | null, prTitle: string): string { | ||
if (prBody == null) { | ||
return prTitle | ||
} | ||
|
||
const groups = prBody.match(bodyRegex) | ||
|
||
if (groups == null || groups[1] == null) { | ||
return prTitle | ||
} | ||
|
||
const resultBody = groups[1] | ||
// remove markdown comments | ||
.replace(commentsPattern, '') | ||
// remove leading line breaks if present | ||
.replace(/^(\r\n|\r|\n)+/, '') | ||
// remove trailing line breaks if present | ||
.replace(/(\r\n|\r|\n)+$/, '') | ||
|
||
return `${prTitle}${resultBody.length > 0 ? `\n${resultBody}` : ''}` | ||
} |
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 was deleted.
Oops, something went wrong.