@ -83,8 +83,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
cond = cond . And ( builder . Eq { "org_id" : opts . OrgID } )
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
sess := db . GetEngine ( db . DefaultContext )
count , err := sess .
Where ( cond ) .
@ -265,17 +264,17 @@ func (t *Team) addAllRepositories(e db.Engine) error {
// AddAllRepositories adds all repositories to the team
func ( t * Team ) AddAllRepositories ( ) ( err error ) {
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if err = t . addAllRepositories ( s ess ) ; err != nil {
if err = t . addAllRepositories ( db . GetEngin e( ctx ) ) ; err != nil {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
// AddRepository adds new repository to team of organization.
@ -286,17 +285,17 @@ func (t *Team) AddRepository(repo *Repository) (err error) {
return nil
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if err = t . addRepository ( s ess , repo ) ; err != nil {
if err = t . addRepository ( db . GetEngin e( ctx ) , repo ) ; err != nil {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
// RemoveAllRepositories removes all repositories from team and recalculates access
@ -305,17 +304,17 @@ func (t *Team) RemoveAllRepositories() (err error) {
return nil
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if err = t . removeAllRepositories ( s ess ) ; err != nil {
if err = t . removeAllRepositories ( db . GetEngin e( ctx ) ) ; err != nil {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
// removeAllRepositories removes all repositories from team and recalculates access
@ -422,17 +421,17 @@ func (t *Team) RemoveRepository(repoID int64) error {
return err
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if err = t . removeRepository ( s ess , repo , true ) ; err != nil {
if err = t . removeRepository ( db . GetEngin e( ctx ) , repo , true ) ; err != nil {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
// UnitEnabled returns if the team has the given unit type enabled
@ -494,17 +493,13 @@ func NewTeam(t *Team) (err error) {
return ErrTeamAlreadyExist { t . OrgID , t . LowerName }
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if _ , err = sess . Insert ( t ) ; err != nil {
errRollback := sess . Rollback ( )
if errRollback != nil {
log . Error ( "NewTeam sess.Rollback: %v" , errRollback )
}
if err = db . Insert ( ctx , t ) ; err != nil {
return err
}
@ -513,32 +508,24 @@ func NewTeam(t *Team) (err error) {
for _ , unit := range t . Units {
unit . TeamID = t . ID
}
if _ , err = sess . Insert ( & t . Units ) ; err != nil {
errRollback := sess . Rollback ( )
if errRollback != nil {
log . Error ( "NewTeam sess.Rollback: %v" , errRollback )
}
if err = db . Insert ( ctx , & t . Units ) ; err != nil {
return err
}
}
// Add all repositories to the team if it has access to all of them.
if t . IncludesAllRepositories {
err = t . addAllRepositories ( s ess )
err = t . addAllRepositories ( db . GetEngin e( ctx ) )
if err != nil {
return fmt . Errorf ( "addAllRepositories: %v" , err )
}
}
// Update organization number of teams.
if _ , err = sess . Exec ( "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?" , t . OrgID ) ; err != nil {
errRollback := sess . Rollback ( )
if errRollback != nil {
log . Error ( "NewTeam sess.Rollback: %v" , errRollback )
}
if _ , err = db . Exec ( ctx , "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?" , t . OrgID ) ; err != nil {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
func getTeam ( e db . Engine , orgID int64 , name string ) ( * Team , error ) {
@ -624,11 +611,12 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
t . Description = t . Description [ : 255 ]
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
sess := db . GetEngine ( ctx )
t . LowerName = strings . ToLower ( t . Name )
has , err := sess .
@ -659,10 +647,6 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
return err
}
if _ , err = sess . Cols ( "org_id" , "team_id" , "type" ) . Insert ( & t . Units ) ; err != nil {
errRollback := sess . Rollback ( )
if errRollback != nil {
log . Error ( "UpdateTeam sess.Rollback: %v" , errRollback )
}
return err
}
}
@ -688,7 +672,7 @@ func UpdateTeam(t *Team, authChanged, includeAllChanged bool) (err error) {
}
}
return sess . Commit ( )
return committer . Commit ( )
}
// DeleteTeam deletes given team.
@ -698,11 +682,12 @@ func DeleteTeam(t *Team) error {
return err
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
sess := db . GetEngine ( ctx )
if err := t . getMembers ( sess ) ; err != nil {
return err
@ -736,7 +721,7 @@ func DeleteTeam(t *Team) error {
return err
}
return sess . Commit ( )
return committer . Commit ( )
}
// ___________ ____ ___
@ -839,13 +824,15 @@ func AddTeamMember(team *Team, userID int64) error {
return err
}
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if _ , err := sess . Insert ( & TeamUser {
sess := db . GetEngine ( ctx )
if err := db . Insert ( ctx , & TeamUser {
UID : userID ,
OrgID : team . OrgID ,
TeamID : team . ID ,
@ -869,7 +856,7 @@ func AddTeamMember(team *Team, userID int64) error {
}
}
return sess . Commit ( )
return committer . Commit ( )
}
func removeTeamMember ( ctx context . Context , team * Team , userID int64 ) error {
@ -1048,25 +1035,21 @@ func getUnitsByTeamID(e db.Engine, teamID int64) (units []*TeamUnit, err error)
// UpdateTeamUnits updates a teams's units
func UpdateTeamUnits ( team * Team , units [ ] TeamUnit ) ( err error ) {
sess := db . NewSession ( db . DefaultContext )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
ctx , committer , err := db . TxContext ( )
if err != nil {
return err
}
defer committer . Close ( )
if _ , err = s ess . Where ( "team_id = ?" , team . ID ) . Delete ( new ( TeamUnit ) ) ; err != nil {
if _ , err = db . GetEngin e( ctx ) . Where ( "team_id = ?" , team . ID ) . Delete ( new ( TeamUnit ) ) ; err != nil {
return err
}
if len ( units ) > 0 {
if _ , err = sess . Insert ( units ) ; err != nil {
errRollback := sess . Rollback ( )
if errRollback != nil {
log . Error ( "UpdateTeamUnits sess.Rollback: %v" , errRollback )
}
if err = db . Insert ( ctx , units ) ; err != nil {
return err
}
}
return sess . Commit ( )
return committer . Commit ( )
}