Skip to content

Commit

Permalink
wip: mvp?
Browse files Browse the repository at this point in the history
  • Loading branch information
corang committed Sep 25, 2024
1 parent 27c8334 commit 966589f
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 80 deletions.
42 changes: 30 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,57 @@ module github.com/defenseunicorns/uds-releaser

go 1.22.6

require github.com/spf13/cobra v1.8.1
require (
github.com/defenseunicorns/uds-cli v0.16.0
github.com/spf13/cobra v1.8.1
github.com/zarf-dev/zarf v0.39.0
)

require (
dario.cat/mergo v1.0.0 // indirect
dario.cat/mergo v1.0.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/cyphar/filepath-securejoin v0.3.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/invopop/jsonschema v0.12.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.25.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/go-git/go-git/v5 v5.12.0
github.com/goccy/go-yaml v1.12.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/go-gitlab v0.109.0
)
105 changes: 69 additions & 36 deletions go.sum

Large diffs are not rendered by default.

45 changes: 20 additions & 25 deletions src/cmd/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
Copyright © 2024 The Authors of uds-releaser
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 cmd

import (
"fmt"

"github.com/defenseunicorns/uds-releaser/src/gitlab"
"github.com/defenseunicorns/uds-releaser/src/utils"
"github.com/defenseunicorns/uds-releaser/src/version"
"github.com/spf13/cobra"
)

Expand All @@ -17,11 +27,11 @@ var gitlabCmd = &cobra.Command{
Short: "Collection of commands for releasing on GitLab",
}

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version [ flavor ]",
Short: "Mutate version fields in the zarf.yaml and uds-bundle.yaml based on flavor",
Args: cobra.ExactArgs(1),
// releaseCmd represents the release command
var releaseCmd = &cobra.Command{
Use: "release [ flavor ]",
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()
if err != nil {
Expand All @@ -33,28 +43,13 @@ var versionCmd = &cobra.Command{
return err
}

err = version.MutateYamls(currentFlavor)
if err != nil {
return err
}
return nil
},
}

// releaseCmd represents the release command
var releaseCmd = &cobra.Command{
Use: "release [ flavor ]",
Short: "Create a tag and release on GitLab based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("release called")
err = gitlab.TagAndRelease(currentFlavor)
return nil
},
}

func init() {
rootCmd.AddCommand(gitlabCmd)
gitlabCmd.AddCommand(versionCmd)
gitlabCmd.AddCommand(releaseCmd)

// Here you will define your flags and configuration settings.
Expand Down
60 changes: 60 additions & 0 deletions src/cmd/mutate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright © 2024 The Authors of uds-releaser
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 cmd

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

// mutateCmd represents the mutate command
var mutateCmd = &cobra.Command{
Use: "mutate [ flavor ]",
Short: "Mutate version fields in the zarf.yaml and uds-bundle.yaml based on flavor",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
releaserConfig, err := utils.LoadReleaserConfig()
if err != nil {
return err
}

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

err = version.MutateYamls(currentFlavor)
if err != nil {
return err
}
return nil
},
}

func init() {
rootCmd.AddCommand(mutateCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// gitlabCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// gitlabCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
90 changes: 90 additions & 0 deletions src/gitlab/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package gitlab

import (
"errors"
"fmt"
"os"
"strings"

"github.com/defenseunicorns/uds-releaser/src/types"
"github.com/defenseunicorns/uds-releaser/src/utils"
gitlab "github.com/xanzy/go-gitlab"
)

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

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

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

// Parse the GitLab base URL from the remote URL
var gitlabBaseURL string
if strings.Contains(remoteURL, "gitlab.com") {
gitlabBaseURL = "https://gitlab.com/api/v4"
} else {
// Extract the base URL for self-hosted GitLab instances
parts := strings.Split(remoteURL, "/")
if len(parts) > 2 {
gitlabBaseURL = fmt.Sprintf("https://%s/api/v4", parts[2])
} else {
return errors.New(fmt.Sprintf("error parsing gitlab base url from remote url: %s", remoteURL))
}
}

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

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

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

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

// setup the release options
releaseOpts := &gitlab.CreateReleaseOptions{
Name: gitlab.Ptr(fmt.Sprintf("%s %s-%s", zarfPackageName, flavor.Version, flavor.Name)),
TagName: gitlab.Ptr(fmt.Sprintf("%s-%s", flavor.Version, flavor.Name)),
Description: gitlab.Ptr("Release description"),
Ref: gitlab.Ptr(defaultBranch),
Assets: &gitlab.ReleaseAssetsOptions{
Links: []*gitlab.ReleaseAssetLinkOptions{
{
Name: gitlab.Ptr("zarf.yaml"), // TODO
URL: gitlab.Ptr("https://example.com/zarf.yaml"), // TODO
LinkType: gitlab.Ptr(gitlab.PackageLinkType),
},
{
Name: gitlab.Ptr("uds-bundle.yaml"), // TODO
URL: gitlab.Ptr("https://example.com/uds-bundle.yaml"), // TODO
},
},
},
}

// Create the release
release, _, err := gitlabClient.Releases.CreateRelease(os.Getenv("CI_PROJECT_ID"), releaseOpts)
if err != nil {
return err
}

fmt.Printf("Release %s created\n", release.Name)

return nil
}
2 changes: 1 addition & 1 deletion src/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Flavor struct {
Version string `yaml:"version"`
PublishBundle bool `yaml:"publishBundle,omitempty,default=false"`
PublishPackageUrl string `yaml:"publishPackageUrl"`
PublishBundleUrl string `yaml:"publishBundleUrl"`
PublishBundleUrl string `yaml:"publishBundleUrl,omitempty"`
}

type ReleaserConfig struct {
Expand Down
10 changes: 9 additions & 1 deletion src/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func DoesTagExist(tag string) (bool, error) {
repo, err := git.PlainOpen(".")
repo, err := OpenRepo()
if err != nil {
return false, err
}
Expand All @@ -27,4 +27,12 @@ func DoesTagExist(tag string) (bool, error) {
return tagExists, err
}
return tagExists, nil
}

func OpenRepo() (*git.Repository, error) {
repo, err := git.PlainOpen(".")
if err != nil {
return nil, err
}
return repo, nil
}
15 changes: 15 additions & 0 deletions src/utils/zarf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import (
zarf "github.com/zarf-dev/zarf/src/api/v1alpha1"
)

func GetPackageName() (string, error) {
var zarfPackage zarf.ZarfPackage
err := LoadYaml("zarf.yaml", &zarfPackage)
if err != nil {
return "", err
}

return zarfPackage.Metadata.Name, nil
}
Loading

0 comments on commit 966589f

Please sign in to comment.