From 272c2300d9561ba461c28a54adb545668975d05c Mon Sep 17 00:00:00 2001 From: porsh Date: Mon, 14 Jun 2021 19:14:47 +0300 Subject: [PATCH 1/4] Fixed params on create Github Hook --- server/store/repo/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/store/repo/repo.go b/server/store/repo/repo.go index c7cf3ca6..2b1b3579 100644 --- a/server/store/repo/repo.go +++ b/server/store/repo/repo.go @@ -215,7 +215,7 @@ func (s repositoryStore) CreateHook(id, userID uint, data gitscm.HookForm) error } target := fmt.Sprintf("%s/webhooks", repo.Provider.Host) - _, err = gitscm.CreateHook(repo.FullName, target, repo.Provider.Name, repo.Provider.Secret, data) + _, err = gitscm.CreateHook(repo.FullName, target, repo.Provider.Secret, repo.Provider.Name, data) return err } From dac452f0dab22526a28e30d481490848cfbbd1bb Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 15 Jun 2021 13:53:56 +0300 Subject: [PATCH 2/4] Added login by UserName (#1) Added check is user already exists when create new user --- server/api/user/login.go | 2 +- server/core/user.go | 8 +++++++- server/store/user/user.go | 25 ++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/server/api/user/login.go b/server/api/user/login.go index b7f6e143..fce832f6 100644 --- a/server/api/user/login.go +++ b/server/api/user/login.go @@ -31,7 +31,7 @@ func HandleLogin(users core.UserStore) http.HandlerFunc { } if users.Login(f.Email, f.Password) { - user, _ := users.FindEmail(f.Email) + user, _ := users.FindEmailOrName(f.Email) token, err := auth.JWT.CreateJWT(user.Claims()) if err != nil { render.InternalServerError(w, err.Error()) diff --git a/server/core/user.go b/server/core/user.go index 28b858a3..ba932b9b 100644 --- a/server/core/user.go +++ b/server/core/user.go @@ -8,7 +8,7 @@ type ( ID uint `gorm:"primary_key;auto_increment;not null" json:"id"` Email string `gorm:"not null;size:255;unique_index" json:"email"` Password string `gorm:"not null;size:255;column:password" json:"-"` - Name string `gorm:"not null;size:255" json:"name"` + Name string `gorm:"not null;size:255;unique_index" json:"name"` Avatar string `gorm:"not null;size:255;default:'/assets/images/avatars/avatar_1.svg'" json:"avatar"` Role string `gorm:"not null;size:20;default:'user'" json:"role"` Active bool `gorm:"not null;default:true" json:"active"` @@ -24,6 +24,12 @@ type ( // FindEmail returns a user from the datastore by email. FindEmail(string) (*User, error) + // FindName returns a user from the datastore by username. + FindName(string) (*User, error) + + // FindEmailOrName returns a user from the datastore by email or username. + FindEmailOrName(string) (*User, error) + // List returns a list of users from datastore. List() ([]*User, error) diff --git a/server/store/user/user.go b/server/store/user/user.go index 6d4e489e..f5b05002 100644 --- a/server/store/user/user.go +++ b/server/store/user/user.go @@ -29,6 +29,19 @@ func (s userStore) FindEmail(email string) (*core.User, error) { return &user, err } +func (s userStore) FindName(name string) (*core.User, error) { + var user core.User + err := s.db.Model(&user).Preload("Teams").Where("name = ?", name).First(&user).Error + return &user, err +} + +func (s userStore) FindEmailOrName(pattern string) (*core.User, error) { + var user core.User + err := s.db.Model(&user).Preload("Teams"). + Where("name = ? OR email = ?", pattern, pattern).First(&user).Error + return &user, err +} + func (s userStore) List() ([]*core.User, error) { var users []*core.User err := s.db.Model(users).Preload("Teams").Find(&users).Error @@ -42,6 +55,16 @@ func (s userStore) Create(user *core.User) error { } user.Password = hash + _, err = s.FindEmail(user.Email) + if err == nil { + return fmt.Errorf("email already exists") + } + + _, err = s.FindName(user.Name) + if err == nil { + return fmt.Errorf("username already exists") + } + return s.db.Create(user).Error } @@ -73,7 +96,7 @@ func (s userStore) Delete(user *core.User) error { } func (s userStore) Login(email, password string) bool { - user, err := s.FindEmail(email) + user, err := s.FindEmailOrName(email) if err != nil { return false } From 3d1b51e3202d00c1352ff659bf46fdef0823e8e5 Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 16 Jun 2021 09:18:59 +0300 Subject: [PATCH 3/4] Remove escape sequences from Log raw view (#2) --- server/api/build/log.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/api/build/log.go b/server/api/build/log.go index 80391a39..b6338bcd 100644 --- a/server/api/build/log.go +++ b/server/api/build/log.go @@ -3,6 +3,7 @@ package build import ( "io" "net/http" + "regexp" "strconv" "github.com/bleenco/abstruse/internal/auth" @@ -45,6 +46,8 @@ func HandleLog(jobs core.JobStore, scheduler core.Scheduler) http.HandlerFunc { w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) - io.WriteString(w, job.Log) + re := regexp.MustCompile(`(\[1;[0-9][0-9]m|\[0m|\x1b|\r)`) + log := re.ReplaceAllString(job.Log, "") + io.WriteString(w, log) } } From 18d3b1af64a488778ec2acf8b65dfeeb145f649a Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 16 Jun 2021 09:45:59 +0300 Subject: [PATCH 4/4] Fix after_* scripts (#3) * Run ater_failure script only if error * Run ater_success script only if no errors * Add color functions --- worker/docker/docker.go | 62 ++++++++++++++++++++++++++++++----------- worker/docker/util.go | 12 ++++++-- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/worker/docker/docker.go b/worker/docker/docker.go index a4997891..9afcf677 100644 --- a/worker/docker/docker.go +++ b/worker/docker/docker.go @@ -54,7 +54,43 @@ func RunContainer(name, image string, job *api.Job, config *config.Config, env [ cacheSaved := false - for i, command := range job.GetCommands() { + ExecCmd := func(command *api.Command) (string, error) { + cmd := strings.Split(command.GetCommand(), " ") + str := yellow("\r==> " + strings.Join(cmd, " ") + "\r\n") + logch <- []byte(str) + shcmd := []string{shell, "-ci", strings.Join(cmd, " ")} + conn, execID, err := exec(cli, containerID, shcmd, env) + if err != nil { + logch <- []byte(err.Error()) + return "", err + } + for { + buf := make([]byte, 4096) + n, err := conn.Reader.Read(buf) + if err != nil { + conn.Close() + break + } + logch <- buf[:n] + } + return execID, nil + } + + var commands []*api.Command + var failureCmd, successCmd *api.Command = nil, nil + for _, command := range job.GetCommands() { + if command.GetType() == api.Command_AfterFailure { + failureCmd = command + continue + } + if command.GetType() == api.Command_AfterSuccess { + successCmd = command + continue + } + commands = append(commands, command) + } + + for i, command := range commands { if !isContainerRunning(cli, containerID) { if err := startContainer(cli, containerID); err != nil { logch <- []byte(err.Error()) @@ -101,25 +137,11 @@ func RunContainer(name, image string, job *api.Job, config *config.Config, env [ } } - cmd := strings.Split(command.GetCommand(), " ") - - str := yellow("\r==> " + strings.Join(cmd, " ") + "\r\n") - logch <- []byte(str) - shcmd := []string{shell, "-ci", strings.Join(cmd, " ")} - conn, execID, err := exec(cli, containerID, shcmd, env) + execID, err := ExecCmd(command) if err != nil { logch <- []byte(err.Error()) return err } - for { - buf := make([]byte, 4096) - n, err := conn.Reader.Read(buf) - if err != nil { - conn.Close() - break - } - logch <- buf[:n] - } inspect, err := cli.ContainerExecInspect(ctx, execID) if err != nil { logch <- []byte(err.Error()) @@ -127,12 +149,20 @@ func RunContainer(name, image string, job *api.Job, config *config.Config, env [ } exitCode = inspect.ExitCode if exitCode != 0 { + if failureCmd != nil { + logch <- []byte(red("==> Starting after_failure script...\n")) + ExecCmd(failureCmd) + } break } } logch <- []byte(genExitMessage(exitCode)) if exitCode == 0 { + if successCmd != nil { + logch <- []byte(green("==> Starting after_success script...\n")) + ExecCmd(successCmd) + } return nil } return fmt.Errorf("errored: %d", exitCode) diff --git a/worker/docker/util.go b/worker/docker/util.go index 0f0bcf68..9ed321c8 100644 --- a/worker/docker/util.go +++ b/worker/docker/util.go @@ -8,11 +8,19 @@ import ( func genExitMessage(code int) string { if code == 0 { - return aurora.Bold(aurora.Green(fmt.Sprintf("\nExit code: %d", code))).String() + return green(fmt.Sprintf("\nExit code: %d", code)) } - return aurora.Bold(aurora.Red(fmt.Sprintf("\nExit code: %d", code))).String() + return red(fmt.Sprintf("\nExit code: %d", code)) +} + +func green(str string) string { + return aurora.Bold(aurora.Green(str)).String() } func yellow(str string) string { return aurora.Bold(aurora.Yellow(str)).String() } + +func red(str string) string { + return aurora.Bold(aurora.Red(str)).String() +}