robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e8ace82093
master
1#!/usr/bin/env -S nvim -l 2 3local function die ( msg ) 4( msg ) 5vim . cmd ( 'cquit 1' ) 6end 7 8--- Executes and returns the output of `cmd`, or nil on failure. 9--- if die_on_fail is true, process dies with die_msg on failure 10--- @param cmd string[] 11--- @param die_on_fail boolean 12--- @param die_msg string 13--- @param stdin string? 14--- 15--- @return string? 16local function _run ( cmd , die_on_fail , die_msg , stdin ) 17local rv =vim . system ( cmd , { stdin =stdin } ): wait () 18if rv . code ~= 0 then 19if rv . stdout : len () > 0 then 20( rv . stdout ) 21end 22if rv . stderr : len () > 0 then 23( rv . stderr ) 24end 25if die_on_fail then 26die ( die_msg ) 27end 28return nil 29end 30return rv . stdout 31end 32 33--- Run a command, return nil on failure 34--- @param cmd string[] 35--- @param stdin string? 36--- 37--- @return string? 38local function run ( cmd , stdin ) 39return _run ( cmd , false , '' , stdin ) 40end 41 42--- Run a command, die on failure with err_msg 43--- @param cmd string[] 44--- @param err_msg string 45--- @param stdin string? 46--- 47--- @return string 48local function run_die ( cmd , err_msg , stdin ) 49return assert ( _run ( cmd , true , err_msg , stdin )) 50end 51 52--- MIME-decode if python3 is available, else returns the input unchanged. 53local function mime_decode ( encoded ) 54local has_python =vim . system ( { 'python3' , '--version' } , { text =true } ): wait () 55if has_python . code ~= 0 then 56return encoded 57end 58 59local pycode =string . format ( 60vim . text . indent ( 610 , 62[[ 63import sys 64from email.header import decode_header 65inp = %q 66parts = [] 67for txt, cs in decode_header(inp): 68if isinstance(txt, bytes): 69try: 70parts.append(txt.decode(cs or "utf-8", errors="replace")) 71except Exception: 72parts.append(txt.decode("utf-8", errors="replace")) 73else: 74parts.append(txt) 75sys.stdout.write("".join(parts)) 76]] 77), 78encoded 79) 80 81local result =vim . system ( { 'python3' , '-c' , pycode } , { text =true } ): wait () 82 83if result . code ~= 0 or not result . stdout then 84return encoded 85end 86 87-- Trim trailing newline Python prints only if program prints it 88return vim . trim ( result . stdout ) 89end 90 91local function get_commit_msg ( close_pr_lines , co_author_lines ) 92return ( 'docs: misc\n\n%s\n\n%s\n' ): format ( 93table . concat ( close_pr_lines , '\n' ), 94table . concat ( co_author_lines , '\n' ) 95) 96end 97 98local function get_fail_msg ( msg , pr_number , close_pr_lines , co_author_lines ) 99return ( '%s %s\n\nPending commit message:\n%s' ): format ( 100msg , 101pr_number or '' , 102get_commit_msg ( close_pr_lines , co_author_lines ) 103) 104end 105 106local function main () 107local pr_list =vim . json . decode ( 108run_die ( 109{ 'gh' , 'pr' , 'list' , '--label' , 'typo' , '--json' , 'number' } , 110'Failed to get list of typo PRs' 111) 112) 113--- @type integer[] 114local pr_numbers =vim 115. iter ( pr_list ) 116: map ( function ( pr ) 117return pr . number 118end ) 119: totable () 120table . sort ( pr_numbers ) 121 122local close_pr_lines ={} 123local co_author_lines ={} 124for _ , pr_number in ipairs ( pr_numbers ) do 125(( 'PR #%s' ): format ( pr_number )) 126local patch_file =run_die ( 127{ 'gh' , 'pr' , 'diff' , tostring ( pr_number ), '--patch' } , 128get_fail_msg ( 'Failed to get patch for PR' , pr_number , close_pr_lines , co_author_lines ) 129) 130-- Using --3way allows skipping changes already included in a previous commit. 131-- If there are conflicts, it will fail and need manual conflict resolution. 132if run ( { 'git' , 'apply' , '--index' , '--3way' , '-' } , patch_file ) then 133table . insert ( close_pr_lines , ( 'Close #%d' ): format ( pr_number )) 134for author in patch_file : gmatch ( '\nFrom: (.- <.->)\n' ) do 135local co_author_line =( 'Co-authored-by: %s' ): format ( mime_decode ( author )) 136if not vim . list_contains ( co_author_lines , co_author_line ) then 137table . insert ( co_author_lines , co_author_line ) 138end 139end 140for author in patch_file : gmatch ( '\nCo%-authored%-by: (.- <.->)\n' ) do 141local co_author_line =( 'Co-authored-by: %s' ): format ( mime_decode ( author )) 142if not vim . list_contains ( co_author_lines , co_author_line ) then 143table . insert ( co_author_lines , co_author_line ) 144end 145end 146else 147( 148get_fail_msg ( 'Failed to apply patch for PR' , pr_number , close_pr_lines , co_author_lines ) 149) 150end 151end 152 153local msg =get_commit_msg ( close_pr_lines , co_author_lines ) 154( 155run_die ( 156{ 'git' , 'commit' , '--file' , '-' } , 157get_fail_msg ( 'Failed to create commit' , nil , close_pr_lines , co_author_lines ), 158msg 159) 160) 161end 162 163main ()