// @bun
import{Zb as z}from"./chunk-bqh4yh6g.js";import D from"process";function S(I){let q=I.replace(/\\/g,"/");return q.includes(".github/workflows/")&&(q.endsWith(".yml")||q.endsWith(".yaml"))}async function v(I,q){try{if(!S(I))return null;let E=[],j=q.split(`
`);for(let L=0;L<j.length;L++){let W=j[L].trim().match(/^-?\s*uses:\s*(.+)/);if(W){let K=W[1].trim().replace(/['"`]/g,"");if(K.startsWith("./")||K.startsWith("docker://"))continue;let Q=K.split("@");if(Q.length===2){let[J,X]=Q;if(!J||!J.trim())continue;let Z=X.split("#")[0].trim(),Y={name:J.trim(),currentVersion:Z,type:"github-actions",file:I};if(!E.find((_)=>_.name===Y.name&&_.currentVersion===Y.currentVersion))E.push(Y)}}}return{path:I,type:"github-actions",content:q,dependencies:E}}catch(E){return console.warn(`Failed to parse GitHub Actions file ${I}:`,E),null}}async function T(I,q,E){try{if(!S(I))return q;let j=q;for(let L of E){let O=L.name.replace(/\s*\(.*\)$/,""),V=new RegExp(`((?:^\\s*-\\s*)?uses:\\s*["']?)(${O.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")})@(${L.currentVersion.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")})(["']?)`,"gm");j=j.replace(V,`$1$2@${L.newVersion}$4`)}return j}catch(j){return console.warn(`Failed to update GitHub Actions file ${I}:`,j),q}}async function y(I){let q=[],E=new Map;for(let j of I)if(S(j.file)){if(!E.has(j.file))E.set(j.file,[]);E.get(j.file).push(j)}for(let[j,L]of E)try{let O=await import("fs");if(O.existsSync(j)){let V=O.readFileSync(j,"utf-8"),W=await T(j,V,L);q.push({path:j,content:W,type:"update"})}}catch(O){console.warn(`Failed to generate updates for GitHub Actions file ${j}:`,O)}return q}async function B(I){try{if(!I.includes("/"))return console.log(`\u26A0\uFE0F Invalid action name format: ${I}`),null;let[q,E]=I.split("/");console.log(`\uD83D\uDD0D Fetching latest version for ${q}/${E}`);let j={Accept:"application/vnd.github.v3+json","User-Agent":"buddy-bot"},L=D.env.GITHUB_TOKEN;if(L)j.Authorization=`Bearer ${L}`;console.log(`\uD83D\uDCE1 Trying latest release for ${q}/${E}`);let O=await fetch(`https://api.github.com/repos/${q}/${E}/releases/latest`,{headers:j});if(O.ok){let K=await O.json();if(K.tag_name)return console.log(`\u2705 Found latest release: ${K.tag_name}`),K.tag_name}else console.log(`\u26A0\uFE0F Latest release not found: ${O.status} ${O.statusText}`);console.log(`\uD83D\uDCE1 Trying all releases for ${q}/${E}`);let V=await fetch(`https://api.github.com/repos/${q}/${E}/releases?per_page=10`,{headers:j});if(V.ok){let K=await V.json();if(!Array.isArray(K))console.log("\u26A0\uFE0F Unexpected releases response format (expected array)");else{console.log(`\uD83D\uDCCB Found ${K.length} releases`);let Q=K.filter((J)=>J.tag_name&&!J.tag_name.includes("-")).sort((J,X)=>{let Z=J.published_at?new Date(J.published_at).getTime():0;return(X.published_at?new Date(X.published_at).getTime():0)-Z});if(Q.length>0)return console.log(`\u2705 Found latest stable release: ${Q[0].tag_name}`),Q[0].tag_name||null}}else console.log(`\u26A0\uFE0F Releases not found: ${V.status} ${V.statusText}`);console.log(`\uD83D\uDCE1 Trying tags for ${q}/${E}`);let W=await fetch(`https://api.github.com/repos/${q}/${E}/tags?per_page=10`,{headers:j});if(W.ok){let K=await W.json();if(!Array.isArray(K))console.log("\u26A0\uFE0F Unexpected tags response format (expected array)");else{console.log(`\uD83D\uDCCB Found ${K.length} tags`);let Q=K.filter((J)=>J.name&&!J.name.includes("-")).map((J)=>J.name).sort((J,X)=>{let Z=J.replace(/^v/,""),Y=X.replace(/^v/,""),x=Z.split(".").map(Number),_=Y.split(".").map(Number);for(let $=0;$<Math.max(x.length,_.length);$++){let k=x[$]||0,M=_[$]||0;if(k!==M)return M-k}return 0});if(Q.length>0)return console.log(`\u2705 Found latest stable tag: ${Q[0]}`),Q[0]}}else console.log(`\u26A0\uFE0F Tags not found: ${W.status} ${W.statusText}`);return console.log(`\u274C No version found for ${I}`),null}catch(q){return console.warn(`Failed to fetch latest version for ${I}:`,q),null}}
export{S as k,v as l,T as m,y as n,B as o};

//# debugId=6D4F1BA0676F5D2F64756E2164756E21
