Repository: xanzy/go-gitlab
Pull request #2019 (2024-09-27 21:20:05)

feat: add new pages api endpoint
GitLab introduced the "Get Pages settings for a project" in 16.8:
https://docs.gitlab.com/ee/api/pages.html#get-pages-settings-for-a-project

This PR adds this missing endpoint.


Patch:
diff --git a/pages.go b/pages.go
index 617b0ba..606baf6 100644
--- a/pages.go
+++ b/pages.go
@@ -19,12 +19,33 @@ package gitlab
 import (
 	"fmt"
 	"net/http"
+	"time"
 )
 
 type PagesService struct {
 	client *Client
 }
 
+// PagesDeployment represents a Pages deployment.
+//
+// GitLab API docs: https://docs.gitlab.com/ee/api/pages.html
+type PagesDeployment struct {
+	CreatedAt     *time.Time `json:"created_at"`
+	URL           string     `json:"url"`
+	PathPrefix    *string    `json:"path_prefix"`
+	RootDirectory *string    `json:"root_directory"`
+}
+
+// Pages represents the Pages of a project.
+//
+// GitLab API docs: https://docs.gitlab.com/ee/api/pages.html
+type Pages struct {
+	URL                   string            `json:"url"`
+	IsUniqueDomainEnabled bool              `json:"is_unique_domain_enabled"`
+	ForceHTTPS            bool              `json:"force_https"`
+	Deployments           []PagesDeployment `json:"deployments"`
+}
+
 // UnpublishPages unpublished pages. The user must have admin privileges.
 //
 // GitLab API docs:
@@ -43,3 +64,30 @@ func (s *PagesService) UnpublishPages(gid interface{}, options ...RequestOptionF
 
 	return s.client.Do(req, nil)
 }
+
+// GetPages lists Pages settings for a project. The user must have at least
+// maintainer privileges.
+//
+// GitLab API Docs:
+// https://docs.gitlab.com/ee/api/pages.html#get-pages-settings-for-a-project
+func (s *PagesService) GetPages(gid interface{}, options ...RequestOptionFunc) (*Pages, *Response, error) {
+	project, err := parseID(gid)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	u := fmt.Sprintf("projects/%s/pages", PathEscape(project))
+
+	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	p := new(Pages)
+	resp, err := s.client.Do(req, p)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return p, resp, nil
+}


Test patch:
diff --git a/pages_test.go b/pages_test.go
index df8e0bb..395ef2c 100644
--- a/pages_test.go
+++ b/pages_test.go
@@ -17,8 +17,12 @@
 package gitlab
 
 import (
+	"fmt"
 	"net/http"
 	"testing"
+	"time"
+
+	"github.com/stretchr/testify/require"
 )
 
 func TestUnpublishPages(t *testing.T) {
@@ -33,3 +37,44 @@ func TestUnpublishPages(t *testing.T) {
 		t.Errorf("Pages.UnpublishPages returned error: %v", err)
 	}
 }
+
+func TestGetPages(t *testing.T) {
+	mux, client := setup(t)
+	mux.HandleFunc("/api/v4/projects/2/pages", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, http.MethodGet)
+		fmt.Fprint(w, `
+		  {
+			"url": "https://ssl.domain.example",
+			"deployments": [
+			  {
+				"created_at": "2021-04-27T21:27:38.584Z",
+				"url": "https://ssl.domain.example/",
+				"path_prefix": "",
+				"root_directory": null
+			  }
+			],
+			"is_unique_domain_enabled": false,
+			"force_https": false
+		  }
+		`)
+	})
+
+	want := &Pages{
+		URL:                   "https://ssl.domain.example",
+		IsUniqueDomainEnabled: false,
+		ForceHTTPS:            false,
+		Deployments: []PagesDeployment{
+			{
+				CreatedAt:     Ptr(time.Date(2021, time.April, 27, 21, 27, 38, 584000000, time.UTC)),
+				URL:           "https://ssl.domain.example/",
+				PathPrefix:    Ptr(""),
+				RootDirectory: nil,
+			},
+		},
+	}
+
+	p, resp, err := client.Pages.GetPages(2)
+	require.NoError(t, err)
+	require.NotNil(t, resp)
+	require.Equal(t, want, p)
+}