0
0
mirror of https://github.com/thegeeklab/wp-git-action.git synced 2024-11-22 10:10:39 +00: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 - name: netrc_machine
description: Netrc remote machine name. description: Netrc remote machine name.
defaultvalue: github.com
type: string type: string
- name: netrc_username - name: netrc_username
description: Netrc login user on the remote machine. description: Netrc login user on the remote machine.
defaultvalue: token
type: string type: string
- name: netrc_password - name: netrc_password
@ -59,8 +61,8 @@ properties:
defaultvalue: false defaultvalue: false
type: bool type: bool
- name: skip_verify - name: insecure_ssl_verify
description: Skip SSL verification of the remote machine. description: Configure git SSL verification of the remote machine.
defaultvalue: false defaultvalue: false
type: bool type: bool

View File

@ -18,7 +18,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.author.name", Name: "commit-author-name",
Usage: "git author name", Usage: "git author name",
EnvVars: []string{"PLUGIN_AUTHOR_NAME", "DRONE_COMMIT_AUTHOR"}, EnvVars: []string{"PLUGIN_AUTHOR_NAME", "DRONE_COMMIT_AUTHOR"},
Destination: &settings.Commit.Author.Name, Destination: &settings.Commit.Author.Name,
@ -26,7 +26,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "commit.author.email", Name: "commit-author-email",
Usage: "git author email", Usage: "git author email",
EnvVars: []string{"PLUGIN_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL"}, EnvVars: []string{"PLUGIN_AUTHOR_EMAIL", "DRONE_COMMIT_AUTHOR_EMAIL"},
Destination: &settings.Commit.Author.Email, Destination: &settings.Commit.Author.Email,
@ -35,21 +35,23 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "netrc.machine", Name: "netrc-machine",
Usage: "netrc remote machine name", Usage: "netrc remote machine name",
EnvVars: []string{"PLUGIN_NETRC_MACHINE", "DRONE_NETRC_MACHINE"}, EnvVars: []string{"PLUGIN_NETRC_MACHINE", "DRONE_NETRC_MACHINE"},
Destination: &settings.Netrc.Machine, Destination: &settings.Netrc.Machine,
Value: "github.com",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "netrc.username", Name: "netrc-username",
Usage: "netrc login user on the remote machine", Usage: "netrc login user on the remote machine",
EnvVars: []string{"PLUGIN_NETRC_USERNAME", "DRONE_NETRC_USERNAME"}, EnvVars: []string{"PLUGIN_NETRC_USERNAME", "DRONE_NETRC_USERNAME"},
Destination: &settings.Netrc.Login, Destination: &settings.Netrc.Login,
Value: "token",
Category: category, Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "netrc.password", Name: "netrc-password",
Usage: "netrc login password on the remote machine", Usage: "netrc login password on the remote machine",
EnvVars: []string{"PLUGIN_NETRC_PASSWORD", "DRONE_NETRC_PASSWORD"}, EnvVars: []string{"PLUGIN_NETRC_PASSWORD", "DRONE_NETRC_PASSWORD"},
Destination: &settings.Netrc.Password, Destination: &settings.Netrc.Password,
@ -92,7 +94,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "commit message", Usage: "commit message",
EnvVars: []string{"PLUGIN_MESSAGE"}, EnvVars: []string{"PLUGIN_MESSAGE"},
Destination: &settings.Message, Destination: &settings.Message,
Value: "[skip ci] Commit dirty state", Value: "[skip ci] commit dirty state",
Category: category, Category: category,
}, },
@ -101,6 +103,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "enable force push to remote repository", Usage: "enable force push to remote repository",
EnvVars: []string{"PLUGIN_FORCE"}, EnvVars: []string{"PLUGIN_FORCE"},
Destination: &settings.Force, Destination: &settings.Force,
Value: false,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
@ -108,13 +111,15 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "follow tags for pushes to remote repository", Usage: "follow tags for pushes to remote repository",
EnvVars: []string{"PLUGIN_FOLLOWTAGS"}, EnvVars: []string{"PLUGIN_FOLLOWTAGS"},
Destination: &settings.FollowTags, Destination: &settings.FollowTags,
Value: false,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "skip-verify", Name: "insecure-ssl-verify",
Usage: "skip ssl verification of the remote machine", Usage: "set SSL verification of the remote machine",
EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, EnvVars: []string{"PLUGIN_INSECURE_SSL_VERIFY"},
Destination: &settings.SkipVerify, Destination: &settings.InsecureSSLVerify,
Value: false,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
@ -122,6 +127,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "allow empty commits", Usage: "allow empty commits",
EnvVars: []string{"PLUGIN_EMPTY_COMMIT"}, EnvVars: []string{"PLUGIN_EMPTY_COMMIT"},
Destination: &settings.EmptyCommit, Destination: &settings.EmptyCommit,
Value: false,
Category: category, Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
@ -129,6 +135,7 @@ func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
Usage: "bypass the pre-commit and commit-msg hooks", Usage: "bypass the pre-commit and commit-msg hooks",
EnvVars: []string{"PLUGIN_NO_VERIFY"}, EnvVars: []string{"PLUGIN_NO_VERIFY"},
Destination: &settings.NoVerify, Destination: &settings.NoVerify,
Value: false,
Category: category, Category: category,
}, },
} }

