workflow-direct: move away from prjgit repo being just repo

This commit is contained in:
2025-04-16 18:07:37 +02:00
parent 1d1602852c
commit 0e036b5ec6
3 changed files with 59 additions and 59 deletions

View File

@@ -313,6 +313,7 @@ func (gitea *GiteaTransport) GetOrganizationRepositories(orgName string) ([]*mod
} }
func (gitea *GiteaTransport) CreateRepositoryIfNotExist(git Git, org, repoName string) (*models.Repository, error) { func (gitea *GiteaTransport) CreateRepositoryIfNotExist(git Git, org, repoName string) (*models.Repository, error) {
log.Println(org, repoName)
repo, err := gitea.client.Repository.RepoGet( repo, err := gitea.client.Repository.RepoGet(
repository.NewRepoGetParams().WithDefaults().WithOwner(org).WithRepo(repoName), repository.NewRepoGetParams().WithDefaults().WithOwner(org).WithRepo(repoName),
gitea.transport.DefaultAuthentication) gitea.transport.DefaultAuthentication)
@@ -320,6 +321,7 @@ func (gitea *GiteaTransport) CreateRepositoryIfNotExist(git Git, org, repoName s
if err != nil { if err != nil {
switch err.(type) { switch err.(type) {
case *repository.RepoGetNotFound: case *repository.RepoGetNotFound:
log.Println("not found", err)
repo, err := gitea.client.Organization.CreateOrgRepo( repo, err := gitea.client.Organization.CreateOrgRepo(
organization.NewCreateOrgRepoParams().WithDefaults().WithBody( organization.NewCreateOrgRepoParams().WithDefaults().WithBody(
&models.CreateRepoOption{ &models.CreateRepoOption{

View File

@@ -80,7 +80,7 @@ type GitUrl struct {
Commit string Commit string
} }
var valid_schemas []string = []string{"https", "ssh", "http"} var valid_schemas []string = []string{"https", "ssh", "http", "file"}
func ParseGitRemoteUrl(urlString string) (*GitUrl, error) { func ParseGitRemoteUrl(urlString string) (*GitUrl, error) {
url, err := url.Parse(urlString) url, err := url.Parse(urlString)

View File

@@ -78,7 +78,7 @@ func (*RepositoryActionProcessor) ProcessFunc(request *common.Request) error {
} }
for _, config := range configs { for _, config := range configs {
if config.GitProjectName == action.Repository.Name { if org, repo, _ := config.GetPrjGit(); org == action.Repository.Owner.Username && repo == action.Repository.Name {
log.Println("+ ignoring repo event for PrjGit repository", config.GitProjectName) log.Println("+ ignoring repo event for PrjGit repository", config.GitProjectName)
return nil return nil
} }
@@ -93,7 +93,7 @@ func (*RepositoryActionProcessor) ProcessFunc(request *common.Request) error {
} }
func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error { func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, config *common.AutogitConfig) error {
prjgit := config.GitProjectName gitOrg, gitPrj, gitBranch := config.GetPrjGit()
git, err := gh.CreateGitHandler(config.Organization) git, err := gh.CreateGitHandler(config.Organization)
common.PanicOnError(err) common.PanicOnError(err)
defer git.Close() defer git.Close()
@@ -102,14 +102,13 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
config.Branch = action.Repository.Default_Branch config.Branch = action.Repository.Default_Branch
} }
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, action.Organization.Username, prjgit) prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, gitOrg, gitPrj)
if err != nil { if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err) return fmt.Errorf("Error accessing/creating prjgit: %s/%s#%d err: %w", gitOrg, gitPrj, gitBranch, err)
} }
if _, err := fs.Stat(os.DirFS(git.GetPath()), config.GitProjectName); errors.Is(err, os.ErrNotExist) { if _, err := fs.Stat(os.DirFS(git.GetPath()), config.GitProjectName); errors.Is(err, os.ErrNotExist) {
common.PanicOnError(git.GitClone(prjgit, config.Branch, prjGitRepo.SSHURL)) common.PanicOnError(git.GitClone(gitPrj, config.Branch, prjGitRepo.SSHURL))
// common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, prjgit))
} }
switch action.Action { switch action.Action {
@@ -117,27 +116,27 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
if action.Repository.Object_Format_Name != "sha256" { if action.Repository.Object_Format_Name != "sha256" {
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(prjgit, "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))
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(prjgit, 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(prjgit, 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 {
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
} }
common.PanicOnError(git.GitExec(path.Join(prjgit, action.Repository.Name), "checkout", config.Branch)) common.PanicOnError(git.GitExec(path.Join(gitPrj, action.Repository.Name), "checkout", config.Branch))
} }
common.PanicOnError(git.GitExec(prjgit, "commit", "-m", "Automatic package inclusion via Direct Workflow")) common.PanicOnError(git.GitExec(gitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow"))
common.PanicOnError(git.GitExec(prjgit, "push")) common.PanicOnError(git.GitExec(gitPrj, "push"))
case "deleted": case "deleted":
if stat, err := os.Stat(filepath.Join(git.GetPath(), prjgit, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(git.GetPath(), gitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if DebugMode { if DebugMode {
log.Println("delete event for", action.Repository.Name, "-- not in project. Ignoring") log.Println("delete event for", action.Repository.Name, "-- not in project. Ignoring")
} }
return nil return nil
} }
common.PanicOnError(git.GitExec(prjgit, "rm", action.Repository.Name)) common.PanicOnError(git.GitExec(gitPrj, "rm", action.Repository.Name))
common.PanicOnError(git.GitExec(prjgit, "commit", "-m", "Automatic package removal via Direct Workflow")) common.PanicOnError(git.GitExec(gitPrj, "commit", "-m", "Automatic package removal via Direct Workflow"))
common.PanicOnError(git.GitExec(prjgit, "push")) common.PanicOnError(git.GitExec(gitPrj, "push"))
default: default:
return fmt.Errorf("%s: %s", "Unknown action type", action.Action) return fmt.Errorf("%s: %s", "Unknown action type", action.Action)
@@ -158,7 +157,7 @@ func (*PushActionProcessor) ProcessFunc(request *common.Request) error {
} }
for _, config := range configs { for _, config := range configs {
if config.GitProjectName == action.Repository.Name { if gitOrg, gitPrj, _ := config.GetPrjGit(); gitOrg == action.Repository.Owner.Username && gitPrj == action.Repository.Name {
log.Println("+ ignoring push to PrjGit repository", config.GitProjectName) log.Println("+ ignoring push to PrjGit repository", config.GitProjectName)
return nil return nil
} }
@@ -173,7 +172,7 @@ func (*PushActionProcessor) ProcessFunc(request *common.Request) error {
} }
func processConfiguredPushAction(action *common.PushWebhookEvent, config *common.AutogitConfig) error { func processConfiguredPushAction(action *common.PushWebhookEvent, config *common.AutogitConfig) error {
prjgit := config.GitProjectName gitOrg, gitPrj, gitBranch := config.GetPrjGit()
git, err := gh.CreateGitHandler(config.Organization) git, err := gh.CreateGitHandler(config.Organization)
common.PanicOnError(err) common.PanicOnError(err)
defer git.Close() defer git.Close()
@@ -183,33 +182,32 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
log.Println(" + default branch", action.Repository.Default_Branch) log.Println(" + default branch", action.Repository.Default_Branch)
} }
prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, action.Repository.Owner.Username, prjgit) prjGitRepo, err := gitea.CreateRepositoryIfNotExist(git, gitOrg, gitPrj)
if err != nil { if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err) return fmt.Errorf("Error accessing/creating prjgit: %s/%s err: %w", gitOrg, gitPrj, err)
} }
if _, err := fs.Stat(os.DirFS(git.GetPath()), config.GitProjectName); errors.Is(err, os.ErrNotExist) { if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
common.PanicOnError(git.GitClone(prjgit, config.Branch, prjGitRepo.SSHURL)) common.PanicOnError(git.GitClone(gitPrj, gitBranch, prjGitRepo.SSHURL))
// common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, prjgit))
} }
if stat, err := os.Stat(filepath.Join(git.GetPath(), prjgit, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(git.GetPath(), gitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if DebugMode { if DebugMode {
log.Println("Pushed to package that is not part of the project. Ignoring:", err) log.Println("Pushed to package that is not part of the project. Ignoring:", err)
} }
return nil return nil
} }
common.PanicOnError(git.GitExec(prjgit, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name)) common.PanicOnError(git.GitExec(gitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name))
if err := git.GitExec(filepath.Join(prjgit, action.Repository.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil { if err := git.GitExec(filepath.Join(gitPrj, 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 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(prjgit, 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(prjgit, action.Repository.Name), "fetch", "--depth", "1", "origin", id)) common.PanicOnError(git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id))
common.PanicOnError(git.GitExec(filepath.Join(prjgit, action.Repository.Name), "checkout", id)) common.PanicOnError(git.GitExec(filepath.Join(gitPrj, action.Repository.Name), "checkout", id))
common.PanicOnError(git.GitExec(prjgit, "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"))
common.PanicOnError(git.GitExec(prjgit, "push")) common.PanicOnError(git.GitExec(gitPrj, "push"))
return nil return nil
} }
} }
@@ -229,23 +227,23 @@ func verifyProjectState(git common.Git, org string, config *common.AutogitConfig
} }
}() }()
repo, err := gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName) gitOrg, gitPrj, gitBranch := config.GetPrjGit()
repo, err := gitea.CreateRepositoryIfNotExist(git, gitOrg, gitPrj)
if err != nil { if err != nil {
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", org, config.GitProjectName, err) return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", gitOrg, gitPrj, err)
} }
if _, err := fs.Stat(os.DirFS(git.GetPath()), config.GitProjectName); errors.Is(err, os.ErrNotExist) { if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
common.PanicOnError(git.GitClone(config.GitProjectName, config.Branch, repo.SSHURL)) common.PanicOnError(git.GitClone(gitPrj, gitBranch, repo.SSHURL))
// common.PanicOnError(git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName))
} }
log.Println(" * Getting submodule list") log.Println(" * Getting submodule list")
sub, err := git.GitSubmoduleList(config.GitProjectName, "HEAD") sub, err := git.GitSubmoduleList(gitPrj, "HEAD")
common.PanicOnError(err) common.PanicOnError(err)
log.Println(" * Getting package links") log.Println(" * Getting package links")
var pkgLinks []*PackageRebaseLink var pkgLinks []*PackageRebaseLink
if f, err := fs.Stat(os.DirFS(path.Join(git.GetPath(), config.GitProjectName)), common.PrjLinksFile); err == nil && (f.Mode()&fs.ModeType == 0) && f.Size() < 1000000 { if f, err := fs.Stat(os.DirFS(path.Join(git.GetPath(), gitPrj)), common.PrjLinksFile); err == nil && (f.Mode()&fs.ModeType == 0) && f.Size() < 1000000 {
if data, err := os.ReadFile(path.Join(git.GetPath(), config.GitProjectName, common.PrjLinksFile)); err == nil { if data, err := os.ReadFile(path.Join(git.GetPath(), gitPrj, common.PrjLinksFile)); err == nil {
pkgLinks, err = parseProjectLinks(data) pkgLinks, err = parseProjectLinks(data)
if err != nil { if err != nil {
log.Println("Cannot parse project links file:", err.Error()) log.Println("Cannot parse project links file:", err.Error())
@@ -264,19 +262,19 @@ func verifyProjectState(git common.Git, org string, config *common.AutogitConfig
next_package: next_package:
for filename, commitId := range sub { for filename, commitId := range sub {
// ignore project gits // ignore project gits
for _, c := range configs { //for _, c := range configs {
if c.GitProjectName == filename { if gitPrj == filename {
log.Println(" prjgit as package? ignoring project git:", filename) log.Println(" prjgit as package? ignoring project git:", filename)
continue next_package continue next_package
} }
} //}
log.Println(" verifying package:", filename, commitId, config.Branch) log.Println(" verifying package:", filename, commitId, config.Branch)
commits, err := gitea.GetRecentCommits(org, filename, config.Branch, 10) commits, err := gitea.GetRecentCommits(org, filename, config.Branch, 10)
if err != nil { if err != nil {
// assumption that package does not exist, remove from project // assumption that package does not exist, remove from project
// https://github.com/go-gitea/gitea/issues/31976 // https://github.com/go-gitea/gitea/issues/31976
if err := git.GitExec(config.GitProjectName, "rm", filename); err != nil { if err := git.GitExec(gitPrj, "rm", filename); err != nil {
return fmt.Errorf("Failed to remove deleted submodule. Err: %w", err) return fmt.Errorf("Failed to remove deleted submodule. Err: %w", err)
} }
isGitUpdated = true isGitUpdated = true
@@ -301,8 +299,8 @@ next_package:
log.Println(" -> linked package") log.Println(" -> linked package")
// so, we need to rebase here. Can't really optimize, so clone entire package tree and remote // so, we need to rebase here. Can't really optimize, so clone entire package tree and remote
pkgPath := path.Join(config.GitProjectName, filename) pkgPath := path.Join(gitPrj, filename)
git.GitExecOrPanic(config.GitProjectName, "submodule", "update", "--init", "--checkout", filename) git.GitExecOrPanic(gitPrj, "submodule", "update", "--init", "--checkout", filename)
git.GitExecOrPanic(pkgPath, "fetch", "origin", commits[0].SHA) git.GitExecOrPanic(pkgPath, "fetch", "origin", commits[0].SHA)
git.GitExecOrPanic(pkgPath, "tag", "NOW") git.GitExecOrPanic(pkgPath, "tag", "NOW")
git.GitExecOrPanic(pkgPath, "fetch", "origin") git.GitExecOrPanic(pkgPath, "fetch", "origin")
@@ -325,9 +323,9 @@ next_package:
// up-to-date // up-to-date
continue continue
} else if idx < len(commits) { // update } else if idx < len(commits) { // update
common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "update", "--init", "--depth", "1", "--checkout", filename)) common.PanicOnError(git.GitExec(gitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", filename))
common.PanicOnError(git.GitExec(filepath.Join(config.GitProjectName, filename), "fetch", "--depth", "1", "origin", commits[0].SHA)) common.PanicOnError(git.GitExec(filepath.Join(gitPrj, filename), "fetch", "--depth", "1", "origin", commits[0].SHA))
common.PanicOnError(git.GitExec(filepath.Join(config.GitProjectName, filename), "checkout", commits[0].SHA)) common.PanicOnError(git.GitExec(filepath.Join(gitPrj, filename), "checkout", commits[0].SHA))
isGitUpdated = true isGitUpdated = true
} else { } else {
// probably need `merge-base` or `rev-list` here instead, or the project updated already // probably need `merge-base` or `rev-list` here instead, or the project updated already
@@ -363,12 +361,12 @@ next_repo:
continue next_repo continue next_repo
} }
for _, c := range configs { // for _, c := range configs {
if c.Organization == org && c.GitProjectName == r.Name { if gitPrj == r.Name {
// ignore project gits // ignore project gits
continue next_repo continue next_repo
} }
} // }
for repo := range sub { for repo := range sub {
if repo == r.Name { if repo == r.Name {
@@ -388,15 +386,15 @@ next_repo:
} }
// add repository to git project // add repository to git project
common.PanicOnError(git.GitExec(config.GitProjectName, "submodule", "--quiet", "add", "--depth", "1", r.CloneURL, r.Name)) common.PanicOnError(git.GitExec(gitPrj, "submodule", "--quiet", "add", "--depth", "1", r.CloneURL, r.Name))
if len(config.Branch) > 0 { if len(config.Branch) > 0 {
branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(config.GitProjectName, r.Name), "branch", "--show-current")) branch := strings.TrimSpace(git.GitExecWithOutputOrPanic(path.Join(gitPrj, r.Name), "branch", "--show-current"))
if branch != config.Branch { if branch != config.Branch {
if err := git.GitExec(path.Join(config.GitProjectName, r.Name), "fetch", "--depth", "1", "origin", config.Branch+":"+config.Branch); err != nil { if err := git.GitExec(path.Join(gitPrj, 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) 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(gitPrj, r.Name), "checkout", config.Branch))
} }
} }
@@ -404,12 +402,12 @@ next_repo:
} }
if isGitUpdated { if isGitUpdated {
common.PanicOnError(git.GitExec(config.GitProjectName, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC")) common.PanicOnError(git.GitExec(gitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow -- SYNC"))
common.PanicOnError(git.GitExec(config.GitProjectName, "push")) common.PanicOnError(git.GitExec(gitPrj, "push"))
} }
if DebugMode { if DebugMode {
log.Println("Verification finished for ", org, ", config", config.GitProjectName) log.Println("Verification finished for ", org, ", prjgit:", config.GitProjectName)
} }
return nil return nil