Repository: xanzy/go-gitlab
Pull request #1204 (2021-09-04 13:27:37)

feat: Added Pages Support
Link to the Docs: https://docs.gitlab.com/ee/api/pages.html#unpublish-pages


Patch:
diff --git a/README.md b/README.md
index 3c8e401..beb003c 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,7 @@ to add new and/or missing endpoints. Currently the following services are suppor
 - [x] Notes (comments)
 - [x] Notification Settings
 - [x] Open Source License Templates
+- [x] Pages
 - [x] Pages Domains
 - [x] Personal Access Tokens
 - [x] Pipeline Schedules
diff --git a/event_webhook_types.go b/event_webhook_types.go
index b467860..638b14b 100644
--- a/event_webhook_types.go
+++ b/event_webhook_types.go
@@ -622,19 +622,17 @@ func (p *MergeParams) UnmarshalJSON(b []byte) error {
 type PipelineEvent struct {
 	ObjectKind       string `json:"object_kind"`
 	ObjectAttributes struct {
-		ID             int      `json:"id"`
-		Ref            string   `json:"ref"`
-		Tag            bool     `json:"tag"`
-		SHA            string   `json:"sha"`
-		BeforeSHA      string   `json:"before_sha"`
-		Source         string   `json:"source"`
-		Status         string   `json:"status"`
-		DetailedStatus string   `json:"detailed_status"`
-		Stages         []string `json:"stages"`
-		CreatedAt      string   `json:"created_at"`
-		FinishedAt     string   `json:"finished_at"`
-		Duration       int      `json:"duration"`
-		QueuedDuration int      `json:"queued_duration"`
+		ID         int      `json:"id"`
+		Ref        string   `json:"ref"`
+		Tag        bool     `json:"tag"`
+		SHA        string   `json:"sha"`
+		BeforeSHA  string   `json:"before_sha"`
+		Source     string   `json:"source"`
+		Status     string   `json:"status"`
+		Stages     []string `json:"stages"`
+		CreatedAt  string   `json:"created_at"`
+		FinishedAt string   `json:"finished_at"`
+		Duration   int      `json:"duration"`
 	} `json:"object_attributes"`
 	MergeRequest struct {
 		ID                 int    `json:"id"`
diff --git a/gitlab.go b/gitlab.go
index aa6529d..fcaf756 100644
--- a/gitlab.go
+++ b/gitlab.go
@@ -58,7 +58,6 @@ type authType int
 // GitLab API docs: https://docs.gitlab.com/ce/api/
 const (
 	basicAuth authType = iota
-	jobToken
 	oAuthToken
 	privateToken
 )
@@ -151,6 +150,7 @@ type Client struct {
 	Notes                 *NotesService
 	NotificationSettings  *NotificationSettingsService
 	Packages              *PackagesService
+	Pages                 *PagesService
 	PagesDomains          *PagesDomainsService
 	PipelineSchedules     *PipelineSchedulesService
 	PipelineTriggers      *PipelineTriggersService
@@ -230,18 +230,6 @@ func NewBasicAuthClient(username, password string, options ...ClientOptionFunc)
 	return client, nil
 }
 
-// NewJobClient returns a new GitLab API client. To use API methods which require
-// authentication, provide a valid job token.
-func NewJobClient(token string, options ...ClientOptionFunc) (*Client, error) {
-	client, err := newClient(options...)
-	if err != nil {
-		return nil, err
-	}
-	client.authType = jobToken
-	client.token = token
-	return client, nil
-}
-
 // NewOAuthClient returns a new GitLab API client. To use API methods which
 // require authentication, provide a valid oauth token.
 func NewOAuthClient(token string, options ...ClientOptionFunc) (*Client, error) {
@@ -337,6 +325,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) {
 	c.Notes = &NotesService{client: c}
 	c.NotificationSettings = &NotificationSettingsService{client: c}
 	c.Packages = &PackagesService{client: c}
+	c.Pages = &PagesService{client: c}
 	c.PagesDomains = &PagesDomainsService{client: c}
 	c.PipelineSchedules = &PipelineSchedulesService{client: c}
 	c.PipelineTriggers = &PipelineTriggersService{client: c}
@@ -658,8 +647,6 @@ func (c *Client) Do(req *retryablehttp.Request, v interface{}) (*Response, error
 			}
 		}
 		req.Header.Set("Authorization", "Bearer "+basicAuthToken)
-	case jobToken:
-		req.Header.Set("JOB-TOKEN", c.token)
 	case oAuthToken:
 		req.Header.Set("Authorization", "Bearer "+c.token)
 	case privateToken:
diff --git a/pages.go b/pages.go
new file mode 100644
index 0000000..167551b
--- /dev/null
+++ b/pages.go
@@ -0,0 +1,45 @@
+//
+// Copyright 2021, Sander van Harmelen
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package gitlab
+
+import (
+	"fmt"
+	"net/http"
+)
+
+type PagesService struct {
+	client *Client
+}
+
+// UnpublishPages unpublished pages. The user must have admin privileges.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/pages.html#unpublish-pages
+func (s *PagesService) UnpublishPages(gid interface{}, options ...RequestOptionFunc) (*Response, error) {
+	page, err := parseID(gid)
+	if err != nil {
+		return nil, err
+	}
+	u := fmt.Sprintf("projects/%s/pages", pathEscape(page))
+
+	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
+	if err != nil {
+		return nil, err
+	}
+
+	return s.client.Do(req, nil)
+}
diff --git a/protected_branches.go b/protected_branches.go
index 11ef66c..af3e7f2 100644
--- a/protected_branches.go
+++ b/protected_branches.go
@@ -138,7 +138,6 @@ type ProtectRepositoryBranchesOptions struct {
 type BranchPermissionOptions struct {
 	UserID      *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
 	GroupID     *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
-	DeployKeyID *int              `url:"deploy_key_id,omitempty" json:"deploy_key_id,omitempty"`
 	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
 }
 
diff --git a/types.go b/types.go
index 9b95aad..e1a1ed9 100644
--- a/types.go
+++ b/types.go
@@ -478,8 +478,8 @@ type SquashOptionValue string
 const (
 	SquashOptionNever      SquashOptionValue = "never"
 	SquashOptionAlways     SquashOptionValue = "always"
-	SquashOptionDefaultOff SquashOptionValue = "default_off"
-	SquashOptionDefaultOn  SquashOptionValue = "default_on"
+	SquashOptionDefaultOff SquashOptionValue = "default_on"
+	SquashOptionDefaultOn  SquashOptionValue = "default_off"
 )
 
 // SquashOption is a helper routine that allocates a new SquashOptionValue


Test patch:
diff --git a/event_webhook_types_test.go b/event_webhook_types_test.go
index 02fd894..14d6683 100644
--- a/event_webhook_types_test.go
+++ b/event_webhook_types_test.go
@@ -301,15 +301,7 @@ func TestPipelineEventUnmarshal(t *testing.T) {
 	}
 
 	if event.ObjectAttributes.ID != 31 {
-		t.Errorf("ObjectAttributes.ID is %v, want %v", event.ObjectAttributes.ID, 1977)
-	}
-
-	if event.ObjectAttributes.DetailedStatus != "passed" {
-		t.Errorf("ObjectAttributes.DetailedStatus is %v, want %v", event.ObjectAttributes.DetailedStatus, "passed")
-	}
-
-	if event.ObjectAttributes.QueuedDuration != 12 {
-		t.Errorf("ObjectAttributes.QueuedDuration is %v, want %v", event.ObjectAttributes.QueuedDuration, 12)
+		t.Errorf("ObjectAttributes is %v, want %v", event.ObjectAttributes.ID, 1977)
 	}
 
 	if event.User.ID != 42 {
diff --git a/pages_test.go b/pages_test.go
new file mode 100644
index 0000000..1d92ffb
--- /dev/null
+++ b/pages_test.go
@@ -0,0 +1,36 @@
+//
+// Copyright 2021, Sander van Harmelen
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package gitlab
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestUnpublishPages(t *testing.T) {
+	mux, server, client := setup(t)
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/2/pages", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, http.MethodDelete)
+	})
+
+	_, err := client.Pages.UnpublishPages(2)
+	if err != nil {
+		t.Errorf("Pages.UnpublishPages returned error: %v", err)
+	}
+}
diff --git a/testdata/webhooks/pipeline.json b/testdata/webhooks/pipeline.json
index 55b3d50..2b7ad47 100644
--- a/testdata/webhooks/pipeline.json
+++ b/testdata/webhooks/pipeline.json
@@ -8,7 +8,6 @@
      "before_sha": "bcbb5ec396a2c0f828686f14fac9b80b780504f2",
      "source": "merge_request_event",
      "status": "success",
-     "detailed_status": "passed",
      "stages":[
         "build",
         "test",
@@ -17,7 +16,6 @@
      "created_at": "2016-08-12 15:23:28 UTC",
      "finished_at": "2016-08-12 15:26:29 UTC",
      "duration": 63,
-     "queued_duration": 12,
      "variables": [
        {
          "key": "NESTOR_PROD_ENVIRONMENT",