import { PolicyResult } from './policy' ; import { Octokit } from '@octokit/rest' ; import { Endpoints } from '@octokit/types' ; export type ListIssuesResult = Endpoints [ 'GET /repos/{owner}/{repo}/issues' ] [ 'response' ] [ 'data' ] [ number ] ; export type UpdateIssueParam = Endpoints [ 'POST /repos/{owner}/{repo}/issues' ] [ 'parameters' ] ; export async function openIssue ( octokit , result ) { const message = `
[Policy Bot](https://github.com/googleapis/repo-automation-bots/tree/main/packages/policy#policy-bot) found one or more issues with this repository.
- [${ result . hasMainDefault ? 'x' : ' ' }] Default branch is 'main'
- [${ result . hasBranchProtection ? 'x' : ' ' }] Branch protection is enabled
- [${ result . hasRenovateConfig ? 'x' : ' ' }] Renovate bot is enabled
- [${ result . hasMergeCommitsDisabled ? 'x' : ' ' }] Merge commits disabled
- [${ result . hasCodeowners ? 'x' : ' ' }] There is a CODEOWNERS file
- [${ result . hasValidLicense ? 'x' : ' ' }] There is a valid LICENSE.md
- [${ result . hasCodeOfConduct ? 'x' : ' ' }] There is a CODE_OF_CONDUCT.md
- [${ result . hasContributing ? 'x' : ' ' }] There is a CONTRIBUTING.md
- [${ result . hasSecurityPolicy ? 'x' : ' ' }] There is a SECURITY.md
` ; const issueIterator = octokit . paginate . iterator ( octokit . issues . listForRepo , { owner : result . org , repo : result . repo , state : 'open' , } ) ; let existingIssue = undefined ; for await ( const page of issueIterator ) { for ( const issue of page . data ) { if ( issue . title . includes ( '[Policy Bot] ' ) ) { existingIssue = issue ; break ; } } if ( existingIssue ) break ; } const isValid = result . hasCodeowners && result . hasContributing && result . hasMergeCommitsDisabled && result . hasRenovateConfig && result . hasSecurityPolicy && result . hasValidLicense && result . hasCodeOfConduct && result . hasBranchProtection && result . hasMainDefault ; if ( existingIssue ) { const labels = existingIssue . labels . map ( x => typeof x === 'string' ? x : x . name ! ) ; if ( ! labels . includes ( 'policybot' ) ) { labels . push ( 'policybot' ) ; } await octokit . issues . update ( { issue_number : existingIssue . number , owner : result . org , repo : result . repo , body : message , state : isValid ? 'closed' : 'open' , labels , } ) ; } else { if ( ! isValid ) { await octokit . issues . create ( { title : '[Policy Bot] found one or more issues with this repository.' , owner : result . org , repo : result . repo , body : message , labels : [ 'policybot' , 'type: process' ] , } ) ; } } }