Add issue closer

This commit is contained in:
mudler
2024-04-11 17:05:41 +02:00
parent fa39ae93f1
commit 3295942e59
6 changed files with 143 additions and 27 deletions

90
external/githubissuecloser.go vendored Normal file
View File

@@ -0,0 +1,90 @@
package external
import (
"context"
"fmt"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
type GithubIssuesCloser struct {
token string
context context.Context
client *github.Client
}
func NewGithubIssueCloser(ctx context.Context, config map[string]string) *GithubIssuesCloser {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesCloser{
client: client,
token: config["token"],
context: ctx,
}
}
func (g *GithubIssuesCloser) Run(params action.ActionParams) (string, error) {
result := struct {
Repository string `json:"repository"`
Owner string `json:"owner"`
IssueNumber int `json:"issue_number"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
// _, _, err = g.client.Issues.CreateComment(
// g.context,
// result.Owner, result.Repository,
// result.IssueNumber, &github.IssueComment{
// Body: &result.Text,
// },
// )
// if err != nil {
// fmt.Printf("error: %v", err)
// return "", err
// }
_, _, err = g.client.Issues.Edit(g.context, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{
State: github.String("closed"),
})
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
resultString := fmt.Sprintf("Closed issue %d in repository %s/%s", result.IssueNumber, result.Owner, result.Repository)
if err != nil {
resultString = fmt.Sprintf("Error closing issue %d in repository %s/%s: %v", result.IssueNumber, result.Owner, result.Repository, err)
}
return resultString, err
}
func (g *GithubIssuesCloser) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "close_github_issue",
Description: "Closes a Github issue.",
Properties: map[string]jsonschema.Definition{
"repository": {
Type: jsonschema.String,
Description: "The repository to add the label to.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"issue_number": {
Type: jsonschema.Number,
Description: "The issue number to close",
},
},
Required: []string{"issue_number", "repository", "owner"},
}
}

View File

@@ -66,7 +66,7 @@ func (g *GithubIssuesLabeler) Run(params action.ActionParams) (string, error) {
func (g *GithubIssuesLabeler) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "add_label_to_github_issue",
Description: "Add a label to a Github issue.",
Description: "Add a label to a Github issue. You might want to assign labels to issues to categorize them.",
Properties: map[string]jsonschema.Definition{
"issue_number": {
Type: jsonschema.Number,

View File

@@ -28,7 +28,7 @@ func NewGithubIssueOpener(ctx context.Context, config map[string]string) *Github
func (g *GithubIssuesOpener) Run(params action.ActionParams) (string, error) {
result := struct {
Title string `json:"title"`
Body string `json:"body"`
Body string `json:"text"`
Repository string `json:"repository"`
Owner string `json:"owner"`
}{}
@@ -60,9 +60,9 @@ func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
Name: "create_github_issue",
Description: "Create a new issue on a GitHub repository.",
Properties: map[string]jsonschema.Definition{
"body": {
"text": {
Type: jsonschema.String,
Description: "The number of the issue to add the label to.",
Description: "The text of the new issue",
},
"title": {
Type: jsonschema.String,
@@ -74,9 +74,9 @@ func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
},
"repository": {
Type: jsonschema.String,
Description: "The repository to create the issue in.",
Description: "The repository where to create the issue.",
},
},
Required: []string{"title", "body", "owner", "repository"},
Required: []string{"title", "text", "owner", "repository"},
}
}