okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f57c0adcb
main
1'use strict' ; 2// GitHub-side handlers for intake.yml, reopen.yml and the 5-day sweep. 3// They take the actions/github-script objects ({ github, context, core }) and never touch PR code: 4// everything comes from the event payload and the REST API. 5 6const check = require ( './item-check' ); 7 8const LABEL = 'needs-format' ; 9const STALE_DAYS = 5 ; 10const MAINTAINER = new Set ([ 'OWNER' , 'MEMBER' , 'COLLABORATOR' ]); 11// Items a maintainer closed on purpose stay closed. 12const BLOCK_LABELS = new Set ([ 'wontfix' , 'invalid' , 'duplicate' , 'declined' ]); 13 14function labelNames ( item ) { 15return ( item . labels || []). map (( l ) => ( typeof l === 'string' ?l :l . name )); 16} 17 18async function ensureLabel ( github , context ) { 19const { owner, repo} = context . repo ; 20try { 21await github . rest . issues . getLabel ({ owner, repo, name :LABEL }); 22} catch ( e ) { 23if ( e . status !== 404 ) throw e ; 24await github . rest . issues . createLabel ({ 25 owner, repo, name :LABEL , color :'fbca04' , 26description :'Title or description does not follow CONTRIBUTING.md yet' , 27}); 28} 29} 30 31async function removeLabel ( github , context , number ) { 32const { owner, repo} = context . repo ; 33try { 34await github . rest . issues . removeLabel ({ owner, repo, issue_number :number , name :LABEL }); 35} catch ( e ) { 36if ( e . status !== 404 ) throw e ; 37} 38} 39 40function itemFromPayload ( context ) { 41const p = context . payload ; 42return p . pull_request || p . issue ; 43} 44 45function isPullItem ( item , context ) { 46return Boolean ( context . payload . pull_request || item . pull_request ); 47} 48 49// issues: opened/edited/reopened, pull_request_target: opened/edited/reopened 50async function intake ({ github, context, core}) { 51const { owner, repo} = context . repo ; 52const item = itemFromPayload ( context ); 53if ( ! item || ( item . user && item . user . type === 'Bot' )) return ; 54if ( MAINTAINER . has ( item . author_association ) && context . payload . action === 'opened' ) { 55// Maintainer items are not nagged on creation. 56return ; 57} 58const isPull = isPullItem ( item , context ); 59const labels = labelNames ( item ); 60const hasLabel = labels . includes ( LABEL ); 61const res = check . checkItem ({ isPull, title :item . title , body :item . body , labels, mode :'intake' }); 62core . info ( 'check ' + ( isPull ?'PR' :'issue' ) + ' #' + item . number + ': ' + ( res . ok ?'ok' :res . missing . join ( ' | ' ))); 63 64if ( res . ok ) { 65if ( hasLabel ) { 66await removeLabel ( github , context , item . number ); 67await github . rest . issues . createComment ({ owner, repo, issue_number :item . number , body :'Thanks, this follows the format now.' }); 68} 69return ; 70} 71if ( context . payload . action === 'edited' && ! hasLabel ) return ; // edits only matter for items already flagged 72if ( hasLabel ) return ; // one note only 73await ensureLabel ( github , context ); 74await github . rest . issues . addLabels ({ owner, repo, issue_number :item . number , labels :[ LABEL ] }); 75await github . rest . issues . createComment ({ owner, repo, issue_number :item . number , body :check . intakeComment ( isPull , res . missing ) }); 76} 77 78// issue_comment created, body starts with /reopen 79async function reopen ({ github, context, core}) { 80const { owner, repo} = context . repo ; 81const comment = context . payload . comment ; 82const issue = context . payload . issue ; 83if ( ! /^\s * \/reopen\b / . test ( comment . body || '' )) return ; 84if ( comment . user . type === 'Bot' ) return ; 85const isAuthor = comment . user . login === issue . user . login ; 86const isMaint = MAINTAINER . has ( comment . author_association ); 87if ( ! isAuthor && ! isMaint ) { 88core . info ( 'ignored /reopen from ' + comment . user . login + ' (not the author or a maintainer)' ); 89return ; 90} 91const isPull = Boolean ( issue . pull_request ); 92const say = ( body ) => github . rest . issues . createComment ({ owner, repo, issue_number :issue . number , body}); 93const labels = labelNames ( issue ); 94const wasOpen = issue . state === 'open' ; 95 96if ( labels . some (( l ) => BLOCK_LABELS . has ( l . toLowerCase ())) && ! isMaint ) { 97await say ( 'This one was closed on purpose (' + labels . filter (( l ) => BLOCK_LABELS . has ( l . toLowerCase ())). join ( ', ' ) + '). Please add your new evidence in a comment and a maintainer will decide.' ); 98return ; 99} 100if ( wasOpen && ! labels . includes ( LABEL )) { 101core . info ( 'already open, nothing to do' ); 102return ; 103} 104 105// The bot reads the live item, not the possibly stale event copy. 106const live = ( await github . rest . issues . get ({ owner, repo, issue_number :issue . number })). data ; 107const res = check . checkItem ({ isPull, title :live . title , body :live . body , labels :labelNames ( live ), mode :'reopen' }); 108core . info ( 'reopen check #' + live . number + ': ' + ( res . ok ?'ok' :res . missing . join ( ' | ' ))); 109 110if ( ! res . ok ) { 111await say ( check . reopenMissingComment ( isPull , res . missing )); 112return ; 113} 114if ( ! wasOpen ) { 115try { 116await github . rest . issues . update ({ owner, repo, issue_number :live . number , state :'open' }); 117} catch ( e ) { 118await say ( 'The check passed, but GitHub would not reopen it (' + ( e . message || e . status ) + '). ' + ( isPull ?'If the branch was deleted, restore it or open a new pull request and link this one.' :'Please open a new issue and link this one.' )); 119return ; 120} 121} 122await removeLabel ( github , context , live . number ); 123let extra = '' ; 124if ( isPull ) { 125try { 126const pr = ( await github . rest . pulls . get ({ owner, repo, pull_number :live . number })). data ; 127if ( pr . mergeable_state === 'dirty' ) extra = '\n\nIt has conflicts with main now: please rebase it.' ; 128} catch ( e ) { /* best effort */ } 129} 130await say ( check . reopenOkComment ( isPull ) + extra ); 131} 132 133async function labelTime ( github , owner , repo , number , fallback ) { 134const events = await github . paginate ( github . rest . issues . listEvents , { owner, repo, issue_number :number , per_page :100 }); 135let t = null ; 136for ( const e of events ) { 137if ( e . event === 'labeled' && e . label && e . label . name === LABEL ) t = e . created_at ; 138} 139return t || fallback ; 140} 141 142// scheduled: close items that kept the needs-format label for more than 5 days 143async function sweep ({ github, context, core}) { 144const { owner, repo} = context . repo ; 145const cutoff = Date . now () - STALE_DAYS * 86400 * 1000 ; 146const items = await github . paginate ( github . rest . issues . listForRepo , { owner, repo, state :'open' , labels :LABEL , per_page :100 }); 147core . info ( items . length + ' open items with ' + LABEL ); 148for ( const item of items ) { 149const since = new Date ( await labelTime ( github , owner , repo , item . number , item . created_at )). getTime (); 150if ( since > cutoff ) continue ; 151const isPull = Boolean ( item . pull_request ); 152// Re-check first: an edit that fixed it should have cleared the label already; do not close good items. 153const res = check . checkItem ({ isPull, title :item . title , body :item . body , labels :labelNames ( item ), mode :'intake' }); 154if ( res . ok ) { 155await removeLabel ( github , context , item . number ); 156continue ; 157} 158await github . rest . issues . createComment ({ owner, repo, issue_number :item . number , body :check . staleComment ( isPull ) }); 159await github . rest . issues . update ({ owner, repo, issue_number :item . number , state :'closed' , state_reason :'not_planned' }); 160core . info ( 'closed #' + item . number ); 161} 162} 163 164module . exports = { intake, reopen, sweep, LABEL , STALE_DAYS };