Skip to content

Share my use case in gitlab #265

Description

@WANGCHIENCHIH

for my workflow use:
do ai code review -> publish review report

.ai-code-review:
  variables:
    GIT_DEPTH: 0
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_PIPELINE_SOURCE == "push"
  tags:
    - docker
  image: ghcr.io/appleboy/codegpt:1.7.0
  stage: test
  before_script:
    - git config --global --add safe.directory "$CI_PROJECT_DIR"
  script:
    - codegpt config list
    - REVIEW_OUTPUT=$(codegpt review --amend 2>&1 || true)
    - printf '%s\n' "$REVIEW_OUTPUT" | tee review_report.md

  artifacts:
    paths:
      - review_report.md
    expire_in: 3 weeks
  allow_failure: true

.publish-code-review:
  stage: test
  rules:
    - if: $CI_PIPELINE_SOURCE == "push"
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  tags:
    - docker
  image: alpine:3.21
  needs:
    - job: ai-code-review
      artifacts: true
  dependencies:
    - ai-code-review
  before_script:
    - apk add --no-cache curl jq
  script:
    - |
      
      REPORT_CONTENT=$(cat review_report.md | awk '{gsub(/================Review Summary====================/,"\n\n================Review Summary====================");print}')
      NOTE=$(printf '## 🤖 Code Review Results\n\n%s' "$REPORT_CONTENT")
      PAYLOAD=$(jq -n --arg body "$NOTE" '{note: $body}')
      
      if [ "$CI_PIPELINE_SOURCE" = "push" ]; then
        API_ENDPOINT="projects/${CI_PROJECT_ID}/repository/commits/${CI_COMMIT_SHA}/comments"
      elif [ "$CI_PIPELINE_SOURCE" = "merge_request_event" ]; then
        API_ENDPOINT="projects/${CI_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/notes"
      else
        echo "Unsupported pipeline source: $CI_PIPELINE_SOURCE"
        exit 1
      fi
      
      API_URL="${CI_API_V4_URL}/${API_ENDPOINT}"
      
      HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
        --request POST \
        --header "Authorization: Bearer ${GITLAB_TOKEN}" \
        --header "Content-Type: application/json" \
        --data "$PAYLOAD" \
        "$API_URL")
      
      if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
        echo "[SUCCESS] Posted to ${CI_PIPELINE_SOURCE}"
      else
        echo "[ERROR] HTTP $HTTP_CODE"
        exit 1
      fi

why use awk is that:
cmd/review.go file not use newline,and show messsage like this

🤖 Code Review Results
Code review your changes using gpt-4o model
We are trying to review code changes
Prompt tokens: 383, Completion tokens: 4802, Total tokens: 5185
we are trying to translate code review to Traditional Chinese language
Prompt tokens: 3371, Completion tokens: 8649, Total tokens: 12020 ================Review Summary====================

summary text

after use awk replace with newline,it can show pretty

🤖 Code Review Results
Code review your changes using gpt-4o  model
We are trying to review code changes
Prompt tokens: 436, Completion tokens: 3448, Total tokens: 3884
we are trying to translate code review to Traditional Chinese language
Prompt tokens: 2309, Completion tokens: 3701, Total tokens: 6010
================Review Summary====================

summary text

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions