This commit is contained in:
2025-02-14 17:13:51 +01:00
parent 072d7b4825
commit 1c38c2105b
10 changed files with 243 additions and 49 deletions

View File

@@ -27,6 +27,7 @@ import (
"path"
"path/filepath"
"slices"
"strings"
"time"
"src.opensuse.org/autogits/common"
@@ -97,7 +98,7 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
config.Branch = action.Repository.Default_Branch
}
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Organization, prjgit)
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, action.Organization.Username, prjgit)
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
@@ -110,12 +111,13 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
return fmt.Errorf(" - '%s' repo is not sha256. Ignoring.", action.Repository.Name)
}
common.PanicOnError(git.GitExec(prjgit, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url, action.Repository.Name))
if _, err := git.GitBranchHead(path.Join(prjgit, action.Repository.Name), config.Branch); err != nil {
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(prjgit, action.Repository.Name), "branch", "--show-current"))
if branch != config.Branch {
if err := git.GitExec(path.Join(prjgit, action.Repository.Name), "fetch", "--depth", "1", "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
}
common.PanicOnError(git.GitExec(path.Join(prjgit, action.Repository.Name), "checkout", config.Branch))
}
common.PanicOnError(git.GitExec(path.Join(prjgit, action.Repository.Name), "checkout", config.Branch))
common.PanicOnError(git.GitExec(prjgit, "commit", "-m", "Automatic package inclusion via Direct Workflow"))
common.PanicOnError(git.GitExec(prjgit, "push"))
@@ -177,7 +179,7 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
log.Println(" + default branch", action.Repository.Default_Branch)
}
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, *action.Repository.Owner, prjgit)
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, action.Repository.Owner.Username, prjgit)
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
@@ -209,7 +211,7 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
return nil
}
func verifyProjectState(git common.Git, orgName string, config *common.AutogitConfig, configs []*common.AutogitConfig) (err error) {
func verifyProjectState(git common.Git, org string, config *common.AutogitConfig, configs []*common.AutogitConfig) (err error) {
defer func() {
e := recover()
if e != nil {
@@ -220,12 +222,9 @@ func verifyProjectState(git common.Git, orgName string, config *common.AutogitCo
}
}()
org := common.Organization{
Username: orgName,
}
repo, err := gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName)
if err != nil {
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", orgName, config.GitProjectName, err)
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", org, config.GitProjectName, err)
}
common.PanicOnError(git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName))
@@ -245,7 +244,7 @@ next_package:
}
log.Println(" verifying package:", filename, commitId, config.Branch)
commits, err := gitea.GetRecentCommits(orgName, filename, config.Branch, 10)
commits, err := gitea.GetRecentCommits(org, filename, config.Branch, 10)
if err != nil {
// assumption that package does not exist, remove from project
// https://github.com/go-gitea/gitea/issues/31976
@@ -285,7 +284,7 @@ next_package:
if DebugMode {
log.Println("checking for missing repositories...")
}
repos, err := gitea.GetOrganizationRepositories(orgName)
repos, err := gitea.GetOrganizationRepositories(org)
if err != nil {
return err
}
@@ -308,7 +307,7 @@ next_repo:
}
for _, c := range configs {
if c.Organization == orgName && c.GitProjectName == r.Name {
if c.Organization == org && c.GitProjectName == r.Name {
// ignore project gits
continue next_repo
}
@@ -325,7 +324,7 @@ next_repo:
log.Println(" -- checking repository:", r.Name)
}
if _, err := gitea.GetRecentCommits(orgName, r.Name, config.Branch, 1); err != nil {
if _, err := gitea.GetRecentCommits(org, r.Name, config.Branch, 1); err != nil {
// assumption that package does not exist, so not part of project
// https://github.com/go-gitea/gitea/issues/31976
continue
@@ -335,10 +334,13 @@ next_repo:
common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.CloneURL, r.Name))
if len(config.Branch) > 0 {
if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil {
return fmt.Errorf("Fetching branch %s for %s/%s failed. Ignoring.", config.Branch, repo.Owner.UserName, r.Name)
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(config.GitProjectName, r.Name), "branch", "--show-current"))
if branch != config.Branch {
if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil {
return fmt.Errorf("Fetching branch %s for %s/%s failed. Ignoring.", config.Branch, repo.Owner.UserName, r.Name)
}
common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch))
}
common.PanicOnError(git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch))
}
isGitUpdated = true
@@ -350,7 +352,7 @@ next_repo:
}
if DebugMode {
log.Println("Verification finished for ", orgName, ", config", config.GitProjectName)
log.Println("Verification finished for ", org, ", config", config.GitProjectName)
}
return nil