2022-01-21 10:10:00 -06:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2022-06-17 10:55:21 -05:00
|
|
|
"context"
|
2022-01-21 10:10:00 -06:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-02-03 13:35:49 -06:00
|
|
|
func TestSetRef(t *testing.T) {
|
2022-01-21 10:10:00 -06:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
oldFindGitRef := findGitRef
|
|
|
|
oldFindGitRevision := findGitRevision
|
|
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
findGitRef = func(ctx context.Context, file string) (string, error) {
|
2022-01-21 10:10:00 -06:00
|
|
|
return "refs/heads/master", nil
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
|
2022-01-21 10:10:00 -06:00
|
|
|
return "", "1234fakesha", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
eventName string
|
|
|
|
event map[string]interface{}
|
|
|
|
ref string
|
2023-03-09 14:03:13 -06:00
|
|
|
refName string
|
2022-01-21 10:10:00 -06:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
eventName: "pull_request_target",
|
2023-02-03 13:35:49 -06:00
|
|
|
event: map[string]interface{}{},
|
|
|
|
ref: "refs/heads/master",
|
2023-03-09 14:03:13 -06:00
|
|
|
refName: "master",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "pull_request",
|
|
|
|
event: map[string]interface{}{
|
2022-07-12 06:36:53 -05:00
|
|
|
"number": 1234.,
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
2023-03-09 14:03:13 -06:00
|
|
|
ref: "refs/pull/1234/merge",
|
|
|
|
refName: "1234/merge",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "deployment",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"deployment": map[string]interface{}{
|
|
|
|
"ref": "refs/heads/somebranch",
|
|
|
|
},
|
|
|
|
},
|
2023-03-09 14:03:13 -06:00
|
|
|
ref: "refs/heads/somebranch",
|
|
|
|
refName: "somebranch",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "release",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"release": map[string]interface{}{
|
|
|
|
"tag_name": "v1.0.0",
|
|
|
|
},
|
|
|
|
},
|
2023-03-09 14:03:13 -06:00
|
|
|
ref: "refs/tags/v1.0.0",
|
|
|
|
refName: "v1.0.0",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "push",
|
|
|
|
event: map[string]interface{}{
|
2023-02-03 13:35:49 -06:00
|
|
|
"ref": "refs/heads/somebranch",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
2023-03-09 14:03:13 -06:00
|
|
|
ref: "refs/heads/somebranch",
|
|
|
|
refName: "somebranch",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "unknown",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"repository": map[string]interface{}{
|
|
|
|
"default_branch": "main",
|
|
|
|
},
|
|
|
|
},
|
2023-03-09 14:03:13 -06:00
|
|
|
ref: "refs/heads/main",
|
|
|
|
refName: "main",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "no-event",
|
|
|
|
event: map[string]interface{}{},
|
|
|
|
ref: "refs/heads/master",
|
2023-03-09 14:03:13 -06:00
|
|
|
refName: "master",
|
2022-01-21 10:10:00 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
|
|
ghc := &GithubContext{
|
|
|
|
EventName: table.eventName,
|
|
|
|
BaseRef: "master",
|
|
|
|
Event: table.event,
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:35:49 -06:00
|
|
|
ghc.SetRef(context.Background(), "main", "/some/dir")
|
2023-03-09 14:03:13 -06:00
|
|
|
ghc.SetRefTypeAndName()
|
2022-01-21 10:10:00 -06:00
|
|
|
|
|
|
|
assert.Equal(t, table.ref, ghc.Ref)
|
2023-03-09 14:03:13 -06:00
|
|
|
assert.Equal(t, table.refName, ghc.RefName)
|
2022-01-21 10:10:00 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no-default-branch", func(t *testing.T) {
|
2022-06-17 10:55:21 -05:00
|
|
|
findGitRef = func(ctx context.Context, file string) (string, error) {
|
2022-01-21 10:10:00 -06:00
|
|
|
return "", fmt.Errorf("no default branch")
|
|
|
|
}
|
|
|
|
|
|
|
|
ghc := &GithubContext{
|
|
|
|
EventName: "no-default-branch",
|
|
|
|
Event: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
|
2023-02-03 13:35:49 -06:00
|
|
|
ghc.SetRef(context.Background(), "", "/some/dir")
|
2022-01-21 10:10:00 -06:00
|
|
|
|
2022-09-26 17:49:19 -05:00
|
|
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
2022-01-21 10:10:00 -06:00
|
|
|
})
|
|
|
|
}
|
2023-02-03 13:35:49 -06:00
|
|
|
|
|
|
|
func TestSetSha(t *testing.T) {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
|
|
|
|
oldFindGitRef := findGitRef
|
|
|
|
oldFindGitRevision := findGitRevision
|
|
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
|
|
|
|
|
|
findGitRef = func(ctx context.Context, file string) (string, error) {
|
|
|
|
return "refs/heads/master", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
|
|
|
|
return "", "1234fakesha", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
eventName string
|
|
|
|
event map[string]interface{}
|
|
|
|
sha string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
eventName: "pull_request_target",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"pull_request": map[string]interface{}{
|
|
|
|
"base": map[string]interface{}{
|
|
|
|
"sha": "pr-base-sha",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sha: "pr-base-sha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "pull_request",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"number": 1234.,
|
|
|
|
},
|
|
|
|
sha: "1234fakesha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "deployment",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"deployment": map[string]interface{}{
|
|
|
|
"sha": "deployment-sha",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sha: "deployment-sha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "release",
|
|
|
|
event: map[string]interface{}{},
|
|
|
|
sha: "1234fakesha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "push",
|
|
|
|
event: map[string]interface{}{
|
|
|
|
"after": "push-sha",
|
|
|
|
"deleted": false,
|
|
|
|
},
|
|
|
|
sha: "push-sha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "unknown",
|
|
|
|
event: map[string]interface{}{},
|
|
|
|
sha: "1234fakesha",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
eventName: "no-event",
|
|
|
|
event: map[string]interface{}{},
|
|
|
|
sha: "1234fakesha",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
|
|
ghc := &GithubContext{
|
|
|
|
EventName: table.eventName,
|
|
|
|
BaseRef: "master",
|
|
|
|
Event: table.event,
|
|
|
|
}
|
|
|
|
|
|
|
|
ghc.SetSha(context.Background(), "/some/dir")
|
|
|
|
|
|
|
|
assert.Equal(t, table.sha, ghc.Sha)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|