Merge pull request #68 from caarlos0/master

feat: support branches as GITHUB_REF
This commit is contained in:
Casey Lee 2019-05-22 23:15:58 -07:00 committed by GitHub
commit 308162e2eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 62 deletions

View file

@ -199,9 +199,9 @@ func (runner *runnerImpl) applyEnvironment(env map[string]string) {
if err != nil { if err != nil {
log.Warningf("unable to get git ref: %v", err) log.Warningf("unable to get git ref: %v", err)
} else { } else {
log.Infof("using github ref: %s", ref)
env["GITHUB_REF"] = ref env["GITHUB_REF"] = ref
} }
} }
func (runner *runnerImpl) createGithubTarball() (io.Reader, error) { func (runner *runnerImpl) createGithubTarball() (io.Reader, error) {

View file

@ -49,20 +49,23 @@ func TestRunEvent(t *testing.T) {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
for _, table := range tables { for _, table := range tables {
runnerConfig := &RunnerConfig{ table := table
Ctx: context.Background(), t.Run(table.workflowPath, func(t *testing.T) {
WorkflowPath: table.workflowPath, runnerConfig := &RunnerConfig{
WorkingDir: "testdata", Ctx: context.Background(),
EventName: table.eventName, WorkflowPath: table.workflowPath,
} WorkingDir: "testdata",
runner, err := NewRunner(runnerConfig) EventName: table.eventName,
assert.NilError(t, err, table.workflowPath) }
runner, err := NewRunner(runnerConfig)
err = runner.RunEvent()
if table.errorMessage == "" {
assert.NilError(t, err, table.workflowPath) assert.NilError(t, err, table.workflowPath)
} else {
assert.ErrorContains(t, err, table.errorMessage) err = runner.RunEvent()
} if table.errorMessage == "" {
assert.NilError(t, err, table.workflowPath)
} else {
assert.ErrorContains(t, err, table.errorMessage)
}
})
} }
} }

View file

@ -1,9 +1,17 @@
workflow "test" { workflow "test" {
on = "push" on = "push"
resolves = ["test-action"] resolves = [
"test-action-repo",
"test-action-ref",
]
} }
action "test-action" { action "test-action-repo" {
uses = "docker://alpine:3.9" uses = "docker://alpine:3.9"
runs = ["sh", "-c", "echo $GITHUB_REPOSITORY | grep '^nektos/act$'"] runs = ["sh", "-c", "echo $GITHUB_REPOSITORY | grep '^nektos/act$'"]
} }
action "test-action-ref" {
uses = "docker://alpine:3.9"
runs = ["sh", "-c", "echo $GITHUB_REF | grep '^refs/'"]
}

View file

@ -1,13 +1,10 @@
package common package common
import ( import (
"bufio"
"bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -17,7 +14,6 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
git "gopkg.in/src-d/go-git.v4" git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/src-d/go-git.v4/plumbing"
yaml "gopkg.in/yaml.v2"
) )
var ( var (
@ -36,15 +32,16 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
return "", "", err return "", "", err
} }
ref, err := FindGitRef(file) bts, err := ioutil.ReadFile(filepath.Join(gitDir, "HEAD"))
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
var ref = strings.TrimSpace(strings.TrimPrefix(string(bts), "ref:"))
var refBuf []byte var refBuf []byte
if strings.HasPrefix(ref, "refs/") { if strings.HasPrefix(ref, "refs/") {
// load commitid ref // load commitid ref
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, ref)) refBuf, err = ioutil.ReadFile(filepath.Join(gitDir, ref))
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -56,19 +53,6 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
return string(refBuf[:7]), strings.TrimSpace(string(refBuf)), nil return string(refBuf[:7]), strings.TrimSpace(string(refBuf)), nil
} }
// FindGitBranch get the current git branch
func FindGitBranch(file string) (string, error) {
ref, err := FindGitRef(file)
if err != nil {
return "", err
}
// get branch name
branch := strings.TrimPrefix(ref, "refs/heads/")
log.Debugf("Found branch: %s", branch)
return branch, nil
}
// FindGitRef get the current git ref // FindGitRef get the current git ref
func FindGitRef(file string) (string, error) { func FindGitRef(file string) (string, error) {
gitDir, err := findGitDirectory(file) gitDir, err := findGitDirectory(file)
@ -77,34 +61,46 @@ func FindGitRef(file string) (string, error) {
} }
log.Debugf("Loading revision from git directory '%s'", gitDir) log.Debugf("Loading revision from git directory '%s'", gitDir)
// load HEAD ref _, ref, err := FindGitRevision(file)
headFile, err := os.Open(fmt.Sprintf("%s/HEAD", gitDir))
if err != nil { if err != nil {
return "", err return "", err
} }
defer func() {
headFile.Close()
}()
headBuffer := new(bytes.Buffer)
_, err = headBuffer.ReadFrom(bufio.NewReader(headFile))
if err != nil {
log.Error(err)
}
headBytes := headBuffer.Bytes()
var ref string
head := make(map[string]string)
err = yaml.Unmarshal(headBytes, head)
if err != nil {
ref = string(headBytes)
} else {
ref = head["ref"]
}
log.Debugf("HEAD points to '%s'", ref) log.Debugf("HEAD points to '%s'", ref)
return strings.TrimSpace(ref), nil // try tags first
tag, err := findGitPrettyRef(ref, gitDir, "refs/tags")
if err != nil || tag != "" {
return tag, err
}
// and then branches
return findGitPrettyRef(ref, gitDir, "refs/heads")
}
func findGitPrettyRef(head, root, sub string) (string, error) {
var name string
var err = filepath.Walk(filepath.Join(root, sub), func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if name != "" {
return nil
}
if info.IsDir() {
return nil
}
bts, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var pointsTo = strings.TrimSpace(string(bts))
if head == pointsTo {
name = strings.TrimPrefix(strings.Replace(path, root, "", 1), "/")
log.Debugf("HEAD matches %s", name)
}
return nil
})
return name, err
} }
// FindGithubRepo get the repo // FindGithubRepo get the repo
@ -169,10 +165,10 @@ func findGitDirectory(fromFile string) (string, error) {
if fi.Mode().IsDir() { if fi.Mode().IsDir() {
dir = absPath dir = absPath
} else { } else {
dir = path.Dir(absPath) dir = filepath.Dir(absPath)
} }
gitPath := path.Join(dir, ".git") gitPath := filepath.Join(dir, ".git")
fi, err = os.Stat(gitPath) fi, err = os.Stat(gitPath)
if err == nil && fi.Mode().IsDir() { if err == nil && fi.Mode().IsDir() {
return gitPath, nil return gitPath, nil
@ -181,7 +177,6 @@ func findGitDirectory(fromFile string) (string, error) {
} }
return findGitDirectory(filepath.Dir(dir)) return findGitDirectory(filepath.Dir(dir))
} }
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor // NewGitCloneExecutorInput the input for the NewGitCloneExecutor

