|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type Message struct { |
| 13 | + Role string `json:"role"` |
| 14 | + Content string `json:"content"` |
| 15 | +} |
| 16 | + |
| 17 | +type RequestBody struct { |
| 18 | + Model string `json:"model"` |
| 19 | + Messages []Message `json:"messages"` |
| 20 | +} |
| 21 | + |
| 22 | +type Choice struct { |
| 23 | + Message Message `json:"message"` |
| 24 | +} |
| 25 | + |
| 26 | +type Response struct { |
| 27 | + Choices []Choice `json:"choices"` |
| 28 | +} |
| 29 | + |
| 30 | +func main() { |
| 31 | + if len(os.Args) < 2 { |
| 32 | + fmt.Println("Error: Missing commit message file argument") |
| 33 | + os.Exit(1) |
| 34 | + } |
| 35 | + |
| 36 | + // 读取commit message文件内容 |
| 37 | + commitMsg := []byte(os.Args[1]) |
| 38 | + if commitMsg == nil { |
| 39 | + fmt.Println("Error: commit message is empty") |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + |
| 43 | + apiKey := os.Getenv("DEEPSEEK_API_KEY") |
| 44 | + if apiKey == "" { |
| 45 | + apiKey = "sk-" |
| 46 | + } |
| 47 | + |
| 48 | + // 构建请求体 |
| 49 | + reqBody := RequestBody{ |
| 50 | + Model: "deepseek-chat", |
| 51 | + Messages: []Message{ |
| 52 | + { |
| 53 | + Role: "user", |
| 54 | + Content: fmt.Sprintf(`请检查以下 git commit message 是否符合规范,需要满足以下条件: |
| 55 | +1. 必须以 feat:, fix:, docs:, style:, refactor:, test:, chore: 等类型开头 |
| 56 | +2. 描述必须清晰明了 |
| 57 | +3. 不能太长,建议不超过50个字符 |
| 58 | +4. commit message 必须用英文书写 |
| 59 | +
|
| 60 | +Commit Message: '%s' |
| 61 | +
|
| 62 | +只需要回复 "通过" 或 "不通过:原因"。`, string(commitMsg)), |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + jsonData, err := json.Marshal(reqBody) |
| 68 | + if err != nil { |
| 69 | + fmt.Printf("Error marshaling request: %v\n", err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + // 发送请求到API |
| 74 | + req, err := http.NewRequest("POST", "https://api.deepseek.com/v1/chat/completions", strings.NewReader(string(jsonData))) |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Error creating request: %v\n", err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + req.Header.Set("Content-Type", "application/json") |
| 81 | + req.Header.Set("Authorization", "Bearer "+apiKey) |
| 82 | + |
| 83 | + client := &http.Client{} |
| 84 | + resp, err := client.Do(req) |
| 85 | + if err != nil { |
| 86 | + fmt.Printf("Error making request: %v\n", err) |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + body, err := io.ReadAll(resp.Body) |
| 92 | + if err != nil { |
| 93 | + fmt.Printf("Error reading response: %v\n", err) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + var response Response |
| 98 | + if err := json.Unmarshal(body, &response); err != nil { |
| 99 | + fmt.Printf("Error parsing response: %v\n", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + |
| 103 | + if len(response.Choices) > 0 { |
| 104 | + result := response.Choices[0].Message.Content |
| 105 | + if strings.Contains(result, "不通过") { |
| 106 | + fmt.Println("❌ Commit message 格式检查失败:") |
| 107 | + fmt.Println(result) |
| 108 | + os.Exit(1) |
| 109 | + } else { |
| 110 | + fmt.Println("✅ Commit message 格式检查通过") |
| 111 | + os.Exit(0) |
| 112 | + } |
| 113 | + } else { |
| 114 | + fmt.Println("Error: Empty response from API") |
| 115 | + os.Exit(1) |
| 116 | + } |
| 117 | +} |
0 commit comments