diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 0b312be..6871bc8 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -177,7 +177,7 @@ func (h writeFunc) Close() error { return err } -func (e *GitHandler) GitExec(cwd string, params ...string) ExecStream { +func (e *GitHandler) GitExec(cwd string, params ...string) error { cmd := exec.Command("/usr/bin/git", params...) cmd.Env = []string{ "GIT_CEILING_DIRECTORIES=" + e.GitPath, @@ -194,8 +194,17 @@ func (e *GitHandler) GitExec(cwd string, params ...string) ExecStream { log.Printf("git execute: %#v\n", cmd.Args) } out, err := cmd.CombinedOutput() + if e.DebugLogger { + log.Println(string(out)) + } + if err != nil { + if e.DebugLogger { + log.Printf(" *** error: %v\n", err) + } + return err + } - return e + return nil } type ChanIO struct { @@ -523,11 +532,15 @@ func (e *GitHandler) GitCatFile(cwd, commitId, filename string) (data []byte, er cmd.Stdout = &data_in cmd.Stdin = &data_out cmd.Stderr = writeFunc(func(data []byte) (int, error) { - e.ErrLogger.Println(data) + if e.DebugLogger { + log.Printf(string(data)) + } return len(data), nil }) - e.StdLogger.Printf("command run: %v\n", cmd.Args) - e.Error = cmd.Run() + if e.DebugLogger { + log.Printf("command run: %v\n", cmd.Args) + } + err = cmd.Run() done.Lock() return @@ -546,19 +559,19 @@ func (e *GitHandler) GitSubmoduleList(cwd, commitId string) (submoduleList map[s data_out.Write([]byte(commitId)) data_out.ch <- '\x00' - c, err := parseGitCommit(data_in.ch) + var c commit + c, err = parseGitCommit(data_in.ch) if err != nil { - e.Error = err - e.ErrLogger.Printf("Error parsing git commit: %v\n", err) + err = fmt.Errorf("Error parsing git commit. Err: %w", err) return } data_out.Write([]byte(c.Tree)) data_out.ch <- '\x00' - tree, err := parseGitTree(data_in.ch) + var tree tree + tree, err = parseGitTree(data_in.ch) if err != nil { - e.Error = err - e.ErrLogger.Printf("Error parsing git tree: %v\n", err) + err = fmt.Errorf("Error parsing git tree: %w", err) return } @@ -575,17 +588,19 @@ func (e *GitHandler) GitSubmoduleList(cwd, commitId string) (submoduleList map[s "GIT_CONFIG_GLOBAL=/dev/null", } cmd.Dir = filepath.Join(e.GitPath, cwd) - cmd.Stdout = &data_in - cmd.Stdin = &data_out cmd.Stderr = writeFunc(func(data []byte) (int, error) { - e.ErrLogger.Println(data) + if e.DebugLogger { + log.Println(string(data)) + } return len(data), nil }) - e.StdLogger.Printf("command run: %v\n", cmd.Args) - e.Error = cmd.Run() + if e.DebugLogger { + log.Printf("command run: %v\n", cmd.Args) + } + err = cmd.Run() done.Lock() - return submoduleList + return submoduleList, err } func (e *GitHandler) GitSubmoduleCommitId(cwd, packageName, commitId string) (subCommitId string, valid bool) { diff --git a/bots-common/git_utils_test.go b/bots-common/git_utils_test.go index de754ec..395a7ff 100644 --- a/bots-common/git_utils_test.go +++ b/bots-common/git_utils_test.go @@ -241,11 +241,8 @@ func TestCommitTreeParsingOfHead(t *testing.T) { t.Run("reads HEAD and parses the tree", func(t *testing.T) { const nodejs21 = "c678c57007d496a98bec668ae38f2c26a695f94af78012f15d044ccf066ccb41" - stdLogger, errLogger:= CreateStdoutLogger(os.Stdout, os.Stdout) - h := RequestHandler{ + h := GitHandler{ GitPath: gitDir, - StdLogger: stdLogger, - ErrLogger: errLogger, } id, ok := h.GitSubmoduleCommitId("", "nodejs21", commitId) if !ok { @@ -257,15 +254,12 @@ func TestCommitTreeParsingOfHead(t *testing.T) { }) t.Run("reads README.md", func (t *testing.T) { - stdLogger, errLogger:= CreateStdoutLogger(os.Stdout, os.Stdout) - h := RequestHandler{ + h := GitHandler{ GitPath: gitDir, - StdLogger: stdLogger, - ErrLogger: errLogger, } - data := h.GitCatFile("", commitId, "README.md") - if h.HasError() { - t.Errorf("failed parse: %v", h.Error) + data, err := h.GitCatFile("", commitId, "README.md") + if err != nil { + t.Errorf("failed parse: %v", err) } if string(data) != "foo\n" || len(data) != 4 { t.Errorf("Wrong data of len: %d", len(data)) diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 4e7c0c4..3c82415 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -1,9 +1,9 @@ package common import ( - "errors" "fmt" "io" + "log" "os" "path/filepath" "slices" @@ -46,10 +46,6 @@ const ( ) func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) { - if h.HasError() { - return nil, nil, h.Error - } - transport, client := h.allocateGiteaTransport() pr, err := client.Repository.RepoGetPullRequest( repository.NewRepoGetPullRequestParams(). @@ -61,8 +57,7 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64 ) if err != nil { - h.Error = err - h.ErrLogger.Println(err.Error()) + log.Println(err.Error()) return nil, nil, err } @@ -78,8 +73,7 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64 ) if err != nil { - h.Error = err - h.ErrLogger.Println(err.Error()) + log.Println(err.Error()) return nil, nil, err } @@ -87,10 +81,6 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64 } func (h *RequestHandler) GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error) { - if h.HasError() { - return nil, h.Error - } - bigLimit := int64(100000) transport, client := h.allocateGiteaTransport() @@ -107,23 +97,17 @@ func (h *RequestHandler) GetPullNotifications(since *time.Time) ([]*models.Notif list, err := client.Notification.NotifyGetList(params, transport.DefaultAuthentication) if err != nil { - h.Error = err return nil, err } if !list.IsSuccess() { - h.Error = fmt.Errorf("Cannot fetch notifications: %s", list.Error()) - return nil, h.Error + return nil, fmt.Errorf("Cannot fetch notifications: %s", list.Error()) } return list.Payload, nil } func (h *RequestHandler) SetNotificationRead(notificationId int64) error { - if h.HasError() { - return h.Error - } - transport, client := h.allocateGiteaTransport() list, err := client.Notification.NotifyReadThread( notification.NewNotifyReadThreadParams(). @@ -133,24 +117,18 @@ func (h *RequestHandler) SetNotificationRead(notificationId int64) error { ) if err != nil { - h.ErrLogger.Printf("Error setting notification: %d: %v\n", notificationId, err) - h.Error = err + log.Printf("Error setting notification: %d: %v\n", notificationId, err) return err } if !list.IsSuccess() { - h.Error = fmt.Errorf("Cannot update notifications: %d", notificationId) - return h.Error + return fmt.Errorf("Cannot update notifications: %d", notificationId) } return nil } -func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) *models.Repository { - if h.HasError() { - return nil - } - +func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) (*models.Repository, error) { transport, client := h.allocateGiteaTransport() repo, err := client.Repository.RepoGet( repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName), @@ -177,50 +155,40 @@ func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName s case *organization.CreateOrgRepoCreated: h.StdLogger.Printf("repo '%s' created, with notification error?\n", repoName) default: - h.Error = err - h.ErrLogger.Printf("error creating repo '%s' under '%s': %s\n", repoName, org.Username, err.Error()) - return nil + log.Printf("error creating repo '%s' under '%s': %s\n", repoName, org.Username, err.Error()) + return nil, err } } else { h.StdLogger.Printf("repo '%s' created\n", repoName) } // initialize repository - h.Error = os.Mkdir(filepath.Join(h.GitPath, DefaultGitPrj), 0700) - if h.HasError() { - return nil + if err = os.Mkdir(filepath.Join(h.Git.GitPath, DefaultGitPrj), 0700); err != nil { + return nil, err } - h.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName) - h.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch) - if h.HasError() { - return nil - } - readmeFilename := filepath.Join(h.GitPath, DefaultGitPrj, "README.md") + h.Git.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName) + h.Git.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch) + readmeFilename := filepath.Join(h.Git.GitPath, DefaultGitPrj, "README.md") { file, _ := os.Create(readmeFilename) defer file.Close() io.WriteString(file, ReadmeBoilerplate) } - h.GitExec(DefaultGitPrj, "add", "README.md") - h.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation") - h.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL) + h.Git.GitExec(DefaultGitPrj, "add", "README.md") + h.Git.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation") + h.Git.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL) - return repo.Payload + return repo.Payload, nil default: - h.Error = err - h.ErrLogger.Printf("cannot fetch repo data for '%s' / '%s' : %v\n", org.Username, repoName, err) + return nil, fmt.Errorf("cannot fetch repo data for '%s' / '%s' : %w", org.Username, repoName, err) } } - return repo.Payload + return repo.Payload, nil } -func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targetId, title, body string) *models.PullRequest { - if h.HasError() { - return nil - } - +func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error) { transport, client := h.allocateGiteaTransport() prOptions := models.CreatePullRequestOption{ @@ -241,25 +209,13 @@ func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targe ) if err != nil { - h.ErrLogger.Printf("Cannot create pull request: %v\n", err) - h.Error = err - return nil + return nil, fmt.Errorf("Cannot create pull request. %w", err) } - if !pr.IsSuccess() { - h.ErrLogger.Printf("PR creation failed: %s\n", pr.Error()) - h.Error = errors.New(pr.Error()) - return nil - } - - return pr.GetPayload() + return pr.GetPayload(), nil } -func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) []*models.PullReview { - if h.HasError() { - return nil - } - +func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) { transport, client := h.allocateGiteaTransport() reviewOptions := models.PullReviewRequestOptions{ @@ -277,25 +233,13 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) ) if err != nil { - h.ErrLogger.Printf("Cannot create pull request: %v\n", err) - h.Error = err - return nil + return nil, fmt.Errorf("Cannot create pull request: %w", err) } - if !review.IsSuccess() { - h.ErrLogger.Printf("PR creation failed: %s\n", review.Error()) - h.Error = errors.New(review.Error()) - return nil - } - - return review.GetPayload() + return review.GetPayload(), nil } func (h *RequestHandler) IsReviewed(pr *models.PullRequest) (bool, error) { - if h.HasError() { - return false, h.Error - } - transport, client := h.allocateGiteaTransport() // TODO: get review from project git reviewers := pr.RequestedReviewers @@ -406,11 +350,7 @@ func (h *RequestHandler) AddReviewComment(pr *models.PullRequest, state models.R return c.Payload, nil } -func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) *models.PullRequest { - if h.HasError() { - return nil - } - +func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) { transport, client := h.allocateGiteaTransport() var page, maxSize int64 @@ -429,12 +369,7 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) *mod transport.DefaultAuthentication) if err != nil { - h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %v", pr.Repository.Owner.Username, pr.Repository.Name, err) - return nil - } - - if !prs.IsSuccess() { - h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %s", pr.Repository.Owner.Username, pr.Repository.Name, prs.Error()) + return nil, fmt.Errorf("cannot fetch PR list for %s / %s : %w", pr.Repository.Owner.Username, pr.Repository.Name, err) } prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number) @@ -446,19 +381,15 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) *mod for _, line := range lines { if strings.TrimSpace(line) == prLine { - return pr + return pr, nil } } } - return nil + return nil, nil } func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error) { - if h.HasError() { - return nil, h.Error - } - transport, client := h.allocateGiteaTransport() var retData []byte @@ -469,7 +400,7 @@ func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, retData = data return len(data), nil }) - file, err := client.Repository.RepoGetRawFile( + _, err := client.Repository.RepoGetRawFile( repository.NewRepoGetRawFileParams(). WithOwner(repo.Owner.UserName). WithRepo(repo.Name). @@ -484,10 +415,6 @@ func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, return nil, err } - if !file.IsSuccess() { - return nil, fmt.Errorf("Invalid response from server (%d): %s", file.Code(), file.Error()) - } - return retData, nil } diff --git a/bots-common/listen.go b/bots-common/listen.go index a54d398..2137ac5 100644 --- a/bots-common/listen.go +++ b/bots-common/listen.go @@ -185,9 +185,15 @@ func ProcessRabbitMQEvents(listenDefs ListenDefinitions, orgs []string) error { org := route[2] if handler, found := listenDefs.Handlers[org]; found { - h := CreateRequestHandler(listenDefs.GitAuthor, listenDefs.GitAuthor) + h, err := CreateRequestHandler(listenDefs.GitAuthor, listenDefs.GitAuthor) + if err != nil { + log.Printf("Cannot create request handler: %v\n", err) + continue + } req, err := ParseRequestJSON(reqType, msg.Body) if err != nil { + log.Printf("Error parsing request JSON: %v\n", err) + continue } else { h.Request = req ProcessEvent(handler, h) diff --git a/bots-common/request_handler.go b/bots-common/request_handler.go index 7615709..16c152d 100644 --- a/bots-common/request_handler.go +++ b/bots-common/request_handler.go @@ -69,36 +69,32 @@ func ParseRequestJSON(reqType string, data []byte) (req *Request, err error) { } type RequestHandler struct { - Error error - GitCommiter string - GitPath string - - Branch string - PrjGit string + Branch string + PrjGit string StdLogger, ErrLogger *log.Logger Request *Request + Git GitHandler } func (r *RequestHandler) WriteError() { r.ErrLogger.Println("internal error sent") } -func CreateRequestHandler(git_author, name string) *RequestHandler { + func CreateRequestHandler(git_author, name string) (*RequestHandler, error) { var h *RequestHandler = new(RequestHandler) h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stderr) - h.GitCommiter = git_author - h.GitPath, h.Error = os.MkdirTemp("", name) - if h.Error != nil { - h.ErrLogger.Printf("Cannot create temp dir: %v\n", h.Error) - return h + var err error + h.Git.GitCommiter = git_author + h.Git.GitPath, err = os.MkdirTemp("", name) + if err != nil { + return nil, fmt.Errorf("Cannot create temp dir: %w", err) } - if h.Error = os.Chmod(h.GitPath, 0700); h.Error != nil { - h.ErrLogger.Printf("Cannot fix permissions of temp dir: %v\n", h.Error) - return h + if err = os.Chmod(h.Git.GitPath, 0700); err != nil { + return nil, fmt.Errorf("Cannot fix permissions of temp dir: %w", err) } - return h + return h, nil } diff --git a/bots-common/request_pr.go b/bots-common/request_pr.go index ff6ad09..7dbbc48 100644 --- a/bots-common/request_pr.go +++ b/bots-common/request_pr.go @@ -2,8 +2,8 @@ package common import ( "encoding/json" + "fmt" "io" - "strings" ) type Head struct { @@ -50,31 +50,18 @@ func (p *PullRequestWebhookEvent) GetAction() string { return p.Action } -func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestWebhookEvent { - if h.HasError() { - return nil +func (h *RequestHandler) parsePullRequest(data io.Reader) (action *PullRequestWebhookEvent, err error) { + action=new(PullRequestWebhookEvent) + err = json.NewDecoder(data).Decode(&action) + + if err != nil { + return nil, fmt.Errorf("Got error while parsing json: %w", err) } - var action PullRequestWebhookEvent - h.Error = json.NewDecoder(data).Decode(&action) - - if h.HasError() { - h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error) - return nil - } - - repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/") - if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" { - h.ErrLogger.Printf("Unexpected URL for SSH repository: '%s'\n", action.Repository.Name) - h.ErrLogger.Printf("%#v\n", action) - return nil - } - h.Request.Data = &action - - // sanity checks on request - return &action + h.Request.Data = action + return } -func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestWebhookEvent { +func (h *RequestHandler) parsePullRequestSync(data io.Reader) (*PullRequestWebhookEvent, error) { return h.parsePullRequest(data) } diff --git a/bots-common/request_pr_test.go b/bots-common/request_pr_test.go index 28257b1..636aceb 100644 --- a/bots-common/request_pr_test.go +++ b/bots-common/request_pr_test.go @@ -11,14 +11,17 @@ func TestPrParsing(t *testing.T) { var h RequestHandler h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) - pr := h.parsePullRequest(strings.NewReader(samplePR_JSON)) + pr, err := h.parsePullRequest(strings.NewReader(samplePR_JSON)) + if err != nil { + t.Fatalf("error parsing PR: %v\n", err) + } if pr == nil { - t.Fatalf("parsing PR failed: %v", h.Error) + t.Fatalf("parsing PR failed without error?: %v") } - prs := h.parsePullRequest(strings.NewReader(samplePRsync_JSON)) - if prs == nil { - t.Fatalf("parsing PR failed: %v", h.Error) + _, err = h.parsePullRequest(strings.NewReader(samplePRsync_JSON)) + if err != nil { + t.Fatalf("parsing PR failed: %v", err) } }) } diff --git a/bots-common/request_push.go b/bots-common/request_push.go index 3eb9a5f..a6032db 100644 --- a/bots-common/request_push.go +++ b/bots-common/request_push.go @@ -2,6 +2,7 @@ package common import ( "encoding/json" + "fmt" "io" "strings" ) @@ -25,31 +26,24 @@ func (*PushWebhookEvent) GetAction() string { return "push" } -func (h *RequestHandler) parsePushRequest(data io.Reader) *PushWebhookEvent { - if h.HasError() { - return nil - } +func (h *RequestHandler) parsePushRequest(data io.Reader) (*PushWebhookEvent, error) { + action := new(PushWebhookEvent) + err := json.NewDecoder(data).Decode(&action) - var action PushWebhookEvent - h.Error = json.NewDecoder(data).Decode(&action) - - if h.HasError() { - h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error) - return nil + if err != nil { + return nil, fmt.Errorf("Got error while parsing: %w", err) } repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/") if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" { - h.ErrLogger.Printf("Unexpected URL for SSH repository: '%s'\n", action.Repository.Name) - return nil + return nil, fmt.Errorf("Unexpected URL for SSH repository: '%s'", action.Repository.Name) } h.StdLogger.Printf("Request push for repo: %s\n", action.Repository.Full_Name) - h.Request.Data = &action + h.Request.Data = action if len(action.Commits) < 1 || len(action.Head_Commit.Id) != 64 { - h.ErrLogger.Println("Request has no action .... skipping") - return nil + return nil, fmt.Errorf("Request has no action .... skipping") } - return &action + return action, nil } diff --git a/bots-common/request_push_test.go b/bots-common/request_push_test.go index 33480a8..476bab9 100644 --- a/bots-common/request_push_test.go +++ b/bots-common/request_push_test.go @@ -1,7 +1,6 @@ package common import ( - "os" "strings" "testing" ) @@ -10,13 +9,9 @@ func TestPushRequestParsing(t *testing.T) { t.Run("parsing repo creation message", func(t *testing.T) { var h RequestHandler - h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) - json := h.parsePushRequest(strings.NewReader(examplePushJSON)) - if json == nil { - t.Fatalf("failed to parser push request: %v", h.Error) - } - if h.HasError() { - t.Fatalf("Can't parse struct: %s", h.Error.Error()) + json, err := h.parsePushRequest(strings.NewReader(examplePushJSON)) + if err != nil { + t.Fatalf("failed to parser push request: %v", err) } if json.Total_Commits < 1 || json.Total_Commits != len(json.Commits) { diff --git a/bots-common/request_repo.go b/bots-common/request_repo.go index 6417cd1..50f3133 100644 --- a/bots-common/request_repo.go +++ b/bots-common/request_repo.go @@ -46,33 +46,24 @@ func (r *RepositoryWebhookEvent) GetAction() string { } // TODO: sanity check values!!!! -func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) *RepositoryWebhookEvent { - if h.HasError() { - return nil - } - - var data RepositoryWebhookEvent - h.Error = json.NewDecoder(dataReader).Decode(&data) - - if h.HasError() { - return nil +func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) (data *RepositoryWebhookEvent, err error) { + data = new(RepositoryWebhookEvent) + if err = json.NewDecoder(dataReader).Decode(&data); err != nil { + return nil, err } repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/") if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" { - h.Error = fmt.Errorf("No data, skipping") - h.ErrLogger.Printf("Unexpected URL for SSH repository: %s\n", data.Repository.Name) - return nil + return nil, fmt.Errorf("Unexpected URL for SSH repository: %w", data.Repository.Name) } data.PrjGit = data.Repository.Ssh_Url[:repoIdx+1] + DefaultGitPrj + ".git" h.StdLogger.Printf("Request '%s' for repo: %s\n", data.Action, data.Repository.Full_Name) if len(data.Action) < 1 { - h.Error = fmt.Errorf("No data, skipping") - h.ErrLogger.Println("Request has no data.... skipping") + return nil, fmt.Errorf("Request has no data.... skipping") } - h.Request.Data = &data - return &data + h.Request.Data = data + return data, nil } diff --git a/bots-common/request_repo_test.go b/bots-common/request_repo_test.go index 484f52e..94d28d7 100644 --- a/bots-common/request_repo_test.go +++ b/bots-common/request_repo_test.go @@ -21,9 +21,9 @@ func TestRepositoryRequestParsing(t *testing.T) { var h RequestHandler h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) - json := h.parseRepositoryRequest(strings.NewReader(repoCreateJSON)) - if h.HasError() { - t.Fatalf("Can't parse struct: %s", h.Error) + json, err := h.parseRepositoryRequest(strings.NewReader(repoCreateJSON)) + if err != nil { + t.Fatalf("Can't parse struct: %s", err) } if json.Action != "created" { diff --git a/prjgit-updater/main.go b/prjgit-updater/main.go index 77703cb..bf3fbeb 100644 --- a/prjgit-updater/main.go +++ b/prjgit-updater/main.go @@ -11,7 +11,6 @@ import ( "sync" "time" - "golang.org/x/tools/go/analysis/passes/defers" "src.opensuse.org/autogits/common" ) @@ -30,21 +29,21 @@ func processRepositoryAction(h *common.RequestHandler) error { } h.CreateRepositoryIfNotExist(*action.Organization, h.PrjGit) - h.GitExec("", "clone", "--depth", "1", action.PrjGit, h.PrjGit) + h.Git.GitExec("", "clone", "--depth", "1", action.PrjGit, h.PrjGit) switch action.Action { case "created": - h.GitExec(common.DefaultGitPrj, "submodule", "--quiet", "add", "--depth", "1", action.Repository.Clone_Url) - h.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion") - h.GitExec(common.DefaultGitPrj, "push") + 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, "push") case "deleted": - if stat, err := os.Stat(filepath.Join(h.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) return nil } - h.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name) - h.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal") - h.GitExec(common.DefaultGitPrj, "push") + h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name) + h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal") + h.Git.GitExec(common.DefaultGitPrj, "push") default: return fmt.Errorf("%s: %s", "Unknown action type", action.Action) } @@ -60,19 +59,19 @@ func processPushAction(h *common.RequestHandler) error { return nil } - h.GitExec("", "clone", "--depth", "1", h.PrjGit, common.DefaultGitPrj) - if stat, err := os.Stat(filepath.Join(h.GitPath, common.DefaultGitPrj, action.Repository.Name)); err != nil || !stat.IsDir() { + h.Git.GitExec("", "clone", "--depth", "1", h.PrjGit, common.DefaultGitPrj) + 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) return nil } - h.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name) - id, _ := h.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch) + h.Git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--depth", "1", "--checkout", action.Repository.Name) + id, _ := h.Git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch) for _, commitId := range action.Commits { if commitId.Id == id { - h.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "fetch", "--depth", "1", "origin", id) - h.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id) - h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push") - h.GitExec(common.DefaultGitPrj, "push") + 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(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push") + h.Git.GitExec(common.DefaultGitPrj, "push") return nil } } @@ -82,7 +81,7 @@ func processPushAction(h *common.RequestHandler) error { } func verifyProjectState(org string, config ConfigRepos) error { - +return nil } var checkOnStart bool