refractored types, added missing data

main v0.0.4
gapodo 2022-10-27 21:39:54 +02:00
parent 7f80c971c3
commit 4c74ff0abb
2 changed files with 181 additions and 93 deletions

108
common/types.go Normal file
View File

@ -0,0 +1,108 @@
// Copyright 2021 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 common
import (
"regexp"
"strings"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend"
)
type (
AdditionalData struct {
WorkspaceBase string
WorkspacePath string
Workspace string
SCM string
}
BuildData struct {
Metadata frontend.Metadata
AdditionalData AdditionalData
}
)
func (b BuildData) IsPR() bool {
return b.Metadata.Curr.Event == frontend.EventPull
}
func (b BuildData) PullRequest() string {
if b.IsPR() {
var pullRegexp = regexp.MustCompile(`\d+`)
return pullRegexp.FindString(b.Metadata.Curr.Commit.Ref)
}
return ""
}
func (b BuildData) IsTag() bool {
return b.Metadata.Curr.Event == frontend.EventTag
}
func (b BuildData) Tag() string {
if b.IsTag() {
return strings.TrimPrefix(b.Metadata.Curr.Commit.Ref, "refs/tags/")
}
return ""
}
func (b BuildData) RepoShortName() string {
_, _, name := b.splitRepoName()
return name
}
func (b BuildData) RepoOwner() string {
hasOwner, owner, _ := b.splitRepoName()
if hasOwner {
return owner
}
return ""
}
func (b BuildData) splitRepoName() (bool, string, string) {
var repoOwner string
var repoName string
repoParts := strings.Split(b.Metadata.Repo.Name, "/")
if len(repoParts) == 2 {
repoOwner = repoParts[0]
repoName = repoParts[1]
} else {
repoName = b.Metadata.Repo.Name
}
return (len(repoParts) == 2), repoOwner, repoName
}
func (b BuildData) SourceBranch() string {
hasSource, source, _ := b.splitRefSpec()
if hasSource {
return source
}
return ""
}
func (b BuildData) TargetBranch() string {
_, _, target := b.splitRefSpec()
return target
}
func (b BuildData) splitRefSpec() (bool, string, string) {
branchParts := strings.Split(b.Metadata.Curr.Commit.Refspec, ":")
if len(branchParts) == 2 {
return true, branchParts[0], branchParts[1]
}
return false, "", branchParts[0]
}

View File

