Commit 07cff51b authored by Patrick Rice's avatar Patrick Rice ❄️
Browse files

Merge branch 'do-requests-part-2' into 'main'

Migrate more endpoints to the `do()` pattern

See merge request !2731
parents 77888749 4b17a473
Loading
Loading
Loading
Loading
Loading
+6 −12
Original line number Diff line number Diff line
@@ -62,16 +62,10 @@ type BulkImportStartMigrationResponse struct {
}

func (b *BulkImportsService) StartMigration(startMigrationOptions *BulkImportStartMigrationOptions, options ...RequestOptionFunc) (*BulkImportStartMigrationResponse, *Response, error) {
	request, err := b.client.NewRequest(http.MethodPost, "bulk_imports", startMigrationOptions, options)
	if err != nil {
		return nil, nil, err
	}

	startMigrationResponse := new(BulkImportStartMigrationResponse)
	response, err := b.client.Do(request, startMigrationResponse)
	if err != nil {
		return nil, response, err
	}

	return startMigrationResponse, response, nil
	return do[*BulkImportStartMigrationResponse](b.client,
		withMethod(http.MethodPost),
		withPath("bulk_imports"),
		withAPIOpts(startMigrationOptions),
		withRequestOpts(options...),
	)
}
+22 −42
Original line number Diff line number Diff line
@@ -16,10 +16,7 @@

package gitlab

import (
	"fmt"
	"net/http"
)
import "net/http"

type (
	GroupLabelsServiceInterface interface {
@@ -125,26 +122,21 @@ type DeleteGroupLabelOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/group_labels/#delete-a-group-label
func (s *GroupLabelsService) DeleteGroupLabel(gid any, lid any, opt *DeleteGroupLabelOptions, options ...RequestOptionFunc) (*Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
	reqOpts := make([]doOption, 0, 4)
	reqOpts = append(reqOpts,
		withMethod(http.MethodDelete),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)

	if lid != nil {
		label, err := parseID(lid)
		if err != nil {
			return nil, err
		}
		u = fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label))
	}

	req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
	if err != nil {
		return nil, err
		reqOpts = append(reqOpts, withPath("groups/%s/labels/%s", GroupID{gid}, LabelID{lid}))
	} else {
		reqOpts = append(reqOpts, withPath("groups/%s/labels", GroupID{gid}))
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client, reqOpts...)
	return resp, err
}

