Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sdk get releases from revision #1277

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `Client`
- `GetReleasesForGitReference` returns all releases for the specified provider and from the specified git reference.
- `GetReleaseForGitReference` returns a release with the specified release version for the specified provider and from
the specified git reference.

## [0.3.0] - 2024-06-12

### Added
Expand Down
83 changes: 79 additions & 4 deletions sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"fmt"
"io"
"net/http"
"path"
"strings"

"sigs.k8s.io/yaml"

. "github.com/giantswarm/releases/sdk/api/v1alpha1"

"github.com/giantswarm/microerror"
"github.com/google/go-github/v62/github"
"sigs.k8s.io/yaml"
)

type Client struct {
Expand Down Expand Up @@ -76,6 +74,83 @@ func (c *Client) GetRelease(ctx context.Context, provider Provider, releaseVersi
return release, nil
}

// GetReleasesForGitReference returns all releases for the specified provider and from the specified git reference.
//
// Currently, the only supported provider is "aws". Git reference can be any commit, branch or tag.
func (c *Client) GetReleasesForGitReference(ctx context.Context, provider Provider, gitReference string) ([]Release, error) {
providerDirectory, err := getProviderDirectory(provider)
if err != nil {
return nil, microerror.Mask(err)
}
opts := &github.RepositoryContentGetOptions{
Ref: gitReference,
}
_, directoryContent, _, err := c.gitHubClient.Repositories.GetContents(ctx, GiantSwarmGitHubOrg, ReleasesRepo, providerDirectory, opts)
if err != nil {
return nil, microerror.Mask(err)
}

var releases []Release
for _, item := range directoryContent {
if item == nil || item.GetType() != "dir" {
continue
}
releaseDirectoryName := item.GetName()
if releaseDirectoryName == "" || releaseDirectoryName == archivedDirectory {
continue
}
_, err = semver.NewVersion(releaseDirectoryName)
if err != nil {
// directory name is not a valid semver version, so treating as unknown directory and skipping
continue
}
release, err := c.GetReleaseForGitReference(ctx, provider, releaseDirectoryName, gitReference)
if err != nil {
return nil, microerror.Mask(err)
}
releases = append(releases, *release)
}

return releases, nil
}

// GetReleaseForGitReference returns a release with the specified release version for the specified provider and from
// the specified git reference.
//
// Currently, the only supported provider is "aws". Release version can contain the 'v' prefix, but it doesn't have to.
// Git reference can be any commit, branch or tag.
func (c *Client) GetReleaseForGitReference(ctx context.Context, provider Provider, releaseVersion, gitReference string) (*Release, error) {
// First we get GitHub release for the specified provider and release version.
releaseVersion = strings.TrimPrefix(releaseVersion, "v")
providerDirectory, err := getProviderDirectory(provider)
if err != nil {
return nil, microerror.Mask(err)
}
releaseManifestPath := path.Join(providerDirectory, fmt.Sprintf("v%s", releaseVersion), ReleaseManifestFileName)
opts := &github.RepositoryContentGetOptions{
Ref: gitReference,
}
fileContentObject, _, _, err := c.gitHubClient.Repositories.GetContents(ctx, GiantSwarmGitHubOrg, ReleasesRepo, releaseManifestPath, opts)
if err != nil {
return nil, microerror.Mask(err)
}
fileContent, err := fileContentObject.GetContent()
if err != nil {
return nil, microerror.Mask(err)
}
if fileContent == "" {
return nil, microerror.Maskf(MissingReleaseManifestError, "release manifest contents not found at path '%s' for git reference '%s'", releaseManifestPath, gitReference)
}

release := &Release{}
err = yaml.Unmarshal([]byte(fileContent), release)
if err != nil {
return nil, microerror.Mask(err)
}

return release, nil
}

// GetLatestRelease returns the latest release for the specified provider.
func (c *Client) GetLatestRelease(ctx context.Context, provider Provider) (*Release, error) {
const releasesPerRequest = 30
Expand Down
41 changes: 41 additions & 0 deletions sdk/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ var (

//go:embed testdata/capa/v25.1.0-demo.0/release.yaml
v2510ReleaseManifest string

//go:embed testdata/capa/github_get_capa_for_git.json
capaDirectoryResponse string

//go:embed testdata/capa/v25.0.0/release_yaml_content_response.json
v25ReleaseManifestContentResponse string

//go:embed testdata/capa/v25.0.0-alpha.3/release_yaml_content_response.json
v25Alpha3ReleaseManifestContentResponse string
)

var _ = Describe("Client", func() {
Expand Down Expand Up @@ -117,6 +126,23 @@ var _ = Describe("Client", func() {
Expect(resultReleaseVersion).To(Equal(expectedLatestReleaseVersion))
})

It("Gets all releases for the specified provider and git reference", func() {
ctx := context.Background()
releases, err := releasesClient.GetReleasesForGitReference(ctx, ProviderAws, "add-capa-v25.0.0")
Expect(err).NotTo(HaveOccurred())

expectedReleases := []string{
"25.0.0-alpha.3",
"25.0.0",
}

for i, release := range releases {
resultReleaseVersion, err := release.GetVersion()
Expect(err).NotTo(HaveOccurred())
Expect(resultReleaseVersion).To(Equal(expectedReleases[i]))
}
})

AfterEach(func() {
server.Close()
})
Expand Down Expand Up @@ -144,6 +170,21 @@ func createAndStartTestServer() *httptest.Server {
_, err := rw.Write([]byte(v2510ReleaseManifest))
Expect(err).NotTo(HaveOccurred())
})
mux.HandleFunc("/api/v3/repos/giantswarm/releases/contents/capa", func(rw http.ResponseWriter, req *http.Request) {
// return GitHub response JSON
_, err := rw.Write([]byte(capaDirectoryResponse))
Expect(err).NotTo(HaveOccurred())
})
mux.HandleFunc("/api/v3/repos/giantswarm/releases/contents/capa/v25.0.0/release.yaml", func(rw http.ResponseWriter, req *http.Request) {
// return GitHub response JSON
_, err := rw.Write([]byte(v25ReleaseManifestContentResponse))
Expect(err).NotTo(HaveOccurred())
})
mux.HandleFunc("/api/v3/repos/giantswarm/releases/contents/capa/v25.0.0-alpha.3/release.yaml", func(rw http.ResponseWriter, req *http.Request) {
// return GitHub response JSON
_, err := rw.Write([]byte(v25Alpha3ReleaseManifestContentResponse))
Expect(err).NotTo(HaveOccurred())
})

// create test server
server := httptest.NewUnstartedServer(mux)
Expand Down
4 changes: 4 additions & 0 deletions sdk/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var InvalidConfigError = &microerror.Error{
Kind: "InvalidConfigError",
}

var InvalidProviderError = &microerror.Error{
Kind: "InvalidProviderError",
}

var ReleaseNotFoundError = &microerror.Error{
Kind: "ReleaseNotFoundError",
}
Expand Down
20 changes: 20 additions & 0 deletions sdk/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sdk

import (
"github.com/giantswarm/microerror"

. "github.com/giantswarm/releases/sdk/api/v1alpha1"
)

const (
archivedDirectory = "archived"
)

func getProviderDirectory(provider Provider) (string, error) {
switch provider {
case ProviderAws:
return "capa", nil
}

return "", microerror.Maskf(InvalidProviderError, "unknown provider: %s", provider)
}
66 changes: 66 additions & 0 deletions sdk/testdata/capa/github_get_capa_for_git.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[
{
"name": "kustomization.yaml",
"path": "capa/kustomization.yaml",
"sha": "94632e6938d4b0558f44ae503ab9d27ba7660d64",
"size": 246,
"url": "https://api.github.com/repos/giantswarm/releases/contents/capa/kustomization.yaml?ref=add-capa-v25.0.0",
"html_url": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/kustomization.yaml",
"git_url": "https://api.github.com/repos/giantswarm/releases/git/blobs/94632e6938d4b0558f44ae503ab9d27ba7660d64",
"download_url": "https://raw.githubusercontent.com/giantswarm/releases/add-capa-v25.0.0/capa/kustomization.yaml",
"type": "file",
"_links": {
"self": "https://api.github.com/repos/giantswarm/releases/contents/capa/kustomization.yaml?ref=add-capa-v25.0.0",
"git": "https://api.github.com/repos/giantswarm/releases/git/blobs/94632e6938d4b0558f44ae503ab9d27ba7660d64",
"html": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/kustomization.yaml"
}
},
{
"name": "releaseNotesTransformer.yaml",
"path": "capa/releaseNotesTransformer.yaml",
"sha": "bf03d340e5c4aefa7da96bb36d45387bc6c4ebfa",
"size": 203,
"url": "https://api.github.com/repos/giantswarm/releases/contents/capa/releaseNotesTransformer.yaml?ref=add-capa-v25.0.0",
"html_url": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/releaseNotesTransformer.yaml",
"git_url": "https://api.github.com/repos/giantswarm/releases/git/blobs/bf03d340e5c4aefa7da96bb36d45387bc6c4ebfa",
"download_url": "https://raw.githubusercontent.com/giantswarm/releases/add-capa-v25.0.0/capa/releaseNotesTransformer.yaml",
"type": "file",
"_links": {
"self": "https://api.github.com/repos/giantswarm/releases/contents/capa/releaseNotesTransformer.yaml?ref=add-capa-v25.0.0",
"git": "https://api.github.com/repos/giantswarm/releases/git/blobs/bf03d340e5c4aefa7da96bb36d45387bc6c4ebfa",
"html": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/releaseNotesTransformer.yaml"
}
},
{
"name": "v25.0.0-alpha.3",
"path": "capa/v25.0.0-alpha.3",
"sha": "9991adcdbea991e6fd68bbf137722511942840ee",
"size": 0,
"url": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0-alpha.3?ref=add-capa-v25.0.0",
"html_url": "https://github.com/giantswarm/releases/tree/add-capa-v25.0.0/capa/v25.0.0-alpha.3",
"git_url": "https://api.github.com/repos/giantswarm/releases/git/trees/9991adcdbea991e6fd68bbf137722511942840ee",
"download_url": null,
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0-alpha.3?ref=add-capa-v25.0.0",
"git": "https://api.github.com/repos/giantswarm/releases/git/trees/9991adcdbea991e6fd68bbf137722511942840ee",
"html": "https://github.com/giantswarm/releases/tree/add-capa-v25.0.0/capa/v25.0.0-alpha.3"
}
},
{
"name": "v25.0.0",
"path": "capa/v25.0.0",
"sha": "ced6727c2b8c72ccbecf1560d467d80142d3f6a6",
"size": 0,
"url": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0?ref=add-capa-v25.0.0",
"html_url": "https://github.com/giantswarm/releases/tree/add-capa-v25.0.0/capa/v25.0.0",
"git_url": "https://api.github.com/repos/giantswarm/releases/git/trees/ced6727c2b8c72ccbecf1560d467d80142d3f6a6",
"download_url": null,
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0?ref=add-capa-v25.0.0",
"git": "https://api.github.com/repos/giantswarm/releases/git/trees/ced6727c2b8c72ccbecf1560d467d80142d3f6a6",
"html": "https://github.com/giantswarm/releases/tree/add-capa-v25.0.0/capa/v25.0.0"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "release.yaml",
"path": "capa/v25.0.0-alpha.3/release.yaml",
"sha": "5f186e1cb0df528c689cc6e3356527882f44c3c3",
"size": 2669,
"url": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0-alpha.3/release.yaml?ref=add-capa-v25.0.0",
"html_url": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/v25.0.0-alpha.3/release.yaml",
"git_url": "https://api.github.com/repos/giantswarm/releases/git/blobs/5f186e1cb0df528c689cc6e3356527882f44c3c3",
"download_url": "https://raw.githubusercontent.com/giantswarm/releases/add-capa-v25.0.0/capa/v25.0.0-alpha.3/release.yaml",
"type": "file",
"content": "YXBpVmVyc2lvbjogcmVsZWFzZS5naWFudHN3YXJtLmlvL3YxYWxwaGExCmtp\nbmQ6IFJlbGVhc2UKbWV0YWRhdGE6CiAgbmFtZTogYXdzLTI1LjAuMC1hbHBo\nYS4zCnNwZWM6CiAgYXBwczoKICAtIG5hbWU6IGF3cy1lYnMtY3NpLWRyaXZl\ncgogICAgdmVyc2lvbjogMi4zMC4xCiAgICBkZXBlbmRzT246CiAgICAtIGNs\nb3VkLXByb3ZpZGVyLWF3cwogIC0gbmFtZTogYXdzLWVicy1jc2ktZHJpdmVy\nLXNlcnZpY2Vtb25pdG9ycwogICAgdmVyc2lvbjogMC4xLjAKICAgIGRlcGVu\nZHNPbjoKICAgIC0gY2Vyd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2ZQo=\n",
"encoding": "base64",
"_links": {
"self": "https://api.github.com/repos/giantswarm/releases/contents/capa/v25.0.0-alpha.3/release.yaml?ref=add-capa-v25.0.0",
"git": "https://api.github.com/repos/giantswarm/releases/git/blobs/5f186e1cb0df528c689cc6e3356527882f44c3c3",
"html": "https://github.com/giantswarm/releases/blob/add-capa-v25.0.0/capa/v25.0.0-alpha.3/release.yaml"
}
}
Loading
Loading