Skip to content

Commit

Permalink
use git pull instead of git merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Apr 12, 2024
1 parent 31b90fa commit a1689bd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fragment PR on PullRequest {
maintainerCanModify
viewerCanUpdateBranch
}
`,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 fW({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 dW(e,A);break;case"MERGEABLE":{let t=[pW(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(!yW(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(!A.viewerCanUpdateBranch){console.info(`[PR ${A.number}] Viewer cannot update branch, skipping update.`);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 t=`pr-${A.number}`,r=jb.join(process.cwd(),t),s=`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${A.headRepository.nameWithOwner}.git`,o=`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${A.baseRepository.nameWithOwner}.git`;console.log(`git clone ${s} ${t} --branch ${A.headRef.name}`),ci("git",["clone",s,t,"--branch",A.headRef.name],{stdio:"inherit"}),console.log(`git remote add parent ${o}`),ci("git",["remote","add","parent",o],{cwd:r,stdio:"inherit"}),console.log("git merge parent/main --no-edit"),ci("git",["merge","parent/main","--no-edit"],{cwd:r,stdio:"inherit"}),console.log(`git push origin ${A.headRef.name}`),ci("git",["push","origin",A.headRef.name],{cwd:r,stdio:"inherit"})}async function dW(e,A){if(!xt&&!Vb||!tD(A))return;zb.push(A.number);let t=[];xt&&!A.labels.nodes.some(r=>r.name===xt)&&(console.info(`[PR ${A.number}] \u2705 Adding conflict label.`),gi||t.push(Or.rest.issues.addLabels({...e,issue_number:A.number,labels:[xt]}))),Vb&&(console.info(`[PR ${A.number}] \u2705 Marking as draft due to conflicts.`),gi||t.push(Or.rest.pulls.update({...e,pull_number:A.number,draft:!0}))),await Promise.all(t)}async function pW(e,A){xt&&A.labels.nodes.some(t=>t.name===xt)&&tD(A)&&(console.info(`[PR ${A.number}] \u2705 No conflict, removing conflict label.`),gi||await Or.rest.issues.removeLabel({...e,issue_number:A.number,name:xt}))}async function $b(e){return(await Or.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 fW({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 dW(e,A);break;case"MERGEABLE":{let t=[pW(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(!yW(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(!A.viewerCanUpdateBranch){console.info(`[PR ${A.number}] Viewer cannot update branch, skipping update.`);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 t=`pr-${A.number}`,r=jb.join(process.cwd(),t),s=`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${A.headRepository.nameWithOwner}.git`,o=`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${A.baseRepository.nameWithOwner}.git`;console.log(`git clone ${s} ${t} --branch ${A.headRef.name}`),ci("git",["clone",s,t,"--branch",A.headRef.name],{stdio:"inherit"}),console.log(`git remote add parent ${o}`),ci("git",["remote","add","parent",o],{cwd:r,stdio:"inherit"}),console.log(`git pull parent ${A.baseRef.name} --no-edit --no-rebase`),ci("git",["pull","parent",A.baseRef.name,"--no-edit","--no-rebase"],{cwd:r,stdio:"inherit"}),console.log(`git push origin ${A.headRef.name}`),ci("git",["push","origin",A.headRef.name],{cwd:r,stdio:"inherit"})}async function dW(e,A){if(!xt&&!Vb||!tD(A))return;zb.push(A.number);let t=[];xt&&!A.labels.nodes.some(r=>r.name===xt)&&(console.info(`[PR ${A.number}] \u2705 Adding conflict label.`),gi||t.push(Or.rest.issues.addLabels({...e,issue_number:A.number,labels:[xt]}))),Vb&&(console.info(`[PR ${A.number}] \u2705 Marking as draft due to conflicts.`),gi||t.push(Or.rest.pulls.update({...e,pull_number:A.number,draft:!0}))),await Promise.all(t)}async function pW(e,A){xt&&A.labels.nodes.some(t=>t.name===xt)&&tD(A)&&(console.info(`[PR ${A.number}] \u2705 No conflict, removing conflict label.`),gi||await Or.rest.issues.removeLabel({...e,issue_number:A.number,name:xt}))}async function $b(e){return(await Or.graphql(`
${Xb}

query ($owner: String!, $repo: String!, $number: Int!) {
Expand Down
4 changes: 2 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ async function updatePrBranch(repositoryId: RepositoryId, pullRequest: PullReque
});

// merge parent branch in local branch
console.log(`git merge parent/main --no-edit`);
execFileSync('git', ['merge', 'parent/main', '--no-edit'], {
console.log(`git pull parent ${pullRequest.baseRef.name} --no-edit --no-rebase`);
execFileSync('git', ['pull', 'parent', pullRequest.baseRef.name, '--no-edit', '--no-rebase'], {
cwd: targetDirectoryPath,
stdio: 'inherit',
});
Expand Down

0 comments on commit a1689bd

Please sign in to comment.