diff --git a/_docs/data/data.yaml b/_docs/data/data.yaml index 44eef11..1abf553 100644 --- a/_docs/data/data.yaml +++ b/_docs/data/data.yaml @@ -17,10 +17,12 @@ properties: - name: netrc_machine description: Netrc remote machine name. + defaultvalue: github.com type: string - name: netrc_username description: Netrc login user on the remote machine. + defaultvalue: token type: string - name: netrc_password @@ -59,8 +61,8 @@ properties: defaultvalue: false type: bool - - name: skip_verify - description: Skip SSL verification of the remote machine. + - name: insecure_ssl_verify + description: Configure git SSL verification of the remote machine. defaultvalue: false type: bool diff --git a/cmd/drone-git-action/config.go b/cmd/drone-git-action/config.go index 8321109..26c36b0 100644 --- a/cmd/drone-git-action/config.go +++ b/cmd/drone-git-action/config.go @@ -18,7 +18,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { }, &cli.StringFlag{ - Name: "commit.author.name", + Name: "commit-author-name", Usage: "git author name", EnvVars: []string{"PLUGIN_AUTHOR_NAME", "DRONE_COMMIT_AUTHOR"}, Destination: &settings.Commit.Author.Name, @@ -26,7 +26,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Category: category, }, &cli.StringFlag{ - Name: "commit.author.email", + Name: "commit-author-email", Usage: "git author email", EnvVars: []string{"PLUGIN_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL"}, Destination: &settings.Commit.Author.Email, @@ -35,21 +35,23 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { }, &cli.StringFlag{ - Name: "netrc.machine", + Name: "netrc-machine", Usage: "netrc remote machine name", EnvVars: []string{"PLUGIN_NETRC_MACHINE", "DRONE_NETRC_MACHINE"}, Destination: &settings.Netrc.Machine, + Value: "github.com", Category: category, }, &cli.StringFlag{ - Name: "netrc.username", + Name: "netrc-username", Usage: "netrc login user on the remote machine", EnvVars: []string{"PLUGIN_NETRC_USERNAME", "DRONE_NETRC_USERNAME"}, Destination: &settings.Netrc.Login, + Value: "token", Category: category, }, &cli.StringFlag{ - Name: "netrc.password", + Name: "netrc-password", Usage: "netrc login password on the remote machine", EnvVars: []string{"PLUGIN_NETRC_PASSWORD", "DRONE_NETRC_PASSWORD"}, Destination: &settings.Netrc.Password, @@ -92,7 +94,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Usage: "commit message", EnvVars: []string{"PLUGIN_MESSAGE"}, Destination: &settings.Message, - Value: "[skip ci] Commit dirty state", + Value: "[skip ci] commit dirty state", Category: category, }, @@ -101,6 +103,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Usage: "enable force push to remote repository", EnvVars: []string{"PLUGIN_FORCE"}, Destination: &settings.Force, + Value: false, Category: category, }, &cli.BoolFlag{ @@ -108,13 +111,15 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Usage: "follow tags for pushes to remote repository", EnvVars: []string{"PLUGIN_FOLLOWTAGS"}, Destination: &settings.FollowTags, + Value: false, Category: category, }, &cli.BoolFlag{ - Name: "skip-verify", - Usage: "skip ssl verification of the remote machine", - EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, - Destination: &settings.SkipVerify, + Name: "insecure-ssl-verify", + Usage: "set SSL verification of the remote machine", + EnvVars: []string{"PLUGIN_INSECURE_SSL_VERIFY"}, + Destination: &settings.InsecureSSLVerify, + Value: false, Category: category, }, &cli.BoolFlag{ @@ -122,6 +127,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Usage: "allow empty commits", EnvVars: []string{"PLUGIN_EMPTY_COMMIT"}, Destination: &settings.EmptyCommit, + Value: false, Category: category, }, &cli.BoolFlag{ @@ -129,6 +135,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag { Usage: "bypass the pre-commit and commit-msg hooks", EnvVars: []string{"PLUGIN_NO_VERIFY"}, Destination: &settings.NoVerify, + Value: false, Category: category, }, } diff --git a/git/config.go b/git/config.go index 18dc964..1b9d5c0 100644 --- a/git/config.go +++ b/git/config.go @@ -2,28 +2,41 @@ package git import ( "os/exec" + "strconv" ) -// GlobalUser sets the global git author email. -func GlobalUser(email string) *exec.Cmd { +// SetUserEmail sets the global git author email. +func SetUserEmail(email string) *exec.Cmd { cmd := exec.Command( "git", "config", - "--global", + "--local", "user.email", email) return cmd } -// GlobalName sets the global git author name. -func GlobalName(author string) *exec.Cmd { +// SetUserName sets the global git author name. +func SetUserName(author string) *exec.Cmd { cmd := exec.Command( "git", "config", - "--global", + "--local", "user.name", author) return cmd } + +// SetSSLSkipVerify disables globally the git ssl verification. +func SetSSLVerify(sslVerify bool) *exec.Cmd { + cmd := exec.Command( + "git", + "config", + "--local", + "http.sslVerify", + strconv.FormatBool(sslVerify)) + + return cmd +} diff --git a/git/utils.go b/git/utils.go index 520f343..95e3cea 100644 --- a/git/utils.go +++ b/git/utils.go @@ -65,10 +65,6 @@ func WriteSSHKey(privateKey string) error { // WriteNetrc writes the netrc file. func WriteNetrc(machine, login, password string) error { - if machine == "" { - return nil - } - netrcContent := fmt.Sprintf( netrcFile, machine, diff --git a/git/verify.go b/git/verify.go deleted file mode 100644 index d119a15..0000000 --- a/git/verify.go +++ /dev/null @@ -1,17 +0,0 @@ -package git - -import ( - "os/exec" -) - -// SkipVerify disables globally the git ssl verification. -func SkipVerify() *exec.Cmd { - cmd := exec.Command( - "git", - "config", - "--global", - "http.sslVerify", - "false") - - return cmd -} diff --git a/plugin/git.go b/plugin/git.go index 80431de..5065b3a 100644 --- a/plugin/git.go +++ b/plugin/git.go @@ -2,6 +2,7 @@ package plugin import ( "fmt" + "os" "os/exec" "path/filepath" @@ -10,7 +11,16 @@ import ( // InitRepo initializes the repository. func (p Plugin) initRepo() error { - if isDirEmpty(filepath.Join(p.settings.Path, ".git")) { + path := filepath.Join(p.settings.Path, ".git") + if err := os.MkdirAll(p.settings.Path, os.ModePerm); err != nil { + return err + } + + if err := os.Chdir(p.settings.Path); err != nil { + return err + } + + if isDirEmpty(path) { return execute(exec.Command( "git", "init", @@ -53,10 +63,6 @@ func (p Plugin) checkoutHead() error { // HandleClone clones remote. func (p Plugin) handleClone() error { - if err := p.initRepo(); err != nil { - return err - } - if err := p.addRemote(); err != nil { return err } diff --git a/plugin/impl.go b/plugin/impl.go index f61d87f..9d9c325 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -25,17 +25,17 @@ type Author struct { // Settings for the Plugin. type Settings struct { - Actions cli.StringSlice - SSHKey string - Remote string - Branch string - Path string - Message string - Force bool - FollowTags bool - SkipVerify bool - EmptyCommit bool - NoVerify bool + Actions cli.StringSlice + SSHKey string + Remote string + Branch string + Path string + Message string + Force bool + FollowTags bool + InsecureSSLVerify bool + EmptyCommit bool + NoVerify bool Netrc Netrc Commit Commit @@ -44,8 +44,19 @@ type Settings struct { // Validate handles the settings validation of the plugin. func (p *Plugin) Validate() error { - if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" { - return fmt.Errorf("either SSH key or netrc password are required") + for _, action := range p.settings.Actions.Value() { + switch action { + case "clone": + continue + case "commit": + continue + case "push": + if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" { + return fmt.Errorf("either SSH key or netrc password are required") + } + default: + return fmt.Errorf("unknown action %s", action) + } } return nil @@ -53,28 +64,29 @@ func (p *Plugin) Validate() error { // Execute provides the implementation of the plugin. func (p *Plugin) Execute() error { + for _, env := range []string{"GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL"} { + if err := os.Unsetenv(env); err != nil { + return err + } + } + if err := os.Setenv("GIT_TERMINAL_PROMPT", "0"); err != nil { + return err + } + if p.settings.Path != "" { - if err := os.MkdirAll(p.settings.Path, os.ModePerm); err != nil { - return err - } - - if err := os.Chdir(p.settings.Path); err != nil { + if err := p.initRepo(); err != nil { return err } } - if err := git.GlobalName(p.settings.Commit.Author.Name).Run(); err != nil { + if err := git.SetUserName(p.settings.Commit.Author.Name).Run(); err != nil { return err } - - if err := git.GlobalUser(p.settings.Commit.Author.Email).Run(); err != nil { + if err := git.SetUserEmail(p.settings.Commit.Author.Email).Run(); err != nil { return err } - - if p.settings.SkipVerify { - if err := git.SkipVerify().Run(); err != nil { - return err - } + if err := git.SetSSLVerify(p.settings.InsecureSSLVerify).Run(); err != nil { + return err } if p.settings.SSHKey != "" { @@ -101,8 +113,6 @@ func (p *Plugin) Execute() error { if err := p.handlePush(); err != nil { return err } - default: - return fmt.Errorf("unknown action %s", action) } }