Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
This is the initial implementation of a cli tool that will
compare the coverage of the current PR to that of the main
branch.

Ref: LOG-9611
Semver: minor
  • Loading branch information
evanlucas committed Apr 30, 2021
1 parent 6ecdb9d commit 135cc34
Show file tree
Hide file tree
Showing 18 changed files with 1,935 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# test artifacts
.nyc_output
coverage/
tmp/
output/
lib-cov
*.lcov
.tap

# IDE files
.idea
*.swp
*.vim

# dev files
*.save
*.tgz
.npm
.pnpm/
.eslintcache/
.node_repl_history
node_modules/
npm-debug.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
19 changes: 19 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ts: false
jsx: false
check-coverage: true
output-file: .tap
coverage-report:
- text
- text-summary
- json
- json-summary
- html
branches: 98
lines: 98
functions: 92
statements: 98
nyc-arg:
- --all
- --exclude=tools/
- --exclude=test/
- --exclude=coverage/
155 changes: 155 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
library 'magic-butler-catalogue'

def PROJECT_NAME = "compare-coverage-to-main"
def CURRENT_BRANCH = [env.CHANGE_BRANCH, env.BRANCH_NAME]?.find{branch -> branch != null}
def DEFAULT_BRANCH = 'main'
def TRIGGER_PATTERN = ".*@logdnabot.*"

pipeline {
agent none

options {
timestamps()
ansiColor 'xterm'
}

triggers {
issueCommentTrigger(TRIGGER_PATTERN)
}

environment {
GITHUB_TOKEN = credentials('github-api-token')
NPM_TOKEN = credentials('npm-publish-token')
NPM_CONFIG_CACHE = '.npm'
NPM_CONFIG_USERCONFIG = '.npm/rc'
SPAWN_WRAP_SHIM_ROOT = '.npm'
}

stages {
stage('Validate PR Source') {
when {
expression { env.CHANGE_FORK }
not {
triggeredBy 'issueCommentCause'
}
}
steps {
error("A maintainer needs to approve this PR for CI by commenting")
}
}

stage('Test Suite') {
matrix {
axes {
axis {
name 'NODE_VERSION'
values '12', '14', '16'
}
}

agent {
docker {
image "us.gcr.io/logdna-k8s/node:${NODE_VERSION}-ci"
customWorkspace "${PROJECT_NAME}-${BUILD_NUMBER}"
}
}

stages {
stage('Test') {
environment {
GIT_BRANCH = "${CURRENT_BRANCH}"
BRANCH_NAME = "${CURRENT_BRANCH}"
}

steps {
sh "mkdir -p ${NPM_CONFIG_CACHE}"
script {
npm.install GITHUB_TOKEN
}
sh 'npm run test:ci'
}

post {
always {
junit 'coverage/test.xml'
script {
if (NODE_VERSION == '14') {
stash(
name: slugify(BUILD_TAG)
, allowEmpty: true
, includes: 'coverage/*.json'
)
}
}
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'coverage/lcov-report',
reportFiles: 'index.html',
reportName: "coverage-node-v${NODE_VERSION}"
]
}
}
}
}
}
}

stage('Test Release') {
when {
beforeAgent true
not {
branch DEFAULT_BRANCH
}
}

agent {
docker {
image "us.gcr.io/logdna-k8s/node:14-ci"
customWorkspace "${PROJECT_NAME}-${BUILD_NUMBER}"
}
}

environment {
GIT_BRANCH = "${CURRENT_BRANCH}"
BRANCH_NAME = "${CURRENT_BRANCH}"
}

steps {
script {
sh "mkdir -p ${NPM_CONFIG_CACHE}"
unstash name: slugify(BUILD_TAG)
npm.install GITHUB_TOKEN
sh "npm run release -- --dry-run --no-ci --branches ${CURRENT_BRANCH}"
}
}
}

stage('Release') {
when {
beforeAgent true
branch DEFAULT_BRANCH
not {
changelog '\\[skip ci\\]'
}
}

agent {
docker {
image "us.gcr.io/logdna-k8s/node:14-ci"
customWorkspace "${PROJECT_NAME}-${BUILD_NUMBER}"
}
}

steps {
script {
sh "mkdir -p ${NPM_CONFIG_CACHE}"
unstash name: slugify(BUILD_TAG)
npm.install GITHUB_TOKEN
sh "npm run release"
}
}
}
}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2021 LogDNA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
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.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
## compare-coverage-to-main

