Skip to content

Commit

Permalink
Merge pull request #2019 from splunk/pages-api-update
Browse files Browse the repository at this point in the history
feat: add new pages api endpoint
  • Loading branch information
svanharmelen authored Oct 6, 2024
2 parents 8a715ef + db332e1 commit 8082427
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
}
45 changes: 45 additions & 0 deletions pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package gitlab

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestUnpublishPages(t *testing.T) {
Expand All @@ -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)
}

0 comments on commit 8082427

Please sign in to comment.