@ -15,105 +15,88 @@
package urfave package urfave
import ( import (
"git.kle.li/gapodo/woodpecker-plugin-lib/common"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend" "github.com/woodpecker-ci/woodpecker/pipeline/frontend"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/matrix" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/matrix"
) )
type ( func BuildDataFromContext(c *cli.Context, axis matrix.Axis) common.BuildData {
OtherBuildData struct { bdata := common.BuildData{
Tag string Metadata: frontend.Metadata{
PullRequest string Repo: frontend.Repo{
WorkspaceBase string Name: c.String("repo"),
WorkspacePath string Link: c.String("repo-link"),
Workspace string Remote: c.String("repo-remote-url"),
} Private: c.Bool("repo-private"),
) Branch: c.String("repo-branch"),
},
func BuildMetadataFromContext(c *cli.Context, axis matrix.Axis) frontend.Metadata { Curr: frontend.Build{
return frontend.Metadata{ Number: c.Int64("build-number"),
Repo: frontend.Repo{ Parent: c.Int64("build-parent"),
Name: c.String("repo"), Created: c.Int64("build-created"),
Link: c.String("repo-link"), Started: c.Int64("build-started"),
Remote: c.String("repo-remote-url"), Finished: c.Int64("build-finished"),
Private: c.Bool("repo-private"), Status: c.String("build-status"),
Branch: c.String("repo-branch"), Event: c.String("build-event"),
}, Link: c.String("build-link"),
Curr: frontend.Build{ Target: c.String("build-target"),
Number: c.Int64("build-number"), Commit: frontend.Commit{
Parent: c.Int64("build-parent"), Sha: c.String("commit-sha"),
Created: c.Int64("build-created"), Ref: c.String("commit-ref"),
Started: c.Int64("build-started"), Refspec: c.String("commit-refspec"),
Finished: c.Int64("build-finished"), Branch: c.String("commit-branch"),
Status: c.String("build-status"), Message: c.String("commit-message"),
Event: c.String("build-event"), Author: frontend.Author{
Link: c.String("build-link"), Name: c.String("commit-author"),
Target: c.String("build-target"), Email: c.String("commit-author-email"),
Commit: frontend.Commit{ Avatar: c.String("commit-author-avatar"),
Sha: c.String("commit-sha"), },
Ref: c.String("commit-ref"),
Refspec: c.String("commit-refspec"),
Branch: c.String("commit-branch"),
Message: c.String("commit-message"),
Author: frontend.Author{
Name: c.String("commit-author"),
Email: c.String("commit-author-email"),
Avatar: c.String("commit-author-avatar"),
}, },
}, },
}, Prev: frontend.Build{
Prev: frontend.Build{ Number: c.Int64("prev-build-number"),
Number: c.Int64("prev-build-number"), Parent: c.Int64("prev-build-parent"),
Parent: c.Int64("prev-build-parent"), Created: c.Int64("prev-build-created"),
Created: c.Int64("prev-build-created"), Started: c.Int64("prev-build-started"),
Started: c.Int64("prev-build-started"), Finished: c.Int64("prev-build-finished"),
Finished: c.Int64("prev-build-finished"), Status: c.String("prev-build-status"),
Status: c.String("prev-build-status"), Event: c.String("prev-build-event"),
Event: c.String("prev-build-event"), Link: c.String("prev-build-link"),
Link: c.String("prev-build-link"), Target: c.String("prev-build-target"),
Target: c.String("prev-build-target"), Commit: frontend.Commit{
Commit: frontend.Commit{ Sha: c.String("prev-commit-sha"),
Sha: c.String("prev-commit-sha"), Ref: c.String("prev-commit-ref"),
Ref: c.String("prev-commit-ref"), Refspec: c.String("prev-commit-refspec"),
Refspec: c.String("prev-commit-refspec"), Branch: c.String("prev-commit-branch"),
Branch: c.String("prev-commit-branch"), Message: c.String("prev-commit-message"),
Message: c.String("prev-commit-message"), Author: frontend.Author{
Author: frontend.Author{ Name: c.String("prev-commit-author"),
Name: c.String("prev-commit-author"), Email: c.String("prev-commit-author-email"),
Email: c.String("prev-commit-author-email"), Avatar: c.String("prev-commit-author-avatar"),
Avatar: c.String("prev-commit-author-avatar"), },
}, },
}, },
Job: frontend.Job{
Number: c.Int("job-number"),
Matrix: axis,
},
Sys: frontend.System{
Name: c.String("system-name"),
Host: c.String("system-host"),
Link: c.String("system-link"),
Arch: c.String("system-arch"),
Version: c.String("system-version"),
},
}, },
Job: frontend.Job{ AdditionalData: common.AdditionalData{
Number: c.Int("job-number"), WorkspaceBase: c.String("workspace-base"),
Matrix: axis, WorkspacePath: c.String("workspace-path"),
}, Workspace: c.String("workspace"),
Sys: frontend.System{ SCM: c.String("repo-scm"),
Name: c.String("system-name"),
Host: c.String("system-host"),
Link: c.String("system-link"),
Arch: c.String("system-arch"),
Version: c.String("system-version"),
}, },
} }
} return bdata
func NonMetadataFromBuildContext(c *cli.Context) OtherBuildData {
opts := OtherBuildData{
WorkspaceBase: c.String("workspace-base"),
WorkspacePath: c.String("workspace-path"),
Workspace: c.String("workspace"),
}
if c.String("build-event") == frontend.EventPull {
opts.PullRequest = c.String("commit-pr")
}
if c.String("build-event") == frontend.EventTag {
opts.Tag = c.String("commit-tag")
}
return opts
} }
func BuildFlags() []cli.Flag { func BuildFlags() []cli.Flag {
@ -322,12 +305,9 @@ func BuildFlags() []cli.Flag {
Name: "workspace", Name: "workspace",
}, },
&cli.StringFlag{ &cli.StringFlag{
EnvVars: []string{"CI_COMMIT_TAG"}, EnvVars: []string{"CI_REPO_SCM"},
Name: "commit-tag", Name: "repo-scm",
}, Value: "git",
&cli.StringFlag{
EnvVars: []string{"CI_COMMIT_PULL_REQUEST"},
Name: "commit-pr",
}, },
} }
} }