From b7f5eb6d50e0f36126214e5d4c0521544bcdce9e50bf45953aa5202bde3b8144 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Thu, 5 Sep 2024 15:02:26 +0200 Subject: [PATCH] . --- bots-common/gitea_utils.go | 25 +++++++++++++++++++ prjgit-updater/main.go | 51 +++++++++++++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 2ef13d6..2b392e2 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -132,6 +132,31 @@ func (gitea *GiteaTransport) GetOrganization(orgName string) (*models.Organizati return org.Payload, nil } +func (gitea *GiteaTransport) GetOrganizationRepositories(orgName string) ([]*models.Repository, error) { + var page int64 + repos := make([]*models.Repository, 0, 100) + + page = 1 + for { + ret, err := gitea.client.Organization.OrgListRepos( + organization.NewOrgListReposParams().WithOrg(orgName).WithPage(&page), + gitea.transport.DefaultAuthentication, + ) + + if err != nil { + return nil, fmt.Errorf("Error retrieving repository list for org: '%s'. Err: %w", orgName, err) + } + + if len(ret.Payload) == 0 { + break + } + + repos = append(repos, ret.Payload...) + } + + return repos, nil +} + func (gitea *GiteaTransport) CreateRepositoryIfNotExist(git *GitHandler, org Organization, repoName string) (*models.Repository, error) { repo, err := gitea.client.Repository.RepoGet( repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName), diff --git a/prjgit-updater/main.go b/prjgit-updater/main.go index c3b2b46..c675110 100644 --- a/prjgit-updater/main.go +++ b/prjgit-updater/main.go @@ -6,6 +6,7 @@ import ( "log" "math/rand" "os" + "path" "path/filepath" "slices" "time" @@ -185,7 +186,7 @@ func processConfiguredPushAction(h *common.RequestHandler, action *common.PushWe return nil } -func verifyProjectState(git *common.GitHandler, orgName string, config *common.AutogitConfig) error { +func verifyProjectState(git *common.GitHandler, orgName string, config *common.AutogitConfig, configs []*common.AutogitConfig) error { org := common.Organization{ Username: orgName, } @@ -210,15 +211,16 @@ func verifyProjectState(git *common.GitHandler, orgName string, config *common.A commits, err := gitea.GetRecentCommits(orgName, filename, config.Branch, 10) if err != nil { // assumption that package does not exist, remove from project + // https://github.com/go-gitea/gitea/issues/31976 if err := git.GitExec(config.GitProjectName, "rm", filename); err != nil { return fmt.Errorf("Failed to remove deleted submodule. Err: %w", err) } isGitUpdated = true continue } -// if err != nil { -// return fmt.Errorf("Failed to fetch recent commits for package: '%s'. Err: %w", filename, err) -// } + // if err != nil { + // return fmt.Errorf("Failed to fetch recent commits for package: '%s'. Err: %w", filename, err) + // } idx := 1000 for i, c := range commits { @@ -248,6 +250,43 @@ func verifyProjectState(git *common.GitHandler, orgName string, config *common.A } } + // find all missing repositories, and add them + repos, err := gitea.GetOrganizationRepositories(orgName) + if err != nil { + return err + } + +next_repo: + for _, r := range repos { + for _, c := range configs { + if c.Organization == orgName && c.GitProjectName == r.Name { + break next_repo + } + } + + if _, err := gitea.GetRecentCommits(orgName, r.Name, config.Branch, 10); err != nil { + // assumption that package does not exist, so not part of project + // https://github.com/go-gitea/gitea/issues/31976 + break + } + + // add repository to git project + if err := git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.SSHURL); err != nil { + return fmt.Errorf("Cannot add submodule '%s' to project '%s'. Err: %w", r.Name, config.GitProjectName, err) + } + + if len(config.Branch) > 0 { + if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch); err != nil { + return fmt.Errorf("Failed to fetch branch '%s' from '%s'/'%s'. Err: %w", config.Branch, orgName, r.Name, err) + } + if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "checkout", config.Branch); err != nil { + return fmt.Errorf("Failed to checkout fetched branch '%s' from '%s'/'%s'. Err: %w", config.Branch, orgName, r.Name, err) + } + } + + isGitUpdated = true + } + if isGitUpdated { if err := git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC"); err != nil { return err @@ -274,7 +313,7 @@ func consistencyCheckProcess() { log.Println("Failed to allocate GitHandler:", err) return } - if err := verifyProjectState(git, org, config); err != nil { + if err := verifyProjectState(git, org, config, configs); err != nil { log.Println("Failed to verify state of org:", org, err) return } @@ -293,7 +332,7 @@ func consistencyCheckProcess() { log.Println("Faield to allocate GitHandler:", err) return } - if err := verifyProjectState(git, org, config); err != nil { + if err := verifyProjectState(git, org, config, configs); err != nil { log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err) } log.Printf(" ++ verification complete, org: `%s`\n", org)