mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 15:12:02 +00:00
chore(sg): extract releaseregistry client package (#63382)
In preparation for reuse elsewhere.
This commit is contained in:
parent
4b3cfa33bf
commit
4641bc5023
@ -21,6 +21,7 @@ go_library(
|
||||
"//dev/sg/internal/repo",
|
||||
"//dev/sg/internal/std",
|
||||
"//internal/jsonc",
|
||||
"//internal/releaseregistry",
|
||||
"//lib/errors",
|
||||
"//lib/output",
|
||||
"@com_github_grafana_regexp//:regexp",
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
package release
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@ -12,70 +9,10 @@ import (
|
||||
|
||||
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
|
||||
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
|
||||
"github.com/sourcegraph/sourcegraph/internal/releaseregistry"
|
||||
"github.com/sourcegraph/sourcegraph/lib/output"
|
||||
)
|
||||
|
||||
const releaseRegistryEndpoint = "https://releaseregistry.sourcegraph.com/v1/"
|
||||
|
||||
type RegistryVersion struct {
|
||||
ID int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Public bool `json:"public"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
PromotedAt *time.Time `json:"promoted_at"`
|
||||
Version string `json:"version"`
|
||||
GitSHA string `json:"git_sha"`
|
||||
IsDevelopment bool `json:"is_development"`
|
||||
}
|
||||
|
||||
type releaseRegistryClient struct {
|
||||
endpoint string
|
||||
client http.Client
|
||||
}
|
||||
|
||||
func newReleaseRegistryClient(endpoint string) *releaseRegistryClient {
|
||||
return &releaseRegistryClient{
|
||||
endpoint: endpoint,
|
||||
client: http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *releaseRegistryClient) newRequest(method, path string) (*http.Request, error) {
|
||||
urlPath, err := url.JoinPath(r.endpoint, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, urlPath, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (r *releaseRegistryClient) ListVersions(ctx context.Context) ([]RegistryVersion, error) {
|
||||
req, err := r.newRequest(http.MethodGet, "releases")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
results := []RegistryVersion{}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&results)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
var listReleaseCommand = &cli.Command{
|
||||
Name: "list",
|
||||
Usage: "list versions from the release registry",
|
||||
@ -92,7 +29,7 @@ var listReleaseCommand = &cli.Command{
|
||||
}
|
||||
|
||||
func listRegistryVersions(cmd *cli.Context) error {
|
||||
client := newReleaseRegistryClient(releaseRegistryEndpoint)
|
||||
client := releaseregistry.NewClient(releaseregistry.Endpoint)
|
||||
|
||||
out := std.NewOutput(os.Stderr, false)
|
||||
pending := out.Pending(output.Line("", output.StylePending, "Fetching versions from the release registry..."))
|
||||
|
||||
8
internal/releaseregistry/BUILD.bazel
Normal file
8
internal/releaseregistry/BUILD.bazel
Normal file
@ -0,0 +1,8 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "releaseregistry",
|
||||
srcs = ["client.go"],
|
||||
importpath = "github.com/sourcegraph/sourcegraph/internal/releaseregistry",
|
||||
visibility = ["//:__subpackages__"],
|
||||
)
|
||||
4
internal/releaseregistry/README.md
Normal file
4
internal/releaseregistry/README.md
Normal file
@ -0,0 +1,4 @@
|
||||
# releaseregistry
|
||||
|
||||
A go client for <https://github.com/sourcegraph/releaseregistry>. Please see
|
||||
that repository for more details.
|
||||
70
internal/releaseregistry/client.go
Normal file
70
internal/releaseregistry/client.go
Normal file
@ -0,0 +1,70 @@
|
||||
package releaseregistry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
const Endpoint = "https://releaseregistry.sourcegraph.com/v1/"
|
||||
|
||||
type ReleaseInfo struct {
|
||||
ID int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Public bool `json:"public"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
PromotedAt *time.Time `json:"promoted_at"`
|
||||
Version string `json:"version"`
|
||||
GitSHA string `json:"git_sha"`
|
||||
IsDevelopment bool `json:"is_development"`
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
endpoint string
|
||||
client http.Client
|
||||
}
|
||||
|
||||
func NewClient(endpoint string) *Client {
|
||||
return &Client{
|
||||
endpoint: endpoint,
|
||||
client: http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Client) newRequest(ctx context.Context, method, path string) (*http.Request, error) {
|
||||
urlPath, err := url.JoinPath(r.endpoint, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, method, urlPath, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (r *Client) ListVersions(ctx context.Context) ([]ReleaseInfo, error) {
|
||||
req, err := r.newRequest(ctx, http.MethodGet, "releases")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
results := []ReleaseInfo{}
|
||||
|
||||
err = json.NewDecoder(resp.Body).Decode(&results)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user