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: case ActionSearch:
a = actions.NewSearch(config) a = actions.NewSearch(config)
case ActionGithubIssueLabeler: case ActionGithubIssueLabeler:
a = actions.NewGithubIssueLabeler(context.Background(), config) a = actions.NewGithubIssueLabeler(config)
case ActionGithubIssueOpener: case ActionGithubIssueOpener:
a = actions.NewGithubIssueOpener(context.Background(), config) a = actions.NewGithubIssueOpener(config)
case ActionGithubIssueCloser: case ActionGithubIssueCloser:
a = actions.NewGithubIssueCloser(context.Background(), config) a = actions.NewGithubIssueCloser(config)
case ActionGithubIssueSearcher: case ActionGithubIssueSearcher:
a = actions.NewGithubIssueSearch(context.Background(), config) a = actions.NewGithubIssueSearch(config)
case ActionGithubIssueReader: case ActionGithubIssueReader:
a = actions.NewGithubIssueReader(context.Background(), config) a = actions.NewGithubIssueReader(config)
case ActionGithubIssueCommenter: case ActionGithubIssueCommenter:
a = actions.NewGithubIssueCommenter(context.Background(), config) a = actions.NewGithubIssueCommenter(config)
case ActionGithubRepositoryGet: case ActionGithubRepositoryGet:
a = actions.NewGithubRepositoryGetContent(context.Background(), config) a = actions.NewGithubRepositoryGetContent(config)
case ActionGithubRepositoryCreateOrUpdate: case ActionGithubRepositoryCreateOrUpdate:
a = actions.NewGithubRepositoryCreateOrUpdateContent(context.Background(), config) a = actions.NewGithubRepositoryCreateOrUpdateContent(config)
case ActionGithubREADME: case ActionGithubREADME:
a = actions.NewGithubRepositoryREADME(context.Background(), config) a = actions.NewGithubRepositoryREADME(config)
case ActionScraper: case ActionScraper:
a = actions.NewScraper(config) a = actions.NewScraper(config)
case ActionWikipedia: case ActionWikipedia:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -12,11 +12,10 @@ import (
type GithubIssueSearch struct { type GithubIssueSearch struct {
token, repository, owner, customActionName string token, repository, owner, customActionName string
context context.Context
client *github.Client 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"]) client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubIssueSearch{ return &GithubIssueSearch{
@@ -25,7 +24,6 @@ func NewGithubIssueSearch(ctx context.Context, config map[string]string) *Github
repository: config["repository"], repository: config["repository"],
owner: config["owner"], owner: config["owner"],
customActionName: config["customActionName"], 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) query := fmt.Sprintf("%s in:%s user:%s", result.Query, result.Repository, result.Owner)
resultString := "" 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}, ListOptions: github.ListOptions{PerPage: 5},
Order: "desc", Order: "desc",
//Sort: "created", //Sort: "created",

View File

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

View File

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

View File

@@ -11,18 +11,16 @@ import (
type GithubRepositoryREADME struct { type GithubRepositoryREADME struct {
token, customActionName string token, customActionName string
context context.Context
client *github.Client 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"]) client := github.NewClient(nil).WithAuthToken(config["token"])
return &GithubRepositoryREADME{ return &GithubRepositoryREADME{
client: client, client: client,
token: config["token"], token: config["token"],
customActionName: config["customActionName"], customActionName: config["customActionName"],
context: ctx,
} }
} }
@@ -37,7 +35,7 @@ func (g *GithubRepositoryREADME) Run(ctx context.Context, params types.ActionPar
return types.ActionResult{}, err 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 { if err != nil {
resultString := fmt.Sprintf("Error getting content : %v", err) resultString := fmt.Sprintf("Error getting content : %v", err)
return types.ActionResult{Result: resultString}, err return types.ActionResult{Result: resultString}, err

View File

@@ -322,7 +322,7 @@ func (a *App) ExecuteAction(pool *state.AgentPool) func(c *fiber.Ctx) error {
return errorJSONMessage(c, err.Error()) return errorJSONMessage(c, err.Error())
} }
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Second) ctx, cancel := context.WithTimeout(c.Context(), 200*time.Second)
defer cancel() defer cancel()
res, err := a.Run(ctx, payload.Params) res, err := a.Run(ctx, payload.Params)