Commit c0fc3db7 authored by Zubeen's avatar Zubeen 🤖 Committed by Timo Furrer
Browse files

feat(protectedTags): add support for `deploy_key_id` to `protected_tags`

Changelog: Improvements
parent 4b447a60
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ type TagAccessDescription struct {
	ID                     int64            `json:"id"`
	UserID                 int64            `json:"user_id"`
	GroupID                int64            `json:"group_id"`
	DeployKeyID            int64            `json:"deploy_key_id"`
	AccessLevel            AccessLevelValue `json:"access_level"`
	AccessLevelDescription string           `json:"access_level_description"`
}
@@ -139,6 +140,7 @@ type ProtectRepositoryTagsOptions struct {
type TagsPermissionOptions struct {
	UserID      *int64            `url:"user_id,omitempty" json:"user_id,omitempty"`
	GroupID     *int64            `url:"group_id,omitempty" json:"group_id,omitempty"`
	DeployKeyID *int64            `url:"deploy_key_id,omitempty" json:"deploy_key_id,omitempty"`
	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
}

+113 −0
Original line number Diff line number Diff line
@@ -60,6 +60,40 @@ func TestListProtectedTags(t *testing.T) {
	assert.Equal(t, expected, tags)
}

func TestListProtectedTagsWithDeployKey(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/projects/1/protected_tags", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `[{"name":"release-1-0", "create_access_levels": [{"id":1,"access_level": 40, "access_level_description": "Maintainers"},{"id":2,"access_level": 40, "access_level_description": "Deploy key", "deploy_key_id": 1}]}]`)
	})

	expected := []*ProtectedTag{
		{
			Name: "release-1-0",
			CreateAccessLevels: []*TagAccessDescription{
				{
					ID:                     1,
					AccessLevel:            40,
					AccessLevelDescription: "Maintainers",
				},
				{
					ID:                     2,
					AccessLevel:            40,
					DeployKeyID:            1,
					AccessLevelDescription: "Deploy key",
				},
			},
		},
	}

	opt := &ListProtectedTagsOptions{}
	tags, _, err := client.ProtectedTags.ListProtectedTags(1, opt)
	assert.NoError(t, err, "failed to get response")
	assert.Equal(t, expected, tags)
}

func TestGetProtectedTag(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
@@ -92,6 +126,40 @@ func TestGetProtectedTag(t *testing.T) {
	assert.Equal(t, expected, tag)
}

func TestGetProtectedTagWithDeployKey(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	tagName := "v1.0.0"

	mux.HandleFunc(fmt.Sprintf("/api/v4/projects/1/protected_tags/%s", tagName), func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodGet)
		fmt.Fprint(w, `{"name":"v1.0.0", "create_access_levels": [{"id": 1, "access_level": 40, "access_level_description": "Maintainers"},{"id": 2, "access_level": 40, "access_level_description": "Deploy key", "deploy_key_id": 5}]}`)
	})

	expected := &ProtectedTag{
		Name: tagName,
		CreateAccessLevels: []*TagAccessDescription{
			{
				ID:                     1,
				AccessLevel:            40,
				AccessLevelDescription: "Maintainers",
			},
			{
				ID:                     2,
				AccessLevel:            40,
				DeployKeyID:            5,
				AccessLevelDescription: "Deploy key",
			},
		},
	}

	tag, _, err := client.ProtectedTags.GetProtectedTag(1, tagName)

	assert.NoError(t, err, "failed to get response")
	assert.Equal(t, expected, tag)
}

func TestProtectRepositoryTags(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)
@@ -131,6 +199,51 @@ func TestProtectRepositoryTags(t *testing.T) {
	assert.Equal(t, expected, tag)
}

func TestProtectRepositoryTagsWithDeployKey(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	mux.HandleFunc("/api/v4/projects/1/protected_tags", func(w http.ResponseWriter, r *http.Request) {
		testMethod(t, r, http.MethodPost)
		fmt.Fprint(w, `{"name":"*-stable", "create_access_levels": [{"id": 1, "access_level": 30, "user_id": 10, "access_level_description": "Administrator"},{"id": 2, "access_level": 40, "deploy_key_id": 20, "access_level_description": "Deploy key"}]}`)
	})

	expected := &ProtectedTag{
		Name: "*-stable",
		CreateAccessLevels: []*TagAccessDescription{
			{
				ID:                     1,
				AccessLevel:            30,
				UserID:                 10,
				AccessLevelDescription: "Administrator",
			},
			{
				ID:                     2,
				AccessLevel:            40,
				DeployKeyID:            20,
				AccessLevelDescription: "Deploy key",
			},
		},
	}

	opt := &ProtectRepositoryTagsOptions{
		Name:              Ptr("*-stable"),
		CreateAccessLevel: Ptr(AccessLevelValue(30)),
		AllowedToCreate: &[]*TagsPermissionOptions{
			{
				UserID: Ptr(int64(10)),
			},
			{
				DeployKeyID: Ptr(int64(20)),
			},
		},
	}
	tag, _, err := client.ProtectedTags.ProtectRepositoryTags(1, opt)

	assert.NoError(t, err, "failed to get response")
	assert.Equal(t, expected, tag)
}

func TestUnprotectRepositoryTags(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)