This commit is contained in:
Adam Majer 2024-08-28 17:20:09 +02:00
parent 8b7f552e77
commit ecf0ec0aea
12 changed files with 146 additions and 239 deletions

View File

@ -177,7 +177,7 @@ func (h writeFunc) Close() error {
return err 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 := exec.Command("/usr/bin/git", params...)
cmd.Env = []string{ cmd.Env = []string{
"GIT_CEILING_DIRECTORIES=" + e.GitPath, "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) log.Printf("git execute: %#v\n", cmd.Args)
} }
out, err := cmd.CombinedOutput() 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 { type ChanIO struct {
@ -523,11 +532,15 @@ func (e *GitHandler) GitCatFile(cwd, commitId, filename string) (data []byte, er
cmd.Stdout = &data_in cmd.Stdout = &data_in
cmd.Stdin = &data_out cmd.Stdin = &data_out
cmd.Stderr = writeFunc(func(data []byte) (int, error) { cmd.Stderr = writeFunc(func(data []byte) (int, error) {
e.ErrLogger.Println(data) if e.DebugLogger {
log.Printf(string(data))
}
return len(data), nil return len(data), nil
}) })
e.StdLogger.Printf("command run: %v\n", cmd.Args) if e.DebugLogger {
e.Error = cmd.Run() log.Printf("command run: %v\n", cmd.Args)
}
err = cmd.Run()
done.Lock() done.Lock()
return return
@ -546,19 +559,19 @@ func (e *GitHandler) GitSubmoduleList(cwd, commitId string) (submoduleList map[s
data_out.Write([]byte(commitId)) data_out.Write([]byte(commitId))
data_out.ch <- '\x00' data_out.ch <- '\x00'
c, err := parseGitCommit(data_in.ch) var c commit
c, err = parseGitCommit(data_in.ch)
if err != nil { if err != nil {
e.Error = err err = fmt.Errorf("Error parsing git commit. Err: %w", err)
e.ErrLogger.Printf("Error parsing git commit: %v\n", err)
return return
} }
data_out.Write([]byte(c.Tree)) data_out.Write([]byte(c.Tree))
data_out.ch <- '\x00' data_out.ch <- '\x00'
tree, err := parseGitTree(data_in.ch) var tree tree
tree, err = parseGitTree(data_in.ch)
if err != nil { if err != nil {
e.Error = err err = fmt.Errorf("Error parsing git tree: %w", err)
e.ErrLogger.Printf("Error parsing git tree: %v\n", err)
return return
} }
@ -575,17 +588,19 @@ func (e *GitHandler) GitSubmoduleList(cwd, commitId string) (submoduleList map[s
"GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_GLOBAL=/dev/null",
} }
cmd.Dir = filepath.Join(e.GitPath, cwd) cmd.Dir = filepath.Join(e.GitPath, cwd)
cmd.Stdout = &data_in
cmd.Stdin = &data_out
cmd.Stderr = writeFunc(func(data []byte) (int, error) { cmd.Stderr = writeFunc(func(data []byte) (int, error) {
e.ErrLogger.Println(data) if e.DebugLogger {
log.Println(string(data))
}
return len(data), nil return len(data), nil
}) })
e.StdLogger.Printf("command run: %v\n", cmd.Args) if e.DebugLogger {
e.Error = cmd.Run() log.Printf("command run: %v\n", cmd.Args)
}
err = cmd.Run()
done.Lock() done.Lock()
return submoduleList return submoduleList, err
} }
func (e *GitHandler) GitSubmoduleCommitId(cwd, packageName, commitId string) (subCommitId string, valid bool) { func (e *GitHandler) GitSubmoduleCommitId(cwd, packageName, commitId string) (subCommitId string, valid bool) {

View File

@ -241,11 +241,8 @@ func TestCommitTreeParsingOfHead(t *testing.T) {
t.Run("reads HEAD and parses the tree", func(t *testing.T) { t.Run("reads HEAD and parses the tree", func(t *testing.T) {
const nodejs21 = "c678c57007d496a98bec668ae38f2c26a695f94af78012f15d044ccf066ccb41" const nodejs21 = "c678c57007d496a98bec668ae38f2c26a695f94af78012f15d044ccf066ccb41"
stdLogger, errLogger:= CreateStdoutLogger(os.Stdout, os.Stdout) h := GitHandler{
h := RequestHandler{
GitPath: gitDir, GitPath: gitDir,
StdLogger: stdLogger,
ErrLogger: errLogger,
} }
id, ok := h.GitSubmoduleCommitId("", "nodejs21", commitId) id, ok := h.GitSubmoduleCommitId("", "nodejs21", commitId)
if !ok { if !ok {
@ -257,15 +254,12 @@ func TestCommitTreeParsingOfHead(t *testing.T) {
}) })
t.Run("reads README.md", func (t *testing.T) { t.Run("reads README.md", func (t *testing.T) {
stdLogger, errLogger:= CreateStdoutLogger(os.Stdout, os.Stdout) h := GitHandler{
h := RequestHandler{
GitPath: gitDir, GitPath: gitDir,
StdLogger: stdLogger,
ErrLogger: errLogger,
} }
data := h.GitCatFile("", commitId, "README.md") data, err := h.GitCatFile("", commitId, "README.md")
if h.HasError() { if err != nil {
t.Errorf("failed parse: %v", h.Error) t.Errorf("failed parse: %v", err)
} }
if string(data) != "foo\n" || len(data) != 4 { if string(data) != "foo\n" || len(data) != 4 {
t.Errorf("Wrong data of len: %d", len(data)) t.Errorf("Wrong data of len: %d", len(data))

View File

@ -1,9 +1,9 @@
package common package common
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"path/filepath" "path/filepath"
"slices" "slices"
@ -46,10 +46,6 @@ const (
) )
func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) { 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() transport, client := h.allocateGiteaTransport()
pr, err := client.Repository.RepoGetPullRequest( pr, err := client.Repository.RepoGetPullRequest(
repository.NewRepoGetPullRequestParams(). repository.NewRepoGetPullRequestParams().
@ -61,8 +57,7 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64
) )
if err != nil { if err != nil {
h.Error = err log.Println(err.Error())
h.ErrLogger.Println(err.Error())
return nil, nil, err return nil, nil, err
} }
@ -78,8 +73,7 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64
) )
if err != nil { if err != nil {
h.Error = err log.Println(err.Error())
h.ErrLogger.Println(err.Error())
return nil, nil, err 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) { func (h *RequestHandler) GetPullNotifications(since *time.Time) ([]*models.NotificationThread, error) {
if h.HasError() {
return nil, h.Error
}
bigLimit := int64(100000) bigLimit := int64(100000)
transport, client := h.allocateGiteaTransport() 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) list, err := client.Notification.NotifyGetList(params, transport.DefaultAuthentication)
if err != nil { if err != nil {
h.Error = err
return nil, err return nil, err
} }
if !list.IsSuccess() { if !list.IsSuccess() {
h.Error = fmt.Errorf("Cannot fetch notifications: %s", list.Error()) return nil, fmt.Errorf("Cannot fetch notifications: %s", list.Error())
return nil, h.Error
} }
return list.Payload, nil return list.Payload, nil
} }
func (h *RequestHandler) SetNotificationRead(notificationId int64) error { func (h *RequestHandler) SetNotificationRead(notificationId int64) error {
if h.HasError() {
return h.Error
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
list, err := client.Notification.NotifyReadThread( list, err := client.Notification.NotifyReadThread(
notification.NewNotifyReadThreadParams(). notification.NewNotifyReadThreadParams().
@ -133,24 +117,18 @@ func (h *RequestHandler) SetNotificationRead(notificationId int64) error {
) )
if err != nil { if err != nil {
h.ErrLogger.Printf("Error setting notification: %d: %v\n", notificationId, err) log.Printf("Error setting notification: %d: %v\n", notificationId, err)
h.Error = err
return err return err
} }
if !list.IsSuccess() { if !list.IsSuccess() {
h.Error = fmt.Errorf("Cannot update notifications: %d", notificationId) return fmt.Errorf("Cannot update notifications: %d", notificationId)
return h.Error
} }
return nil return nil
} }
func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) *models.Repository { func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) (*models.Repository, error) {
if h.HasError() {
return nil
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
repo, err := client.Repository.RepoGet( repo, err := client.Repository.RepoGet(
repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName), repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName),
@ -177,50 +155,40 @@ func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName s
case *organization.CreateOrgRepoCreated: case *organization.CreateOrgRepoCreated:
h.StdLogger.Printf("repo '%s' created, with notification error?\n", repoName) h.StdLogger.Printf("repo '%s' created, with notification error?\n", repoName)
default: default:
h.Error = err log.Printf("error creating repo '%s' under '%s': %s\n", repoName, org.Username, err.Error())
h.ErrLogger.Printf("error creating repo '%s' under '%s': %s\n", repoName, org.Username, err.Error()) return nil, err
return nil
} }
} else { } else {
h.StdLogger.Printf("repo '%s' created\n", repoName) h.StdLogger.Printf("repo '%s' created\n", repoName)
} }
// initialize repository // initialize repository
h.Error = os.Mkdir(filepath.Join(h.GitPath, DefaultGitPrj), 0700) if err = os.Mkdir(filepath.Join(h.Git.GitPath, DefaultGitPrj), 0700); err != nil {
if h.HasError() { return nil, err
return nil
} }
h.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName) h.Git.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName)
h.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch) h.Git.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch)
if h.HasError() { readmeFilename := filepath.Join(h.Git.GitPath, DefaultGitPrj, "README.md")
return nil
}
readmeFilename := filepath.Join(h.GitPath, DefaultGitPrj, "README.md")
{ {
file, _ := os.Create(readmeFilename) file, _ := os.Create(readmeFilename)
defer file.Close() defer file.Close()
io.WriteString(file, ReadmeBoilerplate) io.WriteString(file, ReadmeBoilerplate)
} }
h.GitExec(DefaultGitPrj, "add", "README.md") h.Git.GitExec(DefaultGitPrj, "add", "README.md")
h.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation") h.Git.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation")
h.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL) h.Git.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL)
return repo.Payload return repo.Payload, nil
default: default:
h.Error = err return nil, fmt.Errorf("cannot fetch repo data for '%s' / '%s' : %w", org.Username, repoName, err)
h.ErrLogger.Printf("cannot fetch repo data for '%s' / '%s' : %v\n", 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 { func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targetId, title, body string) (*models.PullRequest, error) {
if h.HasError() {
return nil
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
prOptions := models.CreatePullRequestOption{ prOptions := models.CreatePullRequestOption{
@ -241,25 +209,13 @@ func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targe
) )
if err != nil { if err != nil {
h.ErrLogger.Printf("Cannot create pull request: %v\n", err) return nil, fmt.Errorf("Cannot create pull request. %w", err)
h.Error = err
return nil
} }
if !pr.IsSuccess() { return pr.GetPayload(), nil
h.ErrLogger.Printf("PR creation failed: %s\n", pr.Error())
h.Error = errors.New(pr.Error())
return nil
}
return pr.GetPayload()
} }
func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) []*models.PullReview { func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) ([]*models.PullReview, error) {
if h.HasError() {
return nil
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
reviewOptions := models.PullReviewRequestOptions{ reviewOptions := models.PullReviewRequestOptions{
@ -277,25 +233,13 @@ func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string)
) )
if err != nil { if err != nil {
h.ErrLogger.Printf("Cannot create pull request: %v\n", err) return nil, fmt.Errorf("Cannot create pull request: %w", err)
h.Error = err
return nil
} }
if !review.IsSuccess() { return review.GetPayload(), nil
h.ErrLogger.Printf("PR creation failed: %s\n", review.Error())
h.Error = errors.New(review.Error())
return nil
}
return review.GetPayload()
} }
func (h *RequestHandler) IsReviewed(pr *models.PullRequest) (bool, error) { func (h *RequestHandler) IsReviewed(pr *models.PullRequest) (bool, error) {
if h.HasError() {
return false, h.Error
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
// TODO: get review from project git // TODO: get review from project git
reviewers := pr.RequestedReviewers reviewers := pr.RequestedReviewers
@ -406,11 +350,7 @@ func (h *RequestHandler) AddReviewComment(pr *models.PullRequest, state models.R
return c.Payload, nil return c.Payload, nil
} }
func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) *models.PullRequest { func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) (*models.PullRequest, error) {
if h.HasError() {
return nil
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
var page, maxSize int64 var page, maxSize int64
@ -429,12 +369,7 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestWebhookEvent) *mod
transport.DefaultAuthentication) transport.DefaultAuthentication)
if err != nil { 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, fmt.Errorf("cannot fetch PR list for %s / %s : %w", 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())
} }
prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number) 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 { for _, line := range lines {
if strings.TrimSpace(line) == prLine { 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) { func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash, path string) ([]byte, error) {
if h.HasError() {
return nil, h.Error
}
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
var retData []byte var retData []byte
@ -469,7 +400,7 @@ func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash,
retData = data retData = data
return len(data), nil return len(data), nil
}) })
file, err := client.Repository.RepoGetRawFile( _, err := client.Repository.RepoGetRawFile(
repository.NewRepoGetRawFileParams(). repository.NewRepoGetRawFileParams().
WithOwner(repo.Owner.UserName). WithOwner(repo.Owner.UserName).
WithRepo(repo.Name). WithRepo(repo.Name).
@ -484,10 +415,6 @@ func (h *RequestHandler) GetRepositoryFileContent(repo *models.Repository, hash,
return nil, err return nil, err
} }
if !file.IsSuccess() {
return nil, fmt.Errorf("Invalid response from server (%d): %s", file.Code(), file.Error())
}
return retData, nil return retData, nil
} }

