gitea获取diff更改 dockerfile更改 日志输出至文件
This commit is contained in:
@ -195,3 +195,38 @@ func (c *httpClient) postWithHeaders(path string, data interface{}, headers map[
|
||||
log.Printf("POST 请求成功: url=%s", url)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *httpClient) getRaw(path string) (string, error) {
|
||||
url := fmt.Sprintf("%s%s", c.url, path)
|
||||
log.Printf("发送 GET 请求: url=%s", url)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
log.Printf("创建 GET 请求失败: url=%s, error=%v", url, err)
|
||||
return "", fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
|
||||
c.setAuthHeaders(req)
|
||||
|
||||
resp, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
log.Printf("发送 GET 请求失败: url=%s, error=%v", url, err)
|
||||
return "", fmt.Errorf("发送请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
log.Printf("GET 请求返回错误状态码: url=%s, status=%d, response=%s", url, resp.StatusCode, string(body))
|
||||
return "", fmt.Errorf("请求失败,状态码: %d,响应: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("读取响应失败: url=%s, error=%v", url, err)
|
||||
return "", fmt.Errorf("读取响应失败: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("GET 请求成功: url=%s", url)
|
||||
return string(body), nil
|
||||
}
|
||||
|
@ -172,27 +172,7 @@ func NewGiteaEvent(apiBase string, auth *AuthConfig, event string) *GiteaEvent {
|
||||
|
||||
// 定义 Gitea commit 响应的结构
|
||||
type giteaCommitResponse struct {
|
||||
Commit struct {
|
||||
Message string `json:"message"`
|
||||
Author struct {
|
||||
Date string `json:"date"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
} `json:"author"`
|
||||
Committer struct {
|
||||
Date string `json:"date"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
} `json:"committer"`
|
||||
} `json:"commit"`
|
||||
Files []struct {
|
||||
Filename string `json:"filename"`
|
||||
Status string `json:"status"`
|
||||
Additions int `json:"additions"`
|
||||
Deletions int `json:"deletions"`
|
||||
Changes int `json:"changes"`
|
||||
Content string `json:"content"`
|
||||
} `json:"files"`
|
||||
Diff string `json:"diff"`
|
||||
}
|
||||
|
||||
func (e *GiteaEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
@ -202,14 +182,6 @@ func (e *GiteaEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 检查是否跳过代码审查
|
||||
for _, commit := range e.Commits {
|
||||
if strings.Contains(commit.Commit.Message, "[skip codereview]") {
|
||||
log.Printf("跳过代码审查: commit=%s", commit.ID)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
if e.client == nil {
|
||||
e.client = newHTTPClient(e.apiBase, e.auth)
|
||||
log.Printf("初始化 HTTP 客户端: url=%s", e.apiBase)
|
||||
@ -223,48 +195,69 @@ func (e *GiteaEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
}
|
||||
|
||||
for _, commit := range e.Commits {
|
||||
// 直接获取 diff 内容
|
||||
apiPath := fmt.Sprintf("/api/v1/repos/%s/%s/git/commits/%s", e.Repository.Owner.Login, e.Repository.Name, e.After)
|
||||
var diffContent giteaCommitResponse
|
||||
// 检查提交信息是否包含跳过标记
|
||||
if strings.Contains(commit.Message, "[skip codereview]") {
|
||||
log.Printf("提交包含跳过标记,跳过所有文件审查: commit=%s", commit.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := e.client.get(apiPath, &diffContent); err != nil {
|
||||
// 检查是否是合并提交
|
||||
if strings.HasPrefix(commit.Message, "Merge remote-tracking branch") ||
|
||||
strings.HasPrefix(commit.Message, "Merge branch") {
|
||||
log.Printf("跳过合并提交的文件审查: commit=%s", commit.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取 diff 内容
|
||||
apiPath := fmt.Sprintf("/api/v1/repos/%s/%s/git/commits/%s.diff", e.Repository.Owner.Login, e.Repository.Name, e.After)
|
||||
diffContent, err := e.client.getRaw(apiPath)
|
||||
if err != nil {
|
||||
log.Printf("获取提交详情失败: commit=%s, error=%v", commit.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 处理每个文件的变更
|
||||
for _, file := range diffContent.Files {
|
||||
var content strings.Builder
|
||||
content.WriteString(fmt.Sprintf("### 变更说明\n"))
|
||||
content.WriteString(fmt.Sprintf("提交信息: %s\n\n", diffContent.Commit.Message))
|
||||
// 解析 diff 内容
|
||||
diffLines := strings.Split(diffContent, "\n")
|
||||
var currentFile *types.FileChange
|
||||
var currentContent strings.Builder
|
||||
|
||||
status := "modified"
|
||||
switch file.Status {
|
||||
case "added":
|
||||
status = "added"
|
||||
content.WriteString(fmt.Sprintf("新增文件: %s\n\n", file.Filename))
|
||||
case "removed":
|
||||
status = "deleted"
|
||||
content.WriteString(fmt.Sprintf("删除文件: %s\n\n", file.Filename))
|
||||
case "renamed":
|
||||
status = "renamed"
|
||||
content.WriteString(fmt.Sprintf("重命名文件: %s\n\n", file.Filename))
|
||||
default:
|
||||
content.WriteString(fmt.Sprintf("修改文件: %s\n\n", file.Filename))
|
||||
for _, line := range diffLines {
|
||||
if strings.HasPrefix(line, "diff --git") {
|
||||
// 保存前一个文件的内容
|
||||
if currentFile != nil {
|
||||
currentFile.Content = currentContent.String() + "```\n"
|
||||
changes.Files = append(changes.Files, *currentFile)
|
||||
}
|
||||
|
||||
// 解析文件名
|
||||
parts := strings.Split(line, " ")
|
||||
if len(parts) >= 3 {
|
||||
filePath := strings.TrimPrefix(parts[2], "b/")
|
||||
if shouldSkipFile(filePath) {
|
||||
log.Printf("跳过文件审查: file=%s", filePath)
|
||||
currentFile = nil
|
||||
continue
|
||||
}
|
||||
|
||||
currentFile = &types.FileChange{
|
||||
Path: filePath,
|
||||
Type: utils.ParseFileType("modified"),
|
||||
}
|
||||
currentContent.Reset()
|
||||
currentContent.WriteString(fmt.Sprintf("### 变更说明\n"))
|
||||
currentContent.WriteString(fmt.Sprintf("提交信息: %s\n\n", commit.Message))
|
||||
currentContent.WriteString("### 变更内容\n")
|
||||
currentContent.WriteString("```diff\n")
|
||||
}
|
||||
} else if currentFile != nil {
|
||||
currentContent.WriteString(line + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
if file.Content != "" {
|
||||
content.WriteString("### 变更内容\n")
|
||||
content.WriteString("```diff\n")
|
||||
content.WriteString(file.Content)
|
||||
content.WriteString("\n```\n")
|
||||
}
|
||||
|
||||
changes.Files = append(changes.Files, types.FileChange{
|
||||
Path: file.Filename,
|
||||
Content: content.String(),
|
||||
Type: utils.ParseFileType(status),
|
||||
})
|
||||
// 保存最后一个文件的内容
|
||||
if currentFile != nil {
|
||||
currentFile.Content = currentContent.String() + "```\n"
|
||||
changes.Files = append(changes.Files, *currentFile)
|
||||
}
|
||||
}
|
||||
|
||||
@ -295,3 +288,24 @@ func (e *GiteaEvent) PostComments(result *types.ReviewResult) error {
|
||||
func (e *GiteaEvent) GetPlatform() string {
|
||||
return "gitea"
|
||||
}
|
||||
|
||||
// 判断是否应该跳过文件审查
|
||||
func shouldSkipFile(filename string) bool {
|
||||
// 跳过特定文件类型
|
||||
skipExtensions := []string{".md", ".txt", ".json", ".yaml", ".yml", ".lock"}
|
||||
for _, ext := range skipExtensions {
|
||||
if strings.HasSuffix(filename, ext) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// 跳过特定目录
|
||||
skipDirs := []string{"node_modules/", "dist/", "build/", "vendor/"}
|
||||
for _, dir := range skipDirs {
|
||||
if strings.Contains(filename, dir) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"code-review/services/types"
|
||||
"code-review/utils"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GiteeEvent Gitee 平台的 webhook 事件
|
||||
@ -70,6 +72,31 @@ func (e *GiteeEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
},
|
||||
}
|
||||
|
||||
// 过滤合并提交
|
||||
validCommits := make([]struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
}, 0)
|
||||
|
||||
for _, commit := range e.PullRequest.Commits {
|
||||
if strings.HasPrefix(commit.Message, "Merge remote-tracking branch") ||
|
||||
strings.HasPrefix(commit.Message, "Merge branch") {
|
||||
log.Printf("跳过合并提交: commit=%s", commit.ID)
|
||||
continue
|
||||
}
|
||||
validCommits = append(validCommits, commit)
|
||||
}
|
||||
|
||||
if len(validCommits) == 0 {
|
||||
log.Printf("没有有效的提交记录(所有提交都是合并提交),跳过代码审查")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 更新最后的提交ID
|
||||
if len(validCommits) > 0 {
|
||||
changes.CommitID = validCommits[len(validCommits)-1].ID
|
||||
}
|
||||
|
||||
for _, change := range e.PullRequest.Changes {
|
||||
fileChange := types.FileChange{
|
||||
Path: change.Path,
|
||||
|
@ -62,11 +62,29 @@ func (e *GitlabEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
}
|
||||
|
||||
// 检查是否跳过代码审查
|
||||
validCommits := make([]struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
Title string `json:"title"`
|
||||
}, 0)
|
||||
|
||||
for _, commit := range e.Commits {
|
||||
if strings.Contains(commit.Message, "[skip codereview]") {
|
||||
log.Printf("跳过代码审查: commit=%s", commit.ID)
|
||||
return nil, nil
|
||||
}
|
||||
// 过滤合并提交
|
||||
if strings.HasPrefix(commit.Message, "Merge remote-tracking branch") ||
|
||||
strings.HasPrefix(commit.Message, "Merge branch") {
|
||||
log.Printf("跳过合并提交: commit=%s", commit.ID)
|
||||
continue
|
||||
}
|
||||
validCommits = append(validCommits, commit)
|
||||
}
|
||||
|
||||
if len(validCommits) == 0 {
|
||||
log.Printf("没有有效的提交记录(所有提交都是合并提交),跳过代码审查")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if e.client == nil {
|
||||
@ -81,7 +99,7 @@ func (e *GitlabEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
Files: make([]types.FileChange, 0),
|
||||
}
|
||||
|
||||
for _, commit := range e.Commits {
|
||||
for _, commit := range validCommits {
|
||||
// 移除 URL 中的 token
|
||||
apiPath := fmt.Sprintf("/api/v4/projects/%d/repository/commits/%s/diff",
|
||||
e.Project.ID, commit.ID)
|
||||
|
Reference in New Issue
Block a user