import * as core from '@actions/core' ; import { Octokit , RestEndpointMethodTypes } from '@octokit/rest' ; import moment from 'moment' ; export interface CommitInfo { sha : string ; summary : string ; message : string ; author : string ; date : moment . Moment ; } export class Commits { constructor ( private octokit ) { } async getDiff ( owner , repo , base , head ) { const commits = await this . getDiffRemote ( owner , repo , base , head ) ; return this . sortCommits ( commits ) ; } private async getDiffRemote ( owner , repo , base , head ) { let commits = [ ] ; let compareHead = head ; while ( true ) { const compareResult = await this . octokit . repos . compareCommits ( { owner , repo , base , head : compareHead } ) ; if ( compareResult . data . total_commits === 0 ) { break ; } commits = compareResult . data . commits . concat ( commits ) ; compareHead = `${ commits [ 0 ] . sha }^` ; } core . info ( `ℹ️ Found ${ commits . length } commits from the GitHub API for ${ owner }/${ repo }` ) ; return commits . filter ( commit => commit . sha ) . map ( commit => ( { sha : commit . sha || '' , summary : commit . commit . message . split ( '\n' ) [ 0 ] , message : commit . commit . message , date : moment ( commit . commit . committer ?. date ) , author : commit . commit . author ?. name || '' , prNumber : undefined } ) ) ; } private sortCommits ( commits ) { const commitsResult = [ ] ; const shas = { } ; for ( const commit of commits ) { if ( shas [ commit . sha ] ) { continue ; } shas [ commit . sha ] = true ; commitsResult . push ( commit ) ; } commitsResult . sort ( ( a , b ) => { if ( a . date . isBefore ( b . date ) ) { return - 1 ; } else if ( b . date . isBefore ( a . date ) ) { return 1 ; } return 0 ; } ) ; return commitsResult ; } } /**
 * Filters out all commits which match the exclude pattern
 */ export function filterCommits ( commits , excludeMergeBranches ) { const filteredCommits = [ ] ; for ( const commit of commits ) { if ( excludeMergeBranches ) { let matched = false ; for ( const excludeMergeBranch of excludeMergeBranches ) { if ( commit . summary . includes ( excludeMergeBranch ) ) { matched = true ; break ; } } if ( matched ) { continue ; } } filteredCommits . push ( commit ) ; } return filteredCommits ; }