From 4506f6fe2d49e29f2fc60e8494e623cb995e0431 Mon Sep 17 00:00:00 2001 From: Timo Furrer Date: Sun, 13 Nov 2022 16:24:46 +0100 Subject: [PATCH] Implement Metadata API --- gitlab.go | 2 ++ metadata.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ metadata_test.go | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 metadata.go create mode 100644 metadata_test.go diff --git a/gitlab.go b/gitlab.go index 50e084c2c..d9001eb7c 100644 --- a/gitlab.go +++ b/gitlab.go @@ -156,6 +156,7 @@ type Client struct { Markdown *MarkdownService MergeRequestApprovals *MergeRequestApprovalsService MergeRequests *MergeRequestsService + Metadata *MetadataService Milestones *MilestonesService Namespaces *NamespacesService Notes *NotesService @@ -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} diff --git a/metadata.go b/metadata.go new file mode 100644 index 000000000..74788c17d --- /dev/null +++ b/metadata.go @@ -0,0 +1,62 @@ +// +// Copyright 2022, Timo Furrer +// +// 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 +} diff --git a/metadata_test.go b/metadata_test.go new file mode 100644 index 000000000..35f1424aa --- /dev/null +++ b/metadata_test.go @@ -0,0 +1,53 @@ +// +// Copyright 2022, Timo Furrer +// +// 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) + } +}