Skip to content

Commit

Permalink
fix: fix getCommitText; add tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
polyakrecords authored Aug 11, 2021
1 parent fbb9940 commit 1032080
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 109 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
"@typescript-eslint/unbound-method": "error",
"filenames/match-regex": "off",
"sort-imports": "off"
},
"env": {
"node": true,
Expand Down
14 changes: 2 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ on:
- ready_for_review
jobs:
build:
runs-on:
- self-hosted
- Linux
- Basic
runs-on: ubuntu-latest
container:
image: node:12
# https://github.com/actions/runner/issues/691
options: --user 1000
steps:
- uses: actions/checkout@v2
- run: |
Expand All @@ -26,12 +21,7 @@ jobs:
test: # make sure the action works on a clean machine without building
container:
image: node:12
# https://github.com/actions/runner/issues/691
options: --user 1000
runs-on:
- self-hosted
- Linux
- Basic
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./
Expand Down
27 changes: 27 additions & 0 deletions __tests__/__snapshots__/main.test.ts.snap
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"
`;
85 changes: 60 additions & 25 deletions __tests__/main.test.ts
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()
})
81 changes: 51 additions & 30 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.

36 changes: 36 additions & 0 deletions src/getCommitText.ts
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}` : ''}`
}
33 changes: 2 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import lint from '@commitlint/lint'
import {getCommitText} from './getCommitText'

const githubToken = process.env.GITHUB_TOKEN
/**
* Regex ожидает текст вида:
*
* <Описание PRа для ревью>
* ...
* ...
* ...
* ***
* <Часть, которая попадает в коммит.
* Включает в себя описание коммита,
* примечания и ссылку на задачу в трекере>
*/
const bodyRegex = /^.*\*{3}(.*)$/s
const commentsPattern = /(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)/g

async function run(): Promise<void> {
if (!githubToken) {
Expand Down Expand Up @@ -58,6 +45,7 @@ async function run(): Promise<void> {
async function validatePrTitle(title: string): Promise<void> {
// TODO: get commitlint config from input
// Currently blocked by @commitlint/load issue on loading configuration
// Similar issue – https://github.com/conventional-changelog/commitlint/issues/613
const result = await lint(title, {
'type-enum': [2, 'always', ['feat', 'fix']]
})
Expand All @@ -69,21 +57,4 @@ async function validatePrTitle(title: string): Promise<void> {
}
}

function getCommitText(prBody: string | null, prTitle: string): string {
if (prBody == null) {
core.debug('prBody is null')
return prTitle
}
core.debug(prBody)

const groups = prBody.match(bodyRegex)

if (groups == null || groups[0] == null) {
core.debug('PR has no description in valid format')
return prTitle
}

return `${prTitle}\n\n${groups[0].replace(commentsPattern, '')}` ?? ''
}

run()
9 changes: 0 additions & 9 deletions src/wait.ts

This file was deleted.

0 comments on commit 1032080

Please sign in to comment.