Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(test): add approve issue which approves tasks in the issue pipeline (#511)

Danny Xuf55dfe3d3d28
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 2e722a7dbb..3909ed253b 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -88,7 +88,7 @@ func TestSchemaUpdate(t *testing.T) {
if err != nil {
t.Fatal(fmt.Errorf("failed to construct issue create context payload, error: %w", err))
}
- _, err = ctl.createIssue(api.IssueCreate{
+ issue, err := ctl.createIssue(api.IssueCreate{
ProjectID: project.ID,
Name: fmt.Sprintf("create database %q", databaseName),
Type: api.IssueDatabaseCreate,
@@ -100,4 +100,7 @@ func TestSchemaUpdate(t *testing.T) {
if err != nil {
t.Fatal(fmt.Errorf("failed to create issue, error: %w", err))
}
+ if err := ctl.approveIssue(issue); err != nil {
+ t.Fatal(fmt.Errorf("failed to approve issue %v, error: %w", issue.ID, err))
+ }
}
diff --git a/tests/tests.go b/tests/tests.go
index 02521278cd..90ac282c67 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -177,6 +177,28 @@ func (ctl *controller) post(shortURL string, body io.Reader) (io.ReadCloser, err
return resp.Body, nil
}
+// patch sends a PATCH client request.
+func (ctl *controller) patch(shortURL string, body io.Reader) (io.ReadCloser, error) {
+ url := fmt.Sprintf("%s%s", rootURL, shortURL)
+ req, err := http.NewRequest("PATCH", url, body)
+ if err != nil {
+ return nil, fmt.Errorf("fail to create a new PATCH request(%q), error: %w", url, err)
+ }
+ req.Header.Set("Cookie", ctl.cookie)
+ resp, err := ctl.client.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("fail to send a PATCH request(%q), error: %w", url, err)
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read http response body, error: %w", err)
+ }
+ return nil, fmt.Errorf("http response error code %v body %q", resp.StatusCode, string(body))
+ }
+ return resp.Body, nil
+}
+
// getProjects gets the projects.
func (ctl *controller) getProjects() ([]*api.Project, error) {
body, err := ctl.get("/project", nil)
@@ -303,3 +325,42 @@ func (ctl *controller) createIssue(issueCreate api.IssueCreate) (*api.Issue, err
}
return issue, nil
}
+
+// patchTaskStatus patches the status of a task in the pipeline stage.
+func (ctl *controller) patchTaskStatus(taskStatusPatch api.TaskStatusPatch, pipelineID int) (*api.Task, error) {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, &taskStatusPatch); err != nil {
+ return nil, fmt.Errorf("failed to marshal patchTaskStatus, error: %w", err)
+ }
+
+ body, err := ctl.patch(fmt.Sprintf("/pipeline/%d/task/%d/status", pipelineID, taskStatusPatch.ID), buf)
+ if err != nil {
+ return nil, err
+ }
+
+ task := new(api.Task)
+ if err = jsonapi.UnmarshalPayload(body, task); err != nil {
+ return nil, fmt.Errorf("fail to unmarshal patchTaskStatus response, error: %w", err)
+ }
+ return task, nil
+}
+
+// approveIssue approves all tasks in the issues till the pipeline is done.
+func (ctl *controller) approveIssue(issue *api.Issue) error {
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ if task.Status == api.TaskPendingApproval {
+ if _, err := ctl.patchTaskStatus(
+ api.TaskStatusPatch{
+ ID: task.ID,
+ Status: api.TaskPending,
+ },
+ issue.Pipeline.ID); err != nil {
+
+ return fmt.Errorf("failed to patch task status for task %d, error: %w", task.ID, err)
+ }
+ }
+ }
+ }
+ return nil
+}