Skip to content

Commit a46dbc2

Browse files
Change to use team names instead of ids.
1 parent 44a6bb2 commit a46dbc2

File tree

7 files changed

+198
-127
lines changed

7 files changed

+198
-127
lines changed

models/org_team.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ func GetTeam(orgID int64, name string) (*Team, error) {
541541
return getTeam(x, orgID, name)
542542
}
543543

544+
// GetTeamIDsByNames returns a slice of team ids corresponds to names.
545+
func GetTeamIDsByNames(orgID int64, names []string, ignoreNonExistent bool) ([]int64, error) {
546+
ids := make([]int64, 0, len(names))
547+
for _, name := range names {
548+
u, err := GetTeam(orgID, name)
549+
if err != nil {
550+
if ignoreNonExistent {
551+
continue
552+
} else {
553+
return nil, err
554+
}
555+
}
556+
ids = append(ids, u.ID)
557+
}
558+
return ids, nil
559+
}
560+
544561
// getOwnerTeam returns team by given team name and organization.
545562
func getOwnerTeam(e Engine, orgID int64) (*Team, error) {
546563
return getTeam(e, orgID, ownerTeamName)
@@ -562,6 +579,25 @@ func GetTeamByID(teamID int64) (*Team, error) {
562579
return getTeamByID(x, teamID)
563580
}
564581

582+
// GetTeamNamesByID returns team's lower name from a list of team ids.
583+
func GetTeamNamesByID(teamIDs []int64) ([]string, error) {
584+
if len(teamIDs) == 0 {
585+
return []string{}, nil
586+
}
587+
588+
teams := make([]*Team, 0, len(teamIDs))
589+
590+
err := x.In("id", teamIDs).
591+
Asc("name").
592+
Find(&teams)
593+
594+
teamnames := make([]string, 0, len(teams))
595+
for _, t := range teams {
596+
teamnames = append(teamnames, t.LowerName)
597+
}
598+
return teamnames, err
599+
}
600+
565601
// UpdateTeam updates information of team.
566602
func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
567603
if len(t.Name) == 0 {

models/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,8 +1349,8 @@ func GetMaileableUsersByIDs(ids []int64) ([]*User, error) {
13491349
Find(&ous)
13501350
}
13511351

1352-
// GetUsernamesByIDs returns usernames for all resolved users from a list of Ids.
1353-
func GetUsernamesByIDs(ids []int64) ([]string, error) {
1352+
// GetUserNamesByIDs returns usernames for all resolved users from a list of Ids.
1353+
func GetUserNamesByIDs(ids []int64) ([]string, error) {
13541354
ous, err := GetUsersByIDs(ids)
13551355
unames := make([]string, 0, len(ous))
13561356
for _, u := range ous {

modules/convert/convert.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.
4242
UserCanPush: true,
4343
UserCanMerge: true,
4444
EffectiveBranchProtectionName: "",
45-
EffectiveBRanchProtectionID: 0,
4645
}
4746
}
4847
branchProtectionName := ""
49-
var branchProtectionID int64
5048
if isRepoAdmin {
5149
branchProtectionName = bp.BranchName
52-
branchProtectionID = bp.ID
5350
}
5451

5552
return &api.Branch{
@@ -62,23 +59,34 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.
6259
UserCanPush: bp.CanUserPush(user.ID),
6360
UserCanMerge: bp.IsUserMergeWhitelisted(user.ID),
6461
EffectiveBranchProtectionName: branchProtectionName,
65-
EffectiveBRanchProtectionID: branchProtectionID,
6662
}
6763
}
6864

6965
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
7066
func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
71-
pushWhitelistUsernames, err := models.GetUsernamesByIDs(bp.WhitelistUserIDs)
67+
pushWhitelistUsernames, err := models.GetUserNamesByIDs(bp.WhitelistUserIDs)
7268
if err != nil {
73-
log.Error("GetUsernamesByIDs (WhitelistUserIDs): %v", err)
69+
log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err)
7470
}
75-
mergeWhitelistUsernames, err := models.GetUsernamesByIDs(bp.MergeWhitelistUserIDs)
71+
mergeWhitelistUsernames, err := models.GetUserNamesByIDs(bp.MergeWhitelistUserIDs)
7672
if err != nil {
77-
log.Error("GetUsernamesByIDs (MergeWhitelistUserIDs): %v", err)
73+
log.Error("GetUserNamesByIDs (MergeWhitelistUserIDs): %v", err)
7874
}
79-
approvalsWhitelistUsernames, err := models.GetUsernamesByIDs(bp.ApprovalsWhitelistUserIDs)
75+
approvalsWhitelistUsernames, err := models.GetUserNamesByIDs(bp.ApprovalsWhitelistUserIDs)
8076
if err != nil {
81-
log.Error("GetUsernamesByIDs (ApprovalsWhitelistUserIDs): %v", err)
77+
log.Error("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v", err)
78+
}
79+
pushWhitelistTeams, err := models.GetTeamNamesByID(bp.WhitelistTeamIDs)
80+
if err != nil {
81+
log.Error("GetTeamNamesByID (WhitelistTeamIDs): %v", err)
82+
}
83+
mergeWhitelistTeams, err := models.GetTeamNamesByID(bp.MergeWhitelistTeamIDs)
84+
if err != nil {
85+
log.Error("GetTeamNamesByID (MergeWhitelistTeamIDs): %v", err)
86+
}
87+
approvalsWhitelistTeams, err := models.GetTeamNamesByID(bp.ApprovalsWhitelistTeamIDs)
88+
if err != nil {
89+
log.Error("GetTeamNamesByID (ApprovalsWhitelistTeamIDs): %v", err)
8290
}
8391

8492
return &api.BranchProtection{
@@ -87,17 +95,17 @@ func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
8795
EnablePush: bp.CanPush,
8896
EnablePushWhitelist: bp.EnableWhitelist,
8997
PushWhitelistUsernames: pushWhitelistUsernames,
90-
PushWhitelistTeamIDs: bp.WhitelistTeamIDs,
98+
PushWhitelistTeams: pushWhitelistTeams,
9199
PushWhitelistDeployKeys: bp.WhitelistDeployKeys,
92100
EnableMergeWhitelist: bp.EnableMergeWhitelist,
93101
MergeWhitelistUsernames: mergeWhitelistUsernames,
94-
MergeWhitelistTeamIDs: bp.MergeWhitelistTeamIDs,
102+
MergeWhitelistTeams: mergeWhitelistTeams,
95103
EnableStatusCheck: bp.EnableStatusCheck,
96104
StatusCheckContexts: bp.StatusCheckContexts,
97105
RequiredApprovals: bp.RequiredApprovals,
98106
EnableApprovalsWhitelist: bp.EnableApprovalsWhitelist,
99107
ApprovalsWhitelistUsernames: approvalsWhitelistUsernames,
100-
ApprovalsWhitelistTeamIDs: bp.ApprovalsWhitelistTeamIDs,
108+
ApprovalsWhitelistTeams: approvalsWhitelistTeams,
101109
Created: bp.CreatedUnix.AsTime(),
102110
Updated: bp.UpdatedUnix.AsTime(),
103111
}

modules/structs/repo_branch.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ type Branch struct {
1919
UserCanPush bool `json:"user_can_push"`
2020
UserCanMerge bool `json:"user_can_merge"`
2121
EffectiveBranchProtectionName string `json:"effective_branch_protection_name"`
22-
EffectiveBRanchProtectionID int64 `json:"effective_branch_protection_id"`
2322
}
2423

2524
// BranchProtection represents a branch protection for a repository
@@ -29,17 +28,17 @@ type BranchProtection struct {
2928
EnablePush bool `json:"enable_push"`
3029
EnablePushWhitelist bool `json:"enable_push_whitelist"`
3130
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
32-
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
31+
PushWhitelistTeams []string `json:"push_whitelist_teams"`
3332
PushWhitelistDeployKeys bool `json:"push_whitelist_deploy_keys"`
3433
EnableMergeWhitelist bool `json:"enable_merge_whitelist"`
3534
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
36-
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_ids"`
35+
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
3736
EnableStatusCheck bool `json:"enable_status_check"`
3837
StatusCheckContexts []string `json:"status_check_contexts"`
3938
RequiredApprovals int64 `json:"required_approvals"`
4039
EnableApprovalsWhitelist bool `json:"enable_approvals_whitelist"`
4140
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
42-
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
41+
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
4342
// swagger:strfmt date-time
4443
Created time.Time `json:"created_at"`
4544
// swagger:strfmt date-time
@@ -52,33 +51,33 @@ type CreateBranchProtectionOption struct {
5251
EnablePush bool `json:"enable_push"`
5352
EnablePushWhitelist bool `json:"enable_push_whitelist"`
5453
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
55-
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
54+
PushWhitelistTeams []string `json:"push_whitelist_teams"`
5655
PushWhitelistDeployKeys bool `json:"push_whitelist_deploy_keys"`
5756
EnableMergeWhitelist bool `json:"enable_merge_whitelist"`
5857
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
59-
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_team_ids"`
58+
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
6059
EnableStatusCheck bool `json:"enable_status_check"`
6160
StatusCheckContexts []string `json:"status_check_contexts"`
6261
RequiredApprovals int64 `json:"required_approvals"`
6362
EnableApprovalsWhitelist bool `json:"enable_approvals_whitelist"`
6463
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
65-
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
64+
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
6665
}
6766

6867
// EditBranchProtectionOption options for editing a branch protection
6968
type EditBranchProtectionOption struct {
7069
EnablePush *bool `json:"enable_push"`
7170
EnablePushWhitelist *bool `json:"enable_push_whitelist"`
7271
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
73-
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
72+
PushWhitelistTeams []string `json:"push_whitelist_teams"`
7473
PushWhitelistDeployKeys *bool `json:"push_whitelist_deploy_keys"`
7574
EnableMergeWhitelist *bool `json:"enable_merge_whitelist"`
7675
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
77-
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_team_ids"`
76+
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
7877
EnableStatusCheck *bool `json:"enable_status_check"`
7978
StatusCheckContexts []string `json:"status_check_contexts"`
8079
RequiredApprovals *int64 `json:"required_approvals"`
8180
EnableApprovalsWhitelist *bool `json:"enable_approvals_whitelist"`
8281
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
83-
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
82+
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
8483
}

routers/api/v1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,9 @@ func RegisterRoutes(m *macaron.Macaron) {
658658
m.Group("/branch_protections", func() {
659659
m.Get("", repo.ListBranchProtections)
660660
m.Post("", bind(api.CreateBranchProtectionOption{}), repo.CreateBranchProtection)
661-
m.Group("/:id", func() {
661+
m.Group("/:name", func() {
662662
m.Get("", repo.GetBranchProtection)
663-
m.Put("", bind(api.EditBranchProtectionOption{}), repo.EditBranchProtection)
663+
m.Patch("", bind(api.EditBranchProtectionOption{}), repo.EditBranchProtection)
664664
m.Delete("", repo.DeleteBranchProtection)
665665
})
666666
}, reqToken(), reqAdmin())

0 commit comments

Comments
 (0)