View File

@ -185,9 +185,15 @@ func ProcessRabbitMQEvents(listenDefs ListenDefinitions, orgs []string) error {
org := route[2] org := route[2]
if handler, found := listenDefs.Handlers[org]; found { 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) req, err := ParseRequestJSON(reqType, msg.Body)
if err != nil { if err != nil {
log.Printf("Error parsing request JSON: %v\n", err)
continue
} else { } else {
h.Request = req h.Request = req
ProcessEvent(handler, h) ProcessEvent(handler, h)

View File

@ -69,36 +69,32 @@ func ParseRequestJSON(reqType string, data []byte) (req *Request, err error) {
} }
type RequestHandler struct { type RequestHandler struct {
Error error Branch string
GitCommiter string PrjGit string
GitPath string
Branch string
PrjGit string
StdLogger, ErrLogger *log.Logger StdLogger, ErrLogger *log.Logger
Request *Request Request *Request
Git GitHandler
} }
func (r *RequestHandler) WriteError() { func (r *RequestHandler) WriteError() {
r.ErrLogger.Println("internal error sent") 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) var h *RequestHandler = new(RequestHandler)
h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stderr) h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stderr)
h.GitCommiter = git_author var err error
h.GitPath, h.Error = os.MkdirTemp("", name) h.Git.GitCommiter = git_author
if h.Error != nil { h.Git.GitPath, err = os.MkdirTemp("", name)
h.ErrLogger.Printf("Cannot create temp dir: %v\n", h.Error) if err != nil {
return h return nil, fmt.Errorf("Cannot create temp dir: %w", err)
} }
if h.Error = os.Chmod(h.GitPath, 0700); h.Error != nil { if err = os.Chmod(h.Git.GitPath, 0700); err != nil {
h.ErrLogger.Printf("Cannot fix permissions of temp dir: %v\n", h.Error) return nil, fmt.Errorf("Cannot fix permissions of temp dir: %w", err)
return h
} }
return h return h, nil
} }

