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

feat: add github release command #19

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ uds-releaser release <flavor>

When running `uds-releaser release gitlab <flavor>` you are expected to have an environment variable set to a GitLab token that has write permissions for your current project. This defaults to `GITLAB_RELEASE_TOKEN` but can be changed with the `--token-var-name` flag.

### GitHub

When running `uds-releaser release github <flavor>` you are expected to have an environment variable set to a GitHub token that has write permissions for your current project. This defaults to `GITHUB_TOKEN` but can be changed with the `--token-var-name` flag.

## Configuration

UDS-Releaser can be configured using a YAML file named uds-releaser.yaml in your project's root directory.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/defenseunicorns/uds-cli v0.16.0
github.com/go-git/go-git/v5 v5.12.0
github.com/goccy/go-yaml v1.12.0
github.com/google/go-github/v66 v66.0.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
github.com/xanzy/go-gitlab v0.109.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4er
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v66 v66.0.0 h1:ADJsaXj9UotwdgK8/iFZtv7MLc8E8WBl62WLd/D/9+M=
github.com/google/go-github/v66 v66.0.0/go.mod h1:+4SO9Zkuyf8ytMj0csN1NR/5OTR+MfqPp8P8dVlcvY4=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
Expand Down
29 changes: 15 additions & 14 deletions src/cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ limitations under the License.
package cmd

import (
"github.com/defenseunicorns/uds-releaser/src/gitlab"
"github.com/defenseunicorns/uds-releaser/src/utils"
"github.com/defenseunicorns/uds-releaser/src/platforms"
"github.com/defenseunicorns/uds-releaser/src/platforms/github"
"github.com/defenseunicorns/uds-releaser/src/platforms/gitlab"
"github.com/spf13/cobra"
)

Expand All @@ -29,19 +30,17 @@ var gitlabCmd = &cobra.Command{
Short: "Create a tag and release on GitLab based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
releaserConfig, err := utils.LoadReleaserConfig(releaserDir)
if err != nil {
return err
}

currentFlavor, err := utils.GetFlavorConfig(args[0], releaserConfig)
if err != nil {
return err
}

rootCmd.SilenceUsage = true
return platforms.LoadAndTag(releaserDir, args[0], tokenVarName, gitlab.Platform{})
},
}

return gitlab.TagAndRelease(currentFlavor, tokenVarName)
// githubCmd represents the github command
var githubCmd = &cobra.Command{
Use: "github flavor",
Short: "Create a tag and release on GitHub based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return platforms.LoadAndTag(releaserDir, args[0], tokenVarName, github.Platform{})
},
}

Expand All @@ -54,5 +53,7 @@ var releaseCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(releaseCmd)
releaseCmd.AddCommand(gitlabCmd)
releaseCmd.AddCommand(githubCmd)
gitlabCmd.Flags().StringVarP(&tokenVarName, "token-var-name", "t", "GITLAB_RELEASE_TOKEN", "Environment variable name for GitLab token")
githubCmd.Flags().StringVarP(&tokenVarName, "token-var-name", "t", "GITHUB_TOKEN", "Environment variable name for GitHub token")
}
107 changes: 107 additions & 0 deletions src/platforms/github/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package github

import (
"context"
"fmt"
"os"
"regexp"
"time"

"github.com/defenseunicorns/uds-releaser/src/types"
"github.com/defenseunicorns/uds-releaser/src/utils"
github "github.com/google/go-github/v66/github"
)

type Platform struct{}

func (Platform) TagAndRelease(flavor types.Flavor, tokenVarName string) error {
remoteURL, _, ref, err := utils.GetRepoInfo()
if err != nil {
return err
}

// Create a new GitHub client
githubClient := github.NewClient(nil)

// Set the authentication token
githubClient = githubClient.WithAuthToken(os.Getenv(tokenVarName))

owner, repoName, err := getGithubOwnerAndRepo(remoteURL)
if err != nil {
return err
}

// Create the tag
zarfPackageName, err := utils.GetPackageName()
if err != nil {
return err
}

tagName := fmt.Sprintf("%s-%s", flavor.Version, flavor.Name)
releaseName := fmt.Sprintf("%s %s", zarfPackageName, tagName)

tag := createGitHubTag(tagName, releaseName, ref.Hash().String())

createdTag, _, err := githubClient.Git.CreateTag(context.Background(), owner, repoName, tag)
if err != nil {
return err
}

// Create a reference for the tag
tagRef := &github.Reference{
Ref: github.String("refs/tags/" + tagName),
Object: &github.GitObject{
SHA: createdTag.SHA,
},
}

_, _, err = githubClient.Git.CreateRef(context.Background(), owner, repoName, tagRef)
if err != nil {
return err
}

// Create the release
release := &github.RepositoryRelease{
TagName: github.String(tagName),
Name: github.String(releaseName),
Body: github.String(releaseName), //TODO @corang release notes
GenerateReleaseNotes: github.Bool(true),
}

_, _, err = githubClient.Repositories.CreateRelease(context.Background(), owner, repoName, release)
if err != nil {
return err
}
return nil
}

