From db332e1854865a4f23a9346e06ea09f1928fac45 Mon Sep 17 00:00:00 2001 From: Nicholas Shine Date: Fri, 27 Sep 2024 16:16:24 -0500 Subject: [PATCH] add new pages api endpoint --- pages.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ pages_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/pages.go b/pages.go index 617b0ba4b..606baf623 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 +} diff --git a/pages_test.go b/pages_test.go index df8e0bb12..395ef2c6a 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) +}