View File

@ -2,8 +2,8 @@ package common
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"strings"
) )
type Head struct { type Head struct {
@ -50,31 +50,18 @@ func (p *PullRequestWebhookEvent) GetAction() string {
return p.Action return p.Action
} }
func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestWebhookEvent { func (h *RequestHandler) parsePullRequest(data io.Reader) (action *PullRequestWebhookEvent, err error) {
if h.HasError() { action=new(PullRequestWebhookEvent)
return nil err = json.NewDecoder(data).Decode(&action)
if err != nil {
return nil, fmt.Errorf("Got error while parsing json: %w", err)
} }
var action PullRequestWebhookEvent h.Request.Data = action
h.Error = json.NewDecoder(data).Decode(&action) return
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
} }
func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestWebhookEvent { func (h *RequestHandler) parsePullRequestSync(data io.Reader) (*PullRequestWebhookEvent, error) {
return h.parsePullRequest(data) return h.parsePullRequest(data)
} }

View File

@ -11,14 +11,17 @@ func TestPrParsing(t *testing.T) {
var h RequestHandler var h RequestHandler
h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) 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 { 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)) _, err = h.parsePullRequest(strings.NewReader(samplePRsync_JSON))
if prs == nil { if err != nil {
t.Fatalf("parsing PR failed: %v", h.Error) t.Fatalf("parsing PR failed: %v", err)
} }
}) })
} }

