This commit is contained in:
Adam Majer 2024-08-30 16:37:51 +02:00
parent a4d2010ca0
commit c380674a71
2 changed files with 56 additions and 16 deletions

View File

@ -10,10 +10,10 @@ import (
) )
type AutogitConfig struct { type AutogitConfig struct {
Workflows []string Workflows []string // [pr, direct, test]
Organization string Organization string
GitProjectName string GitProjectName string // Organization/GitProjectName.git is PrjGit
Branch string Branch string // branch name of PkgGit that aligns with PrjGit submodules
} }
func ReadWorkflowConfigs(reader io.Reader) ([]*AutogitConfig, error) { func ReadWorkflowConfigs(reader io.Reader) ([]*AutogitConfig, error) {
@ -27,7 +27,7 @@ func ReadWorkflowConfigs(reader io.Reader) ([]*AutogitConfig, error) {
return nil, fmt.Errorf("Error parsing config file. err: %w", err) return nil, fmt.Errorf("Error parsing config file. err: %w", err)
} }
availableWorkflows := []string{"pr", "push", "test"} availableWorkflows := []string{"pr", "direct", "test"}
for _, workflow := range config { for _, workflow := range config {
for _, w := range workflow.Workflows { for _, w := range workflow.Workflows {
if !slices.Contains(availableWorkflows, w) { if !slices.Contains(availableWorkflows, w) {

View File

@ -21,30 +21,53 @@ type ConfigRepos struct {
var configuredRepos map[string]ConfigRepos var configuredRepos map[string]ConfigRepos
func isConfiguredOrg(org *common.Organization) bool {
_, found := configuredRepos[org.Username]
return found
}
func processRepositoryAction(h *common.RequestHandler) error { func processRepositoryAction(h *common.RequestHandler) error {
action := h.Request.Data.(*common.RepositoryWebhookEvent) action := h.Request.Data.(*common.RepositoryWebhookEvent)
config, configFound := configuredRepos[action.Organization.Username]
if action.Repository.Name == common.DefaultGitPrj { if !configFound {
h.StdLogger.Printf("repository event %s for %s. Ignoring\n", common.DefaultGitPrj, action.Action) if h.Git.DebugLogger {
h.StdLogger.Printf("Repository event for %s. Not configured. Ignoring.\n", action.Organization.Username)
}
return nil return nil
} }
h.CreateRepositoryIfNotExist(*action.Organization, h.PrjGit) prjgit := config.prjgit
h.Git.GitExec("", "clone", "--depth", "1", action.PrjGit, h.PrjGit) if action.Repository.Name == prjgit {
if h.Git.DebugLogger {
h.StdLogger.Printf("repository event %s for PrjGit '%s'. Ignoring\n", common.DefaultGitPrj, action.Action)
}
return nil
}
prjGitRepo, err := h.CreateRepositoryIfNotExist(*action.Organization, prjgit)
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
h.Git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)
switch action.Action { switch action.Action {
case "created": case "created":
h.Git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url) h.Git.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion") h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push") h.Git.GitExec(common.DefaultGitPrj, "push")
case "deleted": case "deleted":
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name) if h.Git.DebugLogger {
h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name)
}
return nil return nil
} }
h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name) h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal") h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push") h.Git.GitExec(common.DefaultGitPrj, "push")
default: default:
return fmt.Errorf("%s: %s", "Unknown action type", action.Action) return fmt.Errorf("%s: %s", "Unknown action type", action.Action)
} }
@ -54,15 +77,32 @@ func processRepositoryAction(h *common.RequestHandler) error {
func processPushAction(h *common.RequestHandler) error { func processPushAction(h *common.RequestHandler) error {
action := h.Request.Data.(*common.PushWebhookEvent) action := h.Request.Data.(*common.PushWebhookEvent)
config, configFound := configuredRepos[action.Repository.Owner.Username]
if action.Repository.Name == common.DefaultGitPrj { if !configFound {
h.StdLogger.Printf("push to %s -- ignoringi\n", common.DefaultGitPrj) if h.Git.DebugLogger {
h.StdLogger.Printf("Push event to Organizationr '%s'. Not configured. Ignoring.\n", action.Repository.Owner.Username)
}
return nil return nil
} }
h.Git.GitExec("", "clone", "--depth", "1", h.PrjGit, common.DefaultGitPrj) prjgit := config.prjgit
if action.Repository.Name == prjgit {
if h.Git.DebugLogger {
h.StdLogger.Printf("push to %s -- ignoring\n", prjgit)
}
return nil
}
prjGitRepo, err := h.CreateRepositoryIfNotExist(*action.Repository.Owner, prjgit)
if err != nil {
return fmt.Errorf("Error accessing/creating prjgit: %s err: %w", prjgit, err)
}
h.Git.GitExec("", "clone", "--depth", "1", prjGitRepo.SSHURL, common.DefaultGitPrj)
if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { if stat, err := os.Stat(filepath.Join(h.Git.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() {
h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring. : %v\n", err) if h.Git.DebugLogger {
h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring: %v\n", err)
}
return nil return nil
} }
h.Git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name) h.Git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name)
@ -71,7 +111,7 @@ func processPushAction(h *common.RequestHandler) error {
if commitId.Id == id { if commitId.Id == id {
h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id) h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id)
h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id) h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id)
h.Git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push") h.Git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push via Direct Workflow")
h.Git.GitExec(common.DefaultGitPrj, "push") h.Git.GitExec(common.DefaultGitPrj, "push")
return nil return nil
} }