Skip to content

Commit

Permalink
feat: add package wd_info_parse and wd_info.RepositoryInfo parse from…
Browse files Browse the repository at this point in the history
… CI_REPO_CLONE_URL

wd_info.RepositoryInfo member CiRepoHost CiRepoHostname CiRepoPort form env CI_REPO_CLONE_URL
  • Loading branch information
sinlov committed Mar 8, 2024
1 parent 80313fa commit e467d02
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.2.3
github.com/aymerick/raymond v2.0.2+incompatible
github.com/chainguard-dev/git-urls v1.0.2
github.com/gookit/color v1.5.4
github.com/joho/godotenv v1.5.1
github.com/sebdah/goldie/v2 v2.5.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj
github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM=
github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0=
github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/chainguard-dev/git-urls v1.0.2 h1:pSpT7ifrpc5X55n4aTTm7FFUE+ZQHKiqpiwNkJrVcKQ=
github.com/chainguard-dev/git-urls v1.0.2/go.mod h1:rbGgj10OS7UgZlbzdUQIQpT0k/D4+An04HJY7Ol+Y/o=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
12 changes: 12 additions & 0 deletions wd_info/wd_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ type (
// Provides the repository trusted flag. repository trusted.
// by env: CI_REPO_TRUSTED
CIRepoTrusted bool `mock_env_key:"CI_REPO_TRUSTED"`

// CIRepoHost
// Provides the repository host, parse from env `CI_REPO_CLONE_URL`
CiRepoHost string

// CIRepoHostname
// Provides the repository hostname, parse from env `CI_REPO_CLONE_URL`
CiRepoHostname string

// CIRepoPort
// Provides the repository port, parse from env `CI_REPO_CLONE_URL`
CiRepoPort string
}

// CurrentCommitInfo
Expand Down
46 changes: 46 additions & 0 deletions wd_info_parse/info_parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package wd_info_parse

import (
"fmt"
gitUrls "github.com/chainguard-dev/git-urls"
"github.com/woodpecker-kit/woodpecker-tools/wd_info"
"strings"
)

func ParseRepositoryInfoByWoodPeckerInfo(info *wd_info.WoodpeckerInfo) error {

if info.RepositoryInfo.CIRepoCloneURL == "" {
return fmt.Errorf("RepositoryInfo.CIRepoCloneURL is empty")
}

parse, errParse := gitUrls.Parse(info.RepositoryInfo.CIRepoCloneURL)
if errParse != nil {
return fmt.Errorf("RepositoryInfo.CIRepoCloneURL parse err: %s", errParse)
}
if strings.Contains(parse.Scheme, "http") {
info.RepositoryInfo.CiRepoHost = parse.Host
info.RepositoryInfo.CiRepoHostname = parse.Hostname()
info.RepositoryInfo.CiRepoPort = parse.Port()
}

return nil
}

func ParseRepositoryInfoByRepositoryInfo(info *wd_info.RepositoryInfo) error {

if info.CIRepoCloneURL == "" {
return fmt.Errorf("RepositoryInfo.CIRepoCloneURL is empty")
}

parse, errParse := gitUrls.Parse(info.CIRepoCloneURL)
if errParse != nil {
return fmt.Errorf("RepositoryInfo.CIRepoCloneURL parse err: %s", errParse)
}
if strings.Contains(parse.Scheme, "http") {
info.CiRepoHost = parse.Host
info.CiRepoHostname = parse.Hostname()
info.CiRepoPort = parse.Port()
}

return nil
}
9 changes: 7 additions & 2 deletions wd_mock/wd_info_mock.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package wd_mock

import "github.com/woodpecker-kit/woodpecker-tools/wd_info"
import (
"github.com/woodpecker-kit/woodpecker-tools/wd_info"
"github.com/woodpecker-kit/woodpecker-tools/wd_info_parse"
)

var (
defaultOptionWoodpeckerInfo = setDefaultOptionWoodpeckerInfo()
)