View File

@ -2,6 +2,7 @@ package common
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"strings" "strings"
) )
@ -25,31 +26,24 @@ func (*PushWebhookEvent) GetAction() string {
return "push" return "push"
} }
func (h *RequestHandler) parsePushRequest(data io.Reader) *PushWebhookEvent { func (h *RequestHandler) parsePushRequest(data io.Reader) (*PushWebhookEvent, error) {
if h.HasError() { action := new(PushWebhookEvent)
return nil err := json.NewDecoder(data).Decode(&action)
}
var action PushWebhookEvent if err != nil {
h.Error = json.NewDecoder(data).Decode(&action) return nil, fmt.Errorf("Got error while parsing: %w", err)
if h.HasError() {
h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error)
return nil
} }
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/") repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" { 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, fmt.Errorf("Unexpected URL for SSH repository: '%s'", action.Repository.Name)
return nil
} }
h.StdLogger.Printf("Request push for repo: %s\n", action.Repository.Full_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 { if len(action.Commits) < 1 || len(action.Head_Commit.Id) != 64 {
h.ErrLogger.Println("Request has no action .... skipping") return nil, fmt.Errorf("Request has no action .... skipping")
return nil
} }
return &action return action, nil
} }

View File

@ -1,7 +1,6 @@
package common package common
import ( import (
"os"
"strings" "strings"
"testing" "testing"
) )
@ -10,13 +9,9 @@ func TestPushRequestParsing(t *testing.T) {
t.Run("parsing repo creation message", func(t *testing.T) { t.Run("parsing repo creation message", func(t *testing.T) {
var h RequestHandler var h RequestHandler
h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) json, err := h.parsePushRequest(strings.NewReader(examplePushJSON))
json := h.parsePushRequest(strings.NewReader(examplePushJSON)) if err != nil {
if json == nil { t.Fatalf("failed to parser push request: %v", err)
t.Fatalf("failed to parser push request: %v", h.Error)
}
if h.HasError() {
t.Fatalf("Can't parse struct: %s", h.Error.Error())
} }
if json.Total_Commits < 1 || json.Total_Commits != len(json.Commits) { if json.Total_Commits < 1 || json.Total_Commits != len(json.Commits) {

View File

@ -46,33 +46,24 @@ func (r *RepositoryWebhookEvent) GetAction() string {
} }
// TODO: sanity check values!!!! // TODO: sanity check values!!!!
func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) *RepositoryWebhookEvent { func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) (data *RepositoryWebhookEvent, err error) {
if h.HasError() { data = new(RepositoryWebhookEvent)
return nil if err = json.NewDecoder(dataReader).Decode(&data); err != nil {
} return nil, err
var data RepositoryWebhookEvent
h.Error = json.NewDecoder(dataReader).Decode(&data)
if h.HasError() {
return nil
} }
repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/") repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/")
if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" { if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" {
h.Error = fmt.Errorf("No data, skipping") return nil, fmt.Errorf("Unexpected URL for SSH repository: %w", data.Repository.Name)
h.ErrLogger.Printf("Unexpected URL for SSH repository: %s\n", data.Repository.Name)
return nil
} }
data.PrjGit = data.Repository.Ssh_Url[:repoIdx+1] + DefaultGitPrj + ".git" 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) h.StdLogger.Printf("Request '%s' for repo: %s\n", data.Action, data.Repository.Full_Name)
if len(data.Action) < 1 { if len(data.Action) < 1 {
h.Error = fmt.Errorf("No data, skipping") return nil, fmt.Errorf("Request has no data.... skipping")
h.ErrLogger.Println("Request has no data.... skipping")
} }
h.Request.Data = &data h.Request.Data = data
return &data return data, nil
} }

