update repo parsing
This commit is contained in:
@@ -166,12 +166,24 @@ func (config *AutogitConfig) GetPrjGit() (string, string, string) {
|
|||||||
branch := "main"
|
branch := "main"
|
||||||
|
|
||||||
a := strings.Split(config.GitProjectName, "/")
|
a := strings.Split(config.GitProjectName, "/")
|
||||||
org = a[0]
|
if len(a[0]) > 0 {
|
||||||
|
repo = strings.TrimSpace(a[0])
|
||||||
|
}
|
||||||
if len(a) == 2 {
|
if len(a) == 2 {
|
||||||
b := strings.Split(a[1], "#")
|
if a[0] = strings.TrimSpace(a[0]); len(a[0]) > 0 {
|
||||||
repo = b[0]
|
org = a[0]
|
||||||
|
}
|
||||||
|
repo = strings.TrimSpace(a[1])
|
||||||
|
}
|
||||||
|
b := strings.Split(repo, "#")
|
||||||
if len(b) == 2 {
|
if len(b) == 2 {
|
||||||
branch = b[1]
|
if b[0] = strings.TrimSpace(b[0]); len(b[0]) > 0 {
|
||||||
|
repo = b[0]
|
||||||
|
} else {
|
||||||
|
repo = DefaultGitPrj
|
||||||
|
}
|
||||||
|
if b[1] = strings.TrimSpace(b[1]); len(b[1]) > 0 {
|
||||||
|
branch = strings.TrimSpace(b[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
package common_test
|
package common_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.uber.org/mock/gomock"
|
"go.uber.org/mock/gomock"
|
||||||
mock_common "src.opensuse.org/autogits/common/mock"
|
|
||||||
"src.opensuse.org/autogits/common"
|
"src.opensuse.org/autogits/common"
|
||||||
"src.opensuse.org/autogits/common/gitea-generated/models"
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
||||||
|
mock_common "src.opensuse.org/autogits/common/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfigWorkflowParser(t *testing.T) {
|
func TestConfigWorkflowParser(t *testing.T) {
|
||||||
@@ -47,3 +48,83 @@ func TestConfigWorkflowParser(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProjectGitParser(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
prjgit string
|
||||||
|
org string
|
||||||
|
branch string
|
||||||
|
res [3]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "repo only",
|
||||||
|
prjgit: "repo.git",
|
||||||
|
org: "org",
|
||||||
|
branch: "br",
|
||||||
|
res: [3]string{"org", "repo.git", "main"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "default",
|
||||||
|
org: "org",
|
||||||
|
res: [3]string{"org", common.DefaultGitPrj, "main"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "repo with branch",
|
||||||
|
org: "org2",
|
||||||
|
prjgit: "repo.git#somebranch",
|
||||||
|
res: [3]string{"org2", "repo.git", "somebranch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "repo org and branch",
|
||||||
|
org: "org3",
|
||||||
|
prjgit: "oorg/foo.bar#point",
|
||||||
|
res: [3]string{"oorg", "foo.bar", "point"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "whitespace shouldn't matter",
|
||||||
|
prjgit: " oorg / \nfoo.bar\t # point ",
|
||||||
|
res: [3]string{"oorg", "foo.bar", "point"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "repo org and empty branch",
|
||||||
|
org: "org3",
|
||||||
|
prjgit: "oorg/foo.bar#",
|
||||||
|
res: [3]string{"oorg", "foo.bar", "main"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only branch defined",
|
||||||
|
org: "org3",
|
||||||
|
prjgit: "#mybranch",
|
||||||
|
res: [3]string{"org3", "_ObsPrj", "mybranch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "only org and branch defined",
|
||||||
|
org: "org3",
|
||||||
|
prjgit: "org1/#mybranch",
|
||||||
|
res: [3]string{"org1", "_ObsPrj", "mybranch"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty org and repo",
|
||||||
|
org: "org3",
|
||||||
|
prjgit: "/repo#",
|
||||||
|
res: [3]string{"org3", "repo", "main"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
c := &common.AutogitConfig{
|
||||||
|
Organization: test.org,
|
||||||
|
Branch: test.branch,
|
||||||
|
GitProjectName: test.prjgit,
|
||||||
|
}
|
||||||
|
|
||||||
|
i, j, k := c.GetPrjGit()
|
||||||
|
res := []string{i, j, k}
|
||||||
|
if !slices.Equal(res, test.res[:]) {
|
||||||
|
t.Error("Expected", test.res, "but received", res)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -117,6 +117,8 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
|
|||||||
return fmt.Errorf(" - '%s' repo is not sha256. Ignoring.", action.Repository.Name)
|
return fmt.Errorf(" - '%s' repo is not sha256. Ignoring.", action.Repository.Name)
|
||||||
}
|
}
|
||||||
common.PanicOnError(git.GitExec(gitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url, action.Repository.Name))
|
common.PanicOnError(git.GitExec(gitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url, action.Repository.Name))
|
||||||
|
defer func() { common.PanicOnError(git.GitExec(gitPrj, "submodule", "deinit", "--all")) }()
|
||||||
|
|
||||||
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(gitPrj, action.Repository.Name), "branch", "--show-current"))
|
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(gitPrj, action.Repository.Name), "branch", "--show-current"))
|
||||||
if branch != config.Branch {
|
if branch != config.Branch {
|
||||||
if err := git.GitExec(path.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil {
|
if err := git.GitExec(path.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil {
|
||||||
@@ -181,6 +183,7 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
|
|||||||
common.PanicOnError(err)
|
common.PanicOnError(err)
|
||||||
defer git.Close()
|
defer git.Close()
|
||||||
|
|
||||||
|
log.Printf("push to: %s/%s for %s/%s#%s", action.Repository.Owner.Username, action.Repository.Name, gitOrg, gitPrj, gitBranch)
|
||||||
if len(config.Branch) == 0 {
|
if len(config.Branch) == 0 {
|
||||||
config.Branch = action.Repository.Default_Branch
|
config.Branch = action.Repository.Default_Branch
|
||||||
log.Println(" + default branch", action.Repository.Default_Branch)
|
log.Println(" + default branch", action.Repository.Default_Branch)
|
||||||
@@ -200,19 +203,20 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
common.PanicOnError(git.GitExec(gitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name))
|
git.GitExecOrPanic(gitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name)
|
||||||
if err := git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil {
|
defer git.GitExecOrPanic(gitPrj, "submodule", "deinit", "--all")
|
||||||
|
|
||||||
|
if err := git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "--force", "origin", config.Branch+":"+config.Branch); err != nil {
|
||||||
return fmt.Errorf("error fetching branch %s. ignoring as non-existent. err: %w", config.Branch, err) // no branch? so ignore repo here
|
return fmt.Errorf("error fetching branch %s. ignoring as non-existent. err: %w", config.Branch, err) // no branch? so ignore repo here
|
||||||
}
|
}
|
||||||
id, err := git.GitBranchHead(filepath.Join(gitPrj, action.Repository.Name), config.Branch)
|
id, err := git.GitBranchHead(filepath.Join(gitPrj, action.Repository.Name), config.Branch)
|
||||||
common.PanicOnError(err)
|
common.PanicOnError(err)
|
||||||
for _, commitId := range action.Commits {
|
for _, commitId := range action.Commits {
|
||||||
if commitId.Id == id {
|
if commitId.Id == id {
|
||||||
common.PanicOnError(git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id))
|
git.GitExecOrPanic(filepath.Join(gitPrj, action.Repository.Name), "checkout", id)
|
||||||
common.PanicOnError(git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "checkout", id))
|
git.GitExecOrPanic(gitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
|
||||||
common.PanicOnError(git.GitExec(gitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow"))
|
|
||||||
if !noop {
|
if !noop {
|
||||||
common.PanicOnError(git.GitExec(gitPrj, "push"))
|
git.GitExecOrPanic(gitPrj, "push")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -242,6 +246,8 @@ func verifyProjectState(git common.Git, org string, config *common.AutogitConfig
|
|||||||
if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
|
if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
|
||||||
common.PanicOnError(git.GitClone(gitPrj, gitBranch, repo.SSHURL))
|
common.PanicOnError(git.GitClone(gitPrj, gitBranch, repo.SSHURL))
|
||||||
}
|
}
|
||||||
|
defer func() { common.PanicOnError(git.GitExec(gitPrj, "submodule", "deinit", "--all")) }()
|
||||||
|
|
||||||
log.Println(" * Getting submodule list")
|
log.Println(" * Getting submodule list")
|
||||||
sub, err := git.GitSubmoduleList(gitPrj, "HEAD")
|
sub, err := git.GitSubmoduleList(gitPrj, "HEAD")
|
||||||
common.PanicOnError(err)
|
common.PanicOnError(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user