0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-06-02 18:29:41 +02:00

fix: unset git env vars to avoid overlapping with plugin config (#4)

This commit is contained in:
Robert Kaussow 2022-11-29 10:40:42 +01:00 committed by GitHub
parent 05857f9db3
commit 6735cb80e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 72 deletions

View File

@ -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

View File

@ -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,
},
}

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}