import myProbotApp from '../src/bot' ; import { resolve } from 'path' ; import { Probot , createProbot , ProbotOctokit } from 'probot' ; import nock from 'nock' ; import { describe , it , beforeEach } from 'mocha' ; import * as sinon from 'sinon' ; import * as botConfigModule from '@google-automations/bot-config-utils' ; import * as releaseTriggerModule from '../src/release-trigger' ; const sandbox = sinon . createSandbox ( ) ; nock . disableNetConnect ( ) ; const fixturesPath = resolve ( __dirname , '../../test/fixtures' ) ; function buildFakePullRequest ( owner , repo , number ) { return { html_url : `https://github.com/${ owner }/${ repo }/pull/${ number }` , number , state : 'closed' , labels : [ { name : 'autorelease: tagged' } ] , merge_commit_sha : 'abcd1234' , base : { repo : { owner : { login : owner , } , name : repo , } , } , closed_at : '2021-08-15T19:01:12Z' , } ; } describe ( 'bot' , ( ) => { let probot ; let getConfigStub ; beforeEach ( ( ) => { probot = createProbot ( { defaults : { githubToken : 'abc123' , Octokit : ProbotOctokit . defaults ( { retry : { enabled : false } , throttle : { enabled : false } , } ) , } , } ) ; probot . load ( myProbotApp ) ; getConfigStub = sandbox . stub ( botConfigModule , 'getConfig' ) ; sandbox . replace ( releaseTriggerModule , 'ALLOWED_ORGANIZATIONS' , [ 'Codertocat' , ] ) ; } ) ; afterEach ( ( ) => { sandbox . restore ( ) ; } ) ; describe ( 'on release publish' , ( ) => { it ( 'should trigger a kokoro job via releasetool' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/release_published' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const findPullRequestsStub = sandbox . stub ( releaseTriggerModule , 'findPendingReleasePullRequests' ) . resolves ( [ buildFakePullRequest ( 'Codertocat' , 'Hello-World' , 1234 ) , buildFakePullRequest ( 'Codertocat' , 'Hello-World' , 1235 ) , ] ) ; const triggerKokoroJobStub = sandbox . stub ( releaseTriggerModule , 'triggerKokoroJob' ) . resolves ( { stdout : '' , stderr : '' } ) ; const markTriggeredStub = sandbox . stub ( releaseTriggerModule , 'markTriggered' ) . resolves ( ) ; await probot . receive ( { name : 'release.published' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . calledOnce ( findPullRequestsStub ) ; sinon . assert . calledWith ( triggerKokoroJobStub , 'https://github.com/Codertocat/Hello-World/pull/1234' ) ; sinon . assert . calledWith ( triggerKokoroJobStub , 'https://github.com/Codertocat/Hello-World/pull/1235' ) ; sinon . assert . calledWith ( markTriggeredStub , sinon . match . any , sinon . match ( { owner : 'Codertocat' , repo : 'Hello-World' , number : 1234 } ) ) ; sinon . assert . calledWith ( markTriggeredStub , sinon . match . any , sinon . match ( { owner : 'Codertocat' , repo : 'Hello-World' , number : 1235 } ) ) ; } ) ; } ) ; describe ( 'on pull request unlabeled' , ( ) => { it ( 'should trigger a kokoro job if the label removed was autorelease: triggered' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/pull_request_unlabeled' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const triggerKokoroJobStub = sandbox . stub ( releaseTriggerModule , 'triggerKokoroJob' ) . resolves ( { stdout : '' , stderr : '' } ) ; const markTriggeredStub = sandbox . stub ( releaseTriggerModule , 'markTriggered' ) . resolves ( ) ; await probot . receive ( { name : 'pull_request.unlabeled' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . calledWith ( triggerKokoroJobStub , 'https://github.com/Codertocat/Hello-World/pull/2' ) ; sinon . assert . calledWith ( markTriggeredStub , sinon . match . any , sinon . match ( { owner : 'Codertocat' , repo : 'Hello-World' , number : 2 } ) ) ; } ) ; it ( 'should ignore if pull request is not tagged' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/pull_request_unlabeled_untagged' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const triggerKokoroJobStub = sandbox . stub ( releaseTriggerModule , 'triggerKokoroJob' ) . resolves ( { stdout : '' , stderr : '' } ) ; const markTriggeredStub = sandbox . stub ( releaseTriggerModule , 'markTriggered' ) . resolves ( ) ; await probot . receive ( { name : 'pull_request.unlabeled' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . notCalled ( triggerKokoroJobStub ) ; sinon . assert . notCalled ( markTriggeredStub ) ; } ) ; it ( 'should ignore other labels' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/pull_request_unlabeled_other' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const triggerKokoroJobStub = sandbox . stub ( releaseTriggerModule , 'triggerKokoroJob' ) . resolves ( { stdout : '' , stderr : '' } ) ; const markTriggeredStub = sandbox . stub ( releaseTriggerModule , 'markTriggered' ) . resolves ( ) ; await probot . receive ( { name : 'pull_request.unlabeled' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . notCalled ( triggerKokoroJobStub ) ; sinon . assert . notCalled ( markTriggeredStub ) ; } ) ; } ) ; describe ( 'on pull request labeled' , ( ) => { it ( 'should remove labels if published' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/pull_request_labeled' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const cleanupStub = sandbox . stub ( releaseTriggerModule , 'cleanupPublished' ) . resolves ( true ) ; await probot . receive ( { name : 'pull_request.labeled' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . calledOnce ( cleanupStub ) ; } ) ; it ( 'should ignore other labels' , async ( ) => { const payload = require ( resolve ( fixturesPath , './events/pull_request_labeled_other' ) ) ; getConfigStub . resolves ( { enabled : true } ) ; const cleanupStub = sandbox . stub ( releaseTriggerModule , 'cleanupPublished' ) . resolves ( true ) ; await probot . receive ( { name : 'pull_request.labeled' , payload : payload , id : 'abc123' , } ) ; sinon . assert . calledOnce ( getConfigStub ) ; sinon . assert . notCalled ( cleanupStub ) ; } ) ; } ) ; } ) ;