Skip to content

Commit

Permalink
Merge pull request #1870 from PatrickRice-KSC/add-restrict-pipeline-c…
Browse files Browse the repository at this point in the history
…ancellation-support

Add support for ci_restrict_pipeline_cancellation_role attribute to Projects
  • Loading branch information
svanharmelen committed Jan 22, 2024
2 parents 0826177 + 0342a41 commit def3c90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions projects.go
Expand Up @@ -139,6 +139,7 @@ type Project struct {
CIJobTokenScopeEnabled bool `json:"ci_job_token_scope_enabled"`
CIOptInJWT bool `json:"ci_opt_in_jwt"`
CIAllowForkPipelinesToRunInParentProject bool `json:"ci_allow_fork_pipelines_to_run_in_parent_project"`
CIRestrictPipelineCancellationRole AccessControlValue `json:"ci_restrict_pipeline_cancellation_role"`
PublicJobs bool `json:"public_jobs"`
BuildTimeout int `json:"build_timeout"`
AutoCancelPendingPipelines string `json:"auto_cancel_pending_pipelines"`
Expand Down Expand Up @@ -842,6 +843,7 @@ type EditProjectOptions struct {
CIDefaultGitDepth *int `url:"ci_default_git_depth,omitempty" json:"ci_default_git_depth,omitempty"`
CIForwardDeploymentEnabled *bool `url:"ci_forward_deployment_enabled,omitempty" json:"ci_forward_deployment_enabled,omitempty"`
CISeperateCache *bool `url:"ci_separated_caches,omitempty" json:"ci_separated_caches,omitempty"`
CIRestrictPipelineCancellationRole *AccessControlValue `url:"ci_restrict_pipeline_cancellation_role,omitempty" json:"ci_restrict_pipeline_cancellation_role,omitempty"`
ContainerExpirationPolicyAttributes *ContainerExpirationPolicyAttributes `url:"container_expiration_policy_attributes,omitempty" json:"container_expiration_policy_attributes,omitempty"`
ContainerRegistryAccessLevel *AccessControlValue `url:"container_registry_access_level,omitempty" json:"container_registry_access_level,omitempty"`
DefaultBranch *string `url:"default_branch,omitempty" json:"default_branch,omitempty"`
Expand Down
54 changes: 52 additions & 2 deletions projects_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -276,6 +277,53 @@ func TestListOwnedProjects(t *testing.T) {
}
}

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

var developerAccessLevel AccessControlValue = "developer"
opt := &EditProjectOptions{
CIRestrictPipelineCancellationRole: Ptr(developerAccessLevel),
}

// Store whether we've set the restrict value in our edit properly
restrictValueSet := false

mux.HandleFunc("/api/v4/projects/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)

// Check that our request properly included ci_restrict_pipeline_cancellation_role
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Unable to read body properly. Error: %v", err)
}

// Set the value to check if our value is included
restrictValueSet = strings.Contains(string(body), "ci_restrict_pipeline_cancellation_role")

// Print the start of the mock example from https://docs.gitlab.com/ee/api/projects.html#edit-project
// including the attribute we edited
fmt.Fprint(w, `
{
"id": 1,
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"description_html": "<p data-sourcepos=\"1:1-1:56\" dir=\"auto\">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>",
"default_branch": "main",
"visibility": "private",
"ssh_url_to_repo": "git@example.com:diaspora/diaspora-project-site.git",
"http_url_to_repo": "http://example.com/diaspora/diaspora-project-site.git",
"web_url": "http://example.com/diaspora/diaspora-project-site",
"readme_url": "http://example.com/diaspora/diaspora-project-site/blob/main/README.md",
"ci_restrict_pipeline_cancellation_role": "developer"
}`)
})

project, resp, err := client.Projects.EditProject(1, opt)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, true, restrictValueSet)
assert.Equal(t, developerAccessLevel, project.CIRestrictPipelineCancellationRole)
}

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

Expand Down Expand Up @@ -323,6 +371,7 @@ func TestGetProjectByID(t *testing.T) {
"name_regex_keep": null,
"next_run_at": "2020-01-07T21:42:58.658Z"
},
"ci_restrict_pipeline_cancellation_role": "developer",
"packages_enabled": false,
"build_coverage_regex": "Total.*([0-9]{1,3})%"
}`)
Expand All @@ -336,8 +385,9 @@ func TestGetProjectByID(t *testing.T) {
Cadence: "7d",
NextRunAt: &wantTimestamp,
},
PackagesEnabled: false,
BuildCoverageRegex: `Total.*([0-9]{1,3})%`,
PackagesEnabled: false,
BuildCoverageRegex: `Total.*([0-9]{1,3})%`,
CIRestrictPipelineCancellationRole: "developer",
}

project, _, err := client.Projects.GetProject(1, nil)
Expand Down

0 comments on commit def3c90

Please sign in to comment.