From 0cfcb8c23bdd7a3f6625909f048034108b2383cb Mon Sep 17 00:00:00 2001 From: crong Date: Mon, 22 Apr 2024 07:44:11 +0900 Subject: [PATCH] Create merge.yml merge script --- .github/workflows/merge.yml | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 .github/workflows/merge.yml diff --git a/.github/workflows/merge.yml b/.github/workflows/merge.yml new file mode 100644 index 0000000..8eea54d --- /dev/null +++ b/.github/workflows/merge.yml @@ -0,0 +1,119 @@ +name: "PR merge on time v3.2 by crong" + +on: + schedule: + - cron: "0 13 * * 3,5" + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +permissions: + contents: write + pull-requests: write + issues: write + +jobs: + merge: + name: "Auto Merge on time" + runs-on: "ubuntu-latest" + + steps: + - name: "Merge pull request" + uses: "actions/github-script@v6" + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const query = `query($owner:String!, $name:String!) { + repository(owner: $owner, name: $name) { + pullRequests(last: 100, states: OPEN) { + edges { + node { + number + headRefName + baseRefName + author { + login + } + repository { + name + } + mergeable + labels(first: 10) { + nodes { + name + } + } + reviews(last: 1) { + nodes { + state + } + } + } + } + } + } + }` + + const variables = { + owner: context.repo.owner, + name: context.repo.repo, + } + + const {repository:{pullRequests:{edges: list}}} = await github.graphql(query, variables) + for( let {node} of list) { + if(node.baseRefName === "main" || !node.labels.nodes.length) { + //add comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: node.number, + body: "main 브랜치로 병합 시도 || 적절한 라벨이 있는지 확인해 주세요." + }) + continue; + } + if (node.reviews.nodes.length && node.reviews.nodes[0].state === "CHANGES_REQUESTED") { + //add comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: node.number, + body: "변경 요청 중인 브랜치의 머지를 연기합니다." + }) + continue; + } + + try { + if(node.mergeable === "CONFLICTING") { + // add comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: node.number, + body: "충돌로 인해 자동 병합이 불가능합니다." + }) + // close pull request + await github.rest.pulls.update({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: node.number, + state: "closed" + }) + } else { + //add comment + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: node.number, + body: "자동 병합 완료" + }) + // merge pull request + await github.rest.pulls.merge({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: node.number, + merge_method: "merge" + }) + } + } catch (e) { + console.log("error", e); + } + }