open-code-review/examples/gitflic_ci/gitflic-ci.yaml
Fedor 7497d5ac6e
docs(examples): add GitFlic CI auto-review example (#201)
* docs(examples): add GitFlic CI auto-review example

Add examples/gitflic_ci/, a CI-layer integration that reviews GitFlic
merge requests with OpenCodeReview and posts the findings as MR
discussions. Like the GitHub and GitLab examples, the posting glue lives
outside the core binary.

post_review.py (standard library only) reads `ocr review --format json`
and posts inline discussions plus a fallback note and a summary. GitFlic's
Discussions API requires an old-side line for inline comments, which the
new-side-only review output lacks, so the script recomputes it from the
same merge-base diff the review ran on. Ships with a stdlib unittest suite
whose line-mapping cases are ported from the review's diff logic.

* docs(readme): list the GitFlic CI example in the localized READMEs

* fix(examples): address GitFlic CI review feedback from PR #201

Apply the five review comments left on the PR:
- gitflic-ci.yaml: guard `ocr config set llm.model` behind a non-empty
  check so the documented-optional OCR_LLM_MODEL no longer breaks the
  config step when it is unset
- gitflic-ci.yaml: skip posting when `ocr review` produced no output
  (the step ends with `|| true`) instead of feeding empty/partial JSON
  to post_review.py
- post_review.py: redact the token from HTTP error snippets so it cannot
  leak into CI logs if GitFlic echoes the request back in an error body
- post_review.py: read the review-result file via a `with` block so the
  handle is closed explicitly
- examples/README.md: add the missing trailing newline
2026-07-02 10:41:39 +08:00

84 lines
3.3 KiB
YAML

# OpenCodeReview - GitFlic CI Merge Request Auto-Review Demo
#
# Reviews Merge Requests with OpenCodeReview and posts the findings onto the
# MR as discussions (inline where possible). The posting glue lives in the CI
# layer, in post_review.py next to this file -- consistent with the GitHub and
# GitLab examples, which keep platform-specific publishing out of the `ocr`
# binary.
#
# Setup:
# - Commit BOTH this file and post_review.py into your repository (adjust the
# `python3 post_review.py` path below if you place the script elsewhere).
# - Enable "Merge Request Pipeline" in Project Settings -> CI/CD Settings.
# - Use a runner able to run the node:20 image (it ships node, python3 and git),
# or any shell runner with node 20+, python3 and git available.
#
# Required CI/CD Variables (Settings -> CI/CD -> Variables):
# OCR_LLM_URL - LLM API endpoint (e.g., https://api.openai.com/v1/chat/completions)
# OCR_LLM_AUTH_TOKEN - Authentication token for the LLM API
# GITFLIC_TOKEN - GitFlic access token used to post MR discussions
#
# Optional CI/CD Variables:
# OCR_LLM_MODEL - Model name (e.g., gpt-4o)
# GITFLIC_API_URL - GitFlic REST API base URL; only needed for self-hosted
# instances (defaults to https://api.gitflic.ru)
#
# post_review.py picks up the MR context automatically from the predefined
# GitFlic CI variables: CI_PROJECT_NAMESPACE, CI_PROJECT_NAME,
# CI_MERGE_REQUEST_LOCAL_ID, CI_MERGE_REQUEST_TARGET_BRANCH_NAME, CI_COMMIT_SHA.
stages:
- review
code-review:
stage: review
image: node:20
script:
# Run only in merge request pipelines
- |
if [ -z "$CI_MERGE_REQUEST_LOCAL_ID" ]; then
echo "Not a merge request pipeline, skipping review."
exit 0
fi
# Install OpenCodeReview
- npm install -g @alibaba-group/open-code-review
# Configure OCR
- |
ocr config set llm.url $OCR_LLM_URL
ocr config set llm.auth_token $OCR_LLM_AUTH_TOKEN
if [ -n "$OCR_LLM_MODEL" ]; then
ocr config set llm.model "$OCR_LLM_MODEL"
fi
ocr config set llm.use_anthropic false
ocr config set llm.extra_body '{"thinking": {"type": "disabled"}}'
# Make sure the target branch and full history are available for merge-base diff
- git fetch --unshallow 2>/dev/null || true
- git fetch origin "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
# Run OCR review (CI_COMMIT_SHA as head supports forked MRs as well)
- |
ocr review \
--from "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}" \
--to "${CI_COMMIT_SHA}" \
--format json \
--audience agent \
> /tmp/ocr-result.json || true
echo "OCR review completed."
# Post review comments onto the MR (inline discussions + summary note).
# post_review.py recomputes the old-side line for each comment from the
# merge-base diff, which the GitFlic Discussions API requires for inline
# (code) comments.
#
# The review step above ends with `|| true`, so a failed `ocr review` (bad
# token, network error) leaves an empty or partial file. Skip posting in
# that case instead of feeding invalid JSON to post_review.py.
- |
if [ ! -s /tmp/ocr-result.json ]; then
echo "OCR review produced no output, skipping post."
exit 0
fi
python3 post_review.py /tmp/ocr-result.json