Add github actions to upload and get files, update github dep
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/mudler/LocalAgent/pkg/xlog"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v61/github"
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/mudler/LocalAgent/pkg/xlog"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
|
||||
107
services/actions/githubrepositorycreateupdatecontent.go
Normal file
107
services/actions/githubrepositorycreateupdatecontent.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
type GithubRepositoryCreateOrUpdateContent struct {
|
||||
token, repository, owner, customActionName string
|
||||
context context.Context
|
||||
client *github.Client
|
||||
}
|
||||
|
||||
func NewGithubRepositoryCreateOrUpdateContent(ctx context.Context, config map[string]string) *GithubRepositoryCreateOrUpdateContent {
|
||||
client := github.NewClient(nil).WithAuthToken(config["token"])
|
||||
|
||||
return &GithubRepositoryCreateOrUpdateContent{
|
||||
client: client,
|
||||
token: config["token"],
|
||||
repository: config["repository"],
|
||||
owner: config["owner"],
|
||||
customActionName: config["customActionName"],
|
||||
context: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubRepositoryCreateOrUpdateContent) Run(ctx context.Context, params action.ActionParams) (action.ActionResult, error) {
|
||||
result := struct {
|
||||
Path string `json:"path"`
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
Content string `json:"content"`
|
||||
}{}
|
||||
err := params.Unmarshal(&result)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
|
||||
return action.ActionResult{}, err
|
||||
}
|
||||
|
||||
if g.repository != "" && g.owner != "" {
|
||||
result.Repository = g.repository
|
||||
result.Owner = g.owner
|
||||
}
|
||||
|
||||
fileContent, _, err := g.client.Repositories.CreateFile(g.context, result.Owner, result.Repository, result.Path, &github.RepositoryContentFileOptions{
|
||||
Content: []byte(result.Content),
|
||||
})
|
||||
if err != nil {
|
||||
resultString := fmt.Sprintf("Error creating content : %v", err)
|
||||
return action.ActionResult{Result: resultString}, err
|
||||
}
|
||||
|
||||
return action.ActionResult{Result: fmt.Sprintf("File created/updated: %s\n", fileContent.GetURL())}, err
|
||||
}
|
||||
|
||||
func (g *GithubRepositoryCreateOrUpdateContent) Definition() action.ActionDefinition {
|
||||
actionName := "github_repository_create_or_update_content"
|
||||
actionDescription := "Create or update a file in a GitHub repository"
|
||||
if g.customActionName != "" {
|
||||
actionName = g.customActionName
|
||||
}
|
||||
if g.repository != "" && g.owner != "" {
|
||||
return action.ActionDefinition{
|
||||
Name: action.ActionDefinitionName(actionName),
|
||||
Description: actionDescription,
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"path": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The path to the file or directory",
|
||||
},
|
||||
"content": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The content to create/update",
|
||||
},
|
||||
},
|
||||
Required: []string{"path", "content"},
|
||||
}
|
||||
}
|
||||
return action.ActionDefinition{
|
||||
Name: action.ActionDefinitionName(actionName),
|
||||
Description: actionDescription,
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"path": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The path to the file or directory",
|
||||
},
|
||||
"repository": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The repository to search in",
|
||||
},
|
||||
"owner": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The owner of the repository",
|
||||
},
|
||||
"content": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The content to create/update",
|
||||
},
|
||||
},
|
||||
Required: []string{"path", "repository", "owner", "content"},
|
||||
}
|
||||
}
|
||||
106
services/actions/githubrepositorygetcontent.go
Normal file
106
services/actions/githubrepositorygetcontent.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/go-github/v69/github"
|
||||
"github.com/mudler/LocalAgent/core/action"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
type GithubRepositoryGetContent struct {
|
||||
token, repository, owner, customActionName string
|
||||
context context.Context
|
||||
client *github.Client
|
||||
}
|
||||
|
||||
func NewGithubRepositoryGetContent(ctx context.Context, config map[string]string) *GithubRepositoryGetContent {
|
||||
client := github.NewClient(nil).WithAuthToken(config["token"])
|
||||
|
||||
return &GithubRepositoryGetContent{
|
||||
client: client,
|
||||
token: config["token"],
|
||||
repository: config["repository"],
|
||||
owner: config["owner"],
|
||||
customActionName: config["customActionName"],
|
||||
context: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubRepositoryGetContent) Run(ctx context.Context, params action.ActionParams) (action.ActionResult, error) {
|
||||
result := struct {
|
||||
Path string `json:"path"`
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
}{}
|
||||
err := params.Unmarshal(&result)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
|
||||
return action.ActionResult{}, err
|
||||
}
|
||||
|
||||
if g.repository != "" && g.owner != "" {
|
||||
result.Repository = g.repository
|
||||
result.Owner = g.owner
|
||||
}
|
||||
|
||||
fileContent, directoryContent, _, err := g.client.Repositories.GetContents(g.context, result.Owner, result.Repository, result.Path, nil)
|
||||
if err != nil {
|
||||
resultString := fmt.Sprintf("Error getting content : %v", err)
|
||||
return action.ActionResult{Result: resultString}, err
|
||||
}
|
||||
|
||||
if len(directoryContent) > 0 {
|
||||
resultString := fmt.Sprintf("Directory found: %s\n", result.Path)
|
||||
for _, f := range directoryContent {
|
||||
resultString += fmt.Sprintf("File: %s\n", f.GetName())
|
||||
}
|
||||
return action.ActionResult{Result: resultString}, err
|
||||
}
|
||||
|
||||
content := fileContent.Content
|
||||
|
||||
return action.ActionResult{Result: fmt.Sprintf("File %s\nContent:%s\n", result.Path, content)}, err
|
||||
}
|
||||
|
||||
func (g *GithubRepositoryGetContent) Definition() action.ActionDefinition {
|
||||
actionName := "get_github_repository_content"
|
||||
actionDescription := "Get content of a file or directory in a github repository"
|
||||
if g.customActionName != "" {
|
||||
actionName = g.customActionName
|
||||
}
|
||||
if g.repository != "" && g.owner != "" {
|
||||
return action.ActionDefinition{
|
||||
Name: action.ActionDefinitionName(actionName),
|
||||
Description: actionDescription,
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"path": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The path to the file or directory",
|
||||
},
|
||||
},
|
||||
Required: []string{"path"},
|
||||
}
|
||||
}
|
||||
return action.ActionDefinition{
|
||||
Name: action.ActionDefinitionName(actionName),
|
||||
Description: actionDescription,
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"path": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The path to the file or directory",
|
||||
},
|
||||
"repository": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The repository to search in",
|
||||
},
|
||||
"owner": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The owner of the repository",
|
||||
},
|
||||
},
|
||||
Required: []string{"path", "repository", "owner"},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user