import { describe , it , beforeEach , afterEach } from 'mocha' ; import nock from 'nock' ; import * as sinon from 'sinon' ; import { SyncRepoSettings } from '../src/sync-repo-settings' ; import { Octokit } from '@octokit/rest' ; import { logger } from 'gcf-utils' ; import { RepoConfig } from '../src/types' ; import { readFileSync } from 'fs' ; import { resolve } from 'path' ; import * as yaml from 'js-yaml' ; import assert from 'assert' ; nock . disableNetConnect ( ) ; const sandbox = sinon . createSandbox ( ) ; function loadConfig ( fixture ) { const content = readFileSync ( resolve ( './test/fixtures' , fixture ) , 'utf8' ) ; return yaml . load ( content ) as RepoConfig ; } describe ( 'SyncRepoSettings' , ( ) => { let runner ; let octokit ; beforeEach ( ( ) => { octokit = new Octokit ( { auth : 'faketoken' , } ) ; sandbox . stub ( logger , 'error' ) . throwsArg ( 0 ) ; sandbox . stub ( logger , 'info' ) ; sandbox . stub ( logger , 'debug' ) ; runner = new SyncRepoSettings ( octokit , logger ) ; } ) ; afterEach ( ( ) => { sandbox . restore ( ) ; nock . cleanAll ( ) ; } ) ; it ( 'should fill in default values' , async ( ) => { const updateRepo = sandbox . stub ( octokit . repos , 'update' ) ; const updateBranch = sandbox . stub ( octokit . repos , 'updateBranchProtection' ) ; const updateTeams = sandbox . stub ( octokit . teams , 'addOrUpdateRepoPermissionsInOrg' ) ; const config = loadConfig ( 'localConfig.yaml' ) ; await runner . syncRepoSettings ( { repo : 'test-owner/test-repo' , config : config , defaultBranch : 'custom-default-branch' , } ) ; sinon . assert . calledOnce ( updateRepo ) ; sinon . assert . calledOnce ( updateBranch ) ; sinon . assert . calledThrice ( updateTeams ) ; const branchProtection = updateBranch . args [ 0 ] [ 0 ] ; assert . strictEqual ( branchProtection ?. branch , 'custom-default-branch' ) ; assert . ok ( branchProtection ?. enforce_admins ) ; } ) ; it ( 'should fill in default values from the repo' , async ( ) => { const updateRepo = sandbox . stub ( octokit . repos , 'update' ) ; const updateBranch = sandbox . stub ( octokit . repos , 'updateBranchProtection' ) ; const updateTeams = sandbox . stub ( octokit . teams , 'addOrUpdateRepoPermissionsInOrg' ) ; const scope = nock ( 'https://api.github.com' ) . get ( '/repos/test-owner/test-repo' ) . reply ( 200 , { default_branch : 'custom-default-branch' } ) ; const config = loadConfig ( 'localConfig.yaml' ) ; await runner . syncRepoSettings ( { repo : 'test-owner/test-repo' , config : config , } ) ; sinon . assert . calledOnce ( updateRepo ) ; sinon . assert . calledOnce ( updateBranch ) ; sinon . assert . calledThrice ( updateTeams ) ; const branchProtection = updateBranch . args [ 0 ] [ 0 ] ; assert . strictEqual ( branchProtection ?. branch , 'custom-default-branch' ) ; assert . ok ( branchProtection ?. enforce_admins ) ; scope . done ( ) ; } ) ; it ( 'should handle multiple branches' , async ( ) => { const updateRepo = sandbox . stub ( octokit . repos , 'update' ) ; const updateBranch = sandbox . stub ( octokit . repos , 'updateBranchProtection' ) ; const updateTeams = sandbox . stub ( octokit . teams , 'addOrUpdateRepoPermissionsInOrg' ) ; nock ( 'https://api.github.com' ) . get ( '/repos/test-owner/test-repo' ) . reply ( 200 , { default_branch : 'custom-default-branch' } ) ; const config = loadConfig ( 'multipleBranches.yaml' ) ; await runner . syncRepoSettings ( { repo : 'test-owner/test-repo' , config : config , } ) ; sinon . assert . calledOnce ( updateRepo ) ; sinon . assert . calledTwice ( updateBranch ) ; sinon . assert . calledThrice ( updateTeams ) ; } ) ; } ) ;