gitea获取diff更改 dockerfile更改 日志输出至文件
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user