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,23 +15,15 @@
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
WorkspaceBase string
WorkspacePath string
Workspace string
}
)
func BuildMetadataFromContext(c *cli.Context, axis matrix.Axis) frontend.Metadata {
return frontend.Metadata{
Repo: frontend.Repo{ Repo: frontend.Repo{
Name: c.String("repo"), Name: c.String("repo"),
Link: c.String("repo-link"), Link: c.String("repo-link"),
@ -96,24 +88,15 @@ func BuildMetadataFromContext(c *cli.Context, axis matrix.Axis) frontend.Metadat
Arch: c.String("system-arch"), Arch: c.String("system-arch"),
Version: c.String("system-version"), Version: c.String("system-version"),
}, },
} },
} AdditionalData: common.AdditionalData{
func NonMetadataFromBuildContext(c *cli.Context) OtherBuildData {
opts := OtherBuildData{
WorkspaceBase: c.String("workspace-base"), WorkspaceBase: c.String("workspace-base"),
WorkspacePath: c.String("workspace-path"), WorkspacePath: c.String("workspace-path"),
Workspace: c.String("workspace"), Workspace: c.String("workspace"),
SCM: c.String("repo-scm"),
},
} }
return bdata
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",
}, },
} }
} }