open-code-review/internal/tool/code_comment.go
kite 533b526b4c
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
chore: add SPDX license headers and automated verification (#740)
* chore: add SPDX license headers to all source files

Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.

Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).

This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.

* fix: restore execute permissions on scripts

* docs: add license header instructions to CONTRIBUTING guides

* docs: add license header instructions to pages contributing guides

* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL

* fix: apply code review suggestions for license scripts

- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u

* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)

* fix(pages): use split/join instead of replace to avoid CodeQL false positive

CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
2026-08-05 21:26:27 +08:00

142 lines
3.9 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package tool
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/alibaba/open-code-review/internal/model"
)
const (
codeCommentCategoryBug = "bug"
codeCommentCategorySecurity = "security"
codeCommentCategoryPerformance = "performance"
codeCommentCategoryMaintainability = "maintainability"
codeCommentCategoryTest = "test"
codeCommentCategoryStyle = "style"
codeCommentCategoryDocumentation = "documentation"
codeCommentCategoryOther = "other"
codeCommentSeverityCritical = "critical"
codeCommentSeverityHigh = "high"
codeCommentSeverityMedium = "medium"
codeCommentSeverityLow = "low"
)
var validCodeCommentCategories = map[string]struct{}{
codeCommentCategoryBug: {},
codeCommentCategorySecurity: {},
codeCommentCategoryPerformance: {},
codeCommentCategoryMaintainability: {},
codeCommentCategoryTest: {},
codeCommentCategoryStyle: {},
codeCommentCategoryDocumentation: {},
codeCommentCategoryOther: {},
}
var validCodeCommentSeverities = map[string]struct{}{
codeCommentSeverityCritical: {},
codeCommentSeverityHigh: {},
codeCommentSeverityMedium: {},
codeCommentSeverityLow: {},
}
// CodeCommentProvider submits review comments to the per-Agent CommentCollector.
type CodeCommentProvider struct {
Collector *CommentCollector
}
func (p *CodeCommentProvider) Tool() Tool { return CodeComment }
func (p *CodeCommentProvider) Execute(_ context.Context, args map[string]any) (string, error) {
if p.Collector == nil {
return "Error: comment collector is not configured", nil
}
comments, errMsg := ParseComments(args)
if errMsg != "" {
return errMsg, nil
}
for i := range comments {
p.Collector.Add(comments[i])
}
return CommentSucceed, nil
}
// ParseComments extracts LlmComment entries from tool call arguments without writing
// to the Collector. Returns parsed comments and an error message (empty on success).
func ParseComments(args map[string]any) ([]model.LlmComment, string) {
var rawComments []any
if arr, ok := args["comments"].([]any); ok && len(arr) > 0 {
rawComments = arr
} else if s, ok := args["comments"].(string); ok && s != "" {
if err := json.Unmarshal([]byte(s), &rawComments); err != nil {
return nil, fmt.Sprintf("Error: failed to parse 'comments' JSON string: %v", err)
}
}
if len(rawComments) == 0 {
raw, _ := json.Marshal(args)
return nil, fmt.Sprintf("Error: 'comments' array is required. Got args: %s", string(raw))
}
var comments []model.LlmComment
for _, raw := range rawComments {
obj, ok := raw.(map[string]any)
if !ok {
continue
}
cm := model.LlmComment{}
if content, ok := obj["content"].(string); ok {
cm.Content = content
}
if suggestion, ok := obj["suggestion_code"].(string); ok {
cm.SuggestionCode = suggestion
}
if existing, ok := obj["existing_code"].(string); ok {
cm.ExistingCode = existing
}
if thinking, ok := obj["thinking"].(string); ok {
cm.Thinking = thinking
}
if category, ok := obj["category"].(string); ok {
cm.Category = normalizeCodeCommentCategory(category)
}
if severity, ok := obj["severity"].(string); ok {
cm.Severity = normalizeCodeCommentSeverity(severity)
}
if path, ok := args["path"].(string); ok {
cm.Path = path
}
if cm.Path == "" || cm.Content == "" {
continue
}
comments = append(comments, cm)
}
return comments, ""
}
func normalizeCodeCommentCategory(category string) string {
normalized := strings.ToLower(category)
if _, ok := validCodeCommentCategories[normalized]; ok {
return normalized
}
return codeCommentCategoryOther
}
func normalizeCodeCommentSeverity(severity string) string {
normalized := strings.ToLower(severity)
if _, ok := validCodeCommentSeverities[normalized]; ok {
return normalized
}
return codeCommentSeverityLow
}