Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Github Actions tests #731

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Check Commit Message
run: yarn check-commit

- name: Run tests
run: yarn test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
"start": "yarn && node ./scripts/wrapper.js \"webpack serve --config ./scripts/webpack.config.js --env PLUGIN=\"",
"host": "yarn && node ./scripts/wrapper.js \"webpack serve --config ./scripts/webpack.config.js --mode=production --env PLUGIN=\"",
"build": "yarn && node ./scripts/wrapper.js \"webpack build --config ./scripts/webpack.config.js --mode=production --env PLUGIN=\"",
"test": "eslint ./src && tsc --noEmit && node ./scripts/wrapper.js \"jest \""
"test": "eslint ./src && tsc --noEmit && node ./scripts/wrapper.js \"jest \"",
"check-commit": "node ./scripts/checkCommit.js"
},
"jest": {
"preset": "ts-jest",
Expand Down
80 changes: 80 additions & 0 deletions scripts/checkCommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const { execSync } = require('child_process')
const { existsSync } = require('fs')

function getChangedFiles () {
const output = execSync('git diff --name-only HEAD~1 HEAD').toString().trim()
return output.split('\n')
}

function getCommitMessage () {
return execSync('git log --format=%B -n 1 HEAD').toString().trim()
}

function checkCommitRules () {
const changedFiles = getChangedFiles()
const commitMessage = getCommitMessage()
const pluginPrefixRegex = /\[(.*?)]/g
let match
const commitPlugins = []

while ((match = pluginPrefixRegex.exec(commitMessage)) !== null) {
commitPlugins.push(match[1])
}

console.log('Plugins in commit:', commitPlugins.join(', '))
const checkedPlugins = new Set()

const errors = []
for (const file of changedFiles) {
if (file.startsWith('src/plugins/')) {
const pluginName = file.split('/')[2]
if (!checkedPlugins.has(pluginName)) {
checkedPlugins.add(pluginName)
} else {
continue
}
console.log('Changes in plugin:', pluginName)
if (!commitPlugins.includes(pluginName)) {
errors.push(`Error: detected changes in plugin '${pluginName}', but [${pluginName}] is not mentioned in commit message.`)
}
} else {
if (!checkedPlugins.has('core')) {
checkedPlugins.add('core')
} else {
continue
}
console.log('Changes in core')
if (!commitPlugins.includes('core')) {
errors.push('Error: for changes outside of plugins [core] should be mentioned in commit name.')
}
}
}

for (const commitPlugin of commitPlugins) {
if (commitPlugin === 'core') {
if (!changedFiles.some(file => !file.startsWith('src/plugins/'))) {
errors.push('Error: commit mentions [core], but no changes outside of plugins found.')
}
continue
}

const exists = existsSync(`src/plugins/${commitPlugin}`)
if (!exists) {
errors.push(`Error: commit mentions plugin [${commitPlugin}], but no such plugin found in src/plugins/`)
continue
}

if (!changedFiles.some(file => file.startsWith(`src/plugins/${commitPlugin}`))) {
errors.push(`Error: commit mentions plugin [${commitPlugin}], but no changes in this plugin found.`)
}
}

if (errors.length > 0) {
console.error(errors.join('\n'))
process.exit(1)
}

console.log('Check passed successfully.')
}

checkCommitRules()