Compares the current json coverage file to that of the latest release
> Compares the current json coverage file to that of the latest release and
> provides a comment on pull requests with that information.
### What this tool does

* Fetches the latest release for the repository and pulls down the `coverage-summary.json` (Must be from the json-summary coverage reporter in istanbul)
* Compares it to the contents of the filepath passed in the `--coverage` flag.
* Posts a comment on the associated pull request

### Usage

```
compare-coverage-to-main - Tool to compare coverage for a PR with the main branch
usage: compare-coverage-to-main -f <path to coverage file> -o <org> -r <repo> -p <pull request id>
options:
-h, --help show help and usage
-v, --version show version
-f, --coverage-filepath <path> path to new coverage summary
-d, --dry-run if true, does not post comment to PR
-o, --owner <string> the repository owner
-p, --pr-id <number> the PR id
-r, --repo <repository name> the repository name
```


## Authors

Expand Down
85 changes: 85 additions & 0 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env node

'use strict'

const path = require('path')
const help = require('help')()
const nopt = require('nopt')
const Comparer = require('../index.js')
const known_opts = {
'help': Boolean
, 'version': Boolean
, 'coverage-filepath': path
, 'dry-run': Boolean
, 'owner': String
, 'pr-id': Number
, 'repo': String
}

const short_hand = {
h: ['--help']
, v: ['--version']
, f: ['--coverage-filepath']
, d: ['--dry-run']
, o: ['--owner']
, p: ['--pr-id']
, r: ['--repo']
}

const parsed = nopt(known_opts, short_hand)

if (parsed.help) {
return help()
}

if (parsed.version) {
process.stdout.write(`${require('../package').version}`)
return
}

if (!process.env.GITHUB_TOKEN) {
console.error('Please set the GITHUB_TOKEN env var.')
process.exitCode = 1
return
}

if (!parsed.repo) {
console.error('missing --repo flag. Please pass the repository name')
process.exitCode = 1
return
}

if (!parsed.owner) {
console.error('missing --owner flag. Please pass the github organization or user.')
process.exitCode = 1
return
}

if (!parsed['coverage-filepath']) {
console.error('missing --coverage-filepath flag. '
+ 'Please pass the path to the coverage-summary.json file.'
)
process.exitCode = 1
return
}

const comparer = new Comparer({
coverage_filepath: parsed['coverage-filepath']
, dry_run: parsed['dry-run']
, owner: parsed.owner
, pr_id: parsed['pr-id']
, repo: parsed.repo
, token: process.env.GITHUB_TOKEN
})

/* istanbul ignore next */
function onError(err) {
console.error(err)
process.nextTick(() => {
throw err
})
}

;(async () => {
await comparer.run()
})().catch(onError)
12 changes: 12 additions & 0 deletions bin/usage.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
compare-coverage-to-main - Tool to compare coverage for a PR with the main branch

usage: compare-coverage-to-main -f <path to coverage file> -o <org> -r <repo> -p <pull request id>

options:
-h, --help show help and usage
-v, --version show version
-f, --coverage-filepath <path> path to new coverage summary
-d, --dry-run if true, does not post comment to PR
-o, --owner <string> the repository owner
-p, --pr-id <number> the PR id
-r, --repo <repository name> the repository name
Loading

0 comments on commit 135cc34

Please sign in to comment.