From 98a99925652eaf003583442401c21cdefdce968c Mon Sep 17 00:00:00 2001 From: Pratik Gaikwad Date: Sat, 26 Aug 2023 09:17:14 -0700 Subject: [PATCH 1/3] feat: add support for merge results/trains --- gitlab.go | 2 + merge_trains.go | 194 ++++++++++++ merge_trains_test.go | 295 ++++++++++++++++++ .../add_merge_request_in_merge_train.json | 42 +++ .../get_merge_request_in_merge_train.json | 40 +++ .../list_merge_requests_in_merge_train.json | 42 +++ testdata/list_merge_trains_in_project.json | 39 +++ 7 files changed, 654 insertions(+) create mode 100644 merge_trains.go create mode 100644 merge_trains_test.go create mode 100644 testdata/add_merge_request_in_merge_train.json create mode 100644 testdata/get_merge_request_in_merge_train.json create mode 100644 testdata/list_merge_requests_in_merge_train.json create mode 100644 testdata/list_merge_trains_in_project.json diff --git a/gitlab.go b/gitlab.go index 322f1a4a0..8fc6cc324 100644 --- a/gitlab.go +++ b/gitlab.go @@ -164,6 +164,7 @@ type Client struct { Markdown *MarkdownService MergeRequestApprovals *MergeRequestApprovalsService MergeRequests *MergeRequestsService + MergeTrains *MergeTrainsService Metadata *MetadataService Milestones *MilestonesService Namespaces *NamespacesService @@ -385,6 +386,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) { c.Markdown = &MarkdownService{client: c} c.MergeRequestApprovals = &MergeRequestApprovalsService{client: c} c.MergeRequests = &MergeRequestsService{client: c, timeStats: timeStats} + c.MergeTrains = &MergeTrainsService{client: c} c.Metadata = &MetadataService{client: c} c.Milestones = &MilestonesService{client: c} c.Namespaces = &NamespacesService{client: c} diff --git a/merge_trains.go b/merge_trains.go new file mode 100644 index 000000000..837668aec --- /dev/null +++ b/merge_trains.go @@ -0,0 +1,194 @@ +package gitlab + +import ( + "fmt" + "net/http" + "time" +) + +// MergeTrainsService handles communication with the merge trains related +// methods of the GitLab API. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html +type MergeTrainsService struct { + client *Client +} + +// MergeTrain represents a Gitlab merge train. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html +type MergeTrain struct { + ID int `json:"id"` + MergeRequest *MergeTrainMergeRequest `json:"merge_request"` + User *MergeTrainUser `json:"user"` + Pipeline *MergeTrainPipeline `json:"pipeline"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + TargetBranch string `json:"target_branch"` + Status string `json:"status"` + MergedAt *time.Time `json:"merged_at"` + Duration int `json:"duration"` +} + + +//MergeTrainMergeRequest represents a Gitlab merge request inside merge train +type MergeTrainMergeRequest struct { + ID int `json:"id"` + IID int `json:"iid"` + ProjectID int `json:"project_id"` + Title string `json:"title"` + Description string `json:"description"` + State string `json:"state"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + WebURL string `json:"web_url"` +} + +//MergeTrainUser represents a Gitlab user inside merge train +type MergeTrainUser struct { + ID int `json:"id"` + Name string `json:"name"` + Username string `json:"username"` + State string `json:"state"` + AvatarURL string `json:"avatar_url"` + WebURL string `json:"web_url"` +} + +//MergeTrainPipeline represents a Gitlab pipeline inside merge train +type MergeTrainPipeline struct { + ID int `json:"id"` + IID int `json:"iid"` + ProjectID int `json:"project_id"` + SHA string `json:"sha"` + Ref string `json:"ref"` + Status string `json:"status"` + Source string `json:"source"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + WebURL string `json:"web_url"` +} + +//ListMergeTrainsOptions represents the available ListMergeTrain() options. +// +// Gitab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project +type ListMergeTrainsOptions struct { + ListOptions + Scope *string `url:"scope,omitempty" json:"scope,omitempty"` + Sort *string `url:"sort,omitempty" json:"sort,omitempty"` +} + +// ListProjectMergeTrains get a list of merge trains in a project +// +// The scope of trains to show: active (to be merged) and complete (have been merged) +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project +func (s *MergeTrainsService) ListProjectMergeTrains(pid interface{}, opt *ListMergeTrainsOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + + u := fmt.Sprintf("projects/%s/merge_trains", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodGet, u, opt, options) + if err != nil { + return nil, nil, err + } + + var mergeTrains []*MergeTrain + resp, err := s.client.Do(req, &mergeTrains) + if err != nil { + return nil, resp, err + } + + return mergeTrains, resp, nil +} + +// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge train +// for the requested target branch +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-requests-in-a-merge-train +func (s *MergeTrainsService) ListMergeRequestInMergeTrain(pid interface{}, targetBranch string, opts *ListMergeTrainsOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + + u := fmt.Sprintf("projects/%s/merge_trains/%s", PathEscape(project), targetBranch) + + req, err := s.client.NewRequest(http.MethodGet, u, opts, options) + if err != nil { + return nil, nil, err + } + + var mergeTrains []*MergeTrain + resp, err := s.client.Do(req, &mergeTrains) + if err != nil { + return nil, resp, err + } + + return mergeTrains, resp, nil +} + +// GetMergeRequestOnAMergeTrain Get merge train information for the requested merge request. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html#get-the-status-of-a-merge-request-on-a-merge-train +func (s *MergeTrainsService) GetMergeRequestOnAMergeTrain(pid interface{}, mergeRequest int, options ...RequestOptionFunc) (*MergeTrain, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + + u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest) + + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) + if err != nil { + return nil, nil, err + } + + var mergeTrain *MergeTrain + resp, err := s.client.Do(req, &mergeTrain) + if err != nil { + return nil, resp, err + } + + return mergeTrain, resp, nil +} + +// AddMergeRequestToMergeTrainOptions represents the available AddMergeRequestToMergeTrain() options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train +type AddMergeRequestToMergeTrainOptions struct { + WhenPipelineSucceeds *bool `json:"when_pipeline_succeeds,omitempty"` + SHA *string `json:"sha,omitempty"` + Squash *string `json:"squash,omitempty"` +} + +// AddMergeRequestToMergeTrain Add a merge request to the merge train targeting the merge request’s target branch +// +// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train +func (s *MergeTrainsService) AddMergeRequestToMergeTrain(pid interface{}, mergeRequest int, opts *AddMergeRequestToMergeTrainOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + + u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest) + + req, err := s.client.NewRequest(http.MethodPost, u, opts, options) + if err != nil { + return nil, nil, err + } + + var mergeTrains []*MergeTrain + resp, err := s.client.Do(req, &mergeTrains) + if err != nil { + return nil, resp, err + } + + return mergeTrains, resp, nil +} \ No newline at end of file diff --git a/merge_trains_test.go b/merge_trains_test.go new file mode 100644 index 000000000..76e6e3e55 --- /dev/null +++ b/merge_trains_test.go @@ -0,0 +1,295 @@ +package gitlab + +import ( + "net/http" + "reflect" + "testing" + "time" +) + +func TestListProjectMergeTrains(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/merge_trains", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + mustWriteHTTPResponse(t, w, "testdata/list_merge_trains_in_project.json") + }) + + opts := &ListMergeTrainsOptions{} + + mergeTrains, _, err := client.MergeTrains.ListProjectMergeTrains(1, opts) + + if err != nil { + t.Errorf("MergeTrains.ListProjectMergeTrains returned error: %v", err) + } + + mergeRequestCreatedAt := time.Date(2020, 2, 6, 8, 39, 14, 883000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2020, 02, 6,8,40,57,38000000, time.UTC) + + pipelineCreatedAt := time.Date(2020, 2,6,8,40,42,410000000, time.UTC) + pipelineUpdatedAt:= time.Date(2020,2,6,8,40,46,912000000, time.UTC) + + mergeTrainCreatedAt := time.Date(2020,2,6,8,39,47,217000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2020,2,6,8,40,57,720000000, time.UTC) + mergeTrainMergedAt := time.Date(2020,2,6,8,40,57,719000000, time.UTC) + + want := []*MergeTrain{ + { + ID: 110, + MergeRequest: &MergeTrainMergeRequest{ + ID: 126, + IID: 59, + ProjectID: 20, + Title: "Test MR 1580978354", + Description: "", + State: "merged", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt:&mergeRequestUpdatedAt, + WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59", + }, + User: &MergeTrainUser{ + ID: 1, + Name: "Administrator", + Username: "root", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + WebURL: "http://local.gitlab.test:8181/root", + }, + Pipeline: &MergeTrainPipeline{ + ID: 246, + SHA: "bcc17a8ffd51be1afe45605e714085df28b80b13", + Ref: "refs/merge-requests/59/train", + Status: "success", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/pipelines/246", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt: &mergeTrainUpdatedAt, + TargetBranch: "feature-1580973432", + Status: "merged", + MergedAt: &mergeTrainMergedAt, + Duration: 70, + }, + } + + if !reflect.DeepEqual(want, mergeTrains) { + t.Errorf("MergeTrains.ListProjectMergeTrains returned %+v, want %+v", mergeTrains, want) + } +} + +func TestListMergeRequestInMergeTrain(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/597/merge_trains/main", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + mustWriteHTTPResponse(t, w, "testdata/list_merge_requests_in_merge_train.json") + }) + + opts := &ListMergeTrainsOptions{} + + mergeTrains, _, err := client.MergeTrains.ListMergeRequestInMergeTrain(597, "main", opts) + + if err != nil { + t.Errorf("MergeTrains.ListMergeRequestInMergeTrain returned error: %v", err) + } + + mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + + pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + + mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + + want := []*MergeTrain{ + { + ID: 267, + MergeRequest: &MergeTrainMergeRequest{ + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", + Description: "", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + }, + User: &MergeTrainUser{ + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + WebURL: "http://localhost/user12", + }, + Pipeline: &MergeTrainPipeline{ + ID: 273, + IID: 1, + ProjectID: 598, + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt:&mergeTrainUpdatedAt, + TargetBranch:"main", + Status:"idle", + MergedAt:nil, + Duration:0, + }, + } + + if !reflect.DeepEqual(want, mergeTrains) { + t.Errorf("MergeTrains.ListMergeRequestInMergeTrain returned %+v, want %+v", mergeTrains, want) + } +} + +func TestGetMergeRequestOnAMergeTrain(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/597/merge_trains/merge_requests/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + mustWriteHTTPResponse(t, w, "testdata/get_merge_request_in_merge_train.json") + }) + + mergeTrain, _, err := client.MergeTrains.GetMergeRequestOnAMergeTrain(597, 1) + + if err != nil { + t.Errorf("MergeTrains.GetMergeRequestOnAMergeTrain returned error: %v", err) + } + + mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + + pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + + mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + + want := &MergeTrain{ + ID: 267, + MergeRequest: &MergeTrainMergeRequest{ + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", + Description: "", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + }, + User: &MergeTrainUser{ + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + WebURL: "http://localhost/user12", + }, + Pipeline: &MergeTrainPipeline{ + ID: 273, + IID: 1, + ProjectID: 598, + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt:&mergeTrainUpdatedAt, + TargetBranch:"main", + Status:"idle", + MergedAt:nil, + Duration:0, + } + + if !reflect.DeepEqual(want, mergeTrain) { + t.Errorf("MergeTrains.GetMergeRequestOnAMergeTrain returned %+v, want %+v", mergeTrain, want) + } +} + +func TestAddMergeRequestToMergeTrain(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/597/merge_trains/merge_requests/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPost) + mustWriteHTTPResponse(t, w, "testdata/add_merge_request_in_merge_train.json") + }) + + opt := &AddMergeRequestToMergeTrainOptions{WhenPipelineSucceeds: Bool(true)} + + mergeTrains, _, err := client.MergeTrains.AddMergeRequestToMergeTrain(597, 1, opt) + + if err != nil { + t.Errorf("MergeTrains.AddMergeRequestToMergeTrain returned error: %v", err) + } + + mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + + pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + + mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + + + want := []*MergeTrain{ + { + ID: 267, + MergeRequest: &MergeTrainMergeRequest{ + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", + Description: "", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + }, + User: &MergeTrainUser{ + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + WebURL: "http://localhost/user12", + }, + Pipeline: &MergeTrainPipeline{ + ID: 273, + IID: 1, + ProjectID: 598, + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt:&mergeTrainUpdatedAt, + TargetBranch:"main", + Status:"idle", + MergedAt:nil, + Duration:0, + }, + } + + if !reflect.DeepEqual(want, mergeTrains) { + t.Errorf("MergeTrains.AddMergeRequestToMergeTrain returned %+v, want %+v", mergeTrains, want) + } +} \ No newline at end of file diff --git a/testdata/add_merge_request_in_merge_train.json b/testdata/add_merge_request_in_merge_train.json new file mode 100644 index 000000000..5e19bcf0e --- /dev/null +++ b/testdata/add_merge_request_in_merge_train.json @@ -0,0 +1,42 @@ +[ + { + "id": 267, + "merge_request": { + "id": 273, + "iid": 1, + "project_id": 597, + "title": "My title 9", + "description": null, + "state": "opened", + "created_at": "2022-10-31T19:06:05.725Z", + "updated_at": "2022-10-31T19:06:05.725Z", + "web_url": "http://localhost/namespace18/project21/-/merge_requests/1" + }, + "user": { + "id": 933, + "username": "user12", + "name": "Sidney Jones31", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + "web_url": "http://localhost/user12" + }, + "pipeline": { + "id": 273, + "iid": 1, + "project_id": 598, + "sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0", + "ref": "main", + "status": "pending", + "source": "push", + "created_at": "2022-10-31T19:06:06.231Z", + "updated_at": "2022-10-31T19:06:06.231Z", + "web_url": "http://localhost/namespace19/project22/-/pipelines/273" + }, + "created_at": "2022-10-31T19:06:06.237Z", + "updated_at":"2022-10-31T19:06:06.237Z", + "target_branch":"main", + "status":"idle", + "merged_at":null, + "duration":null + } +] \ No newline at end of file diff --git a/testdata/get_merge_request_in_merge_train.json b/testdata/get_merge_request_in_merge_train.json new file mode 100644 index 000000000..fd3d6ee0e --- /dev/null +++ b/testdata/get_merge_request_in_merge_train.json @@ -0,0 +1,40 @@ +{ + "id": 267, + "merge_request": { + "id": 273, + "iid": 1, + "project_id": 597, + "title": "My title 9", + "description": null, + "state": "opened", + "created_at": "2022-10-31T19:06:05.725Z", + "updated_at": "2022-10-31T19:06:05.725Z", + "web_url": "http://localhost/namespace18/project21/-/merge_requests/1" + }, + "user": { + "id": 933, + "username": "user12", + "name": "Sidney Jones31", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + "web_url": "http://localhost/user12" + }, + "pipeline": { + "id": 273, + "iid": 1, + "project_id": 598, + "sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0", + "ref": "main", + "status": "pending", + "source": "push", + "created_at": "2022-10-31T19:06:06.231Z", + "updated_at": "2022-10-31T19:06:06.231Z", + "web_url": "http://localhost/namespace19/project22/-/pipelines/273" + }, + "created_at": "2022-10-31T19:06:06.237Z", + "updated_at":"2022-10-31T19:06:06.237Z", + "target_branch":"main", + "status":"idle", + "merged_at":null, + "duration":null +} \ No newline at end of file diff --git a/testdata/list_merge_requests_in_merge_train.json b/testdata/list_merge_requests_in_merge_train.json new file mode 100644 index 000000000..5e19bcf0e --- /dev/null +++ b/testdata/list_merge_requests_in_merge_train.json @@ -0,0 +1,42 @@ +[ + { + "id": 267, + "merge_request": { + "id": 273, + "iid": 1, + "project_id": 597, + "title": "My title 9", + "description": null, + "state": "opened", + "created_at": "2022-10-31T19:06:05.725Z", + "updated_at": "2022-10-31T19:06:05.725Z", + "web_url": "http://localhost/namespace18/project21/-/merge_requests/1" + }, + "user": { + "id": 933, + "username": "user12", + "name": "Sidney Jones31", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + "web_url": "http://localhost/user12" + }, + "pipeline": { + "id": 273, + "iid": 1, + "project_id": 598, + "sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0", + "ref": "main", + "status": "pending", + "source": "push", + "created_at": "2022-10-31T19:06:06.231Z", + "updated_at": "2022-10-31T19:06:06.231Z", + "web_url": "http://localhost/namespace19/project22/-/pipelines/273" + }, + "created_at": "2022-10-31T19:06:06.237Z", + "updated_at":"2022-10-31T19:06:06.237Z", + "target_branch":"main", + "status":"idle", + "merged_at":null, + "duration":null + } +] \ No newline at end of file diff --git a/testdata/list_merge_trains_in_project.json b/testdata/list_merge_trains_in_project.json new file mode 100644 index 000000000..08387fb41 --- /dev/null +++ b/testdata/list_merge_trains_in_project.json @@ -0,0 +1,39 @@ +[ + { + "id": 110, + "merge_request": { + "id": 126, + "iid": 59, + "project_id": 20, + "title": "Test MR 1580978354", + "description": "", + "state": "merged", + "created_at": "2020-02-06T08:39:14.883Z", + "updated_at": "2020-02-06T08:40:57.038Z", + "web_url": "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59" + }, + "user": { + "id": 1, + "name": "Administrator", + "username": "root", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + "web_url": "http://local.gitlab.test:8181/root" + }, + "pipeline": { + "id": 246, + "sha": "bcc17a8ffd51be1afe45605e714085df28b80b13", + "ref": "refs/merge-requests/59/train", + "status": "success", + "created_at": "2020-02-06T08:40:42.410Z", + "updated_at": "2020-02-06T08:40:46.912Z", + "web_url": "http://local.gitlab.test:8181/root/merge-train-race-condition/pipelines/246" + }, + "created_at": "2020-02-06T08:39:47.217Z", + "updated_at": "2020-02-06T08:40:57.720Z", + "target_branch": "feature-1580973432", + "status": "merged", + "merged_at": "2020-02-06T08:40:57.719Z", + "duration": 70 + } +] \ No newline at end of file From 4a23587f2c4d718b8bd110229def6149005c4b82 Mon Sep 17 00:00:00 2001 From: Pratik Gaikwad Date: Sat, 2 Sep 2023 07:06:28 +0000 Subject: [PATCH 2/3] fix: formatting and linting --- gitlab.go | 2 +- merge_trains.go | 95 +++++++------- merge_trains_test.go | 305 +++++++++++++++++++++---------------------- 3 files changed, 198 insertions(+), 204 deletions(-) diff --git a/gitlab.go b/gitlab.go index 8fc6cc324..10e5426b4 100644 --- a/gitlab.go +++ b/gitlab.go @@ -164,7 +164,7 @@ type Client struct { Markdown *MarkdownService MergeRequestApprovals *MergeRequestApprovalsService MergeRequests *MergeRequestsService - MergeTrains *MergeTrainsService + MergeTrains *MergeTrainsService Metadata *MetadataService Milestones *MilestonesService Namespaces *NamespacesService diff --git a/merge_trains.go b/merge_trains.go index 837668aec..e0abd3faa 100644 --- a/merge_trains.go +++ b/merge_trains.go @@ -11,76 +11,75 @@ import ( // // GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html type MergeTrainsService struct { - client *Client + client *Client } // MergeTrain represents a Gitlab merge train. // // GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html type MergeTrain struct { - ID int `json:"id"` + ID int `json:"id"` MergeRequest *MergeTrainMergeRequest `json:"merge_request"` - User *MergeTrainUser `json:"user"` - Pipeline *MergeTrainPipeline `json:"pipeline"` - CreatedAt *time.Time `json:"created_at"` - UpdatedAt *time.Time `json:"updated_at"` - TargetBranch string `json:"target_branch"` - Status string `json:"status"` - MergedAt *time.Time `json:"merged_at"` - Duration int `json:"duration"` + User *MergeTrainUser `json:"user"` + Pipeline *MergeTrainPipeline `json:"pipeline"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + TargetBranch string `json:"target_branch"` + Status string `json:"status"` + MergedAt *time.Time `json:"merged_at"` + Duration int `json:"duration"` } - -//MergeTrainMergeRequest represents a Gitlab merge request inside merge train +// MergeTrainMergeRequest represents a Gitlab merge request inside merge train type MergeTrainMergeRequest struct { - ID int `json:"id"` - IID int `json:"iid"` - ProjectID int `json:"project_id"` - Title string `json:"title"` - Description string `json:"description"` - State string `json:"state"` - CreatedAt *time.Time `json:"created_at"` - UpdatedAt *time.Time `json:"updated_at"` - WebURL string `json:"web_url"` + ID int `json:"id"` + IID int `json:"iid"` + ProjectID int `json:"project_id"` + Title string `json:"title"` + Description string `json:"description"` + State string `json:"state"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + WebURL string `json:"web_url"` } -//MergeTrainUser represents a Gitlab user inside merge train +// MergeTrainUser represents a Gitlab user inside merge train type MergeTrainUser struct { - ID int `json:"id"` - Name string `json:"name"` - Username string `json:"username"` - State string `json:"state"` + ID int `json:"id"` + Name string `json:"name"` + Username string `json:"username"` + State string `json:"state"` AvatarURL string `json:"avatar_url"` - WebURL string `json:"web_url"` + WebURL string `json:"web_url"` } -//MergeTrainPipeline represents a Gitlab pipeline inside merge train +// MergeTrainPipeline represents a Gitlab pipeline inside merge train type MergeTrainPipeline struct { - ID int `json:"id"` - IID int `json:"iid"` - ProjectID int `json:"project_id"` - SHA string `json:"sha"` - Ref string `json:"ref"` - Status string `json:"status"` - Source string `json:"source"` + ID int `json:"id"` + IID int `json:"iid"` + ProjectID int `json:"project_id"` + SHA string `json:"sha"` + Ref string `json:"ref"` + Status string `json:"status"` + Source string `json:"source"` CreatedAt *time.Time `json:"created_at"` UpdatedAt *time.Time `json:"updated_at"` - WebURL string `json:"web_url"` + WebURL string `json:"web_url"` } -//ListMergeTrainsOptions represents the available ListMergeTrain() options. +// ListMergeTrainsOptions represents the available ListMergeTrain() options. // -// Gitab API docs: +// Gitab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project type ListMergeTrainsOptions struct { ListOptions - Scope *string `url:"scope,omitempty" json:"scope,omitempty"` - Sort *string `url:"sort,omitempty" json:"sort,omitempty"` + Scope *string `url:"scope,omitempty" json:"scope,omitempty"` + Sort *string `url:"sort,omitempty" json:"sort,omitempty"` } // ListProjectMergeTrains get a list of merge trains in a project // -// The scope of trains to show: active (to be merged) and complete (have been merged) +// The scope of trains to show: active (to be merged) and complete (have been merged) // // GitLab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project @@ -106,7 +105,7 @@ func (s *MergeTrainsService) ListProjectMergeTrains(pid interface{}, opt *ListMe return mergeTrains, resp, nil } -// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge train +// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge train // for the requested target branch // // GitLab API docs: @@ -160,13 +159,13 @@ func (s *MergeTrainsService) GetMergeRequestOnAMergeTrain(pid interface{}, merge // AddMergeRequestToMergeTrainOptions represents the available AddMergeRequestToMergeTrain() options. // -// GitLab API docs: +// GitLab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train type AddMergeRequestToMergeTrainOptions struct { - WhenPipelineSucceeds *bool `json:"when_pipeline_succeeds,omitempty"` - SHA *string `json:"sha,omitempty"` - Squash *string `json:"squash,omitempty"` -} + WhenPipelineSucceeds *bool `json:"when_pipeline_succeeds,omitempty"` + SHA *string `json:"sha,omitempty"` + Squash *string `json:"squash,omitempty"` +} // AddMergeRequestToMergeTrain Add a merge request to the merge train targeting the merge request’s target branch // @@ -191,4 +190,4 @@ func (s *MergeTrainsService) AddMergeRequestToMergeTrain(pid interface{}, mergeR } return mergeTrains, resp, nil -} \ No newline at end of file +} diff --git a/merge_trains_test.go b/merge_trains_test.go index 76e6e3e55..b7f84e170 100644 --- a/merge_trains_test.go +++ b/merge_trains_test.go @@ -18,58 +18,57 @@ func TestListProjectMergeTrains(t *testing.T) { opts := &ListMergeTrainsOptions{} mergeTrains, _, err := client.MergeTrains.ListProjectMergeTrains(1, opts) - if err != nil { t.Errorf("MergeTrains.ListProjectMergeTrains returned error: %v", err) } mergeRequestCreatedAt := time.Date(2020, 2, 6, 8, 39, 14, 883000000, time.UTC) - mergeRequestUpdatedAt := time.Date(2020, 02, 6,8,40,57,38000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2020, 0o2, 6, 8, 40, 57, 38000000, time.UTC) - pipelineCreatedAt := time.Date(2020, 2,6,8,40,42,410000000, time.UTC) - pipelineUpdatedAt:= time.Date(2020,2,6,8,40,46,912000000, time.UTC) + pipelineCreatedAt := time.Date(2020, 2, 6, 8, 40, 42, 410000000, time.UTC) + pipelineUpdatedAt := time.Date(2020, 2, 6, 8, 40, 46, 912000000, time.UTC) - mergeTrainCreatedAt := time.Date(2020,2,6,8,39,47,217000000, time.UTC) - mergeTrainUpdatedAt := time.Date(2020,2,6,8,40,57,720000000, time.UTC) - mergeTrainMergedAt := time.Date(2020,2,6,8,40,57,719000000, time.UTC) + mergeTrainCreatedAt := time.Date(2020, 2, 6, 8, 39, 47, 217000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2020, 2, 6, 8, 40, 57, 720000000, time.UTC) + mergeTrainMergedAt := time.Date(2020, 2, 6, 8, 40, 57, 719000000, time.UTC) want := []*MergeTrain{ { ID: 110, - MergeRequest: &MergeTrainMergeRequest{ - ID: 126, - IID: 59, - ProjectID: 20, - Title: "Test MR 1580978354", - Description: "", - State: "merged", - CreatedAt: &mergeRequestCreatedAt, - UpdatedAt:&mergeRequestUpdatedAt, - WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59", - }, - User: &MergeTrainUser{ - ID: 1, - Name: "Administrator", - Username: "root", - State: "active", - AvatarURL: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", - WebURL: "http://local.gitlab.test:8181/root", - }, - Pipeline: &MergeTrainPipeline{ - ID: 246, - SHA: "bcc17a8ffd51be1afe45605e714085df28b80b13", - Ref: "refs/merge-requests/59/train", - Status: "success", - CreatedAt: &pipelineCreatedAt, - UpdatedAt: &pipelineUpdatedAt, - WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/pipelines/246", - }, - CreatedAt: &mergeTrainCreatedAt, - UpdatedAt: &mergeTrainUpdatedAt, - TargetBranch: "feature-1580973432", - Status: "merged", - MergedAt: &mergeTrainMergedAt, - Duration: 70, + MergeRequest: &MergeTrainMergeRequest{ + ID: 126, + IID: 59, + ProjectID: 20, + Title: "Test MR 1580978354", + Description: "", + State: "merged", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59", + }, + User: &MergeTrainUser{ + ID: 1, + Name: "Administrator", + Username: "root", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + WebURL: "http://local.gitlab.test:8181/root", + }, + Pipeline: &MergeTrainPipeline{ + ID: 246, + SHA: "bcc17a8ffd51be1afe45605e714085df28b80b13", + Ref: "refs/merge-requests/59/train", + Status: "success", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/pipelines/246", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt: &mergeTrainUpdatedAt, + TargetBranch: "feature-1580973432", + Status: "merged", + MergedAt: &mergeTrainMergedAt, + Duration: 70, }, } @@ -89,61 +88,60 @@ func TestListMergeRequestInMergeTrain(t *testing.T) { opts := &ListMergeTrainsOptions{} mergeTrains, _, err := client.MergeTrains.ListMergeRequestInMergeTrain(597, "main", opts) - if err != nil { t.Errorf("MergeTrains.ListMergeRequestInMergeTrain returned error: %v", err) } - mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) - mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + mergeRequestCreatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) - pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) - pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + pipelineCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) - mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) - mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + mergeTrainCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) want := []*MergeTrain{ { - ID: 267, - MergeRequest: &MergeTrainMergeRequest{ - ID: 273, - IID: 1, - ProjectID: 597, - Title: "My title 9", - Description: "", - State: "opened", - CreatedAt: &mergeRequestCreatedAt, - UpdatedAt: &mergeRequestUpdatedAt, - WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", - }, - User: &MergeTrainUser{ - ID: 933, - Username: "user12", - Name: "Sidney Jones31", - State: "active", - AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", - WebURL: "http://localhost/user12", - }, - Pipeline: &MergeTrainPipeline{ - ID: 273, - IID: 1, - ProjectID: 598, - SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", - Ref: "main", - Status: "pending", - Source: "push", - CreatedAt: &pipelineCreatedAt, - UpdatedAt: &pipelineUpdatedAt, - WebURL: "http://localhost/namespace19/project22/-/pipelines/273", - }, - CreatedAt: &mergeTrainCreatedAt, - UpdatedAt:&mergeTrainUpdatedAt, - TargetBranch:"main", - Status:"idle", - MergedAt:nil, - Duration:0, - }, + ID: 267, + MergeRequest: &MergeTrainMergeRequest{ + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", + Description: "", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + }, + User: &MergeTrainUser{ + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", + AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", + WebURL: "http://localhost/user12", + }, + Pipeline: &MergeTrainPipeline{ + ID: 273, + IID: 1, + ProjectID: 598, + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", + CreatedAt: &pipelineCreatedAt, + UpdatedAt: &pipelineUpdatedAt, + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + }, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt: &mergeTrainUpdatedAt, + TargetBranch: "main", + Status: "idle", + MergedAt: nil, + Duration: 0, + }, } if !reflect.DeepEqual(want, mergeTrains) { @@ -160,59 +158,58 @@ func TestGetMergeRequestOnAMergeTrain(t *testing.T) { }) mergeTrain, _, err := client.MergeTrains.GetMergeRequestOnAMergeTrain(597, 1) - if err != nil { t.Errorf("MergeTrains.GetMergeRequestOnAMergeTrain returned error: %v", err) } - mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) - mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) + mergeRequestCreatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) - pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) - pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + pipelineCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) - mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) - mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + mergeTrainCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) want := &MergeTrain{ ID: 267, MergeRequest: &MergeTrainMergeRequest{ - ID: 273, - IID: 1, - ProjectID: 597, - Title: "My title 9", + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", Description: "", - State: "opened", - CreatedAt: &mergeRequestCreatedAt, - UpdatedAt: &mergeRequestUpdatedAt, - WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", }, User: &MergeTrainUser{ - ID: 933, - Username: "user12", - Name: "Sidney Jones31", - State: "active", + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", - WebURL: "http://localhost/user12", + WebURL: "http://localhost/user12", }, Pipeline: &MergeTrainPipeline{ - ID: 273, - IID: 1, + ID: 273, + IID: 1, ProjectID: 598, - SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", - Ref: "main", - Status: "pending", - Source: "push", + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", CreatedAt: &pipelineCreatedAt, UpdatedAt: &pipelineUpdatedAt, - WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", }, - CreatedAt: &mergeTrainCreatedAt, - UpdatedAt:&mergeTrainUpdatedAt, - TargetBranch:"main", - Status:"idle", - MergedAt:nil, - Duration:0, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt: &mergeTrainUpdatedAt, + TargetBranch: "main", + Status: "idle", + MergedAt: nil, + Duration: 0, } if !reflect.DeepEqual(want, mergeTrain) { @@ -231,65 +228,63 @@ func TestAddMergeRequestToMergeTrain(t *testing.T) { opt := &AddMergeRequestToMergeTrainOptions{WhenPipelineSucceeds: Bool(true)} mergeTrains, _, err := client.MergeTrains.AddMergeRequestToMergeTrain(597, 1, opt) - if err != nil { t.Errorf("MergeTrains.AddMergeRequestToMergeTrain returned error: %v", err) } - mergeRequestCreatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) - mergeRequestUpdatedAt := time.Date(2022,10,31,19,6,5,725000000, time.UTC) - - pipelineCreatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) - pipelineUpdatedAt := time.Date(2022,10,31,19,06,06,231000000, time.UTC) + mergeRequestCreatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) + mergeRequestUpdatedAt := time.Date(2022, 10, 31, 19, 6, 5, 725000000, time.UTC) - mergeTrainCreatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) - mergeTrainUpdatedAt := time.Date(2022,10,31,19,06,06,237000000, time.UTC) + pipelineCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) + pipelineUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 231000000, time.UTC) + mergeTrainCreatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) + mergeTrainUpdatedAt := time.Date(2022, 10, 31, 19, 0o6, 0o6, 237000000, time.UTC) want := []*MergeTrain{ { ID: 267, MergeRequest: &MergeTrainMergeRequest{ - ID: 273, - IID: 1, - ProjectID: 597, - Title: "My title 9", + ID: 273, + IID: 1, + ProjectID: 597, + Title: "My title 9", Description: "", - State: "opened", - CreatedAt: &mergeRequestCreatedAt, - UpdatedAt: &mergeRequestUpdatedAt, - WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", + State: "opened", + CreatedAt: &mergeRequestCreatedAt, + UpdatedAt: &mergeRequestUpdatedAt, + WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", }, User: &MergeTrainUser{ - ID: 933, - Username: "user12", - Name: "Sidney Jones31", - State: "active", + ID: 933, + Username: "user12", + Name: "Sidney Jones31", + State: "active", AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", - WebURL: "http://localhost/user12", + WebURL: "http://localhost/user12", }, Pipeline: &MergeTrainPipeline{ - ID: 273, - IID: 1, + ID: 273, + IID: 1, ProjectID: 598, - SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", - Ref: "main", - Status: "pending", - Source: "push", + SHA: "b83d6e391c22777fca1ed3012fce84f633d7fed0", + Ref: "main", + Status: "pending", + Source: "push", CreatedAt: &pipelineCreatedAt, UpdatedAt: &pipelineUpdatedAt, - WebURL: "http://localhost/namespace19/project22/-/pipelines/273", + WebURL: "http://localhost/namespace19/project22/-/pipelines/273", }, - CreatedAt: &mergeTrainCreatedAt, - UpdatedAt:&mergeTrainUpdatedAt, - TargetBranch:"main", - Status:"idle", - MergedAt:nil, - Duration:0, + CreatedAt: &mergeTrainCreatedAt, + UpdatedAt: &mergeTrainUpdatedAt, + TargetBranch: "main", + Status: "idle", + MergedAt: nil, + Duration: 0, }, } if !reflect.DeepEqual(want, mergeTrains) { t.Errorf("MergeTrains.AddMergeRequestToMergeTrain returned %+v, want %+v", mergeTrains, want) } -} \ No newline at end of file +} From c9b2e7791fbe9bee5345f649469da7e593055296 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Sat, 2 Sep 2023 13:41:47 +0200 Subject: [PATCH 3/3] Few minor adjustments to finish the PR --- merge_trains.go | 89 +++++++------------ merge_trains_test.go | 16 ++-- .../add_merge_request_in_merge_train.json | 12 +-- .../get_merge_request_in_merge_train.json | 12 +-- .../list_merge_requests_in_merge_train.json | 12 +-- testdata/list_merge_trains_in_project.json | 2 +- 6 files changed, 60 insertions(+), 83 deletions(-) diff --git a/merge_trains.go b/merge_trains.go index e0abd3faa..cbfd99703 100644 --- a/merge_trains.go +++ b/merge_trains.go @@ -20,8 +20,8 @@ type MergeTrainsService struct { type MergeTrain struct { ID int `json:"id"` MergeRequest *MergeTrainMergeRequest `json:"merge_request"` - User *MergeTrainUser `json:"user"` - Pipeline *MergeTrainPipeline `json:"pipeline"` + User *BasicUser `json:"user"` + Pipeline *Pipeline `json:"pipeline"` CreatedAt *time.Time `json:"created_at"` UpdatedAt *time.Time `json:"updated_at"` TargetBranch string `json:"target_branch"` @@ -30,7 +30,9 @@ type MergeTrain struct { Duration int `json:"duration"` } -// MergeTrainMergeRequest represents a Gitlab merge request inside merge train +// MergeTrainMergeRequest represents a Gitlab merge request inside merge train. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html type MergeTrainMergeRequest struct { ID int `json:"id"` IID int `json:"iid"` @@ -43,30 +45,6 @@ type MergeTrainMergeRequest struct { WebURL string `json:"web_url"` } -// MergeTrainUser represents a Gitlab user inside merge train -type MergeTrainUser struct { - ID int `json:"id"` - Name string `json:"name"` - Username string `json:"username"` - State string `json:"state"` - AvatarURL string `json:"avatar_url"` - WebURL string `json:"web_url"` -} - -// MergeTrainPipeline represents a Gitlab pipeline inside merge train -type MergeTrainPipeline struct { - ID int `json:"id"` - IID int `json:"iid"` - ProjectID int `json:"project_id"` - SHA string `json:"sha"` - Ref string `json:"ref"` - Status string `json:"status"` - Source string `json:"source"` - CreatedAt *time.Time `json:"created_at"` - UpdatedAt *time.Time `json:"updated_at"` - WebURL string `json:"web_url"` -} - // ListMergeTrainsOptions represents the available ListMergeTrain() options. // // Gitab API docs: @@ -77,9 +55,7 @@ type ListMergeTrainsOptions struct { Sort *string `url:"sort,omitempty" json:"sort,omitempty"` } -// ListProjectMergeTrains get a list of merge trains in a project -// -// The scope of trains to show: active (to be merged) and complete (have been merged) +// ListProjectMergeTrains get a list of merge trains in a project. // // GitLab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-trains-for-a-project @@ -88,7 +64,6 @@ func (s *MergeTrainsService) ListProjectMergeTrains(pid interface{}, opt *ListMe if err != nil { return nil, nil, err } - u := fmt.Sprintf("projects/%s/merge_trains", PathEscape(project)) req, err := s.client.NewRequest(http.MethodGet, u, opt, options) @@ -96,17 +71,17 @@ func (s *MergeTrainsService) ListProjectMergeTrains(pid interface{}, opt *ListMe return nil, nil, err } - var mergeTrains []*MergeTrain - resp, err := s.client.Do(req, &mergeTrains) + var mts []*MergeTrain + resp, err := s.client.Do(req, &mts) if err != nil { return nil, resp, err } - return mergeTrains, resp, nil + return mts, resp, nil } -// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge train -// for the requested target branch +// ListMergeRequestInMergeTrain gets a list of merge requests added to a merge +// train for the requested target branch. // // GitLab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#list-merge-requests-in-a-merge-train @@ -115,7 +90,6 @@ func (s *MergeTrainsService) ListMergeRequestInMergeTrain(pid interface{}, targe if err != nil { return nil, nil, err } - u := fmt.Sprintf("projects/%s/merge_trains/%s", PathEscape(project), targetBranch) req, err := s.client.NewRequest(http.MethodGet, u, opts, options) @@ -123,24 +97,25 @@ func (s *MergeTrainsService) ListMergeRequestInMergeTrain(pid interface{}, targe return nil, nil, err } - var mergeTrains []*MergeTrain - resp, err := s.client.Do(req, &mergeTrains) + var mts []*MergeTrain + resp, err := s.client.Do(req, &mts) if err != nil { return nil, resp, err } - return mergeTrains, resp, nil + return mts, resp, nil } -// GetMergeRequestOnAMergeTrain Get merge train information for the requested merge request. +// GetMergeRequestOnAMergeTrain Get merge train information for the requested +// merge request. // -// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html#get-the-status-of-a-merge-request-on-a-merge-train +// GitLab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#get-the-status-of-a-merge-request-on-a-merge-train func (s *MergeTrainsService) GetMergeRequestOnAMergeTrain(pid interface{}, mergeRequest int, options ...RequestOptionFunc) (*MergeTrain, *Response, error) { project, err := parseID(pid) if err != nil { return nil, nil, err } - u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest) req, err := s.client.NewRequest(http.MethodGet, u, nil, options) @@ -148,34 +123,36 @@ func (s *MergeTrainsService) GetMergeRequestOnAMergeTrain(pid interface{}, merge return nil, nil, err } - var mergeTrain *MergeTrain - resp, err := s.client.Do(req, &mergeTrain) + mt := new(MergeTrain) + resp, err := s.client.Do(req, mt) if err != nil { return nil, resp, err } - return mergeTrain, resp, nil + return mt, resp, nil } -// AddMergeRequestToMergeTrainOptions represents the available AddMergeRequestToMergeTrain() options. +// AddMergeRequestToMergeTrainOptions represents the available +// AddMergeRequestToMergeTrain() options. // // GitLab API docs: // https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train type AddMergeRequestToMergeTrainOptions struct { - WhenPipelineSucceeds *bool `json:"when_pipeline_succeeds,omitempty"` - SHA *string `json:"sha,omitempty"` - Squash *string `json:"squash,omitempty"` + WhenPipelineSucceeds *bool `url:"when_pipeline_succeeds,omitempty" json:"when_pipeline_succeeds,omitempty"` + SHA *string `url:"sha,omitempty" json:"sha,omitempty"` + Squash *string `url:"squash,omitempty" json:"squash,omitempty"` } -// AddMergeRequestToMergeTrain Add a merge request to the merge train targeting the merge request’s target branch +// AddMergeRequestToMergeTrain Add a merge request to the merge train targeting +// the merge request’s target branch. // -// GitLab API docs: https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train +// GitLab API docs: +// https://docs.gitlab.com/ee/api/merge_trains.html#add-a-merge-request-to-a-merge-train func (s *MergeTrainsService) AddMergeRequestToMergeTrain(pid interface{}, mergeRequest int, opts *AddMergeRequestToMergeTrainOptions, options ...RequestOptionFunc) ([]*MergeTrain, *Response, error) { project, err := parseID(pid) if err != nil { return nil, nil, err } - u := fmt.Sprintf("projects/%s/merge_trains/merge_requests/%d", PathEscape(project), mergeRequest) req, err := s.client.NewRequest(http.MethodPost, u, opts, options) @@ -183,11 +160,11 @@ func (s *MergeTrainsService) AddMergeRequestToMergeTrain(pid interface{}, mergeR return nil, nil, err } - var mergeTrains []*MergeTrain - resp, err := s.client.Do(req, &mergeTrains) + var mts []*MergeTrain + resp, err := s.client.Do(req, &mts) if err != nil { return nil, resp, err } - return mergeTrains, resp, nil + return mts, resp, nil } diff --git a/merge_trains_test.go b/merge_trains_test.go index b7f84e170..e1d61faa1 100644 --- a/merge_trains_test.go +++ b/merge_trains_test.go @@ -46,7 +46,7 @@ func TestListProjectMergeTrains(t *testing.T) { UpdatedAt: &mergeRequestUpdatedAt, WebURL: "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59", }, - User: &MergeTrainUser{ + User: &BasicUser{ ID: 1, Name: "Administrator", Username: "root", @@ -54,7 +54,7 @@ func TestListProjectMergeTrains(t *testing.T) { AvatarURL: "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", WebURL: "http://local.gitlab.test:8181/root", }, - Pipeline: &MergeTrainPipeline{ + Pipeline: &Pipeline{ ID: 246, SHA: "bcc17a8ffd51be1afe45605e714085df28b80b13", Ref: "refs/merge-requests/59/train", @@ -115,7 +115,7 @@ func TestListMergeRequestInMergeTrain(t *testing.T) { UpdatedAt: &mergeRequestUpdatedAt, WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", }, - User: &MergeTrainUser{ + User: &BasicUser{ ID: 933, Username: "user12", Name: "Sidney Jones31", @@ -123,7 +123,7 @@ func TestListMergeRequestInMergeTrain(t *testing.T) { AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", WebURL: "http://localhost/user12", }, - Pipeline: &MergeTrainPipeline{ + Pipeline: &Pipeline{ ID: 273, IID: 1, ProjectID: 598, @@ -184,7 +184,7 @@ func TestGetMergeRequestOnAMergeTrain(t *testing.T) { UpdatedAt: &mergeRequestUpdatedAt, WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", }, - User: &MergeTrainUser{ + User: &BasicUser{ ID: 933, Username: "user12", Name: "Sidney Jones31", @@ -192,7 +192,7 @@ func TestGetMergeRequestOnAMergeTrain(t *testing.T) { AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", WebURL: "http://localhost/user12", }, - Pipeline: &MergeTrainPipeline{ + Pipeline: &Pipeline{ ID: 273, IID: 1, ProjectID: 598, @@ -255,7 +255,7 @@ func TestAddMergeRequestToMergeTrain(t *testing.T) { UpdatedAt: &mergeRequestUpdatedAt, WebURL: "http://localhost/namespace18/project21/-/merge_requests/1", }, - User: &MergeTrainUser{ + User: &BasicUser{ ID: 933, Username: "user12", Name: "Sidney Jones31", @@ -263,7 +263,7 @@ func TestAddMergeRequestToMergeTrain(t *testing.T) { AvatarURL: "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon", WebURL: "http://localhost/user12", }, - Pipeline: &MergeTrainPipeline{ + Pipeline: &Pipeline{ ID: 273, IID: 1, ProjectID: 598, diff --git a/testdata/add_merge_request_in_merge_train.json b/testdata/add_merge_request_in_merge_train.json index 5e19bcf0e..071126849 100644 --- a/testdata/add_merge_request_in_merge_train.json +++ b/testdata/add_merge_request_in_merge_train.json @@ -33,10 +33,10 @@ "web_url": "http://localhost/namespace19/project22/-/pipelines/273" }, "created_at": "2022-10-31T19:06:06.237Z", - "updated_at":"2022-10-31T19:06:06.237Z", - "target_branch":"main", - "status":"idle", - "merged_at":null, - "duration":null + "updated_at": "2022-10-31T19:06:06.237Z", + "target_branch": "main", + "status": "idle", + "merged_at": null, + "duration": null } -] \ No newline at end of file +] diff --git a/testdata/get_merge_request_in_merge_train.json b/testdata/get_merge_request_in_merge_train.json index fd3d6ee0e..7c299a213 100644 --- a/testdata/get_merge_request_in_merge_train.json +++ b/testdata/get_merge_request_in_merge_train.json @@ -32,9 +32,9 @@ "web_url": "http://localhost/namespace19/project22/-/pipelines/273" }, "created_at": "2022-10-31T19:06:06.237Z", - "updated_at":"2022-10-31T19:06:06.237Z", - "target_branch":"main", - "status":"idle", - "merged_at":null, - "duration":null -} \ No newline at end of file + "updated_at": "2022-10-31T19:06:06.237Z", + "target_branch": "main", + "status": "idle", + "merged_at": null, + "duration": null +} diff --git a/testdata/list_merge_requests_in_merge_train.json b/testdata/list_merge_requests_in_merge_train.json index 5e19bcf0e..071126849 100644 --- a/testdata/list_merge_requests_in_merge_train.json +++ b/testdata/list_merge_requests_in_merge_train.json @@ -33,10 +33,10 @@ "web_url": "http://localhost/namespace19/project22/-/pipelines/273" }, "created_at": "2022-10-31T19:06:06.237Z", - "updated_at":"2022-10-31T19:06:06.237Z", - "target_branch":"main", - "status":"idle", - "merged_at":null, - "duration":null + "updated_at": "2022-10-31T19:06:06.237Z", + "target_branch": "main", + "status": "idle", + "merged_at": null, + "duration": null } -] \ No newline at end of file +] diff --git a/testdata/list_merge_trains_in_project.json b/testdata/list_merge_trains_in_project.json index 08387fb41..3c74da084 100644 --- a/testdata/list_merge_trains_in_project.json +++ b/testdata/list_merge_trains_in_project.json @@ -36,4 +36,4 @@ "merged_at": "2020-02-06T08:40:57.719Z", "duration": 70 } -] \ No newline at end of file +]