Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add own workflow model #1784

Merged
merged 43 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
683e657
Add own workflow model
qwerty287 May 29, 2023
c85fe84
Merge branch 'master' into workflow-model
qwerty287 May 29, 2023
11d79d1
fix tests
qwerty287 May 29, 2023
473175c
Fix lint
qwerty287 May 29, 2023
a2feac8
Fix lint
qwerty287 May 29, 2023
5ad86a5
Remove file
qwerty287 May 29, 2023
b43c5bc
Merge branch 'master' into workflow-model
qwerty287 May 30, 2023
a0ec798
use `Sync`
qwerty287 May 30, 2023
ab01351
some small improvements
qwerty287 May 30, 2023
783f337
Merge branch 'master' into workflow-model
qwerty287 May 31, 2023
10d2904
Merge branch 'master' into workflow-model
qwerty287 May 31, 2023
1b86518
Merge branch 'master' into workflow-model
qwerty287 Jun 1, 2023
d688878
Merge branch 'master' into workflow-model
qwerty287 Jun 3, 2023
e655110
Merge branch 'master' into workflow-model
qwerty287 Jun 4, 2023
6bdb5a5
Merge branch 'master' into workflow-model
qwerty287 Jun 5, 2023
1a21c9d
Merge branch 'master' into workflow-model
qwerty287 Jun 6, 2023
757c075
Fix pipeline
qwerty287 Jun 6, 2023
1c9a36b
Fix missing table
qwerty287 Jun 6, 2023
79417f1
Fix api client and ui
qwerty287 Jun 6, 2023
915fca3
Merge branch 'master' into workflow-model
qwerty287 Jun 6, 2023
c51149d
Fix tests
qwerty287 Jun 6, 2023
f6f056b
Review suggestions
qwerty287 Jun 6, 2023
be82d4c
Better naming
qwerty287 Jun 6, 2023
d6b9383
Merge branch 'master' into workflow-model
qwerty287 Jun 7, 2023
a439541
Merge branch 'master' into workflow-model
qwerty287 Jun 7, 2023
49619c1
gen swagger
qwerty287 Jun 9, 2023
7e6d840
Merge branch 'master' into workflow-model
qwerty287 Jun 9, 2023
2fd7326
Merge branch 'master' into workflow-model
6543 Jun 10, 2023
9cfc331
codformat & nits
6543 Jun 10, 2023
180a8be
rename to workflow
6543 Jun 10, 2023
06da1c3
refactor
6543 Jun 10, 2023
9c53119
fix session
6543 Jun 10, 2023
0bec66e
Merge branch 'master' into workflow-model
qwerty287 Jun 16, 2023
2b2887a
Merge branch 'master' into workflow-model
qwerty287 Jun 19, 2023
2198e6c
Merge branch 'master' into workflow-model
6543 Jun 19, 2023
af20d2b
Add GetTree test
qwerty287 Jun 19, 2023
3e0bb3d
Add Update and Load tests
qwerty287 Jun 19, 2023
dbd0aff
mv server/store/datastore/migration/0{19,20}_parent_steps_to_workflow…
6543 Jun 19, 2023
7a4a5f0
close transaction in any case
6543 Jun 19, 2023
f6aa0ee
Update server/forge/gitea/gitea_test.go
6543 Jun 20, 2023
1917a91
format
qwerty287 Jun 21, 2023
c0dcaf2
Merge branch 'master' into workflow-model
6543 Jun 21, 2023
fffe780
Merge branch 'master' into workflow-model
6543 Jun 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/pipeline/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func pipelinePs(c *cli.Context) error {
return err
}