View File

@ -2,28 +2,41 @@ package git
import ( import (
"os/exec" "os/exec"
"strconv"
) )
// GlobalUser sets the global git author email. // SetUserEmail sets the global git author email.
func GlobalUser(email string) *exec.Cmd { func SetUserEmail(email string) *exec.Cmd {
cmd := exec.Command( cmd := exec.Command(
"git", "git",
"config", "config",
"--global", "--local",
"user.email", "user.email",
email) email)
return cmd return cmd
} }
// GlobalName sets the global git author name. // SetUserName sets the global git author name.
func GlobalName(author string) *exec.Cmd { func SetUserName(author string) *exec.Cmd {
cmd := exec.Command( cmd := exec.Command(
"git", "git",
"config", "config",
"--global", "--local",
"user.name", "user.name",
author) author)
return cmd 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. // WriteNetrc writes the netrc file.
func WriteNetrc(machine, login, password string) error { func WriteNetrc(machine, login, password string) error {
if machine == "" {
return nil
}
netrcContent := fmt.Sprintf( netrcContent := fmt.Sprintf(
netrcFile, netrcFile,
machine, 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 ( import (
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -10,7 +11,16 @@ import (
// InitRepo initializes the repository. // InitRepo initializes the repository.
func (p Plugin) initRepo() error { 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( return execute(exec.Command(
"git", "git",
"init", "init",
@ -53,10 +63,6 @@ func (p Plugin) checkoutHead() error {
// HandleClone clones remote. // HandleClone clones remote.
func (p Plugin) handleClone() error { func (p Plugin) handleClone() error {
if err := p.initRepo(); err != nil {
return err
}
if err := p.addRemote(); err != nil { if err := p.addRemote(); err != nil {
return err return err
} }

View File

@ -33,7 +33,7 @@ type Settings struct {
Message string Message string
Force bool Force bool
FollowTags bool FollowTags bool
SkipVerify bool InsecureSSLVerify bool
EmptyCommit bool EmptyCommit bool
NoVerify bool NoVerify bool
@ -44,38 +44,50 @@ type Settings struct {
// Validate handles the settings validation of the plugin. // Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error { func (p *Plugin) Validate() error {
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 == "" { if p.settings.SSHKey == "" && p.settings.Netrc.Password == "" {
return fmt.Errorf("either SSH key or netrc password are required") return fmt.Errorf("either SSH key or netrc password are required")
} }
default:
return fmt.Errorf("unknown action %s", action)
}
}
return nil return nil
} }
// Execute provides the implementation of the plugin. // Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error { 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 p.settings.Path != "" {
if err := os.MkdirAll(p.settings.Path, os.ModePerm); err != nil { if err := p.initRepo(); err != nil {
return err
}
if err := os.Chdir(p.settings.Path); err != nil {
return err 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 return err
} }
if err := git.SetUserEmail(p.settings.Commit.Author.Email).Run(); err != nil {
if err := git.GlobalUser(p.settings.Commit.Author.Email).Run(); err != nil {
return err return err
} }
if err := git.SetSSLVerify(p.settings.InsecureSSLVerify).Run(); err != nil {
if p.settings.SkipVerify {
if err := git.SkipVerify().Run(); err != nil {
return err return err
} }
}
if p.settings.SSHKey != "" { if p.settings.SSHKey != "" {
if err := git.WriteSSHKey(p.settings.SSHKey); err != nil { if err := git.WriteSSHKey(p.settings.SSHKey); err != nil {
@ -101,8 +113,6 @@ func (p *Plugin) Execute() error {
if err := p.handlePush(); err != nil { if err := p.handlePush(); err != nil {
return err return err
} }
default:
return fmt.Errorf("unknown action %s", action)
} }
} }