-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AI provider and commit message with AI, update dependencies (#55)
* commit: Add AI-powered commit message generation Go commit message: ``` Add AI-powered commit message generation This commit adds AI-powered commit message generation using the OneAPI server. The commit message is generated using the OneAPI token and the AI_PROVIDER environment variable. Features: - AI-powered commit message generation - Supports OneAPI server Bugs fixed: - None Refs: - OneAPI: https://github.com/songquanpeng/one-api ``` * Git commit message: Update Go version to 1.22 in various files * `.github/workflows/pull-request.yaml`: Update the Go version used in the \"Build\" job to 1.22. * `.github/workflows/release.yaml`: Update the Go version used in the \"Set up Go\" job to 1.22. * `cmd/argoworkflow/go.mod`: Update the Go version to 1.22. * `go.mod`: Update the Go version to 1.22. * `.github/workflows/pull-request.yaml`: Update the Go version used in the \"Build Argo Workflow Executor\" job to 1.22. * `.github/workflows/release.yaml`: Update the Go version used in the \"Set up Go\" job to 1.22. * Implement conventional git commit message * Update Dockerfile to use Go 1.22 --------- Co-authored-by: rick <[email protected]>
- Loading branch information
1 parent
d4969c9
commit d0b162e
Showing
12 changed files
with
195 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,10 @@ jobs: | |
name: Build | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Set up Go 1.18 | ||
- name: Set up go 1.22 | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
go-version: 1.22 | ||
id: go | ||
- name: Check out code | ||
uses: actions/[email protected] | ||
|
@@ -44,10 +44,10 @@ jobs: | |
build-argo-workflow-executor: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Set up Go 1.18 | ||
- name: Set up go 1.22 | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
go-version: 1.22 | ||
id: go | ||
- name: Check out code | ||
uses: actions/[email protected] | ||
|
@@ -60,10 +60,10 @@ jobs: | |
runs-on: ubuntu-20.04 | ||
if: github.ref != 'refs/heads/master' | ||
steps: | ||
- name: Set up Go 1.18 | ||
- name: Set up go 1.22 | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
go-version: 1.22 | ||
id: go | ||
- name: Check out code | ||
uses: actions/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ jobs: | |
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
go-version: 1.22 | ||
- name: Run GoReleaser | ||
uses: goreleaser/[email protected] | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.18 as builder | ||
FROM golang:1.22 as builder | ||
|
||
WORKDIR /workspace | ||
COPY cmd/ cmd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
"github.com/linuxsuren/gogit/pkg/oneapi" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type commitOption struct { | ||
provider string | ||
token string | ||
flags []string | ||
runtime fakeruntime.Execer | ||
} | ||
|
||
func newCommitCmd() *cobra.Command { | ||
opt := &commitOption{ | ||
runtime: fakeruntime.NewDefaultExecer(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "commit", | ||
RunE: opt.runE, | ||
PreRunE: opt.preRunE, | ||
Short: "Commit the current changes with AI", | ||
Long: `Commit the current changes with AI. | ||
The AI provider is defined by the environment variable AI_PROVIDER, | ||
and the token is defined by the environment variable ONEAPI_TOKEN.`, | ||
} | ||
cmd.Flags().StringSliceVarP(&opt.flags, "flag", "", []string{}, "The flags of the git commit command") | ||
return cmd | ||
} | ||
|
||
func (o *commitOption) preRunE(cmd *cobra.Command, args []string) (err error) { | ||
o.provider = os.Getenv("AI_PROVIDER") | ||
if o.provider == "" { | ||
err = fmt.Errorf("AI_PROVIDER is not set") | ||
} | ||
o.token = os.Getenv("ONEAPI_TOKEN") | ||
if o.token == "" { | ||
err = errors.Join(err, fmt.Errorf("ONEAPI_TOKEN is not set")) | ||
} | ||
return | ||
} | ||
|
||
func (o *commitOption) runE(cmd *cobra.Command, args []string) (err error) { | ||
var gitdiff string | ||
gitdiff, err = o.getGitDiff() | ||
|
||
payload := oneapi.NewChatPayload(fmt.Sprintf("Please write a conventional git commit message for the following git diff:\n%s", gitdiff), "chatglm_std") | ||
|
||
var body []byte | ||
if body, err = json.Marshal(payload); err != nil { | ||
return | ||
} | ||
|
||
var req *http.Request | ||
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/chat/completions", o.provider), io.NopCloser(bytes.NewReader(body))) | ||
if err != nil { | ||
return | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.token)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
var resp *http.Response | ||
resp, err = http.DefaultClient.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
// read the body and parse to oenapi.ChatResponse | ||
var chatResp oneapi.ChatResponse | ||
if err = json.NewDecoder(resp.Body).Decode(&chatResp); err != nil { | ||
return | ||
} | ||
|
||
var tempF *os.File | ||
if tempF, err = os.CreateTemp(os.TempDir(), "msg"); err != nil { | ||
return | ||
} | ||
|
||
content := chatResp.Choices[0].Message.Content | ||
// convert \n to new line | ||
content = strings.ReplaceAll(content, "\\n", "\n") | ||
if _, err = io.WriteString(tempF, content); err != nil { | ||
return | ||
} | ||
if err = tempF.Close(); err != nil { | ||
return | ||
} | ||
|
||
cmd.Println("start to commit with", tempF.Name()) | ||
|
||
var gitExe string | ||
if gitExe, err = o.runtime.LookPath("git"); err != nil { | ||
return | ||
} | ||
|
||
if err = o.runtime.RunCommand(gitExe, "add", "."); err != nil { | ||
return | ||
} | ||
|
||
opts := []string{"git", "commit", "--edit", "--file", tempF.Name()} | ||
for _, flag := range o.flags { | ||
opts = append(opts, flag) | ||
} | ||
|
||
err = syscall.Exec(gitExe, opts, append(os.Environ(), "GIT_EDITOR=vim")) | ||
} | ||
return | ||
} | ||
|
||
func (o *commitOption) getGitDiff() (diff string, err error) { | ||
// run command git diff and get the output | ||
diff, err = o.runtime.RunCommandAndReturn("git", ".", "diff") | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
go 1.18 | ||
go 1.22 | ||
|
||
use . | ||
use ./cmd/argoworkflow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package oneapi | ||
|
||
type ChatMessage struct { | ||
Content string `json:"content"` | ||
Role string `json:"role"` | ||
} | ||
|
||
type ChatPayload struct { | ||
Messages []ChatMessage `json:"messages"` | ||
Model string `json:"model"` | ||
} | ||
|
||
type ChatResponse struct { | ||
Created int64 `json:"created"` | ||
ID string `json:"id"` | ||
Object string `json:"object"` | ||
Usage map[string]int `json:"usage"` | ||
Choices []ChatResponseChoice `json:"choices"` | ||
} | ||
|
||
type ChatResponseChoice struct { | ||
FinishReason string `json:"finish_reason"` | ||
Index int `json:"index"` | ||
Message ChatMessage `json:"message"` | ||
} | ||
|
||
func NewChatPayload(message string, model string) ChatPayload { | ||
return ChatPayload{ | ||
Messages: []ChatMessage{{Content: message, Role: "user"}}, | ||
Model: model, | ||
} | ||
} |