Skip to content

Commit

Permalink
Implement Metadata API
Browse files Browse the repository at this point in the history
  • Loading branch information
Timo Furrer committed Nov 20, 2022
1 parent 4ec1dc6 commit 4c641df
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gitlab.go
Expand Up @@ -156,6 +156,7 @@ type Client struct {
Markdown *MarkdownService
MergeRequestApprovals *MergeRequestApprovalsService
MergeRequests *MergeRequestsService
Metadata *MetadataService
Milestones *MilestonesService
Namespaces *NamespacesService
Notes *NotesService
Expand Down Expand Up @@ -361,6 +362,7 @@ func newClient(options ...ClientOptionFunc) (*Client, error) {
c.Markdown = &MarkdownService{client: c}
c.MergeRequestApprovals = &MergeRequestApprovalsService{client: c}
c.MergeRequests = &MergeRequestsService{client: c, timeStats: timeStats}
c.Metadata = &MetadataService{client: c}
c.Milestones = &MilestonesService{client: c}
c.Namespaces = &NamespacesService{client: c}
c.Notes = &NotesService{client: c}
Expand Down
63 changes: 63 additions & 0 deletions metadata.go
@@ -0,0 +1,63 @@
//
// Copyright 2022, Timo Furrer <tuxtimo@gmail.com>
//
// 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"

// MetadataService handles communication with the GitLab server instance to
// retrieve its metadata information via the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/version.md
type MetadataService struct {
client *Client
}

// Metadata represents a GitLab instance version.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/metadata.md
type Metadata struct {
Version string `json:"version"`
Revision string `json:"revision"`
KAS struct {
Enabled bool `json:"enabled"`
ExternalURL string `json:"externalUrl"`
Version string `json:"version"`
} `json:"kas"`
}

func (s Metadata) String() string {
return Stringify(s)
}

// GetMetadata gets a GitLab server instance meteadata.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/metadata.md
func (s *MetadataService) GetMetadata(options ...RequestOptionFunc) (*Metadata, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "metadata", nil, options)
if err != nil {
return nil, nil, err
}

v := new(Metadata)
resp, err := s.client.Do(req, v)
if err != nil {
return nil, resp, err
}

return v, resp, err
}

54 changes: 54 additions & 0 deletions metadata_test.go
@@ -0,0 +1,54 @@
//
// Copyright 2022, Timo Furrer <tuxtimo@gmail.com>
//
// 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"
"reflect"
"testing"
)

func TestGetMetadata(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/metadata",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"version":"15.6.0-pre","revision":"016e8d8bdc3","kas":{"enabled":true,"externalUrl":"wss://kas.gitlab.com","version":"15.6.0-rc2"}}`)
})

version, _, err := client.Metadata.GetMetadata()
if err != nil {
t.Errorf("Metadata.GetMetadata returned error: %v", err)
}

want := &Metadata{Version: "15.6.0-pre", Revision: "016e8d8bdc3", KAS: struct {
Enabled bool `json:"enabled"`
ExternalURL string `json:"externalUrl"`
Version string `json:"version"`
}{
Enabled: true,
ExternalURL: "wss://kas.gitlab.com",
Version: "15.6.0-rc2",
}}
if !reflect.DeepEqual(want, version) {
t.Errorf("Metadata.GetMetadata returned %+v, want %+v", version, want)
}
}

0 comments on commit 4c641df

Please sign in to comment.