// UpdateGroupLabelOptions represents the available UpdateGroupLabel() options.
@@ -165,32 +157,20 @@ type UpdateGroupLabelOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/group_labels/#update-a-group-label
func (s *GroupLabelsService) UpdateGroupLabel(gid any, lid any, opt *UpdateGroupLabelOptions, options ...RequestOptionFunc) (*GroupLabel, *Response, error) {
	group, err := parseID(gid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("groups/%s/labels", PathEscape(group))
	reqOpts := make([]doOption, 0, 4)
	reqOpts = append(reqOpts,
		withMethod(http.MethodPut),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)

	if lid != nil {
		label, err := parseID(lid)
		if err != nil {
			return nil, nil, err
		}
		u = fmt.Sprintf("groups/%s/labels/%s", PathEscape(group), PathEscape(label))
	}

	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	l := new(GroupLabel)
	resp, err := s.client.Do(req, l)
	if err != nil {
		return nil, resp, err
		reqOpts = append(reqOpts, withPath("groups/%s/labels/%s", GroupID{gid}, LabelID{lid}))
	} else {
		reqOpts = append(reqOpts, withPath("groups/%s/labels", GroupID{gid}))
	}

	return l, resp, nil
	return do[*GroupLabel](s.client, reqOpts...)
}

// SubscribeToGroupLabel subscribes the authenticated user to a label to receive
+46 −130
Original line number Diff line number Diff line
@@ -14,10 +14,7 @@

package gitlab

import (
	"fmt"
	"net/http"
)
import "net/http"

type (
	JobTokenScopeServiceInterface interface {
@@ -54,24 +51,10 @@ type JobTokenAccessSettings struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-access-settings
func (j *JobTokenScopeService) GetProjectJobTokenAccessSettings(pid any, options ...RequestOptionFunc) (*JobTokenAccessSettings, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodGet, u, nil, options)
	if err != nil {
		return nil, nil, err
	}

	jt := new(JobTokenAccessSettings)
	resp, err := j.client.Do(req, jt)
	if err != nil {
		return nil, resp, err
	}

	return jt, resp, err
	return do[*JobTokenAccessSettings](j.client,
		withPath("projects/%s/job_token_scope", ProjectID{pid}),
		withRequestOpts(options...),
	)
}

// PatchProjectJobTokenAccessSettingsOptions represents the available
@@ -88,18 +71,13 @@ type PatchProjectJobTokenAccessSettingsOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#patch-a-projects-cicd-job-token-access-settings
func (j *JobTokenScopeService) PatchProjectJobTokenAccessSettings(pid any, opt *PatchProjectJobTokenAccessSettingsOptions, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodPatch, u, opt, options)
	if err != nil {
		return nil, err
	}

	return j.client.Do(req, nil)
	_, resp, err := do[none](j.client,
		withMethod(http.MethodPatch),
		withPath("projects/%s/job_token_scope", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
	return resp, err
}

// JobTokenInboundAllowItem represents a single job token inbound allowlist item.
@@ -125,24 +103,11 @@ type GetJobTokenInboundAllowListOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-inbound-allowlist
func (j *JobTokenScopeService) GetProjectJobTokenInboundAllowList(pid any, opt *GetJobTokenInboundAllowListOptions, options ...RequestOptionFunc) ([]*Project, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/allowlist`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var ps []*Project
	resp, err := j.client.Do(req, &ps)
	if err != nil {
		return nil, resp, err
	}

	return ps, resp, nil
	return do[[]*Project](j.client,
		withPath("projects/%s/job_token_scope/allowlist", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// JobTokenInboundAllowOptions represents the available
@@ -160,24 +125,12 @@ type JobTokenInboundAllowOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#add-a-project-to-a-cicd-job-token-inbound-allowlist
func (j *JobTokenScopeService) AddProjectToJobScopeAllowList(pid any, opt *JobTokenInboundAllowOptions, options ...RequestOptionFunc) (*JobTokenInboundAllowItem, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/allowlist`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodPost, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	jt := new(JobTokenInboundAllowItem)
	resp, err := j.client.Do(req, jt)
	if err != nil {
		return nil, resp, err
	}

	return jt, resp, nil
	return do[*JobTokenInboundAllowItem](j.client,
		withMethod(http.MethodPost),
		withPath("projects/%s/job_token_scope/allowlist", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// RemoveProjectFromJobScopeAllowList removes a project from a project's job
@@ -186,18 +139,12 @@ func (j *JobTokenScopeService) AddProjectToJobScopeAllowList(pid any, opt *JobTo
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#remove-a-project-from-a-cicd-job-token-inbound-allowlist
func (j *JobTokenScopeService) RemoveProjectFromJobScopeAllowList(pid any, targetProject int64, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/allowlist/%d`, PathEscape(project), targetProject)

	req, err := j.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return j.client.Do(req, nil)
	_, resp, err := do[none](j.client,
		withMethod(http.MethodDelete),
		withPath("projects/%s/job_token_scope/allowlist/%d", ProjectID{pid}, targetProject),
		withRequestOpts(options...),
	)
	return resp, err
}

// JobTokenAllowlistItem represents a single job token allowlist item.
@@ -223,24 +170,11 @@ type GetJobTokenAllowlistGroupsOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-allowlist-of-groups
func (j *JobTokenScopeService) GetJobTokenAllowlistGroups(pid any, opt *GetJobTokenAllowlistGroupsOptions, options ...RequestOptionFunc) ([]*Group, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/groups_allowlist`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var ps []*Group
	resp, err := j.client.Do(req, &ps)
	if err != nil {
		return nil, resp, err
	}

	return ps, resp, nil
	return do[[]*Group](j.client,
		withPath("projects/%s/job_token_scope/groups_allowlist", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// AddGroupToJobTokenAllowlistOptions represents the available
@@ -258,24 +192,12 @@ type AddGroupToJobTokenAllowlistOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#add-a-group-to-a-cicd-job-token-allowlist
func (j *JobTokenScopeService) AddGroupToJobTokenAllowlist(pid any, opt *AddGroupToJobTokenAllowlistOptions, options ...RequestOptionFunc) (*JobTokenAllowlistItem, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/groups_allowlist`, PathEscape(project))

	req, err := j.client.NewRequest(http.MethodPost, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	jt := new(JobTokenAllowlistItem)
	resp, err := j.client.Do(req, jt)
	if err != nil {
		return nil, resp, err
	}

	return jt, resp, nil
	return do[*JobTokenAllowlistItem](j.client,
		withMethod(http.MethodPost),
		withPath("projects/%s/job_token_scope/groups_allowlist", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}

// RemoveGroupFromJobTokenAllowlist removes a group from a project's job
@@ -284,16 +206,10 @@ func (j *JobTokenScopeService) AddGroupToJobTokenAllowlist(pid any, opt *AddGrou
// GitLab API docs:
// https://docs.gitlab.com/api/project_job_token_scopes/#remove-a-group-from-a-cicd-job-token-allowlist
func (j *JobTokenScopeService) RemoveGroupFromJobTokenAllowlist(pid any, targetGroup int64, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf(`projects/%s/job_token_scope/groups_allowlist/%d`, PathEscape(project), targetGroup)

	req, err := j.client.NewRequest(http.MethodDelete, u, nil, options)
	if err != nil {
		return nil, err
	}

	return j.client.Do(req, nil)
	_, resp, err := do[none](j.client,
		withMethod(http.MethodDelete),
		withPath("projects/%s/job_token_scope/groups_allowlist/%d", ProjectID{pid}, targetGroup),
		withRequestOpts(options...),
	)
	return resp, err
}
+21 −39
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package gitlab

import (
	"encoding/json"
	"fmt"
	"net/http"
)

@@ -151,26 +150,21 @@ type DeleteLabelOptions struct {
//
// GitLab API docs: https://docs.gitlab.com/api/labels/#delete-a-label
func (s *LabelsService) DeleteLabel(pid any, lid any, opt *DeleteLabelOptions, options ...RequestOptionFunc) (*Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, err
	}
	u := fmt.Sprintf("projects/%s/labels", PathEscape(project))
	reqOpts := make([]doOption, 0, 4)
	reqOpts = append(reqOpts,
		withMethod(http.MethodDelete),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)

	if lid != nil {
		label, err := parseID(lid)
		if err != nil {
			return nil, err
		}
		u = fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label))
		reqOpts = append(reqOpts, withPath("projects/%s/labels/%s", ProjectID{pid}, LabelID{lid}))
	} else {
		reqOpts = append(reqOpts, withPath("projects/%s/labels", ProjectID{pid}))
	}

	req, err := s.client.NewRequest(http.MethodDelete, u, opt, options)
	if err != nil {
		return nil, err
	}

	return s.client.Do(req, nil)
	_, resp, err := do[none](s.client, reqOpts...)
	return resp, err
}

// UpdateLabelOptions represents the available UpdateLabel() options.
@@ -189,32 +183,20 @@ type UpdateLabelOptions struct {
//
// GitLab API docs: https://docs.gitlab.com/api/labels/#edit-an-existing-label
func (s *LabelsService) UpdateLabel(pid any, lid any, opt *UpdateLabelOptions, options ...RequestOptionFunc) (*Label, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/labels", PathEscape(project))
	reqOpts := make([]doOption, 0, 4)
	reqOpts = append(reqOpts,
		withMethod(http.MethodPut),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)

	if lid != nil {
		label, err := parseID(lid)
		if err != nil {
			return nil, nil, err
		}
		u = fmt.Sprintf("projects/%s/labels/%s", PathEscape(project), PathEscape(label))
	}

	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	l := new(Label)
	resp, err := s.client.Do(req, l)
	if err != nil {
		return nil, resp, err
		reqOpts = append(reqOpts, withPath("projects/%s/labels/%s", ProjectID{pid}, LabelID{lid}))
	} else {
		reqOpts = append(reqOpts, withPath("projects/%s/labels", ProjectID{pid}))
	}

	return l, resp, nil
	return do[*Label](s.client, reqOpts...)
}

// SubscribeToLabel subscribes the authenticated user to a label to receive
+6 −23
Original line number Diff line number Diff line
@@ -16,11 +16,7 @@

package gitlab

import (
	"fmt"
	"net/http"
	"time"
)
import "time"

type (
	ProjectIterationsServiceInterface interface {
@@ -77,22 +73,9 @@ type ListProjectIterationsOptions struct {
// GitLab API docs:
// https://docs.gitlab.com/api/iterations/#list-project-iterations
func (i *ProjectIterationsService) ListProjectIterations(pid any, opt *ListProjectIterationsOptions, options ...RequestOptionFunc) ([]*ProjectIteration, *Response, error) {
	project, err := parseID(pid)
	if err != nil {
		return nil, nil, err
	}
	u := fmt.Sprintf("projects/%s/iterations", PathEscape(project))

	req, err := i.client.NewRequest(http.MethodGet, u, opt, options)
	if err != nil {
		return nil, nil, err
	}

	var pis []*ProjectIteration
	resp, err := i.client.Do(req, &pis)
	if err != nil {
		return nil, resp, err
	}

	return pis, resp, nil
	return do[[]*ProjectIteration](i.client,
		withPath("projects/%s/iterations", ProjectID{pid}),
		withAPIOpts(opt),
		withRequestOpts(options...),
	)
}
Loading