feat: add option to specify git remote name (#1104)

fixes https://github.com/nektos/act/issues/1099
fixes https://github.com/nektos/act/issues/983

Signed-off-by: Ryan <me@hackerc.at>

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Ryan 2022-04-04 19:53:08 +02:00 committed by GitHub
parent 9dc17f9144
commit d4272bd9fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 7 deletions

View file

@ -40,6 +40,7 @@ type Input struct {
artifactServerPort string
jsonLogger bool
noSkipCheckout bool
remoteName string
}
func (i *Input) resolve(path string) string {

View file

@ -43,6 +43,7 @@ func Execute(ctx context.Context, version string) {
rootCmd.Flags().StringP("job", "j", "", "run job")
rootCmd.Flags().BoolP("bug-report", "", false, "Display system information for bug report")
rootCmd.Flags().StringVar(&input.remoteName, "remote-name", "origin", "git remote name that will be used to retrieve url of git repo")
rootCmd.Flags().StringArrayVarP(&input.secrets, "secret", "s", []string{}, "secret to make available to actions with optional value (e.g. -s mysecret=foo or -s mysecret)")
rootCmd.Flags().StringArrayVarP(&input.envs, "env", "", []string{}, "env to make available to actions with optional value (e.g. --env myenv=foo or --env myenv)")
rootCmd.Flags().StringArrayVarP(&input.platforms, "platform", "P", []string{}, "custom image to use per platform (e.g. -P ubuntu-18.04=nektos/act-environments-ubuntu:18.04)")
@ -379,6 +380,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
ArtifactServerPath: input.artifactServerPath,
ArtifactServerPort: input.artifactServerPort,
NoSkipCheckout: input.noSkipCheckout,
RemoteName: input.remoteName,
}
r, err := runner.New(config)
if err != nil {

View file

@ -142,8 +142,12 @@ func findGitPrettyRef(head, root, sub string) (string, error) {
}
// FindGithubRepo get the repo
func FindGithubRepo(file string, githubInstance string) (string, error) {
url, err := findGitRemoteURL(file)
func FindGithubRepo(file, githubInstance, remoteName string) (string, error) {
if remoteName == "" {
remoteName = "origin"
}
url, err := findGitRemoteURL(file, remoteName)
if err != nil {
return "", err
}
@ -151,7 +155,7 @@ func FindGithubRepo(file string, githubInstance string) (string, error) {
return slug, err
}
func findGitRemoteURL(file string) (string, error) {
func findGitRemoteURL(file, remoteName string) (string, error) {
gitDir, err := findGitDirectory(file)
if err != nil {
return "", err
@ -162,7 +166,7 @@ func findGitRemoteURL(file string) (string, error) {
if err != nil {
return "", err
}
remote, err := gitconfig.GetSection("remote \"origin\"")
remote, err := gitconfig.GetSection(fmt.Sprintf(`remote "%s"`, remoteName))
if err != nil {
return "", err
}

View file

@ -47,7 +47,7 @@ func TestFindGitSlug(t *testing.T) {
func testDir(t *testing.T) string {
basedir, err := ioutil.TempDir("", "act-test")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(basedir) })
t.Cleanup(func() { _ = os.RemoveAll(basedir) })
return basedir
}
@ -86,7 +86,7 @@ func TestFindGitRemoteURL(t *testing.T) {
err = gitCmd("config", "-f", fmt.Sprintf("%s/.git/config", basedir), "--add", "remote.origin.url", remoteURL)
assert.NoError(err)
u, err := findGitRemoteURL(basedir)
u, err := findGitRemoteURL(basedir, "origin")
assert.NoError(err)
assert.Equal(remoteURL, u)
}

View file

@ -510,7 +510,7 @@ func (rc *RunContext) getGithubContext() *model.GithubContext {
}
repoPath := rc.Config.Workdir
repo, err := common.FindGithubRepo(repoPath, rc.Config.GitHubInstance)
repo, err := common.FindGithubRepo(repoPath, rc.Config.GitHubInstance, rc.Config.RemoteName)
if err != nil {
log.Warningf("unable to get git repo: %v", err)
} else {

View file

@ -51,6 +51,7 @@ type Config struct {
ArtifactServerPort string // the port the artifact server binds to
CompositeRestrictions *model.CompositeRestrictions // describes which features are available in composite actions
NoSkipCheckout bool // do not skip actions/checkout
RemoteName string // remote name in local git repo config
}
// Resolves the equivalent host path inside the container