func createGitHubTag(tagName string, releaseName string, hash string) *github.Tag {
tag := &github.Tag{
Tag: github.String(tagName),
Message: github.String(releaseName),
Object: &github.GitObject{
SHA: github.String(hash),
Type: github.String("commit"),
},
Tagger: &github.CommitAuthor{
Name: github.String(os.Getenv("GITHUB_ACTOR")),
Email: github.String(os.Getenv("GITHUB_ACTOR") + "@users.noreply.github.com"),
Date: &github.Timestamp{Time: time.Now()},
},
}
return tag
}

func getGithubOwnerAndRepo(remoteURL string) (string, string, error) {
// Parse the GitHub owner and repository name from the remote URL
ownerRepoRegex := regexp.MustCompile(`github\.com[:/](.*)/(.*)\.git`)
matches := ownerRepoRegex.FindStringSubmatch(remoteURL)
if len(matches) != 3 {
return "", "", fmt.Errorf("could not parse GitHub owner and repository name from remote URL: %s", remoteURL)
}

owner := matches[1]
repo := matches[2]

return owner, repo, nil
}
42 changes: 42 additions & 0 deletions src/platforms/github/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package github

import (
"testing"

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

func testCreateGitHubTag(t *testing.T) {
// Create a new tag
tagName := "v1.0.0-uds.0-unicorn"
releaseName := "testing-package v1.0.0-uds.0-unicorn"
hash := "1234567890"

tag := createGitHubTag(tagName, releaseName, hash)

assert.Equal(t, tagName, *tag.Tag)
assert.Equal(t, releaseName, *tag.Message)
assert.Equal(t, hash, *tag.Object.SHA)
}

func testgetGithubOwnerAndRepo(t *testing.T) {
// Get the owner and repo from a remote URL
httpsRemoteURL := "https://github.com/defenseunicorns/uds-releaser.git"
sshRemoteURL := "git@github.com:defenseunicorns/uds-releaser.git"

owner, repo, err := getGithubOwnerAndRepo(httpsRemoteURL)
assert.NoError(t, err)
assert.Equal(t, "defenseunicorns", owner)
assert.Equal(t, "uds-releaser", repo)

owner, repo, err = getGithubOwnerAndRepo(sshRemoteURL)
assert.NoError(t, err)
assert.Equal(t, "defenseunicorns", owner)
assert.Equal(t, "uds-releaser", repo)

gitlabRemoteURL := "https://gitlab.com/defenseunicorns/uds-releaser.git"
owner, repo, err = getGithubOwnerAndRepo(gitlabRemoteURL)
assert.Error(t, err)
assert.Empty(t, owner)
assert.Empty(t, repo)
}
29 changes: 5 additions & 24 deletions src/gitlab/release.go → src/platforms/gitlab/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,29 @@ import (
gitlab "github.com/xanzy/go-gitlab"
)

var newGitlabClient = gitlab.NewClient
type Platform struct{}

var openRepo = utils.OpenRepo

var getPackageName = utils.GetPackageName

func TagAndRelease(flavor types.Flavor, tokenVarName string) error {
repo, err := openRepo()
if err != nil {
return err
}

remote, err := repo.Remote("origin")
func (Platform) TagAndRelease(flavor types.Flavor, tokenVarName string) error {
remoteURL, defaultBranch, _, err := utils.GetRepoInfo()
if err != nil {
return err
}

remoteURL := remote.Config().URLs[0]

// Parse the GitLab base URL from the remote URL
gitlabBaseURL, err := getGitlabBaseUrl(remoteURL)
if err != nil {
return err
}

// Get the default branch of the current repository
ref, err := repo.Head()
if err != nil {
return err
}

defaultBranch := ref.Name().Short()

fmt.Printf("Default branch: %s\n", defaultBranch)

// Create a new GitLab client
gitlabClient, err := newGitlabClient(os.Getenv(tokenVarName), gitlab.WithBaseURL(gitlabBaseURL))
gitlabClient, err := gitlab.NewClient(os.Getenv(tokenVarName), gitlab.WithBaseURL(gitlabBaseURL))
if err != nil {
return err
}

zarfPackageName, err := getPackageName()
zarfPackageName, err := utils.GetPackageName()
if err != nil {
return err
}
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions src/platforms/platform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package platforms

import (
"github.com/defenseunicorns/uds-releaser/src/types"
"github.com/defenseunicorns/uds-releaser/src/utils"
)

type Platform interface {
TagAndRelease(flavor types.Flavor, tokenVarName string) error
}

func LoadAndTag(releaserDir, flavor, tokenVarName string, platform Platform) error {
releaserConfig, err := utils.LoadReleaserConfig(releaserDir)
if err != nil {
return err
}

currentFlavor, err := utils.GetFlavorConfig(flavor, releaserConfig)
if err != nil {
return err
}

return platform.TagAndRelease(currentFlavor, tokenVarName)
}
22 changes: 22 additions & 0 deletions src/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,25 @@ func DoesTagExist(tag string) (bool, error) {
func OpenRepo() (*git.Repository, error) {
return git.PlainOpen(".")
}

func GetRepoInfo() (remoteURL string, defaultBranch string, ref *plumbing.Reference, err error) {
repo, err := OpenRepo()
if err != nil {
return "", "", ref, err
}

remote, err := repo.Remote("origin")
if err != nil {
return "", "", ref, err
}

remoteURL = remote.Config().URLs[0]

ref, err = repo.Head()
if err != nil {
return "", "", ref, err
}

defaultBranch = ref.Name().Short()
return remoteURL, defaultBranch, ref, nil
}
Loading