gitlab webhook

ChatGPT+ 极狐 GitLab

在这里插入图片描述

极狐 GitLab 代码变更
webhook 发送事件消息
ai-code-review App
发送 Review Request
ChatGPT
返回 Review Response
ai-code-review App
发送 Review Comment 至极狐 GitLab MR

ai-code-review 环境

  • 基本条件:机器能够访问 api.openai.com,且与极狐 GitLab 机器网络可互相访问。

  • *Linux / MacOS / Unix:
    Docker 安装:若未安装 Docker,可使用命令 curl -fsSL https://get.docker.com | bash -s docker 进行安装。
    运行程序:使用命令 docker run -itd -p 8888:8888 --restart=always --name ai-code-review -e gitlab_private_token=“<你的GitLab Token>” -e language=“Chinese” -e openai_api_key=“<你的OpenAI Token>” -e acr_license=“test” satomic/ai-code-review:20230321 启动程序,需将其中的 Token/License 参数替换为实际参数。

极狐 GitLab Webhook 配置

  • 在极狐 GitLab 中,对需要进行 AI code review 的 group 或 project 进行配置,建议使用 group 级别的 webhook,这样 group 内所有项目的 MR 都会被 AI 审查,无需为每个 project 重复配置。

  • 配置完成后,每次合并请求产生及变化时,都会在 webhook 发送记录中看到相应记录。

ChatGPT 评审效果

Review 效果示例可访问 Public 演示仓库 AI 自动 MR 评审演示。(见连接展示)

定义自己的评审机器人

  • 前文中的自动化评审机器人封装在 docker 镜像中,其角色声明为资深编程专家,以精炼、严厉的语气给出审查建议。

  • 如果想让机器人“注入感情”,可以自定义角色声明,例如定义一个傲娇少女来评审。

  • 创建 ai_code_review.py 文件,编写代码实现自动化评审功能,核心函数包括:
    (1)ai_code_review:调用 openai.ChatCompletion.create() 方法基于预设 prompt 角色发起对话,得到评审结果。
    (2)comment:调用 .merge_request.notes.create() 方法将评审内容追加到 MR 中。

  • 单次评审的使用方式为传递对应参数实例化 AICodeReview 对象并调用相应方法。

参考

CI/CD内部集成

ai-code-reviewer

缺点

ai-code-reviewer的限制在于:
(1)只能使用chatptp

export const openAiCompletionsConfig = {
    "temperature": 0,
    "model": "gpt-3.5-turbo",
    "stream": false,
}

(2)提示词有交互,回复不包含666才会在gitlab中增加comment

调试以及使用

安装

npm i @buxuku/ai-code-reviewer

安装到本地后,按照优化代码的方式优化代码,然后执行以下命令进行调试
eg:

 ./node_modules/.bin/ai-code-reviewer -g 'https://gitlab.xxxx.net.cn/api/v4' -t 'glpat-EYfAk6MhdsaSsJCZ_QPm' -o 'https://openrouter.ai/api' -a 'sk-or-v1-xxx' -p 248 -m 175

(4)gitlab.yaml中使用npm

在 GitLab CI/CD 中设置 GITLAB_TOKEN 和 CHATGPT_KEY 变量,.gitlab-ci.yml 如下:

stages:
  - merge-request

Code Review:
  stage: merge-request  
  image: node:latest
  script:
    - npm i @buxuku/ai-code-reviewer -g
    - ai-code-reviewer -t "$GITLAB_TOKEN" -a "$CHATGPT_KEY"  -p "$CI_MERGE_REQUEST_PROJECT_ID" -m "$CI_MERGE_REQUEST_IID"
  only:
    - merge_requests
  when: on_success

优化代码

针对于以上缺陷,我希望AI自动code review是不需要与人进行连续交互,另外还需要支持其他的API。所以需要改造点如下:

(1)修改能支持的模型,以openrouter为例

exports.openAiCompletionsConfig = {
    "temperature": 0,
    "model": "deepseek/deepseek-chat-v3-0324:free",
    "stream": false,
};

(2)修改建议提示词,删除需要用户输入666

exports.suggestContent = {
    "role": "user",
    "content": `Next, I will send you each step of the merge request in standard git diff format, your task is:
      - Review the code changes (diffs) in the patch and provide feedback.
      - Examine it carefully to see if it really has bugs or needs room for optimization, highlight them. 
      - Do not highlight minor issues and nitpicks.
      - Use bullet points if you have multiple comments.
      - You don't have to explain what the code does
      - please use chinese to give feedback.
      Here are the changes that were committed this time`
};

(3)在下面位置增加关键打印

                            const suggestion = yield openai.reviewCodeChange(item);
                            console.log('suggestion:', suggestion);
                            
                            if (!suggestion.includes('666')) {
                                yield gitlab.addReviewComment(lineObj, change, suggestion);
                            }

如果显示以下内容,说明大模型API对话失败了,此外这种错误也不会再gitlab中增加comment,因为是错误的格式

 ./node_modules/.bin/ai-code-reviewer -g 'https://gitlab.xxxx.net.cn/api/v4' -t 'glpat-EYfAk6MhdsaSsJCZ_QPm' -o 'https://openrouter.ai/api' -a 'sk-or-v1-xxx' -p 248 -m 175
ai code review is underway...
suggestion: undefined
suggestion: undefined
suggestion: undefined
done

(4)在.gitlab-ci.yml中使用

Ai-Code-Review:
  stage: ai-code-review
  before_script:
    # 使用淘宝镜像源安装 nodejs
    - curl -fsSL https://npmmirror.com/mirrors/node/v16.20.2/node-v16.20.2-linux-x64.tar.gz -o node.tar.gz
    - tar -xzf node.tar.gz
    - export PATH=$PATH:$PWD/node-v16.20.2-linux-x64/bin
    - pushd scripts/ai-code-reviewer-main
    - npm install typescript --save-dev --registry=https://registry.npmmirror.com
    - npx tsc
    - popd
  script:
    - echo "CI_MERGE_REQUEST_PROJECT_ID = $CI_MERGE_REQUEST_PROJECT_ID"
    - echo "CI_MERGE_REQUEST_IID = $CI_MERGE_REQUEST_IID"
    - echo "GITLAB_TOKEN= ${GITLAB_TOKEN}"
    - echo "CHATGPT_KEY= ${CHATGPT_KEY}"
    - node scripts/ai-code-reviewer-main/bin/index.js -g 'https://gitlab.xxxx.net.cn/api/v4' -t "$GITLAB_TOKEN" -o 'http://chat.api.autocore.ai' -a "$CHATGPT_KEY"  -p "$CI_MERGE_REQUEST_PROJECT_ID" -m "$CI_MERGE_REQUEST_IID" 
  only:
    - merge_requests
  tags:
    - docker
  when: on_success
  except:
    - tags

在gitlab中添加GITLAB_TOKEN以及CI_MERGE_REQUEST_IID
在这里插入图片描述
每个变量的属性设置如下
在这里插入图片描述

Chat-CodeReview

Code-Review-GPT-Gitlab

参考

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