Skip to content

Commit

Permalink
Change to use team names instead of ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsvantesson committed Jan 18, 2020
1 parent 44a6bb2 commit a46dbc2
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 127 deletions.
36 changes: 36 additions & 0 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,23 @@ func GetTeam(orgID int64, name string) (*Team, error) {
return getTeam(x, orgID, name)
}

// GetTeamIDsByNames returns a slice of team ids corresponds to names.
func GetTeamIDsByNames(orgID int64, names []string, ignoreNonExistent bool) ([]int64, error) {
ids := make([]int64, 0, len(names))
for _, name := range names {
u, err := GetTeam(orgID, name)
if err != nil {
if ignoreNonExistent {
continue
} else {
return nil, err
}
}
ids = append(ids, u.ID)
}
return ids, nil
}

// getOwnerTeam returns team by given team name and organization.
func getOwnerTeam(e Engine, orgID int64) (*Team, error) {
return getTeam(e, orgID, ownerTeamName)
Expand All @@ -562,6 +579,25 @@ func GetTeamByID(teamID int64) (*Team, error) {
return getTeamByID(x, teamID)
}

// GetTeamNamesByID returns team's lower name from a list of team ids.
func GetTeamNamesByID(teamIDs []int64) ([]string, error) {
if len(teamIDs) == 0 {
return []string{}, nil
}

teams := make([]*Team, 0, len(teamIDs))

err := x.In("id", teamIDs).
Asc("name").
Find(&teams)

teamnames := make([]string, 0, len(teams))
for _, t := range teams {
teamnames = append(teamnames, t.LowerName)
}
return teamnames, err
}

// UpdateTeam updates information of team.
func UpdateTeam(t *Team, authChanged bool, includeAllChanged bool) (err error) {
if len(t.Name) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,8 +1349,8 @@ func GetMaileableUsersByIDs(ids []int64) ([]*User, error) {
Find(&ous)
}

// GetUsernamesByIDs returns usernames for all resolved users from a list of Ids.
func GetUsernamesByIDs(ids []int64) ([]string, error) {
// GetUserNamesByIDs returns usernames for all resolved users from a list of Ids.
func GetUserNamesByIDs(ids []int64) ([]string, error) {
ous, err := GetUsersByIDs(ids)
unames := make([]string, 0, len(ous))
for _, u := range ous {
Expand Down
34 changes: 21 additions & 13 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.
UserCanPush: true,
UserCanMerge: true,
EffectiveBranchProtectionName: "",
EffectiveBRanchProtectionID: 0,
}
}
branchProtectionName := ""
var branchProtectionID int64
if isRepoAdmin {
branchProtectionName = bp.BranchName
branchProtectionID = bp.ID
}

return &api.Branch{
Expand All @@ -62,23 +59,34 @@ func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.
UserCanPush: bp.CanUserPush(user.ID),
UserCanMerge: bp.IsUserMergeWhitelisted(user.ID),
EffectiveBranchProtectionName: branchProtectionName,
EffectiveBRanchProtectionID: branchProtectionID,
}
}

// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
pushWhitelistUsernames, err := models.GetUsernamesByIDs(bp.WhitelistUserIDs)
pushWhitelistUsernames, err := models.GetUserNamesByIDs(bp.WhitelistUserIDs)
if err != nil {
log.Error("GetUsernamesByIDs (WhitelistUserIDs): %v", err)
log.Error("GetUserNamesByIDs (WhitelistUserIDs): %v", err)
}
mergeWhitelistUsernames, err := models.GetUsernamesByIDs(bp.MergeWhitelistUserIDs)
mergeWhitelistUsernames, err := models.GetUserNamesByIDs(bp.MergeWhitelistUserIDs)
if err != nil {
log.Error("GetUsernamesByIDs (MergeWhitelistUserIDs): %v", err)
log.Error("GetUserNamesByIDs (MergeWhitelistUserIDs): %v", err)
}
approvalsWhitelistUsernames, err := models.GetUsernamesByIDs(bp.ApprovalsWhitelistUserIDs)
approvalsWhitelistUsernames, err := models.GetUserNamesByIDs(bp.ApprovalsWhitelistUserIDs)
if err != nil {
log.Error("GetUsernamesByIDs (ApprovalsWhitelistUserIDs): %v", err)
log.Error("GetUserNamesByIDs (ApprovalsWhitelistUserIDs): %v", err)
}
pushWhitelistTeams, err := models.GetTeamNamesByID(bp.WhitelistTeamIDs)
if err != nil {
log.Error("GetTeamNamesByID (WhitelistTeamIDs): %v", err)
}
mergeWhitelistTeams, err := models.GetTeamNamesByID(bp.MergeWhitelistTeamIDs)
if err != nil {
log.Error("GetTeamNamesByID (MergeWhitelistTeamIDs): %v", err)
}
approvalsWhitelistTeams, err := models.GetTeamNamesByID(bp.ApprovalsWhitelistTeamIDs)
if err != nil {
log.Error("GetTeamNamesByID (ApprovalsWhitelistTeamIDs): %v", err)
}

return &api.BranchProtection{
Expand All @@ -87,17 +95,17 @@ func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
EnablePush: bp.CanPush,
EnablePushWhitelist: bp.EnableWhitelist,
PushWhitelistUsernames: pushWhitelistUsernames,
PushWhitelistTeamIDs: bp.WhitelistTeamIDs,
PushWhitelistTeams: pushWhitelistTeams,
PushWhitelistDeployKeys: bp.WhitelistDeployKeys,
EnableMergeWhitelist: bp.EnableMergeWhitelist,
MergeWhitelistUsernames: mergeWhitelistUsernames,
MergeWhitelistTeamIDs: bp.MergeWhitelistTeamIDs,
MergeWhitelistTeams: mergeWhitelistTeams,
EnableStatusCheck: bp.EnableStatusCheck,
StatusCheckContexts: bp.StatusCheckContexts,
RequiredApprovals: bp.RequiredApprovals,
EnableApprovalsWhitelist: bp.EnableApprovalsWhitelist,
ApprovalsWhitelistUsernames: approvalsWhitelistUsernames,
ApprovalsWhitelistTeamIDs: bp.ApprovalsWhitelistTeamIDs,
ApprovalsWhitelistTeams: approvalsWhitelistTeams,
Created: bp.CreatedUnix.AsTime(),
Updated: bp.UpdatedUnix.AsTime(),
}
Expand Down
19 changes: 9 additions & 10 deletions modules/structs/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type Branch struct {
UserCanPush bool `json:"user_can_push"`
UserCanMerge bool `json:"user_can_merge"`
EffectiveBranchProtectionName string `json:"effective_branch_protection_name"`
EffectiveBRanchProtectionID int64 `json:"effective_branch_protection_id"`
}

// BranchProtection represents a branch protection for a repository
Expand All @@ -29,17 +28,17 @@ type BranchProtection struct {
EnablePush bool `json:"enable_push"`
EnablePushWhitelist bool `json:"enable_push_whitelist"`
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
PushWhitelistTeams []string `json:"push_whitelist_teams"`
PushWhitelistDeployKeys bool `json:"push_whitelist_deploy_keys"`
EnableMergeWhitelist bool `json:"enable_merge_whitelist"`
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_ids"`
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
EnableStatusCheck bool `json:"enable_status_check"`
StatusCheckContexts []string `json:"status_check_contexts"`
RequiredApprovals int64 `json:"required_approvals"`
EnableApprovalsWhitelist bool `json:"enable_approvals_whitelist"`
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
// swagger:strfmt date-time
Created time.Time `json:"created_at"`
// swagger:strfmt date-time
Expand All @@ -52,33 +51,33 @@ type CreateBranchProtectionOption struct {
EnablePush bool `json:"enable_push"`
EnablePushWhitelist bool `json:"enable_push_whitelist"`
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
PushWhitelistTeams []string `json:"push_whitelist_teams"`
PushWhitelistDeployKeys bool `json:"push_whitelist_deploy_keys"`
EnableMergeWhitelist bool `json:"enable_merge_whitelist"`
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_team_ids"`
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
EnableStatusCheck bool `json:"enable_status_check"`
StatusCheckContexts []string `json:"status_check_contexts"`
RequiredApprovals int64 `json:"required_approvals"`
EnableApprovalsWhitelist bool `json:"enable_approvals_whitelist"`
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
}

// EditBranchProtectionOption options for editing a branch protection
type EditBranchProtectionOption struct {
EnablePush *bool `json:"enable_push"`
EnablePushWhitelist *bool `json:"enable_push_whitelist"`
PushWhitelistUsernames []string `json:"push_whitelist_usernames"`
PushWhitelistTeamIDs []int64 `json:"push_whitelist_team_ids"`
PushWhitelistTeams []string `json:"push_whitelist_teams"`
PushWhitelistDeployKeys *bool `json:"push_whitelist_deploy_keys"`
EnableMergeWhitelist *bool `json:"enable_merge_whitelist"`
MergeWhitelistUsernames []string `json:"merge_whitelist_usernames"`
MergeWhitelistTeamIDs []int64 `json:"merge_whitelist_team_ids"`
MergeWhitelistTeams []string `json:"merge_whitelist_teams"`
EnableStatusCheck *bool `json:"enable_status_check"`
StatusCheckContexts []string `json:"status_check_contexts"`
RequiredApprovals *int64 `json:"required_approvals"`
EnableApprovalsWhitelist *bool `json:"enable_approvals_whitelist"`
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
ApprovalsWhitelistTeamIDs []int64 `json:"approvals_whitelist_team_ids"`
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
}
4 changes: 2 additions & 2 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/branch_protections", func() {
m.Get("", repo.ListBranchProtections)
m.Post("", bind(api.CreateBranchProtectionOption{}), repo.CreateBranchProtection)
m.Group("/:id", func() {
m.Group("/:name", func() {
m.Get("", repo.GetBranchProtection)
m.Put("", bind(api.EditBranchProtectionOption{}), repo.EditBranchProtection)
m.Patch("", bind(api.EditBranchProtectionOption{}), repo.EditBranchProtection)
m.Delete("", repo.DeleteBranchProtection)
})
}, reqToken(), reqAdmin())
Expand Down
Loading

0 comments on commit a46dbc2

Please sign in to comment.