Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: update for external approval demo (#6990)
ecmadaoa576117620a9
diff --git a/backend/legacyapi/external_approval.go b/backend/legacyapi/external_approval.goindex 56874d711c..11cbf0c6c8 100644--- a/backend/legacyapi/external_approval.go+++ b/backend/legacyapi/external_approval.go@@ -25,7 +25,7 @@ type ExternalApprovalPayloadFeishu struct {// ExternalApprovalPayloadRelay is the payload for relay type ExternalApproval.type ExternalApprovalPayloadRelay struct {ExternalApprovalNodeID string `json:"externalApprovalNodeId"`- URI string `json:"uri"`+ ID string `json:"id"`}// ExternalApprovalEventActionType is the type of the action which the user took.diff --git a/backend/plugin/app/relay/relay_client.go b/backend/plugin/app/relay/relay_client.goindex dae5c0ec5f..ad65457817 100644--- a/backend/plugin/app/relay/relay_client.go+++ b/backend/plugin/app/relay/relay_client.go@@ -28,21 +28,39 @@ func NewClient() *Client {// CreatePayload is the message to create external approval.type CreatePayload struct {- // TODO(d): content TBD.+ IssueID string `json:"issueId"`+ Creator string `json:"creator"`+ Title string `json:"title"`+ Description string `json:"description"`+ Project string `json:"project"`+ Assignee string `json:"assignee"`+ Statement string `json:"statement"`+ CreateTime time.Time `json:"createTime"`}-// CreateResponse is the response message to create external approval.-type CreateResponse struct {- URI string `json:"uri"`+// Status is the status of the external approval.+type Status string++const (+ // StatusApproved means that the external approval is approved.+ StatusApproved Status = "APPROVED"+ // StatusRejected means that the external approval is rejected.+ StatusRejected Status = "REJECTED"+)++// ResponsePayload is the response message to for external approval.+type ResponsePayload struct {+ ID string `json:"id"`+ Status Status `json:"status"`}// Create sends a message to create external approval.-func (c *Client) Create(relayEndpoint string, payload CreatePayload) (string, error) {+func (c *Client) Create(relayEndpoint string, payload *CreatePayload) (string, error) {out, err := json.Marshal(&payload)if err != nil {return "", err}- req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/approvals", relayEndpoint), bytes.NewBuffer(out))+ req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/approval", relayEndpoint), bytes.NewBuffer(out))if err != nil {return "", err}@@ -53,27 +71,27 @@ func (c *Client) Create(relayEndpoint string, payload CreatePayload) (string, erif resp.StatusCode != http.StatusOK {return "", errors.Errorf("failed to create external approval with status %v", resp.Status)}- responseBody := &CreateResponse{}+ responseBody := &ResponsePayload{}err = json.NewDecoder(resp.Body).Decode(responseBody)if err != nil {return "", err}- return responseBody.URI, nil+ return responseBody.ID, nil}// UpdatePayload is the message to update external approval.type UpdatePayload struct {- URI string `json:"uri"`+ Title string `json:"title"`+ Statement string `json:"statement"`}-// UpdateStatus sends a message to update the status of an external approval.-func (c *Client) UpdateStatus(relayEndpoint string, uri string) error {- payload := &UpdatePayload{URI: uri}+// UpdateApproval sends a message to update the external approval.+func (c *Client) UpdateApproval(relayEndpoint string, id string, payload *UpdatePayload) error {out, err := json.Marshal(&payload)if err != nil {return err}- req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/approvals", relayEndpoint), bytes.NewBuffer(out))+ req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/approval/%s", relayEndpoint, id), bytes.NewBuffer(out))if err != nil {return err}@@ -87,41 +105,23 @@ func (c *Client) UpdateStatus(relayEndpoint string, uri string) error {return nil}-// Status is the status of the external approval.-type Status string--// GetStatus is the response message to get the status of an external approval.-type GetStatus struct {- Status Status `json:"status"`-}--const (- // StatusApproved means that the external approval is approved.- StatusApproved Status = "APPROVED"- // StatusRejected means that the external approval is rejected.- StatusRejected Status = "REJECTED"-)--// GetStatus gets the status of an external approval.-func (c *Client) GetStatus(relayEndpoint string, uri string) (Status, error) {- req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/approvals/status", relayEndpoint), strings.NewReader(""))+// GetApproval gets the approval of an external approval.+func (c *Client) GetApproval(relayEndpoint string, id string) (*ResponsePayload, error) {+ req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/approval/%s", relayEndpoint, id), strings.NewReader(""))if err != nil {- return "", err+ return nil, err}- q := req.URL.Query()- q.Add("uri", uri)- req.URL.RawQuery = q.Encode()resp, err := c.client.Do(req)if err != nil {- return "", err+ return nil, err}if resp.StatusCode != http.StatusOK {- return "", errors.Errorf("failed to get external approval status with status %v", resp.Status)+ return nil, errors.Errorf("failed to get external approval status with status %v", resp.Status)}- responseBody := &GetStatus{}+ responseBody := &ResponsePayload{}err = json.NewDecoder(resp.Body).Decode(responseBody)if err != nil {- return "", err+ return nil, err}- return responseBody.Status, nil+ return responseBody, nil}diff --git a/backend/runner/relay/runner.go b/backend/runner/relay/runner.goindex 93aaca87e8..be290c9d24 100644--- a/backend/runner/relay/runner.go+++ b/backend/runner/relay/runner.go@@ -130,12 +130,12 @@ func (r *Runner) checkExternalApproval(ctx context.Context, approval *store.Exteif err != nil {return errors.Wrapf(err, "failed to get external approval node %s", payload.ExternalApprovalNodeID)}- uri := payload.URI- status, err := r.Client.GetStatus(node.Endpoint, uri)+ id := payload.ID+ resp, err := r.Client.GetApproval(node.Endpoint, id)if err != nil {- return errors.Wrapf(err, "failed to get external approval status, id: %v, endpoint: %s, uri: %s", node.Id, node.Endpoint, uri)+ return errors.Wrapf(err, "failed to get external approval status, id: %v, endpoint: %s, id: %s", node.Id, node.Endpoint, id)}- if status == relayplugin.StatusApproved {+ if resp.Status == relayplugin.StatusApproved {if err := r.approveExternalApprovalNode(ctx, approval.IssueUID); err != nil {return err}@@ -145,7 +145,7 @@ func (r *Runner) checkExternalApproval(ctx context.Context, approval *store.Exte}); err != nil {return err}- } else if status == relayplugin.StatusRejected {+ } else if resp.Status == relayplugin.StatusRejected {if err := r.rejectExternalApprovalNode(ctx, approval.IssueUID); err != nil {return err}@@ -207,8 +207,8 @@ func (r *Runner) cancelExternalApproval(ctx context.Context, issueUID int) {continue}go func() {- if err := r.Client.UpdateStatus(node.Endpoint, payload.URI); err != nil {- log.Error("failed to update external approval status", zap.String("endpoint", node.Endpoint), zap.String("uri", payload.URI), zap.Error(err))+ if err := r.Client.UpdateApproval(node.Endpoint, payload.ID, &relayplugin.UpdatePayload{}); err != nil {+ log.Error("failed to update external approval status", zap.String("endpoint", node.Endpoint), zap.String("id", payload.ID), zap.Error(err))}}()}diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex 235a6a91cb..3dc1575d1f 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -764,13 +764,21 @@ func handleApprovalNodeExternalNode(ctx context.Context, s *store.Store, relayClif node == nil {return errors.Errorf("external approval node %s not found", externalNodeID)}- uri, err := relayClient.Create(node.Endpoint, relay.CreatePayload{})+ id, err := relayClient.Create(node.Endpoint, &relay.CreatePayload{+ IssueID: fmt.Sprintf("%d", issue.UID),+ Title: issue.Title,+ Description: issue.Description,+ Project: issue.Project.ResourceID,+ CreateTime: issue.CreatedTime,+ Creator: issue.Creator.Email,+ Assignee: issue.Assignee.Email,+ })if err != nil {return errors.Wrapf(err, "failed to create external approval")}payload, err := json.Marshal(&api.ExternalApprovalPayloadRelay{ExternalApprovalNodeID: node.Id,- URI: uri,+ ID: id,})if err != nil {return errors.Wrapf(err, "failed to marshal external approval payload")