workflow-direct: no panic if no changes

This commit is contained in:
2025-04-17 15:12:51 +02:00
parent bfeac63c57
commit f0de3ad54a
4 changed files with 55 additions and 39 deletions

View File

@@ -19,7 +19,6 @@ package main
*/
import (
"errors"
"flag"
"fmt"
"io/fs"
@@ -107,9 +106,8 @@ func processConfiguredRepositoryAction(action *common.RepositoryWebhookEvent, co
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) {
common.PanicOnError(git.GitClone(gitPrj, gitBranch, prjGitRepo.SSHURL))
}
_, err = git.GitClone(gitPrj, gitBranch, prjGitRepo.SSHURL)
common.PanicOnError(err)
switch action.Action {
case "created":
@@ -194,9 +192,16 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
return fmt.Errorf("Error accessing/creating prjgit: %s/%s err: %w", gitOrg, gitPrj, err)
}
if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
common.PanicOnError(git.GitClone(gitPrj, gitBranch, prjGitRepo.SSHURL))
remoteName, err := git.GitClone(gitPrj, gitBranch, prjGitRepo.SSHURL)
common.PanicOnError(err)
headCommitId, err := git.GitRemoteHead(gitPrj, remoteName, gitBranch)
common.PanicOnError(err)
commit, ok := git.GitSubmoduleCommitId(gitPrj, action.Repository.Name, headCommitId)
for ok && action.Head_Commit.Id == commit {
log.Println(" -- nothing to do, commit already in ProjectGit")
return nil
}
if stat, err := os.Stat(filepath.Join(git.GetPath(), gitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
if DebugMode {
log.Println("Pushed to package that is not part of the project. Ignoring:", err)
@@ -209,17 +214,15 @@ func processConfiguredPushAction(action *common.PushWebhookEvent, config *common
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
}
id, err := git.GitBranchHead(filepath.Join(gitPrj, action.Repository.Name), config.Branch)
id, err := git.GitRemoteHead(filepath.Join(gitPrj, action.Repository.Name), "origin", config.Branch)
common.PanicOnError(err)
for _, commitId := range action.Commits {
if commitId.Id == id {
git.GitExecOrPanic(filepath.Join(gitPrj, action.Repository.Name), "checkout", id)
git.GitExecOrPanic(gitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
if !noop {
git.GitExecOrPanic(gitPrj, "push")
}
return nil
if action.Head_Commit.Id == id {
git.GitExecOrPanic(filepath.Join(gitPrj, action.Repository.Name), "checkout", id)
git.GitExecOrPanic(gitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
if !noop {
git.GitExecOrPanic(gitPrj, "push")
}
return nil
}
log.Println("push of refs not on the configured branch", config.Branch, ". ignoring.")
@@ -243,9 +246,8 @@ func verifyProjectState(git common.Git, org string, config *common.AutogitConfig
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", gitOrg, gitPrj, err)
}
if _, err := fs.Stat(os.DirFS(git.GetPath()), gitPrj); errors.Is(err, os.ErrNotExist) {
common.PanicOnError(git.GitClone(gitPrj, gitBranch, repo.SSHURL))
}
_, err = git.GitClone(gitPrj, gitBranch, repo.SSHURL)
common.PanicOnError(err)
defer func() { common.PanicOnError(git.GitExec(gitPrj, "submodule", "deinit", "--all")) }()
log.Println(" * Getting submodule list")
@@ -281,20 +283,18 @@ next_package:
}
//}
log.Println(" verifying package:", filename, commitId, config.Branch)
log.Printf(" verifying package: %s -> %s(%s)", commitId, filename, config.Branch)
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
if err := git.GitExec(gitPrj, "rm", filename); err != nil {
return fmt.Errorf("Failed to remove deleted submodule. Err: %w", err)
if len(commits) == 0 {
if repo, err := gitea.GetRepository(org, filename); repo == nil && err == nil {
git.GitExecOrPanic(gitPrj, "rm", filename)
isGitUpdated = true
}
isGitUpdated = true
}
if err != nil {
log.Println(" -> failed to fetch recent commits for package:", filename, " Err:", err)
continue
}
// 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 {
@@ -340,10 +340,11 @@ next_package:
common.PanicOnError(git.GitExec(gitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", filename))
common.PanicOnError(git.GitExec(filepath.Join(gitPrj, filename), "fetch", "--depth", "1", "origin", commits[0].SHA))
common.PanicOnError(git.GitExec(filepath.Join(gitPrj, filename), "checkout", commits[0].SHA))
log.Println(" -> updated to", commits[0].SHA)
isGitUpdated = true
} else {
// probably need `merge-base` or `rev-list` here instead, or the project updated already
return fmt.Errorf("Cannot find SHA of last matching update for package: '%s'. idx: %d", filename, idx)
log.Println(" *** Cannot find SHA of last matching update for package:", filename, " Ignoring")
}
}
}