Add github actions:

This commit is contained in:
mudler
2024-04-11 13:05:35 +02:00
parent d237e17719
commit fa39ae93f1
4 changed files with 193 additions and 7 deletions

91
external/githubissuelabeler.go vendored Normal file
View File

@@ -0,0 +1,91 @@
package external
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/v61/github"
"github.com/mudler/local-agent-framework/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
type GithubIssuesLabeler struct {
token string
availableLabels []string
context context.Context
client *github.Client
}
func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *GithubIssuesLabeler {
client := github.NewClient(nil).WithAuthToken(config["token"])
// Get available labels
availableLabels := []string{"bug", "enhancement"}
if config["availableLabels"] != "" {
availableLabels = strings.Split(config["availableLabels"], ",")
}
return &GithubIssuesLabeler{
client: client,
token: config["token"],
context: ctx,
availableLabels: availableLabels,
}
}
func (g *GithubIssuesLabeler) Run(params action.ActionParams) (string, error) {
result := struct {
Repository string `json:"repository"`
Owner string `json:"owner"`
Label string `json:"label"`
IssueNumber int `json:"issue_number"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
labels, _, err := g.client.Issues.AddLabelsToIssue(g.context, result.Owner, result.Repository, result.IssueNumber, []string{result.Label})
//labelsNames := []string{}
for _, l := range labels {
fmt.Println("Label added:", l.Name)
//labelsNames = append(labelsNames, l.GetName())
}
resultString := fmt.Sprintf("Added label '%s' to issue %d in repository %s/%s", result.Label, result.IssueNumber, result.Owner, result.Repository)
if err != nil {
resultString = fmt.Sprintf("Error adding label '%s' to issue %d in repository %s/%s: %v", result.Label, result.IssueNumber, result.Owner, result.Repository, err)
}
return resultString, err
}
func (g *GithubIssuesLabeler) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "add_label_to_github_issue",
Description: "Add a label to a Github issue.",
Properties: map[string]jsonschema.Definition{
"issue_number": {
Type: jsonschema.Number,
Description: "The number of the issue to add the label to.",
},
"repository": {
Type: jsonschema.String,
Description: "The repository to add the label to.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"label": {
Type: jsonschema.String,
Description: "The label to add to the issue.",
Enum: g.availableLabels,
},
},
Required: []string{"issue_number", "repository", "owner", "label"},
}
}

82
external/githubissueopener.go vendored Normal file
View File

@@ -0,0 +1,82 @@
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 GithubIssuesOpener struct {
token string
context context.Context
client *github.Client
}
func NewGithubIssueOpener(ctx context.Context, config map[string]string) *GithubIssuesOpener {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesOpener{
client: client,
token: config["token"],
context: ctx,
}
}
func (g *GithubIssuesOpener) Run(params action.ActionParams) (string, error) {
result := struct {
Title string `json:"title"`
Body string `json:"body"`
Repository string `json:"repository"`
Owner string `json:"owner"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return "", err
}
issue := &github.IssueRequest{
Title: &result.Title,
Body: &result.Body,
}
resultString := ""
createdIssue, _, err := g.client.Issues.Create(g.context, result.Owner, result.Repository, issue)
if err != nil {
resultString = fmt.Sprintf("Error creating issue: %v", err)
} else {
resultString = fmt.Sprintf("Created issue %d in repository %s/%s", createdIssue.GetNumber(), result.Owner, result.Repository)
}
return resultString, err
}
func (g *GithubIssuesOpener) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "create_github_issue",
Description: "Create a new issue on a GitHub repository.",
Properties: map[string]jsonschema.Definition{
"body": {
Type: jsonschema.String,
Description: "The number of the issue to add the label to.",
},
"title": {
Type: jsonschema.String,
Description: "The title of the issue.",
},
"owner": {
Type: jsonschema.String,
Description: "The owner of the repository.",
},
"repository": {
Type: jsonschema.String,
Description: "The repository to create the issue in.",
},
},
Required: []string{"title", "body", "owner", "repository"},
}
}