diff options
author | Bwko <bouwko@gmail.com> | 2017-10-26 02:49:16 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-10-26 08:49:16 +0800 |
commit | 3ab580c8d6b8a2c063d848f8e3002347c9e5cebb (patch) | |
tree | 3f66f793be25db1ca8baac8d5333e39bfdd4f7e1 /models | |
parent | e86a0bf3feab82c1b3439806245083dffb2f37c9 (diff) | |
download | gitea-3ab580c8d6b8a2c063d848f8e3002347c9e5cebb.tar.gz gitea-3ab580c8d6b8a2c063d848f8e3002347c9e5cebb.zip |
Add branch overiew page (#2108)
* Add branch overiew page
* fix changed method name on sub menu
* remove unused code
Diffstat (limited to 'models')
-rw-r--r-- | models/branches.go | 107 | ||||
-rw-r--r-- | models/branches_test.go | 89 | ||||
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v47.go | 29 | ||||
-rw-r--r-- | models/models.go | 1 |
5 files changed, 228 insertions, 0 deletions
diff --git a/models/branches.go b/models/branches.go index 6a0bf6c19f..1c06a08357 100644 --- a/models/branches.go +++ b/models/branches.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" @@ -193,3 +194,109 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) { return sess.Commit() } + +// DeletedBranch struct +type DeletedBranch struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` + Name string `xorm:"UNIQUE(s) NOT NULL"` + Commit string `xorm:"UNIQUE(s) NOT NULL"` + DeletedByID int64 `xorm:"INDEX"` + DeletedBy *User `xorm:"-"` + Deleted time.Time `xorm:"-"` + DeletedUnix int64 `xorm:"INDEX created"` +} + +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (deletedBranch *DeletedBranch) AfterLoad() { + deletedBranch.Deleted = time.Unix(deletedBranch.DeletedUnix, 0).Local() +} + +// AddDeletedBranch adds a deleted branch to the database +func (repo *Repository) AddDeletedBranch(branchName, commit string, deletedByID int64) error { + deletedBranch := &DeletedBranch{ + RepoID: repo.ID, + Name: branchName, + Commit: commit, + DeletedByID: deletedByID, + } + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + if _, err := sess.InsertOne(deletedBranch); err != nil { + return err + } + + return sess.Commit() +} + +// GetDeletedBranches returns all the deleted branches +func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) { + deletedBranches := make([]*DeletedBranch, 0) + return deletedBranches, x.Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches) +} + +// GetDeletedBranchByID get a deleted branch by its ID +func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) { + deletedBranch := &DeletedBranch{ID: ID} + has, err := x.Get(deletedBranch) + if err != nil { + return nil, err + } + if !has { + return nil, nil + } + return deletedBranch, nil +} + +// RemoveDeletedBranch removes a deleted branch from the database +func (repo *Repository) RemoveDeletedBranch(id int64) (err error) { + deletedBranch := &DeletedBranch{ + RepoID: repo.ID, + ID: id, + } + + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + if affected, err := sess.Delete(deletedBranch); err != nil { + return err + } else if affected != 1 { + return fmt.Errorf("remove deleted branch ID(%v) failed", id) + } + + return sess.Commit() +} + +// LoadUser loads the user that deleted the branch +// When there's no user found it returns a NewGhostUser +func (deletedBranch *DeletedBranch) LoadUser() { + user, err := GetUserByID(deletedBranch.DeletedByID) + if err != nil { + user = NewGhostUser() + } + deletedBranch.DeletedBy = user +} + +// RemoveOldDeletedBranches removes old deleted branches +func RemoveOldDeletedBranches() { + if !taskStatusTable.StartIfNotRunning(`deleted_branches_cleanup`) { + return + } + defer taskStatusTable.Stop(`deleted_branches_cleanup`) + + log.Trace("Doing: DeletedBranchesCleanup") + + deleteBefore := time.Now().Add(-setting.Cron.DeletedBranchesCleanup.OlderThan) + _, err := x.Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch)) + if err != nil { + log.Error(4, "DeletedBranchesCleanup: %v", err) + } +} diff --git a/models/branches_test.go b/models/branches_test.go new file mode 100644 index 0000000000..512104cdd0 --- /dev/null +++ b/models/branches_test.go @@ -0,0 +1,89 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +var firstBranch = DeletedBranch{ + ID: 1, + Name: "foo", + Commit: "1213212312313213213132131", + DeletedByID: int64(1), +} + +var secondBranch = DeletedBranch{ + ID: 2, + Name: "bar", + Commit: "5655464564554545466464655", + DeletedByID: int64(99), +} + +func TestAddDeletedBranch(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + assert.NoError(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) + assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID)) + assert.NoError(t, repo.AddDeletedBranch(secondBranch.Name, secondBranch.Commit, secondBranch.DeletedByID)) +} +func TestGetDeletedBranches(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}) + repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + + branches, err := repo.GetDeletedBranches() + assert.NoError(t, err) + assert.Len(t, branches, 2) +} + +func TestGetDeletedBranch(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + assert.NotNil(t, getDeletedBranch(t, firstBranch)) +} + +func TestDeletedBranchLoadUser(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + branch := getDeletedBranch(t, firstBranch) + assert.Nil(t, branch.DeletedBy) + branch.LoadUser() + assert.NotNil(t, branch.DeletedBy) + assert.Equal(t, "user1", branch.DeletedBy.Name) + + branch = getDeletedBranch(t, secondBranch) + assert.Nil(t, branch.DeletedBy) + branch.LoadUser() + assert.NotNil(t, branch.DeletedBy) + assert.Equal(t, "Ghost", branch.DeletedBy.Name) +} + +func TestRemoveDeletedBranch(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + branch := DeletedBranch{ID: 1} + AssertExistsAndLoadBean(t, &branch) + repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + + err := repo.RemoveDeletedBranch(1) + assert.NoError(t, err) + AssertNotExistsBean(t, &branch) + AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}) +} + +func getDeletedBranch(t *testing.T, branch DeletedBranch) *DeletedBranch { + AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}) + repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository) + + deletedBranch, err := repo.GetDeletedBranchByID(branch.ID) + assert.NoError(t, err) + assert.Equal(t, branch.ID, deletedBranch.ID) + assert.Equal(t, branch.Name, deletedBranch.Name) + assert.Equal(t, branch.Commit, deletedBranch.Commit) + assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID) + + return deletedBranch +} diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 33678c0d29..f1cb1a67ed 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -142,6 +142,8 @@ var migrations = []Migration{ NewMigration("remove index column from repo_unit table", removeIndexColumnFromRepoUnitTable), // v46 -> v47 NewMigration("remove organization watch repositories", removeOrganizationWatchRepo), + // v47 -> v48 + NewMigration("add deleted branches", addDeletedBranch), } // Migrate database to current version diff --git a/models/migrations/v47.go b/models/migrations/v47.go new file mode 100644 index 0000000000..7a217e6f01 --- /dev/null +++ b/models/migrations/v47.go @@ -0,0 +1,29 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "fmt" + + "github.com/go-xorm/xorm" +) + +func addDeletedBranch(x *xorm.Engine) (err error) { + // DeletedBranch contains the deleted branch information + type DeletedBranch struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` + Name string `xorm:"UNIQUE(s) NOT NULL"` + Commit string `xorm:"UNIQUE(s) NOT NULL"` + DeletedByID int64 `xorm:"INDEX NOT NULL"` + DeletedUnix int64 `xorm:"INDEX"` + } + + if err = x.Sync2(new(DeletedBranch)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + + return nil +} diff --git a/models/models.go b/models/models.go index a461dbc47b..66a7d97872 100644 --- a/models/models.go +++ b/models/models.go @@ -114,6 +114,7 @@ func init() { new(CommitStatus), new(Stopwatch), new(TrackedTime), + new(DeletedBranch), ) gonicNames := []string{"SSL", "UID"} |