CreateDeployment creates a new deployment for a project.
(ctx context.Context, req *adminv1.CreateDeploymentRequest)
| 247 | |
| 248 | // CreateDeployment creates a new deployment for a project. |
| 249 | func (s *Server) CreateDeployment(ctx context.Context, req *adminv1.CreateDeploymentRequest) (*adminv1.CreateDeploymentResponse, error) { |
| 250 | observability.AddRequestAttributes(ctx, |
| 251 | attribute.String("args.organization_name", req.Org), |
| 252 | attribute.String("args.project_name", req.Project), |
| 253 | attribute.String("args.environment", req.Environment), |
| 254 | attribute.String("args.branch", req.Branch), |
| 255 | attribute.Bool("args.editable", req.Editable), |
| 256 | ) |
| 257 | |
| 258 | proj, err := s.admin.DB.FindProjectByName(ctx, req.Org, req.Project) |
| 259 | if err != nil { |
| 260 | return nil, err |
| 261 | } |
| 262 | |
| 263 | claims := auth.GetClaims(ctx) |
| 264 | permissions := claims.ProjectPermissions(ctx, proj.OrganizationID, proj.ID) |
| 265 | |
| 266 | if req.Environment == "dev" { |
| 267 | if !permissions.ManageDev { |
| 268 | return nil, status.Error(codes.PermissionDenied, "does not have permission to manage dev deployment") |
| 269 | } |
| 270 | } else { |
| 271 | if !permissions.ManageProd { |
| 272 | return nil, status.Error(codes.PermissionDenied, "does not have permission to manage prod deployment") |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Determine branch and slots based on environment. |
| 277 | var branch string |
| 278 | var autogeneratedBranch bool |
| 279 | var slots int |
| 280 | switch req.Environment { |
| 281 | case "prod": |
| 282 | if req.Branch != "" { |
| 283 | branch = req.Branch |
| 284 | } else { |
| 285 | branch = proj.PrimaryBranch |
| 286 | } |
| 287 | if req.Editable { |
| 288 | return nil, status.Error(codes.InvalidArgument, "editable cannot be set for prod deployments") |
| 289 | } |
| 290 | slots = proj.ProdSlots |
| 291 | case "dev": |
| 292 | if req.Branch != "" { |
| 293 | branch = req.Branch |
| 294 | } else { |
| 295 | autogeneratedBranch = true |
| 296 | // Generate a random branch name for dev deployments |
| 297 | b := make([]byte, 8) |
| 298 | _, err := rand.Read(b) |
| 299 | if err != nil { |
| 300 | return nil, status.Error(codes.Internal, err.Error()) |
| 301 | } |
| 302 | branch = fmt.Sprintf("rill/%s", hex.EncodeToString(b)) |
| 303 | } |
| 304 | slots = proj.DevSlots |
| 305 | default: |
| 306 | return nil, status.Error(codes.InvalidArgument, "invalid environment specified, must be 'prod' or 'dev'") |
nothing calls this directly
no test coverage detected