Verified Commit 2fbc1b54 authored by Hamza Hassanain's avatar Hamza Hassanain 💬 Committed by GitLab
Browse files

feat(merge_requests): support blocking MR IID in dependency options

Changelog: Improvements
parent 23eb0a84
Loading
Loading
Loading
Loading
+8 −0
Changes for merge_requests.go: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1080,10 +1080,18 @@ func (m MergeRequestDependency) String() string {
// CreateMergeRequestDependencyOptions represents the available CreateMergeRequestDependency()
// options.
//
// Exactly one of BlockingMergeRequestID or BlockingMergeRequestIID must be
// set. The server returns 400 otherwise. BlockingProjectID takes a project ID
// or a full path such as "group/project" (not URL-encoded, it is sent in the
// request body). It is only used with BlockingMergeRequestIID and defaults to
// the current project.
//
// GitLab API docs:
// https://docs.gitlab.com/api/merge_requests/#create-a-merge-request-dependency
type CreateMergeRequestDependencyOptions struct {
	BlockingMergeRequestID  *int64 `url:"blocking_merge_request_id,omitempty" json:"blocking_merge_request_id,omitempty"`
	BlockingMergeRequestIID *int64 `url:"blocking_merge_request_iid,omitempty" json:"blocking_merge_request_iid,omitempty"`
	BlockingProjectID       any    `url:"blocking_project_id,omitempty" json:"blocking_project_id,omitempty"`
}

// CreateMergeRequestDependency creates a new merge request dependency for a given
+57 −5
Changes for merge_requests_test.go: 57 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -600,22 +600,72 @@ func TestAssigneeIDMarshalling(t *testing.T) {

func TestCreateMergeRequestDependency(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
	const project = "12345"
	const mergeRequest = 1

	tests := []struct {
		name     string
		opts     CreateMergeRequestDependencyOptions
		wantBody map[string]any
	}{
		{
			name: "blocking merge request by global ID",
			opts: CreateMergeRequestDependencyOptions{
				BlockingMergeRequestID: new(int64(2)),
			},
			wantBody: map[string]any{"blocking_merge_request_id": float64(2)},
		},
		{
			name: "blocking merge request by IID in the same project",
			opts: CreateMergeRequestDependencyOptions{
				BlockingMergeRequestIID: new(int64(5)),
			},
			wantBody: map[string]any{"blocking_merge_request_iid": float64(5)},
		},
		{
			name: "blocking merge request by IID in another project by ID",
			opts: CreateMergeRequestDependencyOptions{
				BlockingMergeRequestIID: new(int64(5)),
				BlockingProjectID:       int64(2),
			},
			wantBody: map[string]any{
				"blocking_merge_request_iid": float64(5),
				"blocking_project_id":        float64(2),
			},
		},
		{
			name: "blocking merge request by IID in another project by path",
			opts: CreateMergeRequestDependencyOptions{
				BlockingMergeRequestIID: new(int64(5)),
				BlockingProjectID:       "group/project",
			},
			wantBody: map[string]any{
				"blocking_merge_request_iid": float64(5),
				"blocking_project_id":        "group/project",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			mux, client := setup(t)

			// GIVEN a server that expects the given request body and returns
			// a created merge request dependency
			path := fmt.Sprintf("/%sprojects/%s/merge_requests/%d/blocks", apiVersionPath, project, mergeRequest)
			mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
				testMethod(t, r, http.MethodPost)
				testBodyJSON(t, r, tt.wantBody)
				w.WriteHeader(http.StatusCreated)

				fmt.Fprint(w, `{"id": 1, "blocking_merge_request": {"iid":12}, "project_id": 7}`)
			})

	opts := CreateMergeRequestDependencyOptions{
		BlockingMergeRequestID: new(int64(2)),
	}
	dependencies, resp, err := client.MergeRequests.CreateMergeRequestDependency(project, mergeRequest, opts, nil)
			// WHEN creating a merge request dependency with the given options
			dependencies, resp, err := client.MergeRequests.CreateMergeRequestDependency(project, mergeRequest, tt.opts, nil)

			// THEN the request succeeds and the dependency is returned
			require.NoError(t, err)
			require.NotNil(t, resp)
			assert.Equal(t, http.StatusCreated, resp.StatusCode)
@@ -628,6 +678,8 @@ func TestCreateMergeRequestDependency(t *testing.T) {
				ProjectID: 7,
			}
			assert.Equal(t, want, dependencies)
		})
	}
}

func TestGetMergeRequestDependencies(t *testing.T) {