From fb77d5a88388cfc882cf3ad45a607b7d7b6931e9 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Thu, 14 Feb 2019 14:24:18 -0600 Subject: [PATCH 1/2] Regex tweaks - No need to compile regexes in the function, moved them to vars - Fixed github http regex: it was not matching https://github.com/foo/bar (no git extension) --- common/git.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/common/git.go b/common/git.go index edcd105..502f2c8 100644 --- a/common/git.go +++ b/common/git.go @@ -20,7 +20,14 @@ import ( yaml "gopkg.in/yaml.v2" ) -var cloneLock sync.Mutex +var ( + codeCommitHTTPRegex = regexp.MustCompile(`^https?://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`) + codeCommitSSHRegex = regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`) + githubHTTPRegex = regexp.MustCompile(`^https?://.*github.com.*/(.+)/(.+?)(?:.git)?$`) + githubSSHRegex = regexp.MustCompile(`github.com[:/](.+)/(.+).git$`) + + cloneLock sync.Mutex +) // FindGitRevision get the current git revision func FindGitRevision(file string) (shortSha string, sha string, err error) { @@ -134,18 +141,13 @@ func findGitRemoteURL(file string) (string, error) { } func findGitSlug(url string) (string, string, error) { - codeCommitHTTPRegex := regexp.MustCompile(`^http(s?)://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`) - codeCommitSSHRegex := regexp.MustCompile(`ssh://git-codecommit\.(.+)\.amazonaws.com/v1/repos/(.+)$`) - httpRegex := regexp.MustCompile("^http(s?)://.*github.com.*/(.+)/(.+).git$") - sshRegex := regexp.MustCompile("github.com[:/](.+)/(.+).git$") - if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil { - return "CodeCommit", matches[3], nil + return "CodeCommit", matches[2], nil } else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil { return "CodeCommit", matches[2], nil - } else if matches := httpRegex.FindStringSubmatch(url); matches != nil { - return "GitHub", fmt.Sprintf("%s/%s", matches[2], matches[3]), nil - } else if matches := sshRegex.FindStringSubmatch(url); matches != nil { + } else if matches := githubHTTPRegex.FindStringSubmatch(url); matches != nil { + return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil + } else if matches := githubSSHRegex.FindStringSubmatch(url); matches != nil { return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil } return "", url, nil From 6addc48cde9a7ecf468d324b4c4f29d02ecf23c8 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Thu, 14 Feb 2019 14:35:02 -0600 Subject: [PATCH 2/2] Add extra test cases for testing git slugs --- common/git_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/git_test.go b/common/git_test.go index f1312d1..2dcc37a 100644 --- a/common/git_test.go +++ b/common/git_test.go @@ -25,6 +25,8 @@ func TestFindGitSlug(t *testing.T) { {"git@github.com:nektos/act.git", "GitHub", "nektos/act"}, {"https://github.com/nektos/act.git", "GitHub", "nektos/act"}, {"http://github.com/nektos/act.git", "GitHub", "nektos/act"}, + {"https://github.com/nektos/act", "GitHub", "nektos/act"}, + {"http://github.com/nektos/act", "GitHub", "nektos/act"}, {"git+ssh://git@github.com/owner/repo.git", "GitHub", "owner/repo"}, {"http://myotherrepo.com/act.git", "", "http://myotherrepo.com/act.git"}, }