From 9cb5e61f1a6e4ed1a3ecba194bec2b9a5d871ba1 Mon Sep 17 00:00:00 2001 From: Steve McKay Date: Mon, 26 Feb 2024 17:14:10 -0600 Subject: [PATCH 1/2] Add support for deployment approval API As documented in https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment --- audit_events.go | 2 +- deployments.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/audit_events.go b/audit_events.go index 6358f0ea0..de312e560 100644 --- a/audit_events.go +++ b/audit_events.go @@ -16,7 +16,7 @@ type AuditEvent struct { EntityType string `json:"entity_type"` Details AuditEventDetails `json:"details"` CreatedAt *time.Time `json:"created_at"` - EventType string `json:"event_type"` + EventType string `json:"event_type"` } // AuditEventDetails represents the details portion of an audit event for diff --git a/deployments.go b/deployments.go index 3ce8ec40f..c75271c63 100644 --- a/deployments.go +++ b/deployments.go @@ -202,6 +202,39 @@ func (s *DeploymentsService) UpdateProjectDeployment(pid interface{}, deployment return d, resp, nil } +type DeploymentApprovalStatus string + +const ( + DeploymentApproved DeploymentApprovalStatus = "approved" + DeploymentRejected DeploymentApprovalStatus = "rejected" +) + +type ApproveOrRejectProjectDeploymentOptions struct { + Status *DeploymentApprovalStatus `url:"status,omitempty" json:"status,omitempty"` + Comment *string `url:"comment,omitempty" json:"comment,omitempty"` + RepresentedAs *string `url:"represented_as,omitempty" json:"represented_as,omitempty"` +} + +// ApproveOrRejectProjectDeployment approve or reject a blocked deployment. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment +func (s *DeploymentsService) ApproveOrRejectProjectDeployment(pid interface{}, deployment int, + opt *ApproveOrRejectProjectDeploymentOptions, options ...RequestOptionFunc, +) (*Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, err + } + u := fmt.Sprintf("projects/%s/deployments/%d/approval", PathEscape(project), deployment) + + req, err := s.client.NewRequest(http.MethodPost, u, opt, options) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + // DeleteProjectDeployment delete a project deployment. // // GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#delete-a-specific-deployment From fa5414aa61a3c4be879ddd57f0bbc030da1126f6 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Mon, 4 Mar 2024 17:32:35 +0100 Subject: [PATCH 2/2] Moved DeploymentApprovalStatus to types.go --- deployments.go | 36 +++++++++++++++++++++--------------- types.go | 9 +++++++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/deployments.go b/deployments.go index c75271c63..05301acfc 100644 --- a/deployments.go +++ b/deployments.go @@ -88,7 +88,8 @@ type ListProjectDeploymentsOptions struct { // ListProjectDeployments gets a list of deployments in a project. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#list-project-deployments +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#list-project-deployments func (s *DeploymentsService) ListProjectDeployments(pid interface{}, opts *ListProjectDeploymentsOptions, options ...RequestOptionFunc) ([]*Deployment, *Response, error) { project, err := parseID(pid) if err != nil { @@ -112,7 +113,8 @@ func (s *DeploymentsService) ListProjectDeployments(pid interface{}, opts *ListP // GetProjectDeployment get a deployment for a project. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#get-a-specific-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#get-a-specific-deployment func (s *DeploymentsService) GetProjectDeployment(pid interface{}, deployment int, options ...RequestOptionFunc) (*Deployment, *Response, error) { project, err := parseID(pid) if err != nil { @@ -137,7 +139,8 @@ func (s *DeploymentsService) GetProjectDeployment(pid interface{}, deployment in // CreateProjectDeploymentOptions represents the available // CreateProjectDeployment() options. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment type CreateProjectDeploymentOptions struct { Environment *string `url:"environment,omitempty" json:"environment,omitempty"` Ref *string `url:"ref,omitempty" json:"ref,omitempty"` @@ -148,7 +151,8 @@ type CreateProjectDeploymentOptions struct { // CreateProjectDeployment creates a project deployment. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#create-a-deployment func (s *DeploymentsService) CreateProjectDeployment(pid interface{}, opt *CreateProjectDeploymentOptions, options ...RequestOptionFunc) (*Deployment, *Response, error) { project, err := parseID(pid) if err != nil { @@ -173,14 +177,16 @@ func (s *DeploymentsService) CreateProjectDeployment(pid interface{}, opt *Creat // UpdateProjectDeploymentOptions represents the available // UpdateProjectDeployment() options. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment type UpdateProjectDeploymentOptions struct { Status *DeploymentStatusValue `url:"status,omitempty" json:"status,omitempty"` } // UpdateProjectDeployment updates a project deployment. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#update-a-deployment func (s *DeploymentsService) UpdateProjectDeployment(pid interface{}, deployment int, opt *UpdateProjectDeploymentOptions, options ...RequestOptionFunc) (*Deployment, *Response, error) { project, err := parseID(pid) if err != nil { @@ -202,13 +208,11 @@ func (s *DeploymentsService) UpdateProjectDeployment(pid interface{}, deployment return d, resp, nil } -type DeploymentApprovalStatus string - -const ( - DeploymentApproved DeploymentApprovalStatus = "approved" - DeploymentRejected DeploymentApprovalStatus = "rejected" -) - +// ApproveOrRejectProjectDeploymentOptions represents the available +// ApproveOrRejectProjectDeployment() options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment type ApproveOrRejectProjectDeploymentOptions struct { Status *DeploymentApprovalStatus `url:"status,omitempty" json:"status,omitempty"` Comment *string `url:"comment,omitempty" json:"comment,omitempty"` @@ -217,7 +221,8 @@ type ApproveOrRejectProjectDeploymentOptions struct { // ApproveOrRejectProjectDeployment approve or reject a blocked deployment. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#approve-or-reject-a-blocked-deployment func (s *DeploymentsService) ApproveOrRejectProjectDeployment(pid interface{}, deployment int, opt *ApproveOrRejectProjectDeploymentOptions, options ...RequestOptionFunc, ) (*Response, error) { @@ -237,7 +242,8 @@ func (s *DeploymentsService) ApproveOrRejectProjectDeployment(pid interface{}, d // DeleteProjectDeployment delete a project deployment. // -// GitLab API docs: https://docs.gitlab.com/ee/api/deployments.html#delete-a-specific-deployment +// GitLab API docs: +// https://docs.gitlab.com/ee/api/deployments.html#delete-a-specific-deployment func (s *DeploymentsService) DeleteProjectDeployment(pid interface{}, deployment int, options ...RequestOptionFunc) (*Response, error) { project, err := parseID(pid) if err != nil { diff --git a/types.go b/types.go index 3accf2ba4..50e03ed5d 100644 --- a/types.go +++ b/types.go @@ -256,6 +256,15 @@ func BuildState(v BuildStateValue) *BuildStateValue { return Ptr(v) } +// DeploymentApprovalStatus represents a Gitlab deployment approval status. +type DeploymentApprovalStatus string + +// These constants represent all valid deployment approval statuses. +const ( + DeploymentApprovalStatusApproved DeploymentApprovalStatus = "approved" + DeploymentApprovalStatusRejected DeploymentApprovalStatus = "rejected" +) + // DeploymentStatusValue represents a Gitlab deployment status. type DeploymentStatusValue string