View file

@ -6,10 +6,12 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"syscall" "syscall"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestFindGitSlug(t *testing.T) { func TestFindGitSlug(t *testing.T) {
@ -61,6 +63,86 @@ func TestFindGitRemoteURL(t *testing.T) {
assert.Equal(remoteURL, u) assert.Equal(remoteURL, u)
} }
func TestGitFindRef(t *testing.T) {
basedir, err := ioutil.TempDir("", "act-test")
defer os.RemoveAll(basedir)
assert.NoError(t, err)
for name, tt := range map[string]struct {
Prepare func(t *testing.T, dir string)
Assert func(t *testing.T, ref string, err error)
}{
"new_repo": {
Prepare: func(t *testing.T, dir string) {},
Assert: func(t *testing.T, ref string, err error) {
require.Error(t, err)
},
},
"new_repo_with_commit": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
},
"current_head_is_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "commit msg"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.2.3"))
require.NoError(t, gitCmd("-C", dir, "checkout", "v1.2.3"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.2.3", ref)
},
},
"current_head_is_same_as_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "1.4.2 release"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.4.2", ref)
},
},
"current_head_is_not_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2"))
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg2"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
},
"current_head_is_another_branch": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "checkout", "-b", "mybranch"))
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/mybranch", ref)
},
},
} {
tt := tt
name := name
t.Run(name, func(t *testing.T) {
dir := filepath.Join(basedir, name)
require.NoError(t, os.MkdirAll(dir, 0755))
require.NoError(t, gitCmd("-C", dir, "init"))
tt.Prepare(t, dir)
ref, err := FindGitRef(dir)
tt.Assert(t, ref, err)
})
}
}
func gitCmd(args ...string) error { func gitCmd(args ...string) error {
var stdout bytes.Buffer var stdout bytes.Buffer
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)