func setDefaultOptionWoodpeckerInfo() *wd_info.WoodpeckerInfo {
return &wd_info.WoodpeckerInfo{
info := wd_info.WoodpeckerInfo{
BasicInfo: *NewBasicInfoFull(),
CiSystemInfo: *NewCiSystemInfoFull(),
CiForgeInfo: *NewCiForgeInfoFull(),
Expand All @@ -23,6 +26,7 @@ func setDefaultOptionWoodpeckerInfo() *wd_info.WoodpeckerInfo {
PreviousPipelineInfo: *NewPreviousPipelineInfoFull(),
},
}
return &info
}

type WoodpeckerInfoOption func(*wd_info.WoodpeckerInfo)
Expand Down Expand Up @@ -70,6 +74,7 @@ func WithCiForgeInfo(opts ...CiForgeInfoOption) WoodpeckerInfoOption {
func WithRepositoryInfo(opts ...RepositoryInfoOption) WoodpeckerInfoOption {
return func(o *wd_info.WoodpeckerInfo) {
o.RepositoryInfo = *NewRepositoryInfo(opts...)
_ = wd_info_parse.ParseRepositoryInfoByRepositoryInfo(&o.RepositoryInfo)
}
}

Expand Down
10 changes: 8 additions & 2 deletions wd_mock/wd_mock_repository_info.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package wd_mock

import "github.com/woodpecker-kit/woodpecker-tools/wd_info"
import (
"github.com/woodpecker-kit/woodpecker-tools/wd_info"
"github.com/woodpecker-kit/woodpecker-tools/wd_info_parse"
)

var defaultRepositoryInfo = setDefaultRepositoryInfo()

type RepositoryInfoOption func(*wd_info.RepositoryInfo)

func setDefaultRepositoryInfo() *wd_info.RepositoryInfo {
return &wd_info.RepositoryInfo{
info := wd_info.RepositoryInfo{
CIRepo: "woodpecker-kit/guidance-woodpecker-agent",
CIRepoOwner: "woodpecker-kit",
CIRepoName: "guidance-woodpecker-agent",
Expand All @@ -20,6 +23,8 @@ func setDefaultRepositoryInfo() *wd_info.RepositoryInfo {
CIRepoPrivate: true,
CIRepoTrusted: false,
}
_ = wd_info_parse.ParseRepositoryInfoByRepositoryInfo(&info)
return &info
}

func NewRepositoryInfo(opts ...RepositoryInfoOption) (opt *wd_info.RepositoryInfo) {
Expand Down Expand Up @@ -70,6 +75,7 @@ func WithCIRepoURL(ciRepoURL string) RepositoryInfoOption {
func WithCIRepoCloneURL(ciRepoCloneURL string) RepositoryInfoOption {
return func(o *wd_info.RepositoryInfo) {
o.CIRepoCloneURL = ciRepoCloneURL
_ = wd_info_parse.ParseRepositoryInfoByRepositoryInfo(o)
}
}

Expand Down
5 changes: 4 additions & 1 deletion wd_mock_test/testdata/TestNewRepositoryInfo/sample.golden
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"CIRepoCloneSshUrl": "git@gitea.domain.com:woodpecker-kit/guidance-woodpecker-agent.git",
"CIRepoDefaultBranch": "main",
"CIRepoPrivate": true,
"CIRepoTrusted": false
"CIRepoTrusted": false,
"CiRepoHost": "gitea.domain.com",
"CiRepoHostname": "gitea.domain.com",
"CiRepoPort": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"CIRepoCloneSshUrl": "git@gitea.domain.com:woodpecker-kit/guidance-woodpecker-agent.git",
"CIRepoDefaultBranch": "main",
"CIRepoPrivate": true,
"CIRepoTrusted": false
"CIRepoTrusted": false,
"CiRepoHost": "gitea.domain.com",
"CiRepoHostname": "gitea.domain.com",
"CiRepoPort": ""
},
"CurrentInfo": {
"CurrentCommitInfo": {
Expand Down
5 changes: 4 additions & 1 deletion wd_mock_test/testdata/TestNewWoodpeckerInfo/sample.golden
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"CIRepoCloneSshUrl": "git@gitea.domain.com:woodpecker-kit/guidance-woodpecker-agent.git",
"CIRepoDefaultBranch": "main",
"CIRepoPrivate": true,
"CIRepoTrusted": false
"CIRepoTrusted": false,
"CiRepoHost": "gitea.domain.com",
"CiRepoHostname": "gitea.domain.com",
"CiRepoPort": ""
},
"CurrentInfo": {
"CurrentCommitInfo": {
Expand Down
5 changes: 4 additions & 1 deletion wd_mock_test/testdata/TestWoodPeckerEnvMock/sample.golden
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"CIRepoCloneSshUrl": "git@gitea.domain.com:woodpecker-kit/guidance-woodpecker-agent.git",
"CIRepoDefaultBranch": "main",
"CIRepoPrivate": true,
"CIRepoTrusted": false
"CIRepoTrusted": false,
"CiRepoHost": "gitea.domain.com",
"CiRepoHostname": "gitea.domain.com",
"CiRepoPort": ""
},
"CurrentInfo": {
"CurrentCommitInfo": {
Expand Down
3 changes: 3 additions & 0 deletions wd_urfave_cli_v2/bind_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/urfave/cli/v2"
"github.com/woodpecker-kit/woodpecker-tools/wd_flag"
"github.com/woodpecker-kit/woodpecker-tools/wd_info"
"github.com/woodpecker-kit/woodpecker-tools/wd_info_parse"
)

// UrfaveCliBindInfo
Expand Down Expand Up @@ -163,5 +164,7 @@ func UrfaveCliBindInfo(c *cli.Context) wd_info.WoodpeckerInfo {

PreviousInfo: previousInfo,
}
_ = wd_info_parse.ParseRepositoryInfoByWoodPeckerInfo(&wdInfo)

return wdInfo
}

0 comments on commit e467d02

Please sign in to comment.