自分が出したpull reqがconflictしていたら通知するGitHub Action君

というのを作った。

仕事で自分が関わっている特定のリポジトリでは、以前からそういうの作ってbotがpull reqに直接コメントするようにしている。(自分のものだけではなく、そのリポジトリのpull req全てに対して)

そうではなく、自分が出したOSS含めた任意のpull reqでconflictしていたら通知欲しいので、作った。

https://github.com/xuwei-k/cron/commit/17c9c731593ce5de9178fabdb26c088631d74e74

通知先はとりあえずslackにしたが、ここはメールでもなんでも、好きものでいいと思う。

自分のJavaScriptの能力はゴミなので、明らかにリファクタ出来そうだけれど、とりあえず動いたので一旦妥協した。 改変して使う際は q のauthor部分は、直接埋め込んであるけれど、自分のgithubのidに変えてください。

f:id:xuwei:20211230163940p:plain
slack

on:
  schedule:
  - cron: '0 1 * * *'
  workflow_dispatch:
jobs:
  check-conflict:
    timeout-minutes: 5
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - run: npm install @actions/core
    - uses: actions/github-script@v5
      id: conflict_pull_requests
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
        script: |
          const script = require(`${process.env.GITHUB_WORKSPACE}/cron.js`)
          console.log(script({github, context}))
    - uses: slackapi/slack-github-action@v1.16.0
      with:
        payload: "{\"text\":\"${{ steps.conflict_pull_requests.outputs.value }}\"}"
      env:
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
const core = require(`${process.env.GITHUB_WORKSPACE}/node_modules/@actions/core`);

module.exports = ({github, context}) => {
  (async () => {
    // https://octokit.github.io/rest.js/v18#search-issues-and-pull-requests
    const pulls = await github.rest.search.issuesAndPullRequests({
      q: "is:open+is:pr+author:xuwei-k+archived:false",
      per_page: 100
    });
    const conflicts = [];
    for (const pull of pulls.data.items) {
      console.log(pull.url);
      const pull_req = await github.request(`GET ${pull.pull_request.url}`)
      if (pull_req.data.mergeable === false) {
        console.log(pull_req.data.mergeable);
        conflicts.push(pull.html_url);
      }
    }
    console.log(conflicts);
    if (conflicts.length == 0) {
      core.setOutput('value', 'There is no conflict pull requests😀');
    } else {
      core.setOutput('value', 'conflict pull requests\\n' + conflicts.join("\\n"));
    }
  })();
};