init
This commit is contained in:
141
services/platforms/gitee.go
Normal file
141
services/platforms/gitee.go
Normal file
@ -0,0 +1,141 @@
|
||||
package platforms
|
||||
|
||||
import (
|
||||
"code-review/services/types"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GiteeEvent Gitee 平台的 webhook 事件
|
||||
type GiteeEvent struct {
|
||||
client *httpClient
|
||||
Action string `json:"action"`
|
||||
ActionDesc string `json:"action_desc"`
|
||||
Hook struct {
|
||||
Password string `json:"password"` // webhook 密码
|
||||
} `json:"hook"`
|
||||
Password string `json:"password"` // 兼容旧版本
|
||||
Project struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
FullName string `json:"full_name"`
|
||||
WebURL string `json:"web_url"`
|
||||
Description string `json:"description"`
|
||||
} `json:"project"`
|
||||
PullRequest struct {
|
||||
ID int `json:"id"`
|
||||
Number int `json:"number"`
|
||||
State string `json:"state"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
SourceBranch string `json:"source_branch"`
|
||||
TargetBranch string `json:"target_branch"`
|
||||
Commits []struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
} `json:"commits"`
|
||||
Changes []struct {
|
||||
Path string `json:"path"`
|
||||
Content string `json:"content"`
|
||||
Type string `json:"type"` // added, modified, deleted, renamed
|
||||
OldPath string `json:"old_path,omitempty"`
|
||||
} `json:"changes"`
|
||||
} `json:"pull_request"`
|
||||
}
|
||||
|
||||
func NewGiteeEvent(baseURL, token string) *GiteeEvent {
|
||||
return &GiteeEvent{
|
||||
client: newHTTPClient(baseURL, token),
|
||||
}
|
||||
}
|
||||
|
||||
func (e *GiteeEvent) ExtractChanges() (*types.CodeChanges, error) {
|
||||
changes := &types.CodeChanges{
|
||||
Repository: e.Project.FullName,
|
||||
Branch: e.PullRequest.SourceBranch,
|
||||
CommitID: e.PullRequest.Commits[len(e.PullRequest.Commits)-1].ID,
|
||||
Files: make([]types.FileChange, 0, len(e.PullRequest.Changes)),
|
||||
PullRequest: &types.PullRequest{
|
||||
ID: e.PullRequest.ID,
|
||||
Title: e.PullRequest.Title,
|
||||
Description: e.PullRequest.Body,
|
||||
SourceBranch: e.PullRequest.SourceBranch,
|
||||
TargetBranch: e.PullRequest.TargetBranch,
|
||||
},
|
||||
}
|
||||
|
||||
for _, change := range e.PullRequest.Changes {
|
||||
fileChange := types.FileChange{
|
||||
Path: change.Path,
|
||||
Content: change.Content,
|
||||
OldPath: change.OldPath,
|
||||
}
|
||||
|
||||
switch change.Type {
|
||||
case "added":
|
||||
fileChange.Type = types.Added
|
||||
case "modified":
|
||||
fileChange.Type = types.Modified
|
||||
case "deleted":
|
||||
fileChange.Type = types.Deleted
|
||||
case "renamed":
|
||||
fileChange.Type = types.Renamed
|
||||
}
|
||||
|
||||
changes.Files = append(changes.Files, fileChange)
|
||||
}
|
||||
|
||||
return changes, nil
|
||||
}
|
||||
|
||||
func (e *GiteeEvent) PostComments(result *types.ReviewResult) error {
|
||||
if e.client == nil {
|
||||
return fmt.Errorf("client not initialized")
|
||||
}
|
||||
|
||||
for _, comment := range result.Comments {
|
||||
body := map[string]interface{}{
|
||||
"access_token": e.client.token,
|
||||
"body": fmt.Sprintf("**Code Review Comment**\n\nFile: %s\nLine: %d\nSeverity: %s\n\n%s",
|
||||
comment.Path,
|
||||
comment.Line,
|
||||
comment.Severity,
|
||||
comment.Content,
|
||||
),
|
||||
"position_line": comment.Line,
|
||||
"path": comment.Path,
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("/v5/repos/%s/pulls/%d/comments",
|
||||
e.Project.FullName,
|
||||
e.PullRequest.Number,
|
||||
)
|
||||
|
||||
if err := e.client.post(path, body); err != nil {
|
||||
return fmt.Errorf("post comment failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送总结评论
|
||||
if result.Summary != "" {
|
||||
body := map[string]interface{}{
|
||||
"access_token": e.client.token,
|
||||
"body": fmt.Sprintf("**Code Review Summary**\n\n%s", result.Summary),
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("/v5/repos/%s/pulls/%d/comments",
|
||||
e.Project.FullName,
|
||||
e.PullRequest.Number,
|
||||
)
|
||||
|
||||
if err := e.client.post(path, body); err != nil {
|
||||
return fmt.Errorf("post summary failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *GiteeEvent) GetPlatform() string {
|
||||
return "gitee"
|
||||
}
|
Reference in New Issue
Block a user