2017-06-29 05:29:41 +00:00
const prWorker = require ( '../../../lib/workers/pr' ) ;
const changelogHelper = require ( '../../../lib/workers/pr/changelog' ) ;
const defaultConfig = require ( '../../../lib/config/defaults' ) . getConfig ( ) ;
2017-02-14 07:08:40 +00:00
2017-06-29 05:29:41 +00:00
const logger = require ( '../../_fixtures/logger' ) ;
2017-06-22 07:03:36 +00:00
2017-06-29 05:29:41 +00:00
jest . mock ( '../../../lib/workers/pr/changelog' ) ;
2017-06-13 09:08:37 +00:00
changelogHelper . getChangeLog = jest . fn ( ) ;
changelogHelper . getChangeLog . mockReturnValue ( 'Mocked changelog' ) ;
changelogHelper . getChangeLogJSON = jest . fn ( ) ;
changelogHelper . getChangeLogJSON . mockReturnValue ( {
project : {
github : 'renovateapp/dummy' ,
repository : 'https://github.com/renovateapp/dummy' ,
} ,
versions : [
{
date : new Date ( '2017-01-01' ) ,
version : '1.1.0' ,
changes : [
{
date : new Date ( '2017-01-01' ) ,
sha : 'abcdefghijklmnopqrstuvwxyz' ,
2017-06-13 10:22:21 +00:00
message : 'foo #3\nbar' ,
2017-06-13 09:08:37 +00:00
} ,
] ,
} ,
] ,
} ) ;
2017-02-14 07:08:40 +00:00
describe ( 'workers/pr' , ( ) => {
2017-06-22 07:03:36 +00:00
describe ( 'checkAutoMerge(pr, config, logger)' , ( ) => {
2017-04-20 11:01:23 +00:00
let config ;
let pr ;
beforeEach ( ( ) => {
config = Object . assign ( { } , defaultConfig ) ;
pr = {
head : {
ref : 'somebranch' ,
} ,
} ;
config . api = {
mergePr : jest . fn ( ) ,
getBranchStatus : jest . fn ( ) ,
} ;
} ) ;
it ( 'should not automerge if not configured' , async ( ) => {
2017-06-22 07:03:36 +00:00
await prWorker . checkAutoMerge ( pr , config , logger ) ;
2017-04-20 11:01:23 +00:00
expect ( config . api . mergePr . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should automerge if enabled and pr is mergeable' , async ( ) => {
config . automergeEnabled = true ;
2017-04-20 11:01:23 +00:00
pr . mergeable = true ;
config . api . getBranchStatus . mockReturnValueOnce ( 'success' ) ;
2017-06-22 07:03:36 +00:00
await prWorker . checkAutoMerge ( pr , config , logger ) ;
2017-04-20 11:01:23 +00:00
expect ( config . api . mergePr . mock . calls . length ) . toBe ( 1 ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should not automerge if enabled and pr is mergeable but branch status is not success' , async ( ) => {
config . automergeEnabled = true ;
2017-04-20 11:01:23 +00:00
pr . mergeable = true ;
config . api . getBranchStatus . mockReturnValueOnce ( 'pending' ) ;
2017-06-22 07:03:36 +00:00
await prWorker . checkAutoMerge ( pr , config , logger ) ;
2017-04-20 11:01:23 +00:00
expect ( config . api . mergePr . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should not automerge if enabled and pr is mergeable but unstable' , async ( ) => {
config . automergeEnabled = true ;
2017-04-20 11:01:23 +00:00
pr . mergeable = true ;
pr . mergeable _state = 'unstable' ;
2017-06-22 07:03:36 +00:00
await prWorker . checkAutoMerge ( pr , config , logger ) ;
2017-04-20 11:01:23 +00:00
expect ( config . api . mergePr . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should not automerge if enabled and pr is unmergeable' , async ( ) => {
config . automergeEnabled = true ;
2017-04-20 11:01:23 +00:00
pr . mergeable = false ;
2017-06-22 07:03:36 +00:00
await prWorker . checkAutoMerge ( pr , config , logger ) ;
2017-04-20 11:01:23 +00:00
expect ( config . api . mergePr . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
} ) ;
2017-06-22 07:03:36 +00:00
describe ( 'ensurePr(upgrades, logger)' , ( ) => {
2017-02-14 07:08:40 +00:00
let config ;
let existingPr ;
beforeEach ( ( ) => {
config = Object . assign ( { } , defaultConfig ) ;
config . api = {
createPr : jest . fn ( ( ) => ( { displayNumber : 'New Pull Request' } ) ) ,
2017-06-08 04:18:21 +00:00
getBranchStatus : jest . fn ( ) ,
2017-02-14 07:08:40 +00:00
} ;
existingPr = {
title : 'Update dependency dummy to version 1.1.0' ,
2017-06-22 13:14:42 +00:00
body : ` <p>This Pull Request updates dependency <a href="https://github.com/renovateapp/dummy">dummy</a> from version <code>1.0.0</code> to <code>1.1.0</code></p>
< h3 id = "commits" > Commits < / h 3 >
< p > < details > < br / >
< summary > renovateapp / dummy < / s u m m a r y > < / p >
< h4 id = "110" > 1.1 . 0 < / h 4 >
< ul >
< li > < a href = "https://github.com/renovateapp/dummy/commit/abcdefghijklmnopqrstuvwxyz" > < code > abcdefg < / c o d e > < / a > f o o < a h r e f = " h t t p s : / / g i t h u b . c o m / r e n o v a t e a p p / d u m m y / i s s u e s / 3 " > # 3 < / a > < / l i >
< / u l >
< p > < /details><br / >
< br / > < / p >
< p > This PR has been generated by < a href = "https://keylocation.sg/our-tech/renovate" > Renovate Bot < / a > . < / p > ` ,
2017-02-14 07:08:40 +00:00
displayNumber : 'Existing PR' ,
} ;
} ) ;
it ( 'should return null if check fails' , async ( ) => {
config . api . getBranchPr = jest . fn ( ( ) => {
throw new Error ( 'oops' ) ;
} ) ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toBe ( null ) ;
} ) ;
2017-04-20 10:11:56 +00:00
it ( 'should return null if waiting for success' , async ( ) => {
config . api . getBranchStatus = jest . fn ( ( ) => 'failed' ) ;
config . prCreation = 'status-success' ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-04-20 10:11:56 +00:00
expect ( pr ) . toBe ( null ) ;
} ) ;
it ( 'should create PR if success' , async ( ) => {
config . api . getBranchStatus = jest . fn ( ( ) => 'success' ) ;
config . api . getBranchPr = jest . fn ( ) ;
config . prCreation = 'status-success' ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-04-20 10:11:56 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
} ) ;
it ( 'should return null if waiting for not pending' , async ( ) => {
config . api . getBranchStatus = jest . fn ( ( ) => 'pending' ) ;
config . prCreation = 'not-pending' ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-04-20 10:11:56 +00:00
expect ( pr ) . toBe ( null ) ;
} ) ;
it ( 'should create PR if no longer pending' , async ( ) => {
config . api . getBranchStatus = jest . fn ( ( ) => 'failed' ) ;
config . api . getBranchPr = jest . fn ( ) ;
config . prCreation = 'not-pending' ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-04-20 10:11:56 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
} ) ;
2017-02-14 07:08:40 +00:00
it ( 'should create new branch if none exists' , async ( ) => {
config . api . getBranchPr = jest . fn ( ) ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
} ) ;
it ( 'should add labels to new PR' , async ( ) => {
config . api . getBranchPr = jest . fn ( ) ;
config . api . addLabels = jest . fn ( ) ;
config . labels = [ 'foo' ] ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
expect ( config . api . addLabels . mock . calls . length ) . toBe ( 1 ) ;
} ) ;
it ( 'should add not labels to new PR if empty' , async ( ) => {
config . api . getBranchPr = jest . fn ( ) ;
config . api . addLabels = jest . fn ( ) ;
config . labels = [ ] ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
expect ( config . api . addLabels . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
it ( 'should add assignees and reviewers to new PR' , async ( ) => {
config . api . getBranchPr = jest . fn ( ) ;
config . api . addAssignees = jest . fn ( ) ;
config . api . addReviewers = jest . fn ( ) ;
config . assignees = [ 'bar' ] ;
config . reviewers = [ 'baz' ] ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
expect ( config . api . addAssignees . mock . calls . length ) . toBe ( 1 ) ;
2017-04-21 05:23:36 +00:00
expect ( config . api . addReviewers . mock . calls . length ) . toBe ( 1 ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should not add assignees and reviewers to new PR if automerging enabled' , async ( ) => {
2017-04-21 05:23:36 +00:00
config . api . getBranchPr = jest . fn ( ) ;
config . api . addAssignees = jest . fn ( ) ;
config . api . addReviewers = jest . fn ( ) ;
config . assignees = [ 'bar' ] ;
config . reviewers = [ 'baz' ] ;
2017-06-08 04:18:21 +00:00
config . automergeEnabled = true ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-04-21 05:23:36 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
expect ( config . api . addAssignees . mock . calls . length ) . toBe ( 0 ) ;
expect ( config . api . addReviewers . mock . calls . length ) . toBe ( 0 ) ;
} ) ;
2017-02-14 07:08:40 +00:00
it ( 'should return unmodified existing PR' , async ( ) => {
config . depName = 'dummy' ;
2017-06-27 12:54:07 +00:00
config . isGitHub = true ;
2017-02-14 07:08:40 +00:00
config . currentVersion = '1.0.0' ;
config . newVersion = '1.1.0' ;
2017-06-27 15:35:23 +00:00
config . repositoryUrl = 'https://github.com/renovateapp/dummy' ;
2017-02-14 07:08:40 +00:00
config . api . getBranchPr = jest . fn ( ( ) => existingPr ) ;
config . api . updatePr = jest . fn ( ) ;
2017-06-29 17:50:26 +00:00
config . semanticPrefix = '' ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-06-16 07:46:57 +00:00
expect ( config . api . updatePr . mock . calls ) . toMatchSnapshot ( ) ;
2017-06-13 09:08:37 +00:00
expect ( config . api . updatePr . mock . calls . length ) . toBe ( 0 ) ;
2017-02-14 07:08:40 +00:00
expect ( pr ) . toMatchObject ( existingPr ) ;
} ) ;
it ( 'should return modified existing PR' , async ( ) => {
config . depName = 'dummy' ;
config . currentVersion = '1.0.0' ;
config . newVersion = '1.2.0' ;
config . api . getBranchPr = jest . fn ( ( ) => existingPr ) ;
config . api . updatePr = jest . fn ( ) ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-02-14 07:08:40 +00:00
const updatedPr = Object . assign ( existingPr , {
2017-06-02 20:40:00 +00:00
body :
2017-06-05 08:21:02 +00:00
'This Pull Request updates dependency dummy from version `1.0.0` to `1.2.0`\n\nNo changelog available' ,
2017-02-14 07:08:40 +00:00
} ) ;
expect ( pr ) . toMatchObject ( updatedPr ) ;
} ) ;
2017-06-08 04:18:21 +00:00
it ( 'should create PR if branch automerging failed' , async ( ) => {
config . automergeEnabled = true ;
config . automergeType = 'branch-push' ;
2017-06-16 13:24:59 +00:00
config . api . getBranchStatus . mockReturnValueOnce ( 'failure' ) ;
2017-06-08 04:18:21 +00:00
config . api . getBranchPr = jest . fn ( ) ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-06-08 04:18:21 +00:00
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
} ) ;
it ( 'should return null if branch automerging not failed' , async ( ) => {
config . automergeEnabled = true ;
config . automergeType = 'branch-push' ;
config . api . getBranchStatus . mockReturnValueOnce ( 'pending' ) ;
2017-06-22 07:03:36 +00:00
const pr = await prWorker . ensurePr ( [ config ] , logger ) ;
2017-06-08 04:18:21 +00:00
expect ( pr ) . toBe ( null ) ;
} ) ;
2017-06-22 19:35:32 +00:00
it ( 'handles duplicate upgrades' , async ( ) => {
config . api . getBranchPr = jest . fn ( ) ;
const pr = await prWorker . ensurePr ( [ config , config ] , logger ) ;
expect ( pr ) . toMatchObject ( { displayNumber : 'New Pull Request' } ) ;
} ) ;
2017-02-14 07:08:40 +00:00
} ) ;
} ) ;