给 GitHub issues 添加相关搜索

972 阅读1分钟

给 GitHub issues 添加相关搜索

有一些开发者给开源项目提交 issues 前没有先搜一下 issues 或搜索的时候操作不当,没有搜到相关的结果。有没有办法解决呢?去年年底 Algolia 提供了一个 GitHub Action 可以为开源项目提供 issues 相关搜索推荐。效果如下:

q1.png

设置也非常简单。

1、在 www.algolia.com/ 网站注册一个账号,新建一个 Application,叫什么名称都可以。

qq2.png

2、切换到搜索 Tab,在 Index 这里新建一个和你项目同名的 Index。(注:一定要同名才可以哦

qq3.png

3、在右上角选择setting,在出现的页面里点击 API Keys。

qq4.png

4、分别复制 Application ID 和 Admin API Key 的值

qq5.png

去到 GitHub 项目的 Actions secrets 里「New repository secret」创建 ALGOLIA_API_KEYALGOLIA_APP_ID ,填好后效果如下。

qq6.png

5、最后在项目的 /.github/workflows/ 目录新建一个文件,填入 action 代码。如下。

name: related-issues
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  issues:
    types: 
      - opened

jobs:
  get-related-issues:
    permissions: 
      # Gives the workflow write permissions only in issues
      issues: write
    runs-on: ubuntu-latest
    steps:
      # Performs a search in an Algolia Index based on Issue Title
      # The Index should have historical Issues
      # Returns two outputs:
      # issues_list: a markdown list of issues
      # comment_body: a generic comment body with the list of issues
      - id: search
        name: Search based on issue title
        uses: brob/algolia-issue-search@v1.0
        with: 
          # Requires an Algolia account with an App ID and write key
          app_id: ${{ secrets.ALGOLIA_APP_ID }}
          api_key: ${{ secrets.ALGOLIA_API_KEY }}
          index_name: ${{ github.event.repository.name }}
          issue_title: ${{ github.event.issue.title }}
      - name: Create or Update Comment
        uses: peter-evans/create-or-update-comment@v1.4.5
        with:
          # GITHUB_TOKEN or a repo scoped PAT.
          token: ${{ github.token }}
          # The number of the issue or pull request in which to create a comment.
          issue-number: ${{ github.event.issue.number }}
          # The comment body. Can use either issues_list or comment_body
          body: |
            等待热心的小伙伴解决问题中..., 有一些相关的 issues 可能帮助到你!
            ${{ steps.search.outputs.issues_list }}
            Thank you so much! 
      # An Action to create a record in an Algolia Index
      # This is a generic Action and can be used outside of this workflow
      - name: Add Algolia Record
        id: ingest
        uses: chuckmeyer/add-algolia-record@v1
        with:
          app_id: ${{ secrets.ALGOLIA_APP_ID }}
          api_key: ${{ secrets.ALGOLIA_API_KEY }}
          index_name: ${{ github.event.repository.name }}
          # Record needs to be a string of JSON
          record: |
            {
              "title": "${{ github.event.issue.title }}", 
              "url": "${{ github.event.issue.html_url }}", 
              "labels": "${{ github.event.issue.labels }}",
              "objectID": "${{ github.event.issue.number }}"
            }

新建一个 issues 就会往 Algolia 里新建的索引里存入数据,可以免费使用 1 万条。

qq7.png

再建一个 issue 就会提供相关issues 了,完成。

q1.png