Skip to content

Commit

Permalink
add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Apr 12, 2024
1 parent 825917f commit fd26572
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fragment PR on PullRequest {
}
maintainerCanModify
}
`,Kb=[],zb=[];switch(process.env.GITHUB_EVENT_NAME){case"push":case"workflow_dispatch":await BW();break;case"pull_request":case"pull_request_target":await hW();break;default:throw new Error(`Event ${process.env.GITHUB_EVENT_NAME} is not supported. Only push, workflow_dispatch, pull_request, and pull_request_target are supported.`)}LA.default.setOutput("updated-prs",Kb.join(","));LA.default.setOutput("conflicted-prs",zb.join(","));async function BW(){li.isString.assert(process.env.GITHUB_EVENT_PATH);let{ref:e,repository:A}=JSON.parse(await Wb.readFile(process.env.GITHUB_EVENT_PATH)),t="refs/heads/";if(!e.startsWith(t))return;let r=e.slice(t.length),s={repo:A.name,owner:A.owner.name??A.owner.login},o=`repo:${s.owner}/${s.repo} is:open is:pr base:${r}`;for await(let n of yW({search:o}))console.info(`Handling PRs ${n.map(i=>i.number).join(", ")}`),await Promise.all(n.map(async i=>Iu(s,i)))}async function hW(){li.isString.assert(process.env.GITHUB_EVENT_PATH);let{number:e,repository:A}=JSON.parse(await Wb.readFile(process.env.GITHUB_EVENT_PATH)),t={repo:A.name,owner:A.owner.name??A.owner.login},r=await $b({...t,number:e});await Iu(t,r)}async function Iu(e,A){switch(A.mergeable){case"CONFLICTING":await pW(e,A);break;case"MERGEABLE":{let t=[fW(e,A),IW(e,A)];await Promise.all(t);break}case"UNKNOWN":{console.info(`[PR ${A.number}] Conflict state is not yet known. Retrying.`),await iW(5e3);let t=await $b({...e,number:A.number});await Iu(e,t);break}}}function Ei(e,A){return e.labels.nodes.some(t=>A.includes(t.name))}async function IW(e,A){if(!EW)return;if(uW&&!A.autoMergeRequest){console.info(`[PR ${A.number}] Auto-merge is not enabled, skipping update.`);return}if(Qu.length>0&&!Ei(A,Qu)){console.info(`[PR ${A.number}] Does not have any of the required labels (${Qu}), skipping update.`);return}if(Cu.length>0&&Ei(A,Cu)){console.info(`[PR ${A.number}] Has one of the excluded (${Cu}), skipping update.`);return}if(QW.includes(eD(A.author))){console.info(`[PR ${A.number}] Was created by an excluded author, skipping update.`);return}if(!AD(A,lW)){console.info(`[PR ${A.number}] Not in the expected ready state, skipping update.`);return}if(!wW(A,CW)){console.info(`[PR ${A.number}] Not from the expected source, skipping update.`);return}if(ui(A)&&!A.maintainerCanModify){console.info(`[PR ${A.number}] Fork PR refuses updates from maintainers, skipping update.`);return}if(!await dW(e,A)||(Kb.push(A.number),console.info(`[PR ${A.number}] \u2705 Updating branch.`),gi))return;if(!ui(A))return aW.rest.pulls.updateBranch({...e,pull_number:A.number});let r=`pr-${A.number}`,s=jb.join(process.cwd(),r);console.log("cloning repo in",s),ci.execFileSync("gh",["repo","clone",A.baseRepository.nameWithOwner,r,"--","--branch",A.baseRef.name],{stdio:"inherit",env:{...process.env,GH_TOKEN:process.env.GITHUB_TOKEN}}),console.log("checking out PR branch"),ci.execFileSync("gh",["pr","checkout",String(A.number)],{cwd:s,stdio:"inherit",env:{...process.env,GH_TOKEN:process.env.GITHUB_TOKEN}}),console.log("executing git merge"),ci.execFileSync("git",["merge",A.baseRef.name,"--no-edit"],{cwd:s,stdio:"inherit"}),console.log("executing git push"),ci.execFileSync("git",["push"],{cwd:s,stdio:"inherit"})}async function dW(e,A){return(await Ot.graphql(`
`,Kb=[],zb=[];switch(process.env.GITHUB_EVENT_NAME){case"push":case"workflow_dispatch":await BW();break;case"pull_request":case"pull_request_target":await hW();break;default:throw new Error(`Event ${process.env.GITHUB_EVENT_NAME} is not supported. Only push, workflow_dispatch, pull_request, and pull_request_target are supported.`)}LA.default.setOutput("updated-prs",Kb.join(","));LA.default.setOutput("conflicted-prs",zb.join(","));async function BW(){li.isString.assert(process.env.GITHUB_EVENT_PATH);let{ref:e,repository:A}=JSON.parse(await Wb.readFile(process.env.GITHUB_EVENT_PATH)),t="refs/heads/";if(!e.startsWith(t))return;let r=e.slice(t.length),s={repo:A.name,owner:A.owner.name??A.owner.login},o=`repo:${s.owner}/${s.repo} is:open is:pr base:${r}`;for await(let n of yW({search:o}))console.info(`Handling PRs ${n.map(i=>i.number).join(", ")}`),await Promise.all(n.map(async i=>Iu(s,i)))}async function hW(){li.isString.assert(process.env.GITHUB_EVENT_PATH);let{number:e,repository:A}=JSON.parse(await Wb.readFile(process.env.GITHUB_EVENT_PATH)),t={repo:A.name,owner:A.owner.name??A.owner.login},r=await $b({...t,number:e});await Iu(t,r)}async function Iu(e,A){switch(A.mergeable){case"CONFLICTING":await pW(e,A);break;case"MERGEABLE":{let t=[fW(e,A),IW(e,A)];await Promise.all(t);break}case"UNKNOWN":{console.info(`[PR ${A.number}] Conflict state is not yet known. Retrying.`),await iW(5e3);let t=await $b({...e,number:A.number});await Iu(e,t);break}}}function Ei(e,A){return e.labels.nodes.some(t=>A.includes(t.name))}async function IW(e,A){if(!EW)return;if(uW&&!A.autoMergeRequest){console.info(`[PR ${A.number}] Auto-merge is not enabled, skipping update.`);return}if(Qu.length>0&&!Ei(A,Qu)){console.info(`[PR ${A.number}] Does not have any of the required labels (${Qu}), skipping update.`);return}if(Cu.length>0&&Ei(A,Cu)){console.info(`[PR ${A.number}] Has one of the excluded (${Cu}), skipping update.`);return}if(QW.includes(eD(A.author))){console.info(`[PR ${A.number}] Was created by an excluded author, skipping update.`);return}if(!AD(A,lW)){console.info(`[PR ${A.number}] Not in the expected ready state, skipping update.`);return}if(!wW(A,CW)){console.info(`[PR ${A.number}] Not from the expected source, skipping update.`);return}if(ui(A)&&!A.maintainerCanModify){console.info(`[PR ${A.number}] Fork PR refuses updates from maintainers, skipping update.`);return}if(!await dW(e,A)){console.info(`[PR ${A.number}] Is up to date.`);return}if(Kb.push(A.number),console.info(`[PR ${A.number}] \u2705 Updating branch.`),gi)return;if(!ui(A))return aW.rest.pulls.updateBranch({...e,pull_number:A.number});let r=`pr-${A.number}`,s=jb.join(process.cwd(),r);console.log("cloning repo in",s),ci.execFileSync("gh",["repo","clone",A.baseRepository.nameWithOwner,r,"--","--branch",A.baseRef.name],{stdio:"inherit",env:{...process.env,GH_TOKEN:process.env.GITHUB_TOKEN}}),console.log("checking out PR branch"),ci.execFileSync("gh",["pr","checkout",String(A.number)],{cwd:s,stdio:"inherit",env:{...process.env,GH_TOKEN:process.env.GITHUB_TOKEN}}),console.log("executing git merge"),ci.execFileSync("git",["merge",A.baseRef.name,"--no-edit"],{cwd:s,stdio:"inherit"}),console.log("executing git push"),ci.execFileSync("git",["push"],{cwd:s,stdio:"inherit"})}async function dW(e,A){return(await Ot.graphql(`
query ($owner: String!, $repository:String!, $baseRef:String!, $headRef:String!) {
repository(owner:$owner, name: $repository) {
ref(qualifiedName: $baseRef) {
Expand Down
2 changes: 2 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ async function updatePrBranch(repositoryId: RepositoryId, pullRequest: PullReque

const isBehind = await checkPrIsBehindTarget(repositoryId, pullRequest);
if (!isBehind) {
console.info(`[PR ${pullRequest.number}] Is up to date.`);

return;
}

Expand Down

0 comments on commit fd26572

Please sign in to comment.