for _, step := range pipeline.Steps {
for _, step := range pipeline.Workflows {
for _, child := range step.Children {
if err := tmpl.Execute(os.Stdout, child); err != nil {
return err
Expand Down
77 changes: 53 additions & 24 deletions cmd/server/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/docs/30-administration/10-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ Context prefix Woodpecker will use to publish status messages to SCM. You probab

Template for the status messages published to forges, uses [Go templates](https://pkg.go.dev/text/template) as template language.
Supported variables:

- `context`: Woodpecker's context (see `WOODPECKER_STATUS_CONTEXT`)
- `event`: the event which started the pipeline
- `pipeline`: the pipeline's name
- `workflow`: the workflow's name
- `owner`: the repo's owner
- `repo`: the repo's name

Expand Down
2 changes: 1 addition & 1 deletion pipeline/frontend/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func EnvVarSubst(yaml string, environ map[string]string) (string, error) {
}

// MetadataFromStruct return the metadata from a pipeline will run with.
func MetadataFromStruct(forge metadata.ServerForge, repo *model.Repo, pipeline, last *model.Pipeline, workflow *model.Step, link string) metadata.Metadata {
func MetadataFromStruct(forge metadata.ServerForge, repo *model.Repo, pipeline, last *model.Pipeline, workflow *model.Workflow, link string) metadata.Metadata {
host := link
uri, err := url.Parse(link)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions pipeline/frontend/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestMetadataFromStruct(t *testing.T) {
forge metadata.ServerForge
repo *model.Repo
pipeline, last *model.Pipeline
workflow *model.Step
workflow *model.Workflow
link string
expectedMetadata metadata.Metadata
expectedEnviron map[string]string
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestMetadataFromStruct(t *testing.T) {
repo: &model.Repo{FullName: "testUser/testRepo", Link: "https://gitea.com/testUser/testRepo", Clone: "https://gitea.com/testUser/testRepo.git", Branch: "main", IsSCMPrivate: true},
pipeline: &model.Pipeline{Number: 3},
last: &model.Pipeline{Number: 2},
workflow: &model.Step{Name: "hello"},
workflow: &model.Workflow{Name: "hello"},
link: "https://example.com",
expectedMetadata: metadata.Metadata{
Forge: metadata.Forge{Type: "gitea", URL: "https://gitea.com"},
Expand Down
10 changes: 4 additions & 6 deletions pipeline/stepBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"path/filepath"
"strings"

"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -53,7 +52,7 @@ type StepBuilder struct {
}

type Item struct {
Workflow *model.Step
Workflow *model.Workflow
Platform string
Labels map[string]string
DependsOn []string
Expand All @@ -79,8 +78,7 @@ func (b *StepBuilder) Build() ([]*Item, error) {
}

for _, axis := range axes {
workflow := &model.Step{
UUID: uuid.New().String(), // TODO(#1784): Remove once workflows are a separate entity in database
workflow := &model.Workflow{
PipelineID: b.Curr.ID,
PID: pidSequence,
State: model.StatusPending,
Expand Down Expand Up @@ -284,7 +282,6 @@ func (b *StepBuilder) toInternalRepresentation(parsed *yaml_types.Workflow, envi
func SetPipelineStepsOnPipeline(pipeline *model.Pipeline, pipelineItems []*Item) *model.Pipeline {
var pidSequence int
for _, item := range pipelineItems {
pipeline.Steps = append(pipeline.Steps, item.Workflow)
if pidSequence < item.Workflow.PID {
pidSequence = item.Workflow.PID
}
Expand All @@ -309,9 +306,10 @@ func SetPipelineStepsOnPipeline(pipeline *model.Pipeline, pipelineItems []*Item)
if item.Workflow.State == model.StatusSkipped {
step.State = model.StatusSkipped
}
pipeline.Steps = append(pipeline.Steps, step)
item.Workflow.Children = append(item.Workflow.Children, step)
}
}
pipeline.Workflows = append(pipeline.Workflows, item.Workflow)
}

return pipeline
Expand Down
9 changes: 3 additions & 6 deletions pipeline/stepBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,11 @@ steps:
if err != nil {
t.Fatal(err)
}
if len(pipeline.Steps) != 3 {
if len(pipeline.Workflows) != 1 {
t.Fatal("Should generate three in total")
}
if pipeline.Steps[1].PPID != 1 {
t.Fatal("Clone step should be a children of the stage")
}
if pipeline.Steps[2].PPID != 1 {
t.Fatal("Pipeline step should be a children of the stage")
if len(pipeline.Workflows[0].Children) != 2 {
t.Fatal("Workflow should have two children")
}
}

Expand Down
10 changes: 2 additions & 8 deletions server/api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ func GetPipeline(c *gin.Context) {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
steps, _ := _store.StepList(pl)
if pl.Steps, err = model.Tree(steps); err != nil {
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
Expand All @@ -172,12 +171,7 @@ func GetPipelineLast(c *gin.Context) {
return
}

steps, err := _store.StepList(pl)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
if pl.Steps, err = model.Tree(steps); err != nil {
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
Expand Down
5 changes: 3 additions & 2 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
"net/http"
"net/url"

shared_utils "github.com/woodpecker-ci/woodpecker/shared/utils"
"golang.org/x/oauth2"

shared_utils "github.com/woodpecker-ci/woodpecker/shared/utils"

"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/forge"
"github.com/woodpecker-ci/woodpecker/server/forge/bitbucket/internal"
Expand Down Expand Up @@ -215,7 +216,7 @@ func (c *config) Dir(_ context.Context, _ *model.User, _ *model.Repo, _ *model.P
}

// Status creates a pipeline status for the Bitbucket commit.
func (c *config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Step) error {
func (c *config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Workflow) error {
status := internal.PipelineStatus{
State: convertStatus(pipeline.Status),
Desc: common.GetPipelineStatusDescription(pipeline.Status),
Expand Down
4 changes: 2 additions & 2 deletions server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func Test_bitbucket(t *testing.T) {
})

g.It("Should update the status", func() {
err := c.Status(ctx, fakeUser, fakeRepo, fakePipeline, fakeStep)
err := c.Status(ctx, fakeUser, fakeRepo, fakePipeline, fakeWorkflow)
g.Assert(err).IsNil()
})

Expand Down Expand Up @@ -309,7 +309,7 @@ var (
Commit: "9ecad50",
}

fakeStep = &model.Step{
fakeWorkflow = &model.Workflow{
Name: "test",
State: model.StatusSuccess,
}
Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (c *Config) Dir(_ context.Context, _ *model.User, _ *model.Repo, _ *model.P
}

// Status is not supported by the bitbucketserver driver.
func (c *Config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Step) error {
func (c *Config) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, _ *model.Workflow) error {
status := internal.PipelineStatus{
State: convertStatus(pipeline.Status),
Desc: common.GetPipelineStatusDescription(pipeline.Status),
Expand Down
10 changes: 5 additions & 5 deletions server/forge/common/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/woodpecker-ci/woodpecker/server/model"
)

func GetPipelineStatusContext(repo *model.Repo, pipeline *model.Pipeline, step *model.Step) string {
func GetPipelineStatusContext(repo *model.Repo, pipeline *model.Pipeline, workflow *model.Workflow) string {
event := string(pipeline.Event)
switch pipeline.Event {
case model.EventPull:
Expand All @@ -38,7 +38,7 @@ func GetPipelineStatusContext(repo *model.Repo, pipeline *model.Pipeline, step *
err = tmpl.Execute(&ctx, map[string]interface{}{
"context": server.Config.Server.StatusContext,
"event": event,
"pipeline": step.Name,
"workflow": workflow.Name,
"owner": repo.Owner,
"repo": repo.Name,
})
Expand Down Expand Up @@ -72,10 +72,10 @@ func GetPipelineStatusDescription(status model.StatusValue) string {
}
}

func GetPipelineStatusLink(repo *model.Repo, pipeline *model.Pipeline, step *model.Step) string {
if step == nil {
func GetPipelineStatusLink(repo *model.Repo, pipeline *model.Pipeline, workflow *model.Workflow) string {
if workflow == nil {
return fmt.Sprintf("%s/repos/%d/pipeline/%d", server.Config.Server.Host, repo.ID, pipeline.Number)
}

return fmt.Sprintf("%s/repos/%d/pipeline/%d/%d", server.Config.Server.Host, repo.ID, pipeline.Number, step.PID)
return fmt.Sprintf("%s/repos/%d/pipeline/%d/%d", server.Config.Server.Host, repo.ID, pipeline.Number, workflow.PID)
}
14 changes: 7 additions & 7 deletions server/forge/common/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func TestGetPipelineStatusContext(t *testing.T) {

repo := &model.Repo{Owner: "user1", Name: "repo1"}
pipeline := &model.Pipeline{Event: model.EventPull}
step := &model.Step{Name: "lint"}
workflow := &model.Workflow{Name: "lint"}

assert.EqualValues(t, "", GetPipelineStatusContext(repo, pipeline, step))
assert.EqualValues(t, "", GetPipelineStatusContext(repo, pipeline, workflow))

server.Config.Server.StatusContext = "ci/woodpecker"
server.Config.Server.StatusContextFormat = "{{ .context }}/{{ .event }}/{{ .pipeline }}"
assert.EqualValues(t, "ci/woodpecker/pr/lint", GetPipelineStatusContext(repo, pipeline, step))
server.Config.Server.StatusContextFormat = "{{ .context }}/{{ .event }}/{{ .workflow }}"
assert.EqualValues(t, "ci/woodpecker/pr/lint", GetPipelineStatusContext(repo, pipeline, workflow))
pipeline.Event = model.EventPush
assert.EqualValues(t, "ci/woodpecker/push/lint", GetPipelineStatusContext(repo, pipeline, step))
assert.EqualValues(t, "ci/woodpecker/push/lint", GetPipelineStatusContext(repo, pipeline, workflow))

server.Config.Server.StatusContext = "ci"
server.Config.Server.StatusContextFormat = "{{ .context }}:{{ .owner }}/{{ .repo }}:{{ .event }}:{{ .pipeline }}"
assert.EqualValues(t, "ci:user1/repo1:push:lint", GetPipelineStatusContext(repo, pipeline, step))
server.Config.Server.StatusContextFormat = "{{ .context }}:{{ .owner }}/{{ .repo }}:{{ .event }}:{{ .workflow }}"
assert.EqualValues(t, "ci:user1/repo1:push:lint", GetPipelineStatusContext(repo, pipeline, workflow))
}
2 changes: 1 addition & 1 deletion server/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Forge interface {

// Status sends the commit status to the forge.
// An example would be the GitHub pull request status.
Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, p *model.Step) error
Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, p *model.Workflow) error

// Netrc returns a .netrc file that can be used to clone
// private repositories from a forge.
Expand Down
10 changes: 5 additions & 5 deletions server/forge/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (c *Gitea) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.
}

// Status is supported by the Gitea driver.
func (c *Gitea) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, step *model.Step) error {
func (c *Gitea) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, workflow *model.Workflow) error {
client, err := c.newClientToken(ctx, user.Token)
if err != nil {
return err
Expand All @@ -332,10 +332,10 @@ func (c *Gitea) Status(ctx context.Context, user *model.User, repo *model.Repo,
repo.Name,
pipeline.Commit,
gitea.CreateStatusOption{
State: getStatus(step.State),
TargetURL: common.GetPipelineStatusLink(repo, pipeline, step),
Description: common.GetPipelineStatusDescription(step.State),
Context: common.GetPipelineStatusContext(repo, pipeline, step),
State: getStatus(workflow.State),
TargetURL: common.GetPipelineStatusLink(repo, pipeline, workflow),
Description: common.GetPipelineStatusDescription(workflow.State),
Context: common.GetPipelineStatusContext(repo, pipeline, workflow),
},
)
return err
Expand Down
Loading