Skip to content

Commit 3197819

Browse files
authored
build: refactor codebase and improve security (#168)
- Add a new `.golangci.yml` configuration file with a list of linters and a 3-minute timeout setting - Refactor `commit.go` to improve readability by breaking long lines of flag definitions - Replace the OpenAI client initialization in `commit.go` with a call to a new function `NewOpenAI` - Change file permission in `commit.go` from `644` to `600` when writing the `outputFile` - Fix a typo in an error message in `hepler.go` (should be `helper.go`) - Create a new `openai.go` file to handle OpenAI client initialization - Remove OpenAI client initialization from `review.go` and replace it with a call to `NewOpenAI` - Refactor `review.go` to improve readability by breaking long lines of flag definitions - Update error messages in `git.go` to remove periods at the end - Change file permission in `git.go` from `755` to `600` when writing the hook file Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 6c81350 commit 3197819

6 files changed

Lines changed: 89 additions & 50 deletions

File tree

.golangci.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
linters:
2+
enable-all: false
3+
disable-all: true
4+
fast: false
5+
enable:
6+
- bodyclose
7+
- dogsled
8+
- dupl
9+
- errcheck
10+
- exportloopref
11+
- exhaustive
12+
- gochecknoinits
13+
- goconst
14+
- gocritic
15+
- gocyclo
16+
- gofmt
17+
- goimports
18+
- goprintffuncname
19+
- gosec
20+
- gosimple
21+
- govet
22+
- ineffassign
23+
- lll
24+
- misspell
25+
- nakedret
26+
- noctx
27+
- nolintlint
28+
- rowserrcheck
29+
- staticcheck
30+
- stylecheck
31+
- typecheck
32+
- unconvert
33+
- unparam
34+
- unused
35+
- whitespace
36+
- gofumpt
37+
run:
38+
timeout: 3m

cmd/commit.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,23 @@ var (
4444
func init() {
4545
commitCmd.PersistentFlags().StringP("file", "f", "", "commit message file")
4646
commitCmd.PersistentFlags().BoolVar(&preview, "preview", false, "preview commit message")
47-
commitCmd.PersistentFlags().IntVar(&diffUnified, "diff_unified", 3, "generate diffs with <n> lines of context, default is 3")
47+
commitCmd.PersistentFlags().IntVar(&diffUnified, "diff_unified", 3,
48+
"generate diffs with <n> lines of context, default is 3")
4849
commitCmd.PersistentFlags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
4950
commitCmd.PersistentFlags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
50-
commitCmd.PersistentFlags().StringSliceVar(&excludeList, "exclude_list", []string{}, "exclude file from git diff command")
51+
commitCmd.PersistentFlags().StringSliceVar(&excludeList, "exclude_list", []string{},
52+
"exclude file from git diff command")
5153
commitCmd.PersistentFlags().StringVar(&httpsProxy, "proxy", "", "http proxy")
5254
commitCmd.PersistentFlags().StringVar(&socksProxy, "socks", "", "socks proxy")
5355
commitCmd.PersistentFlags().StringVar(&templateFile, "template_file", "", "git commit message file")
5456
commitCmd.PersistentFlags().StringVar(&templateString, "template_string", "", "git commit message string")
5557
commitCmd.PersistentFlags().StringSliceVar(&templateVars, "template_vars", []string{}, "template variables")
5658
commitCmd.PersistentFlags().StringVar(&templateVarsFile, "template_vars_file", "", "template variables file")
57-
commitCmd.PersistentFlags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
59+
commitCmd.PersistentFlags().BoolVar(&commitAmend, "amend", false,
60+
"replace the tip of the current branch by creating a new commit.")
5861
commitCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", defaultTimeout, "request timeout")
59-
commitCmd.PersistentFlags().BoolVar(&promptOnly, "prompt_only", false, "show prompt only, don't send request to openai")
62+
commitCmd.PersistentFlags().BoolVar(&promptOnly, "prompt_only", false,
63+
"show prompt only, don't send request to openai")
6064
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
6165
}
6266

@@ -86,24 +90,7 @@ var commitCmd = &cobra.Command{
8690

8791
currentModel := viper.GetString("openai.model")
8892
color.Green("Summarize the commit message use " + currentModel + " model")
89-
client, err := openai.New(
90-
openai.WithToken(viper.GetString("openai.api_key")),
91-
openai.WithModel(viper.GetString("openai.model")),
92-
openai.WithOrgID(viper.GetString("openai.org_id")),
93-
openai.WithProxyURL(viper.GetString("openai.proxy")),
94-
openai.WithSocksURL(viper.GetString("openai.socks")),
95-
openai.WithBaseURL(viper.GetString("openai.base_url")),
96-
openai.WithTimeout(viper.GetDuration("openai.timeout")),
97-
openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
98-
openai.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
99-
openai.WithProvider(viper.GetString("openai.provider")),
100-
openai.WithSkipVerify(viper.GetBool("openai.skip_verify")),
101-
openai.WithHeaders(viper.GetStringSlice("openai.headers")),
102-
openai.WithApiVersion(viper.GetString("openai.api_version")),
103-
openai.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
104-
openai.WithFrequencyPenalty(float32(viper.GetFloat64("openai.frequency_penalty"))),
105-
openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
106-
)
93+
client, err := NewOpenAI()
10794
if err != nil && !promptOnly {
10895
return err
10996
}
@@ -300,7 +287,7 @@ var commitCmd = &cobra.Command{
300287
}
301288
color.Cyan("Write the commit message to " + outputFile + " file")
302289
// write commit message to git staging file
303-
err = os.WriteFile(outputFile, []byte(commitMessage), 0o644)
290+
err = os.WriteFile(outputFile, []byte(commitMessage), 0o600)
304291
if err != nil {
305292
return err
306293
}

cmd/hepler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func check() error {
1616
// Check if the Git command is available on the system's PATH
1717
if !util.IsCommandAvailable("git") {
18-
return errors.New("Git command not found on your system's PATH. Please install Git and try again.")
18+
return errors.New("git command not found on your system's PATH. Please install Git and try again")
1919
}
2020

2121
// Update Viper configuration values based on the CLI flags

cmd/openai.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmd
2+
3+
import (
4+
"github.com/appleboy/CodeGPT/openai"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
func NewOpenAI() (*openai.Client, error) {
10+
return openai.New(
11+
openai.WithToken(viper.GetString("openai.api_key")),
12+
openai.WithModel(viper.GetString("openai.model")),
13+
openai.WithOrgID(viper.GetString("openai.org_id")),
14+
openai.WithProxyURL(viper.GetString("openai.proxy")),
15+
openai.WithSocksURL(viper.GetString("openai.socks")),
16+
openai.WithBaseURL(viper.GetString("openai.base_url")),
17+
openai.WithTimeout(viper.GetDuration("openai.timeout")),
18+
openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
19+
openai.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
20+
openai.WithProvider(viper.GetString("openai.provider")),
21+
openai.WithSkipVerify(viper.GetBool("openai.skip_verify")),
22+
openai.WithHeaders(viper.GetStringSlice("openai.headers")),
23+
openai.WithApiVersion(viper.GetString("openai.api_version")),
24+
openai.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
25+
openai.WithFrequencyPenalty(float32(viper.GetFloat64("openai.frequency_penalty"))),
26+
openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
27+
)
28+
}

cmd/review.go

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/appleboy/CodeGPT/git"
8-
"github.com/appleboy/CodeGPT/openai"
98
"github.com/appleboy/CodeGPT/prompt"
109
"github.com/appleboy/CodeGPT/util"
1110

@@ -19,12 +18,15 @@ import (
1918
var maxTokens int
2019

2120
func init() {
22-
reviewCmd.Flags().IntVar(&diffUnified, "diff_unified", 3, "generate diffs with <n> lines of context, default is 3")
23-
reviewCmd.Flags().IntVar(&maxTokens, "max_tokens", 300, "the maximum number of tokens to generate in the chat completion.")
21+
reviewCmd.Flags().IntVar(&diffUnified, "diff_unified", 3,
22+
"generate diffs with <n> lines of context, default is 3")
23+
reviewCmd.Flags().IntVar(&maxTokens, "max_tokens", 300,
24+
"the maximum number of tokens to generate in the chat completion.")
2425
reviewCmd.Flags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
2526
reviewCmd.Flags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
2627
reviewCmd.Flags().StringSliceVar(&excludeList, "exclude_list", []string{}, "exclude file from git diff command")
27-
reviewCmd.Flags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
28+
reviewCmd.Flags().BoolVar(&commitAmend, "amend", false,
29+
"replace the tip of the current branch by creating a new commit.")
2830
}
2931

3032
var reviewCmd = &cobra.Command{
@@ -53,24 +55,7 @@ var reviewCmd = &cobra.Command{
5355

5456
currentModel := viper.GetString("openai.model")
5557
color.Green("Code review your changes using " + currentModel + " model")
56-
client, err := openai.New(
57-
openai.WithToken(viper.GetString("openai.api_key")),
58-
openai.WithModel(viper.GetString("openai.model")),
59-
openai.WithOrgID(viper.GetString("openai.org_id")),
60-
openai.WithProxyURL(viper.GetString("openai.proxy")),
61-
openai.WithSocksURL(viper.GetString("openai.socks")),
62-
openai.WithBaseURL(viper.GetString("openai.base_url")),
63-
openai.WithTimeout(viper.GetDuration("openai.timeout")),
64-
openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
65-
openai.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
66-
openai.WithProvider(viper.GetString("openai.provider")),
67-
openai.WithSkipVerify(viper.GetBool("openai.skip_verify")),
68-
openai.WithHeaders(viper.GetStringSlice("openai.headers")),
69-
openai.WithApiVersion(viper.GetString("openai.api_version")),
70-
openai.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
71-
openai.WithFrequencyPenalty(float32(viper.GetFloat64("openai.frequency_penalty"))),
72-
openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
73-
)
58+
client, err := NewOpenAI()
7459
if err != nil {
7560
return err
7661
}
@@ -110,7 +95,8 @@ var reviewCmd = &cobra.Command{
11095
}
11196

11297
// translate a git commit message
113-
color.Cyan("We are trying to translate code review to " + prompt.GetLanguage(viper.GetString("output.lang")) + " language")
98+
color.Cyan("we are trying to translate code review to " +
99+
prompt.GetLanguage(viper.GetString("output.lang")) + " language")
114100
resp, err := client.Completion(cmd.Context(), out)
115101
if err != nil {
116102
return err

git/git.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ func (c *Command) InstallHook() error {
170170

171171
target := path.Join(strings.TrimSpace(string(hookPath)), HookPrepareCommitMessageTemplate)
172172
if file.IsFile(target) {
173-
return errors.New("hook file prepare-commit-msg exist.")
173+
return errors.New("hook file prepare-commit-msg exist")
174174
}
175175

176176
content, err := util.GetTemplateByBytes(HookPrepareCommitMessageTemplate, nil)
177177
if err != nil {
178178
return err
179179
}
180180

181-
return os.WriteFile(target, content, 0o755)
181+
return os.WriteFile(target, content, 0o600)
182182
}
183183

184184
func (c *Command) UninstallHook() error {
@@ -189,7 +189,7 @@ func (c *Command) UninstallHook() error {
189189

190190
target := path.Join(strings.TrimSpace(string(hookPath)), HookPrepareCommitMessageTemplate)
191191
if !file.IsFile(target) {
192-
return errors.New("hook file prepare-commit-msg is not exist.")
192+
return errors.New("hook file prepare-commit-msg is not exist")
193193
}
194194
return os.Remove(target)
195195
}

0 commit comments

Comments
 (0)