mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
fix: explain why a workflow cannot be used to run a job (#666)
- report back to the Forgejo instance - log Forgejo runner side in debug mode - display the workflow with line number to facilitate matching with errors - split the error into multiple lines if possible  Refs forgejo/act#170 Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/666 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
d23b354cbc
commit
6e6ebbc981
2 changed files with 36 additions and 1 deletions
|
@ -183,6 +183,22 @@ func (r *Runner) Run(ctx context.Context, task *runnerv1.Task) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logAndReport(reporter *report.Reporter, message string, args ...any) {
|
||||||
|
log.Debugf(message, args...)
|
||||||
|
reporter.Logf(message, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func explainFailedGenerateWorkflow(task *runnerv1.Task, log func(message string, args ...any), err error) error {
|
||||||
|
for n, line := range strings.Split(string(task.WorkflowPayload), "\n") {
|
||||||
|
log("%d: %s", n+1, line)
|
||||||
|
}
|
||||||
|
log("Errors were found and although they tend to be cryptic the line number they refer to gives a hint as to where the problem might be.")
|
||||||
|
for _, line := range strings.Split(err.Error(), "\n") {
|
||||||
|
log("%s", line)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("the workflow file is not usable")
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.Reporter) (err error) {
|
func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.Reporter) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
|
@ -194,7 +210,9 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||||
|
|
||||||
workflow, jobID, err := generateWorkflow(task)
|
workflow, jobID, err := generateWorkflow(task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return explainFailedGenerateWorkflow(task, func(message string, args ...any) {
|
||||||
|
logAndReport(reporter, message, args...)
|
||||||
|
}, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
plan, err := model.CombineWorkflowPlanner(workflow).PlanJob(jobID)
|
plan, err := model.CombineWorkflowPlanner(workflow).PlanJob(jobID)
|
||||||
|
|
|
@ -2,13 +2,30 @@ package run
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
runnerv1 "code.forgejo.org/forgejo/actions-proto/runner/v1"
|
||||||
"runner.forgejo.org/internal/pkg/labels"
|
"runner.forgejo.org/internal/pkg/labels"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestExplainFailedGenerateWorkflow(t *testing.T) {
|
||||||
|
logged := ""
|
||||||
|
log := func(message string, args ...any) {
|
||||||
|
logged += fmt.Sprintf(message, args...) + "\n"
|
||||||
|
}
|
||||||
|
task := &runnerv1.Task{
|
||||||
|
WorkflowPayload: []byte("on: [push]\njobs:\n"),
|
||||||
|
}
|
||||||
|
generateWorkflowError := errors.New("message 1\nmessage 2")
|
||||||
|
err := explainFailedGenerateWorkflow(task, log, generateWorkflowError)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Equal(t, "1: on: [push]\n2: jobs:\n3: \nErrors were found and although they tend to be cryptic the line number they refer to gives a hint as to where the problem might be.\nmessage 1\nmessage 2\n", logged)
|
||||||
|
}
|
||||||
|
|
||||||
func TestLabelUpdate(t *testing.T) {
|
func TestLabelUpdate(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ls := labels.Labels{}
|
ls := labels.Labels{}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue