import nock from 'nock' ; import { describe , it } from 'mocha' ; import * as sinon from 'sinon' ; import assert from 'assert' ; import * as gh from '../src/issue' ; import { Octokit } from '@octokit/rest' ; nock . disableNetConnect ( ) ; const octokit = new Octokit ( ) ; const githubHost = 'https://api.github.com' ; describe ( 'issue' , ( ) => { const repo = 'nodejs-storage' ; const org = 'googleapis' ; const policyResult = { repo , org , topics : [ ] , language : 'dart' , hasRenovateConfig : true , hasValidLicense : true , hasCodeOfConduct : true , hasContributing : true , hasCodeowners : true , hasBranchProtection : true , hasMergeCommitsDisabled : true , hasSecurityPolicy : true , hasMainDefault : true , timestamp : new Date ( ) , } ; afterEach ( ( ) => { sinon . restore ( ) ; nock . cleanAll ( ) ; } ) ; it ( 'should create a new issue if the results are invalid' , async ( ) => { const invalidResult = Object . assign ( { } , policyResult ) ; invalidResult . hasContributing = false ; const scope = nock ( githubHost ) . get ( `/repos/${ org }/${ repo }/issues?state=open` ) . reply ( 200 , [ ] ) . post ( `/repos/${ org }/${ repo }/issues` ) . reply ( 200 ) ; await gh . openIssue ( octokit , invalidResult ) ; scope . done ( ) ; } ) ; it ( 'should not create a new issue if results are valid' , async ( ) => { const scope = nock ( githubHost ) . get ( `/repos/${ org }/${ repo }/issues?state=open` ) . reply ( 200 , [ ] ) ; await gh . openIssue ( octokit , policyResult ) ; scope . done ( ) ; } ) ; it ( 'should close an open issue if suddenly valid' , async ( ) => { const issueNumber = 42 ; const scope = nock ( githubHost ) . get ( `/repos/${ org }/${ repo }/issues?state=open` ) . reply ( 200 , [ { title : '[Policy Bot] hello' , number : issueNumber , labels : [ ] , } , ] ) . patch ( `/repos/${ org }/${ repo }/issues/${ issueNumber }` ) . reply ( 200 ) ; await gh . openIssue ( octokit , policyResult ) ; scope . done ( ) ; } ) ; it ( 'should update the body if validity changes' , async ( ) => { const invalidResult = Object . assign ( { } , policyResult ) ; invalidResult . hasContributing = false ; const scope = nock ( githubHost ) . get ( `/repos/${ org }/${ repo }/issues?state=open` ) . reply ( 200 , [ ] ) . post ( `/repos/${ org }/${ repo }/issues` ) . reply ( 200 ) ; await gh . openIssue ( octokit , invalidResult ) ; scope . done ( ) ; } ) ; it ( 'it should preserve original labels on an issue' , async ( ) => { const issueNumber = 42 ; const invalidResult = Object . assign ( { } , policyResult ) ; invalidResult . hasContributing = false ; const scope = nock ( githubHost ) . get ( `/repos/${ org }/${ repo }/issues?state=open` ) . reply ( 200 , [ { title : '[Policy Bot] hello' , number : issueNumber , labels : [ 'boop' ] , } , ] ) . patch ( `/repos/${ org }/${ repo }/issues/${ issueNumber }` , body => { assert . deepStrictEqual ( body . labels , [ 'boop' , 'policybot' ] ) ; return true ; } ) . reply ( 200 ) ; await gh . openIssue ( octokit , invalidResult ) ; scope . done ( ) ; } ) ; } ) ;