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

WIP: implement "cicd_feedback" for integration into external forges #3836

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ccmenu",
"certmagic",
"charmbracelet",
"CICD",
"ciphertext",
"Codeberg",
"compatiblelicenses",
Expand Down Expand Up @@ -212,5 +213,7 @@
// ignore docker image names
"\\s*docker\\.io/.*"
],
"enableFiletypes": ["dockercompose"]
"enableFiletypes": [
"dockercompose"
]
}
3 changes: 3 additions & 0 deletions cmd/server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4833,6 +4833,9 @@ const docTemplate = `{
"$ref": "#/definitions/WebhookEvent"
}
},
"cicd_feedback": {
"type": "boolean"
},
"clone_url": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions cmd/server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ var flags = append([]cli.Flag{
Name: "log-store-file-path",
Usage: "directory used for file based log storage",
},
&cli.BoolFlag{
EnvVars: []string{"WOODPECKER_CICD_FEEDBACK"},
Name: "cicd-feedback",
Usage: "woodpecker cicd-feedback protocol support",
Value: true,
},
//
// backend options for pipeline compiler
//
Expand Down
1 change: 1 addition & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func setupEvilGlobals(c *cli.Context, s store.Store) error {
server.Config.Server.RootPath = rootPath
server.Config.Server.CustomCSSFile = strings.TrimSpace(c.String("custom-css-file"))
server.Config.Server.CustomJsFile = strings.TrimSpace(c.String("custom-js-file"))
server.Config.Server.CICDFeedback = c.Bool("cicd-feedback")
server.Config.Pipeline.Networks = c.StringSlice("network")
server.Config.Pipeline.Volumes = c.StringSlice("volume")
server.Config.Pipeline.Privileged = c.StringSlice("escalate")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
codeberg.org/6543/go-yaml2json v1.0.0
codeberg.org/6543/xyaml v1.1.0
codeberg.org/mvdkleijn/forgejo-sdk/forgejo v1.1.0
github.com/6543/cicd_feedback v0.0.0-20240625213231-0f894fa6f0f9
github.com/6543/logfile-open v1.2.1
github.com/adrg/xdg v0.4.0
github.com/alessio/shellescape v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
gitee.com/travelliu/dm v1.8.11192/go.mod h1:DHTzyhCrM843x9VdKVbZ+GKXGRbKM2sJ4LxihRxShkE=
github.com/6543/cicd_feedback v0.0.0-20240625213231-0f894fa6f0f9 h1:/QqqEnwNhZk4GwAk0GE6F6lfdIUfjWTod+zCa/jBQSI=
github.com/6543/cicd_feedback v0.0.0-20240625213231-0f894fa6f0f9/go.mod h1:xJjRB6hyl1f8XMjBajWkP98wk4Jq+Kxm+eIGy+Piozo=
github.com/6543/logfile-open v1.2.1 h1:az+TtNHclTAKaHfFCTSbuduMllANox1gM9qLQr7LV5I=
github.com/6543/logfile-open v1.2.1/go.mod h1:ZoEy7pW2mexmQxiZIqPCeh8vUxVuiHYXmSZNbvEb51g=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw=
Expand Down
4 changes: 4 additions & 0 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/rs/zerolog/log"

"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/feedback"
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
Expand Down Expand Up @@ -242,6 +243,9 @@ func PostHook(c *gin.Context) {
//

pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
if server.Config.Server.CICDFeedback && repo.CICDFeedback && pl != nil {
c.Writer.Header().Set("CICD-Feedback", feedback.PipelineURL(pl))
}
if err != nil {
handlePipelineErr(c, err)
} else {
Expand Down
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var Config = struct {
RootPath string
CustomCSSFile string
CustomJsFile string
CICDFeedback bool
}
WebUI struct {
EnableSwagger bool
Expand Down
128 changes: 128 additions & 0 deletions server/feedback/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package feedback

import (
"errors"
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/router/middleware/session"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
"go.woodpecker-ci.org/woodpecker/v2/server/store/types"

cicd_feedback "github.com/6543/cicd_feedback"
)

func Get(c *gin.Context) {
_store := store.FromContext(c)

repo := session.Repo(c)
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
if err != nil {
handleError(c, err)
return
}

pl, err := _store.GetPipelineNumber(repo, num)
if err != nil {
handleError(c, err)
return
}
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
handleError(c, err)
return
}

resp, err := Convert(pl, pl.Workflows)
if err != nil {
handleError(c, err)
return
}

c.JSON(http.StatusOK, resp)
}

func GetStepLog(c *gin.Context) {
_store := store.FromContext(c)
repo := session.Repo(c)

num, err := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
if err != nil {
handleError(c, err)
return
}

pl, err := _store.GetPipelineNumber(repo, num)
if err != nil {
handleError(c, err)
return
}

stepID, err := strconv.ParseInt(c.Params.ByName("stepId"), 10, 64)
if err != nil {
handleError(c, err)
return
}

step, err := _store.StepLoad(stepID)
if err != nil {
handleError(c, err)
return
}

if step.PipelineID != pl.ID {
// make sure we cannot read arbitrary logs by id
err := fmt.Errorf("step with id %d is not part of repo %s", stepID, repo.FullName)
handleError(c, err)
return
}

logs, err := server.Config.Services.LogStore.LogFind(step)
if err != nil {
handleError(c, err)
return
}

feedbackLog, err := convertLogs(logs)
if err != nil {
handleError(c, err)
return
}

// TODO: use c.Stream if step is currently running and pipe stream to it
c.String(http.StatusOK, feedbackLog)
}

func handleError(c *gin.Context, err error) {
if errors.Is(err, types.RecordNotExist) ||
errors.Is(err, ErrNonConvertableStatus) ||
errors.Is(err, ErrStepDepResolve) ||
errors.Is(err, ErrWorkflowDepResolve) {
c.JSON(http.StatusInternalServerError, cicd_feedback.ErrorResponse{
Error: cicd_feedback.ErrorInternal,
ErrorDescription: err.Error(),
})
return
}

c.JSON(http.StatusBadRequest, cicd_feedback.ErrorResponse{
Error: cicd_feedback.ErrorOther,
ErrorDescription: err.Error(),
})
}
161 changes: 161 additions & 0 deletions server/feedback/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package feedback

import (
"errors"
"fmt"
"strings"

"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/model"

cicd_feedback "github.com/6543/cicd_feedback"
)

var (
ErrNonConvertableStatus = errors.New("got non convertable status")
ErrWorkflowDepResolve = errors.New("could not resolve workflow dependency")
ErrStepDepResolve = errors.New("could not resolve step dependency")
)

func Convert(in *model.Pipeline, workflows []*model.Workflow) (*cicd_feedback.PipelineResponse, error) {
pipeStatus, err := convertStatus(in.Status)
if err != nil {
return nil, err
}

feedbackWorkflows := make([]cicd_feedback.Workflow, 0, len(workflows))
for _, w := range workflows {
fw, err := convertWorkflow(*w, in.RepoID, in.Number)
if err != nil {
return nil, err
}
feedbackWorkflows = append(feedbackWorkflows, *fw)
}

out := &cicd_feedback.PipelineResponse{
PipelineID: fmt.Sprintf("pipeline_%d", in.ID),
Title: in.Title,
Status: pipeStatus,
RequiresManualAction: (in.Status == model.StatusBlocked),
Workflows: feedbackWorkflows,
ExternalURI: fmt.Sprintf("%s/repos/%d/pipeline/%d", server.Config.Server.Host, in.RepoID, in.Number),
}

return out, nil
}

func convertWorkflow(workflow model.Workflow, repoID, pipelineNumber int64) (*cicd_feedback.Workflow, error) {
feedbackSteps, err := convertSteps(workflow.Children, repoID, pipelineNumber)
if err != nil {
return nil, err
}
status, err := convertStatus(workflow.State)
if err != nil {
return nil, err
}
return &cicd_feedback.Workflow{
ID: fmt.Sprintf("workflow_%d", workflow.ID),
Name: workflow.Name,
Steps: feedbackSteps,
Status: status,
}, nil
}

func convertSteps(in []*model.Step, repoID, pipelineNumber int64) ([]cicd_feedback.Step, error) {
result := make([]cicd_feedback.Step, 0, len(in))

nameUUIDMap := make(map[string]string, len(in))
for _, s := range in {
nameUUIDMap[s.Name] = s.UUID
}

for _, s := range in {
status, err := convertStatus(s.State)
if err != nil {
return nil, err
}

deps := []string{}
/* TODO: Dependencies
deps := make([]string, 0, len(s.Dependencies))
for _, dep := range s.Dependencies {
uuid, exist := nameUUIDMap[dep]
if !exist {
return nil, ErrStepDepResolve
}
deps = append(deps, uuid)
}
*/

result = append(result,
cicd_feedback.Step{
ID: s.UUID,
Name: s.Name,
Status: status,
Inputs: cicd_feedback.Inputs{
Commands: nil, // TODO,
Environment: nil, // TODO
},
Outputs: cicd_feedback.Outputs{
Logs: []cicd_feedback.Log{{
Name: "console",
URI: fmt.Sprintf("%s/api/feedback/%d/%d/%d", server.Config.Server.Host, repoID, pipelineNumber, s.ID),
}},
},
Dependencies: deps,
})
}

return result, nil
}

func PipelineURL(in *model.Pipeline) string {
return fmt.Sprintf("%s/api/feedback/%d/%d", server.Config.Server.Host, in.RepoID, in.Number)
}

func convertLogs(logs []*model.LogEntry) (string, error) {
builder := strings.Builder{}
for _, log := range logs {
builder.Write(log.Data)
builder.WriteString("\n")
}

return builder.String(), nil
}

func convertStatus(in model.StatusValue) (cicd_feedback.Status, error) {
switch in {
case model.StatusPending, model.StatusCreated:
return cicd_feedback.StatusPending, nil
case model.StatusSkipped:
return cicd_feedback.StatusSkipped, nil
case model.StatusRunning:
return cicd_feedback.StatusRunning, nil
case model.StatusSuccess:
return cicd_feedback.StatusSuccess, nil
case model.StatusFailure, model.StatusError:
return cicd_feedback.StatusFailed, nil
case model.StatusKilled:
return cicd_feedback.StatusKilled, nil
case model.StatusBlocked:
return cicd_feedback.StatusManual, nil
case model.StatusDeclined:
return cicd_feedback.StatusDeclined, nil
default:
return "", ErrNonConvertableStatus
}
}
Loading