mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
parent
a55d257be1
commit
2d5b34cfc2
2 changed files with 40 additions and 14 deletions
|
@ -238,6 +238,9 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
|
||||||
|
|
||||||
// At this point we need to know if it's a tag or a branch
|
// At this point we need to know if it's a tag or a branch
|
||||||
// And the easiest way to do it is duck typing
|
// And the easiest way to do it is duck typing
|
||||||
|
//
|
||||||
|
// If err is nil, it's a tag so let's proceed with that hash like we would if
|
||||||
|
// it was a sha
|
||||||
refType := "tag"
|
refType := "tag"
|
||||||
rev := plumbing.Revision(path.Join("refs", "tags", input.Ref))
|
rev := plumbing.Revision(path.Join("refs", "tags", input.Ref))
|
||||||
if _, err := r.Tag(input.Ref); errors.Is(err, git.ErrTagNotFound) {
|
if _, err := r.Tag(input.Ref); errors.Is(err, git.ErrTagNotFound) {
|
||||||
|
@ -258,29 +261,20 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) Executor {
|
||||||
if hash.String() != input.Ref {
|
if hash.String() != input.Ref {
|
||||||
|
|
||||||
// Run git fetch to make sure we have the latest sha
|
// Run git fetch to make sure we have the latest sha
|
||||||
err = r.Fetch(&git.FetchOptions{})
|
err := r.Fetch(&git.FetchOptions{})
|
||||||
if err != nil && err.Error() != "already up-to-date" {
|
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||||
logger.Debugf("Unable to fetch: %v", err)
|
logger.Debugf("Unable to fetch: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point we need to know if it's a tag or a branch
|
|
||||||
// And the easiest way to do it is duck typing
|
|
||||||
//
|
|
||||||
// If err is nil, it's a tag so let's proceed with that hash like we would if
|
|
||||||
// it was a sha
|
|
||||||
hash, err = r.ResolveRevision(rev)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("Unable to resolve %s: %v", rev, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if refType == "branch" {
|
if refType == "branch" {
|
||||||
logger.Debugf("Provided ref is not a sha. Checking out branch before pulling changes")
|
logger.Debugf("Provided ref is not a sha. Checking out branch before pulling changes")
|
||||||
|
sourceRef := plumbing.ReferenceName(path.Join("refs", "remotes", "origin", input.Ref))
|
||||||
err := w.Checkout(&git.CheckoutOptions{
|
err := w.Checkout(&git.CheckoutOptions{
|
||||||
Branch: refName,
|
Branch: sourceRef,
|
||||||
Force: true,
|
Force: true,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Unable to checkout %s: %v", refName, err)
|
logger.Errorf("Unable to checkout %s: %v", sourceRef, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -169,6 +170,37 @@ func TestGitFindRef(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGitCloneExecutor(t *testing.T) {
|
||||||
|
for name, tt := range map[string]struct {
|
||||||
|
URL string
|
||||||
|
Ref string
|
||||||
|
Err error
|
||||||
|
}{
|
||||||
|
"tag": {
|
||||||
|
URL: "https://github.com/actions/checkout",
|
||||||
|
Ref: "v2",
|
||||||
|
Err: nil,
|
||||||
|
},
|
||||||
|
"branch": {
|
||||||
|
URL: "https://github.com/anchore/scan-action",
|
||||||
|
Ref: "act-fails",
|
||||||
|
Err: nil,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
tt := tt
|
||||||
|
name := name
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
||||||
|
URL: tt.URL,
|
||||||
|
Ref: tt.Ref,
|
||||||
|
Dir: testDir(t),
|
||||||
|
})
|
||||||
|
err := clone(context.Background())
|
||||||
|
assert.ErrorIs(t, err, tt.Err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func gitConfig() {
|
func gitConfig() {
|
||||||
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
||||||
_ = gitCmd("config", "--global", "user.email", "test@test.com")
|
_ = gitCmd("config", "--global", "user.email", "test@test.com")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue