// 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] }