Skip to content

Commit

Permalink
Cleanups and updates (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 authored Aug 13, 2023
1 parent fcfd4a8 commit f5be692
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage.out
plugin-git

.vscode
.idea
8 changes: 0 additions & 8 deletions .woodpecker/test-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ variables:
- &publish_repos 'woodpeckerci/plugin-git,quay.io/woodpeckerci/plugin-git'

steps:
version:
group: prepare
image: *golang
commands:
- apk add make
- make version

vendor:
group: prepare
image: *golang
commands:
- go mod vendor
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# plugin-git

<p align="center">
<a href="https://wp.laszlo.cloud/woodpecker-ci/plugin-git" title="Build Status">
<img src="https://wp.laszlo.cloud/api/badges/woodpecker-ci/plugin-git/status.svg">
<a href="https://ci.woodpecker-ci.org/repos/5586" title="Build Status">
<img src="https://ci.woodpecker-ci.org/api/badges/5586/status.svg">
</a>
<a href="https://discord.gg/fcMQqSMXJy" title="Join the Discord chat at https://discord.gg/fcMQqSMXJy">
<img src="https://img.shields.io/discord/838698813463724034.svg">
</a>
<a href="https://goreportcard.com/badge/github.com/woodpecker-ci/plugin-git" title="Go Report Card">
<a href="https://goreportcard.com/report/github.com/woodpecker-ci/plugin-git" title="Go Report Card">
<img src="https://goreportcard.com/badge/github.com/woodpecker-ci/plugin-git">
</a>
<a href="https://godoc.org/github.com/woodpecker-ci/plugin-git" title="GoDoc">
Expand Down
2 changes: 1 addition & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func SetDefaults(c *cli.Context, p *Plugin) {
if p.Build.Event == "tag" && !c.IsSet("tags") {
if p.Pipeline.Event == "tag" && !c.IsSet("tags") {
// tags clone not explicit set but pipeline is triggered by a tag
// auto set tags cloning to true
p.Config.Tags = true
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/woodpecker-ci/plugin-git

go 1.18
go 1.20

require (
github.com/adrg/xdg v0.4.0
github.com/joho/godotenv v1.4.0
github.com/urfave/cli/v2 v2.17.1
github.com/joho/godotenv v1.5.1
github.com/urfave/cli/v2 v2.25.7
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 // indirect
golang.org/x/sys v0.11.0 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.17.1 h1:UzjDEw2dJQUE3iRaiNQ1VrVFbyAtKGH3VdkMoHA58V0=
github.com/urfave/cli/v2 v2.17.1/go.mod h1:1CNUng3PtjQMtRzJO4FMXBQvkGtuYRxxiR9xMa7jMwI=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359 h1:2B5p2L5IfGiD7+b9BOoRMC6DgObAVZV+Fsp050NqXik=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
&cli.StringFlag{
Name: "remote",
Usage: "git remote url",
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL", "CI_REPO_REMOTE", "CI_REMOTE_URL"},
EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"},
},
&cli.StringFlag{
Name: "path",
Expand All @@ -42,8 +42,8 @@ func main() {
&cli.StringFlag{
Name: "event",
Value: "push",
Usage: "build event",
EnvVars: []string{"CI_PIPELINE_EVENT", "CI_BUILD_EVENT"},
Usage: "pipeline event",
EnvVars: []string{"CI_PIPELINE_EVENT"},
},
&cli.StringFlag{
Name: "netrc.machine",
Expand Down Expand Up @@ -156,7 +156,7 @@ func run(c *cli.Context) error {
Repo: Repo{
Clone: c.String("remote"),
},
Build: Build{
Pipeline: Pipeline{
Commit: c.String("sha"),
Event: c.String("event"),
Path: c.String("path"),
Expand Down
26 changes: 13 additions & 13 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

type Plugin struct {
Repo Repo
Build Build
Netrc Netrc
Config Config
Backoff Backoff
Repo Repo
Pipeline Pipeline
Netrc Netrc
Config Config
Backoff Backoff
}

const customCertTmpPath = "/tmp/customCert.pem"
Expand All @@ -29,8 +29,8 @@ var defaultEnvVars = []string{
}

func (p Plugin) Exec() error {
if p.Build.Path != "" {
err := os.MkdirAll(p.Build.Path, 0o777)
if p.Pipeline.Path != "" {
err := os.MkdirAll(p.Pipeline.Path, 0o777)
if err != nil {
return err
}
Expand Down Expand Up @@ -60,22 +60,22 @@ func (p Plugin) Exec() error {
}
}

if isDirEmpty(filepath.Join(p.Build.Path, ".git")) {
if isDirEmpty(filepath.Join(p.Pipeline.Path, ".git")) {
cmds = append(cmds, initGit(p.Config.Branch))
cmds = append(cmds, safeDirectory(p.Config.SafeDirectory))
cmds = append(cmds, remote(p.Repo.Clone))
}

// fetch ref in any case
cmds = append(cmds, fetch(p.Build.Ref, p.Config.Tags, p.Config.Depth, p.Config.filter))
cmds = append(cmds, fetch(p.Pipeline.Ref, p.Config.Tags, p.Config.Depth, p.Config.filter))

if p.Build.Commit == "" {
if p.Pipeline.Commit == "" {
// checkout by fetched ref
fmt.Println("no commit information: using head checkout")
cmds = append(cmds, checkoutHead())
} else {
// checkout by commit sha
cmds = append(cmds, checkoutSha(p.Build.Commit))
cmds = append(cmds, checkoutSha(p.Pipeline.Commit))
}

for name, submoduleUrl := range p.Config.Submodules {
Expand All @@ -94,7 +94,7 @@ func (p Plugin) Exec() error {

for _, cmd := range cmds {
buf := new(bytes.Buffer)
cmd.Dir = p.Build.Path
cmd.Dir = p.Pipeline.Path
cmd.Stdout = io.MultiWriter(os.Stdout, buf)
cmd.Stderr = io.MultiWriter(os.Stderr, buf)
trace(cmd)
Expand Down Expand Up @@ -256,7 +256,7 @@ func checkoutLFS() *exec.Cmd {
), defaultEnvVars...)
}

// fetch retuns git command that fetches from origin. If tags is true
// fetch returns git command that fetches from origin. If tags is true
// then tags will be fetched.
func fetch(ref string, tags bool, depth int, filter string) *exec.Cmd {
tags_option := "--no-tags"
Expand Down
14 changes: 7 additions & 7 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestClone(t *testing.T) {
Repo: Repo{
Clone: c.clone,
},
Build: Build{
Pipeline: Pipeline{
Path: filepath.Join(dir, c.path),
Commit: c.commit,
Event: c.event,
Expand All @@ -151,14 +151,14 @@ func TestClone(t *testing.T) {
}

if c.data != "" {
data := readFile(plugin.Build.Path, c.file)
data := readFile(plugin.Pipeline.Path, c.file)
if data != c.data {
t.Errorf("Expected %s to contain [%s]. Got [%s].", c.file, c.data, data)
}
}

if c.dataSize != 0 {
size := getFileSize(plugin.Build.Path, c.file)
size := getFileSize(plugin.Pipeline.Path, c.file)
if size != c.dataSize {
t.Errorf("Expected %s size to be [%d]. Got [%d].", c.file, c.dataSize, size)
}
Expand All @@ -169,7 +169,7 @@ func TestClone(t *testing.T) {

// TestCloneNonEmpty tests the ability to clone a specific commit into
// a non-empty directory. This is useful if the git workspace is cached
// and re-stored for every build.
// and re-stored for every workflow.
func TestCloneNonEmpty(t *testing.T) {
dir := setup()
defer teardown(dir)
Expand All @@ -180,7 +180,7 @@ func TestCloneNonEmpty(t *testing.T) {
Repo: Repo{
Clone: c.clone,
},
Build: Build{
Pipeline: Pipeline{
Path: filepath.Join(dir, c.path),
Commit: c.commit,
Event: c.event,
Expand All @@ -198,15 +198,15 @@ func TestCloneNonEmpty(t *testing.T) {
}

if c.data != "" {
data := readFile(plugin.Build.Path, c.file)
data := readFile(plugin.Pipeline.Path, c.file)
if data != c.data {
t.Errorf("Expected %s to contain [%s]. Got [%s].", c.file, c.data, data)
break
}
}

if c.dataSize != 0 {
size := getFileSize(plugin.Build.Path, c.file)
size := getFileSize(plugin.Pipeline.Path, c.file)
if size != c.dataSize {
t.Errorf("Expected %s size to be [%d]. Got [%d].", c.file, c.dataSize, size)
}
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type (
Clone string
}

Build struct {
Pipeline struct {
Path string
Event string
Number int
Expand Down
14 changes: 1 addition & 13 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -40,17 +39,6 @@ func isDirEmpty(dir string) bool {
return err == io.EOF
}

// helper function returns true if the commit is a pull_request.
func isPullRequest(event string) bool {
return event == "pull_request"
}

// helper function returns true if the commit is a tag.
func isTag(event, ref string) bool {
return event == "tag" ||
strings.HasPrefix(ref, "refs/tags/")
}

// helper function to write a netrc file.
func writeNetrc(home, machine, login, password string) error {
if machine == "" || (login == "" && password == "") {
Expand All @@ -64,7 +52,7 @@ func writeNetrc(home, machine, login, password string) error {
)

path := filepath.Join(home, ".netrc")
return ioutil.WriteFile(path, []byte(out), 0o600)
return os.WriteFile(path, []byte(out), 0o600)
}

const netrcFile = `
Expand Down

0 comments on commit f5be692

Please sign in to comment.