Forráskód Böngészése

add auto reply issue

chenshuai2144 4 éve
szülő
commit
69eb71f491
2 módosított fájl, 129 hozzáadás és 0 törlés
  1. 44 0
      .github/workflows/reply-issue.yml
  2. 85 0
      tests/issue.js

+ 44 - 0
.github/workflows/reply-issue.yml

@@ -0,0 +1,44 @@
+name: auto issue
+
+on:
+  issues:
+    types: [opened]
+
+jobs:
+  reply:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      - name: Use Node.js ${{ matrix.node_version }}
+        uses: actions/setup-node@v1
+        with:
+          node-version: ${{ matrix.node_version }}
+      - name: Get yarn cache directory path
+        id: yarn-cache-dir-path
+        run: echo "::set-output name=dir::$(yarn cache dir)"
+      - name: Cache yarn cache
+        uses: actions/cache@v2
+        id: cache-yarn-cache
+        with:
+          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
+          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
+          restore-keys: |
+            ${{ runner.os }}-yarn-
+      - name: Cache node_modules
+        id: cache-node-modules
+        uses: actions/cache@v2
+        with:
+          path: node_modules
+          key: ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-${{ hashFiles('**/yarn.lock') }}
+          restore-keys: |
+            ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-
+      - run: yarn --ignore-engines
+        if: |
+          steps.cache-yarn-cache.outputs.cache-hit != 'true' ||
+          steps.cache-node-modules.outputs.cache-hit != 'true'
+      - run: yarn add @octokit/core
+      - name: Auto reply to issue
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          ISSUE_NUMBER: ${{ github.event.issue.number }}
+        run: node ./tests/issue.js

+ 85 - 0
tests/issue.js

@@ -0,0 +1,85 @@
+const Octokit = require('@octokit/core');
+
+const octokit = new Octokit.Octokit({
+  auth: process.env.GITHUB_TOKEN || process.env.GITHUB_AUTH,
+});
+
+const queryIssue = ({ title, id }) => {
+  return octokit
+    .request('GET /search/issues', {
+      q: title,
+      per_page: 5,
+    })
+    .then(({ data }) => {
+      const list = data.items
+        .map((item) => {
+          return {
+            title: item.title,
+            url: item.html_url,
+            id: item.id,
+          };
+        })
+        .filter((item) => {
+          return item.id !== id;
+        });
+
+      if (list.length > 0) {
+        return `
+> Issue Robot generation
+
+### 以下的issue可能会帮助到你 :
+
+${list
+  .map((item) => {
+    return `* [${item.title}](${item.url})`;
+  })
+  .join('\n')}`;
+      }
+      return null;
+    })
+    .then(async (markdown) => {
+      return markdown;
+    });
+};
+
+const findIssue = async (issueId) => {
+  const { data } = await octokit.request('GET /repos/{owner}/{repo}/issues/{issue_number}', {
+    owner: 'ant-design',
+    repo: 'ant-design-pro',
+    issue_number: issueId,
+  });
+  return data;
+};
+const closeIssue = async (issueId) => {
+  await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
+    owner: 'ant-design',
+    repo: 'ant-design-pro',
+    issue_number: issueId,
+    state: 'closed',
+  });
+};
+const replyCommit = async (issueId, markdown) => {
+  await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
+    owner: 'ant-design',
+    repo: 'ant-design-pro',
+    issue_number: issueId,
+    body: markdown,
+  });
+};
+
+const reply = async () => {
+  const issueId = process.env.ISSUE_NUMBER;
+  const issue = await findIssue(issueId);
+  if (!issue.title || issue.title.length < 6) {
+    replyCommit(issueId, '**请写标题!**');
+    closeIssue(issueId);
+    return;
+  }
+  const markdown = await queryIssue({
+    title: issue.title,
+    id: issue.id,
+  });
+  replyCommit(issueId, markdown);
+};
+
+reply();