/
projectgroup.go
158 lines (133 loc) · 4.99 KB
/
projectgroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package action
import (
"context"
"path"
csapi "agola.io/agola/internal/services/configstore/api"
"agola.io/agola/internal/services/types"
"agola.io/agola/internal/util"
errors "golang.org/x/xerrors"
)
func (h *ActionHandler) GetProjectGroup(ctx context.Context, projectGroupRef string) (*csapi.ProjectGroup, error) {
projectGroup, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projectGroup, nil
}
func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*csapi.ProjectGroup, error) {
projectGroups, resp, err := h.configstoreClient.GetProjectGroupSubgroups(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projectGroups, nil
}
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*csapi.Project, error) {
projects, resp, err := h.configstoreClient.GetProjectGroupProjects(ctx, projectGroupRef)
if err != nil {
return nil, ErrFromRemote(resp, err)
}
return projects, nil
}
type CreateProjectGroupRequest struct {
CurrentUserID string
Name string
ParentRef string
Visibility types.Visibility
}
func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProjectGroupRequest) (*csapi.ProjectGroup, error) {
if !util.ValidateName(req.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid projectGroup name %q", req.Name))
}
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentRef)
if err != nil {
return nil, errors.Errorf("failed to get project group %q: %w", req.ParentRef, ErrFromRemote(resp, err))
}
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
if err != nil {
return nil, errors.Errorf("failed to determine ownership: %w", err)
}
if !isProjectOwner {
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
}
user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil {
return nil, errors.Errorf("failed to get user %q: %w", req.CurrentUserID, ErrFromRemote(resp, err))
}
parentRef := req.ParentRef
if parentRef == "" {
// create projectGroup in current user namespace
parentRef = path.Join("user", user.Name)
}
p := &types.ProjectGroup{
Name: req.Name,
Parent: types.Parent{
Type: types.ConfigTypeProjectGroup,
ID: parentRef,
},
Visibility: req.Visibility,
}
h.log.Infof("creating projectGroup")
rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p)
if err != nil {
return nil, errors.Errorf("failed to create projectGroup: %w", ErrFromRemote(resp, err))
}
h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID)
return rp, nil
}
type UpdateProjectGroupRequest struct {
Name string
Visibility types.Visibility
}
func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *UpdateProjectGroupRequest) (*csapi.ProjectGroup, error) {
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
if err != nil {
return nil, errors.Errorf("failed to get project group %q: %w", projectGroupRef, ErrFromRemote(resp, err))
}
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
if err != nil {
return nil, errors.Errorf("failed to determine ownership: %w", err)
}
if !isProjectOwner {
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
}
pg.Name = req.Name
pg.Visibility = req.Visibility
h.log.Infof("updating project group")
rp, resp, err := h.configstoreClient.UpdateProjectGroup(ctx, pg.ID, pg.ProjectGroup)
if err != nil {
return nil, errors.Errorf("failed to update project group: %w", ErrFromRemote(resp, err))
}
h.log.Infof("project group %q updated, ID: %s", pg.Name, pg.ID)
return rp, nil
}
func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectRef string) error {
p, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectRef)
if err != nil {
return errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
}
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
if err != nil {
return errors.Errorf("failed to determine ownership: %w", err)
}
if !isProjectOwner {
return util.NewErrForbidden(errors.Errorf("user not authorized"))
}
resp, err = h.configstoreClient.DeleteProjectGroup(ctx, projectRef)
if err != nil {
return ErrFromRemote(resp, err)
}
return nil
}