fix(be): remove checking of updates from system info endpoint

This commit is contained in:
Denis Gukov 2022-01-20 19:53:48 +05:00
parent 755dca67e2
commit f6b5a39432
6 changed files with 101 additions and 29 deletions

View File

@ -335,16 +335,16 @@ func getSystemInfo(w http.ResponseWriter, r *http.Request) {
return
}
updateAvailable, err := util.CheckUpdate()
//updateAvailable, err := util.CheckUpdate()
if err != nil {
helpers.WriteError(w, err)
return
}
//if err != nil {
// helpers.WriteError(w, err)
// return
//}
body := map[string]interface{}{
"version": util.Version,
"update": updateAvailable,
//"update": updateAvailable,
"config": map[string]string{
"dbHost": dbConfig.Hostname,
"dbName": dbConfig.DbName,

View File

@ -55,8 +55,7 @@ func (t *task) getRepoPath() string {
}
func (t *task) validateRepo() error {
path := t.getRepoPath()
_, err := os.Stat(path)
_, err := os.Stat(t.getRepoPath())
return err
}
@ -503,7 +502,7 @@ func (t *task) getCommitMessage() (res string, err error) {
return
}
func (t *task) updateRepository() error {
func (t *task) makeGitCommand() *exec.Cmd {
var gitSSHCommand string
if t.repository.SSHKey.Type == db.AccessKeySSH {
gitSSHCommand = t.repository.SSHKey.GetSshCommand()
@ -513,28 +512,68 @@ func (t *task) updateRepository() error {
cmd.Dir = util.Config.TmpPath
t.setCmdEnvironment(cmd, gitSSHCommand)
repoURL, repoTag := t.repository.GitURL, "master"
if split := strings.Split(repoURL, "#"); len(split) > 1 {
repoURL, repoTag = split[0], split[1]
return cmd
}
func (t *task) canRepositoryBePulled() bool {
fetchCmd := t.makeGitCommand()
fetchCmd.Args = append(fetchCmd.Args, "fetch")
t.logCmd(fetchCmd)
err := fetchCmd.Run()
if err != nil {
return false
}
checkCmd := t.makeGitCommand()
checkCmd.Args = append(checkCmd.Args, "merge-base", "--is-ancestor", "HEAD", "origin/"+t.repository.GitBranch)
t.logCmd(checkCmd)
err = checkCmd.Run()
return err != nil
}
func (t *task) cloneRepository() error {
cmd := t.makeGitCommand()
t.log("Cloning repository " + t.repository.GitURL)
cmd.Args = append(cmd.Args, "clone", "--recursive", "--branch", t.repository.GitURL, t.repository.GitBranch, t.getRepoName())
t.logCmd(cmd)
return cmd.Run()
}
func (t *task) pullRepository() error {
cmd := t.makeGitCommand()
t.log("Updating repository " + t.repository.GitURL)
cmd.Dir = t.getRepoPath()
cmd.Args = append(cmd.Args, "pull", "origin", t.repository.GitBranch)
t.logCmd(cmd)
return cmd.Run()
}
func (t *task) updateRepository() error {
err := t.validateRepo()
if err != nil {
if !os.IsNotExist(err) {
return err
err = os.RemoveAll(t.getRepoPath())
if err != nil {
return err
}
}
t.log("Cloning repository " + repoURL)
cmd.Args = append(cmd.Args, "clone", "--recursive", "--branch", repoTag, repoURL, t.getRepoName())
} else {
t.log("Updating repository " + repoURL)
cmd.Dir = t.getRepoPath()
cmd.Args = append(cmd.Args, "pull", "origin", repoTag)
return t.cloneRepository()
}
t.logCmd(cmd)
return cmd.Run()
if t.canRepositoryBePulled() {
err = t.pullRepository()
if err == nil {
return nil
}
}
err = os.RemoveAll(t.getRepoPath())
if err != nil {
return err
}
return t.cloneRepository()
}
func (t *task) installRequirements() error {

View File

@ -6,6 +6,7 @@ type Repository struct {
Name string `db:"name" json:"name" binding:"required"`
ProjectID int `db:"project_id" json:"project_id"`
GitURL string `db:"git_url" json:"git_url" binding:"required"`
GitBranch string `db:"git_branch" json:"git_branch" binding:"required"`
SSHKeyID int `db:"ssh_key_id" json:"ssh_key_id" binding:"required"`
Removed bool `db:"removed" json:"removed"`

View File

@ -5,17 +5,18 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/go-gorp/gorp/v3"
"regexp"
"strings"
"time"
)
var (
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
dropForeignKey = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
dropForeignKey = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
)
// prepareMigration converts migration SQLite-query to current dialect.
@ -85,6 +86,26 @@ func (d *SqlDb) applyMigration(version *Version) error {
return err
}
switch version.VersionString() {
case "2.8.26":
rows, err2 := d.sql.Query("SELECT id, git_url FROM project__repositories")
if err2 == nil {
defer rows.Close()
for rows.Next() {
var id, url string
if err3 := rows.Scan(&id, &url); err3 != nil {
branch := "master"
if parts := strings.Split(url, "#"); len(parts) > 1 {
url, branch = parts[0], parts[1]
}
_, _ = d.sql.Exec("UPDATE project__repositories "+
"SET git_url = ?, git_branch = ? "+
"WHERE id = ?", url, branch, id)
}
}
}
}
fmt.Println()
return tx.Commit()

View File

@ -0,0 +1 @@
alter table `project__repository` add git_branch varchar(255);

View File

@ -22,7 +22,7 @@
<v-text-field
v-model="item.git_url"
label="Repository URL"
label="URL"
:rules="[v => !!v || 'Repository is required']"
required
:disabled="formSaving"
@ -30,6 +30,16 @@
@click:append-outer="showHelpDialog('url')"
></v-text-field>
<v-text-field
v-model="item.git_branch"
label="Branch"
:rules="[v => !!v || 'Branch is required']"
required
:disabled="formSaving"
append-outer-icon="mdi-help-circle"
@click:append-outer="showHelpDialog('url')"
></v-text-field>
<v-select
v-model="item.ssh_key_id"
label="Access Key"