View File

@ -21,9 +21,9 @@ func TestRepositoryRequestParsing(t *testing.T) {
var h RequestHandler var h RequestHandler
h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout) h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stdout)
json := h.parseRepositoryRequest(strings.NewReader(repoCreateJSON)) json, err := h.parseRepositoryRequest(strings.NewReader(repoCreateJSON))
if h.HasError() { if err != nil {
t.Fatalf("Can't parse struct: %s", h.Error) t.Fatalf("Can't parse struct: %s", err)
} }
if json.Action != "created" { if json.Action != "created" {

View File

@ -11,7 +11,6 @@ import (
"sync" "sync"
"time" "time"
"golang.org/x/tools/go/analysis/passes/defers"
"src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common"
) )
@ -30,21 +29,21 @@ func processRepositoryAction(h *common.RequestHandler) error {
} }
h.CreateRepositoryIfNotExist(*action.Organization, h.PrjGit) 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 { switch action.Action {
case "created": case "created":
h.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.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion") h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package inclusion")
h.GitExec(common.DefaultGitPrj, "push") h.Git.GitExec(common.DefaultGitPrj, "push")
case "deleted": 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) h.StdLogger.Printf("delete event for %s -- not in project. Ignoring\n", action.Repository.Name)
return nil return nil
} }
h.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name) h.Git.GitExec(common.DefaultGitPrj, "rm", action.Repository.Name)
h.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal") h.Git.GitExec(common.DefaultGitPrj, "commit", "-m", "Automatic package removal")
h.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)
} }
@ -60,19 +59,19 @@ func processPushAction(h *common.RequestHandler) error {
return nil return nil
} }
h.GitExec("", "clone", "--depth", "1", h.PrjGit, common.DefaultGitPrj) h.Git.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() { 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) h.StdLogger.Printf("Pushed to package that is not part of the project. Ignoring. : %v\n", err)
return nil return nil
} }
h.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)
id, _ := h.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch) id, _ := h.Git.GitBranchHead(filepath.Join(common.DefaultGitPrj, action.Repository.Name), action.Repository.Default_Branch)
for _, commitId := range action.Commits { for _, commitId := range action.Commits {
if commitId.Id == id { if commitId.Id == id {
h.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.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id) h.Git.GitExec(filepath.Join(common.DefaultGitPrj, action.Repository.Name), "checkout", id)
h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push") h.Git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "Automatic update via push")
h.GitExec(common.DefaultGitPrj, "push") h.Git.GitExec(common.DefaultGitPrj, "push")
return nil return nil
} }
} }
@ -82,7 +81,7 @@ func processPushAction(h *common.RequestHandler) error {
} }
func verifyProjectState(org string, config ConfigRepos) error { func verifyProjectState(org string, config ConfigRepos) error {
return nil
} }
var checkOnStart bool var checkOnStart bool