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