This commit is contained in:
Adam Majer 2024-09-05 15:02:26 +02:00
parent df5e073893
commit b7f5eb6d50
2 changed files with 70 additions and 6 deletions

View File

@ -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),

View File

@ -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)