fix(actions): respect running context

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-03-26 22:58:52 +01:00
parent 6e888f6008
commit 0f2731f9e8
11 changed files with 29 additions and 47 deletions

View File

@@ -94,23 +94,23 @@ func Action(name string, config map[string]string, pool *state.AgentPool) (types
case ActionSearch:
a = actions.NewSearch(config)
case ActionGithubIssueLabeler:
a = actions.NewGithubIssueLabeler(context.Background(), config)
a = actions.NewGithubIssueLabeler(config)
case ActionGithubIssueOpener:
a = actions.NewGithubIssueOpener(context.Background(), config)
a = actions.NewGithubIssueOpener(config)
case ActionGithubIssueCloser:
a = actions.NewGithubIssueCloser(context.Background(), config)
a = actions.NewGithubIssueCloser(config)
case ActionGithubIssueSearcher:
a = actions.NewGithubIssueSearch(context.Background(), config)
a = actions.NewGithubIssueSearch(config)
case ActionGithubIssueReader:
a = actions.NewGithubIssueReader(context.Background(), config)
a = actions.NewGithubIssueReader(config)
case ActionGithubIssueCommenter:
a = actions.NewGithubIssueCommenter(context.Background(), config)
a = actions.NewGithubIssueCommenter(config)
case ActionGithubRepositoryGet:
a = actions.NewGithubRepositoryGetContent(context.Background(), config)
a = actions.NewGithubRepositoryGetContent(config)
case ActionGithubRepositoryCreateOrUpdate:
a = actions.NewGithubRepositoryCreateOrUpdateContent(context.Background(), config)
a = actions.NewGithubRepositoryCreateOrUpdateContent(config)
case ActionGithubREADME:
a = actions.NewGithubRepositoryREADME(context.Background(), config)
a = actions.NewGithubRepositoryREADME(config)
case ActionScraper:
a = actions.NewScraper(config)
case ActionWikipedia:

View File

@@ -11,11 +11,10 @@ import (
type GithubIssuesCloser struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubIssueCloser(ctx context.Context, config map[string]string) *GithubIssuesCloser {
func NewGithubIssueCloser(config map[string]string) *GithubIssuesCloser {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesCloser{
client: client,
@@ -23,7 +22,6 @@ func NewGithubIssueCloser(ctx context.Context, config map[string]string) *Github
repository: config["repository"],
owner: config["owner"],
customActionName: config["customActionName"],
context: ctx,
}
}
@@ -60,7 +58,7 @@ func (g *GithubIssuesCloser) Run(ctx context.Context, params types.ActionParams)
// return "", err
// }
_, _, err = g.client.Issues.Edit(g.context, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{
_, _, err = g.client.Issues.Edit(ctx, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{
State: github.String("closed"),
})

View File

@@ -11,11 +11,10 @@ import (
type GithubIssuesCommenter struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubIssueCommenter(ctx context.Context, config map[string]string) *GithubIssuesCommenter {
func NewGithubIssueCommenter(config map[string]string) *GithubIssuesCommenter {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesCommenter{
@@ -24,7 +23,6 @@ func NewGithubIssueCommenter(ctx context.Context, config map[string]string) *Git
customActionName: config["customActionName"],
repository: config["repository"],
owner: config["owner"],
context: ctx,
}
}
@@ -45,7 +43,7 @@ func (g *GithubIssuesCommenter) Run(ctx context.Context, params types.ActionPara
result.Owner = g.owner
}
_, _, err = g.client.Issues.CreateComment(g.context, result.Owner, result.Repository, result.IssueNumber,
_, _, err = g.client.Issues.CreateComment(ctx, result.Owner, result.Repository, result.IssueNumber,
&github.IssueComment{
Body: &result.Comment,
})

View File

@@ -14,11 +14,10 @@ import (
type GithubIssuesLabeler struct {
token, repository, owner, customActionName string
availableLabels []string
context context.Context
client *github.Client
}
func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *GithubIssuesLabeler {
func NewGithubIssueLabeler(config map[string]string) *GithubIssuesLabeler {
client := github.NewClient(nil).WithAuthToken(config["token"])
// Get available labels
@@ -34,7 +33,6 @@ func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *Githu
customActionName: config["customActionName"],
repository: config["repository"],
owner: config["owner"],
context: ctx,
availableLabels: availableLabels,
}
}
@@ -56,7 +54,7 @@ func (g *GithubIssuesLabeler) Run(ctx context.Context, params types.ActionParams
result.Owner = g.owner
}
labels, _, err := g.client.Issues.AddLabelsToIssue(g.context, result.Owner, result.Repository, result.IssueNumber, []string{result.Label})
labels, _, err := g.client.Issues.AddLabelsToIssue(ctx, result.Owner, result.Repository, result.IssueNumber, []string{result.Label})
//labelsNames := []string{}
for _, l := range labels {
xlog.Info("Label added", "label", l.Name)

View File

@@ -11,11 +11,10 @@ import (
type GithubIssuesOpener struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubIssueOpener(ctx context.Context, config map[string]string) *GithubIssuesOpener {
func NewGithubIssueOpener(config map[string]string) *GithubIssuesOpener {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesOpener{
@@ -24,7 +23,6 @@ func NewGithubIssueOpener(ctx context.Context, config map[string]string) *Github
repository: config["repository"],
owner: config["owner"],
customActionName: config["customActionName"],
context: ctx,
}
}
@@ -53,7 +51,7 @@ func (g *GithubIssuesOpener) Run(ctx context.Context, params types.ActionParams)
}
resultString := ""
createdIssue, _, err := g.client.Issues.Create(g.context, result.Owner, result.Repository, issue)
createdIssue, _, err := g.client.Issues.Create(ctx, result.Owner, result.Repository, issue)
if err != nil {
resultString = fmt.Sprintf("Error creating issue: %v", err)
} else {

View File

@@ -11,11 +11,10 @@ import (
type GithubIssuesReader struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubIssueReader(ctx context.Context, config map[string]string) *GithubIssuesReader {
func NewGithubIssueReader(config map[string]string) *GithubIssuesReader {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssuesReader{
@@ -24,7 +23,6 @@ func NewGithubIssueReader(ctx context.Context, config map[string]string) *Github
customActionName: config["customActionName"],
repository: config["repository"],
owner: config["owner"],
context: ctx,
}
}
@@ -45,7 +43,7 @@ func (g *GithubIssuesReader) Run(ctx context.Context, params types.ActionParams)
result.Owner = g.owner
}
issue, _, err := g.client.Issues.Get(g.context, result.Owner, result.Repository, result.IssueNumber)
issue, _, err := g.client.Issues.Get(ctx, result.Owner, result.Repository, result.IssueNumber)
if err == nil && issue != nil {
return types.ActionResult{
Result: fmt.Sprintf(

View File

@@ -12,11 +12,10 @@ import (
type GithubIssueSearch struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubIssueSearch(ctx context.Context, config map[string]string) *GithubIssueSearch {
func NewGithubIssueSearch(config map[string]string) *GithubIssueSearch {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssueSearch{
@@ -25,7 +24,6 @@ func NewGithubIssueSearch(ctx context.Context, config map[string]string) *Github
repository: config["repository"],
owner: config["owner"],
customActionName: config["customActionName"],
context: ctx,
}
}
@@ -49,7 +47,7 @@ func (g *GithubIssueSearch) Run(ctx context.Context, params types.ActionParams)
query := fmt.Sprintf("%s in:%s user:%s", result.Query, result.Repository, result.Owner)
resultString := ""
issues, _, err := g.client.Search.Issues(g.context, query, &github.SearchOptions{
issues, _, err := g.client.Search.Issues(ctx, query, &github.SearchOptions{
ListOptions: github.ListOptions{PerPage: 5},
Order: "desc",
//Sort: "created",

View File

@@ -11,11 +11,10 @@ import (
type GithubRepositoryCreateOrUpdateContent struct {
token, repository, owner, customActionName, defaultBranch, commitAuthor, commitMail string
context context.Context
client *github.Client
}
func NewGithubRepositoryCreateOrUpdateContent(ctx context.Context, config map[string]string) *GithubRepositoryCreateOrUpdateContent {
func NewGithubRepositoryCreateOrUpdateContent(config map[string]string) *GithubRepositoryCreateOrUpdateContent {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubRepositoryCreateOrUpdateContent{
@@ -27,7 +26,6 @@ func NewGithubRepositoryCreateOrUpdateContent(ctx context.Context, config map[st
commitAuthor: config["commitAuthor"],
commitMail: config["commitMail"],
defaultBranch: config["defaultBranch"],
context: ctx,
}
}
@@ -65,12 +63,12 @@ func (g *GithubRepositoryCreateOrUpdateContent) Run(ctx context.Context, params
}
var sha *string
c, _, _, _ := g.client.Repositories.GetContents(g.context, result.Owner, result.Repository, result.Path, nil)
c, _, _, _ := g.client.Repositories.GetContents(ctx, result.Owner, result.Repository, result.Path, nil)
if c != nil {
sha = c.SHA
}
fileContent, _, err := g.client.Repositories.CreateFile(g.context, result.Owner, result.Repository, result.Path, &github.RepositoryContentFileOptions{
fileContent, _, err := g.client.Repositories.CreateFile(ctx, result.Owner, result.Repository, result.Path, &github.RepositoryContentFileOptions{
Message: &result.CommitMessage,
SHA: sha,
Committer: &github.CommitAuthor{

View File

@@ -11,11 +11,10 @@ import (
type GithubRepositoryGetContent struct {
token, repository, owner, customActionName string
context context.Context
client *github.Client
}
func NewGithubRepositoryGetContent(ctx context.Context, config map[string]string) *GithubRepositoryGetContent {
func NewGithubRepositoryGetContent(config map[string]string) *GithubRepositoryGetContent {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubRepositoryGetContent{
@@ -24,7 +23,6 @@ func NewGithubRepositoryGetContent(ctx context.Context, config map[string]string
repository: config["repository"],
owner: config["owner"],
customActionName: config["customActionName"],
context: ctx,
}
}
@@ -46,7 +44,7 @@ func (g *GithubRepositoryGetContent) Run(ctx context.Context, params types.Actio
result.Owner = g.owner
}
fileContent, directoryContent, _, err := g.client.Repositories.GetContents(g.context, result.Owner, result.Repository, result.Path, nil)
fileContent, directoryContent, _, err := g.client.Repositories.GetContents(ctx, result.Owner, result.Repository, result.Path, nil)
if err != nil {
resultString := fmt.Sprintf("Error getting content : %v", err)
return types.ActionResult{Result: resultString}, err

View File

@@ -11,18 +11,16 @@ import (
type GithubRepositoryREADME struct {
token, customActionName string
context context.Context
client *github.Client
}
func NewGithubRepositoryREADME(ctx context.Context, config map[string]string) *GithubRepositoryREADME {
func NewGithubRepositoryREADME(config map[string]string) *GithubRepositoryREADME {
client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubRepositoryREADME{
client: client,
token: config["token"],
customActionName: config["customActionName"],
context: ctx,
}
}
@@ -37,7 +35,7 @@ func (g *GithubRepositoryREADME) Run(ctx context.Context, params types.ActionPar
return types.ActionResult{}, err
}
fileContent, _, err := g.client.Repositories.GetReadme(g.context, result.Owner, result.Repository, &github.RepositoryContentGetOptions{})
fileContent, _, err := g.client.Repositories.GetReadme(ctx, result.Owner, result.Repository, &github.RepositoryContentGetOptions{})
if err != nil {
resultString := fmt.Sprintf("Error getting content : %v", err)
return types.ActionResult{Result: resultString}, err