Skip to content

Commit

Permalink
add tag ui/dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbreno committed Aug 29, 2024
1 parent ee09f33 commit 6e81e6d
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 6 deletions.
60 changes: 60 additions & 0 deletions cmd/release/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"time"

"github.com/rancher/ecm-distro-tools/release/dashboard"
"github.com/rancher/ecm-distro-tools/release/k3s"
"github.com/rancher/ecm-distro-tools/release/rancher"
"github.com/rancher/ecm-distro-tools/release/rke2"
"github.com/rancher/ecm-distro-tools/release/ui"
"github.com/rancher/ecm-distro-tools/repository"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -204,13 +206,71 @@ var systemAgentInstallerK3sTagSubCmd = &cobra.Command{
},
}

var uiTagSubCmd = &cobra.Command{
Use: "ui [ga,rc] [version]",
Short: "Tag ui releases",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("expected at least two arguments: [ga,rc] [version]")
}
rc, err := releaseTypePreRelease(args[0])
if err != nil {
return err
}
tag := args[1]
uiRelease, found := rootConfig.UI.Versions[tag]
if !found {
return errors.New("verify your config file, version not found: " + tag)
}
ctx := context.Background()
ghClient := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)
opts := &repository.CreateReleaseOpts{
Tag: tag,
Repo: "ui",
Owner: uiRelease.UIRepoOwner,
Branch: uiRelease.ReleaseBranch,
}
return ui.CreateRelease(ctx, ghClient, &uiRelease, opts, rc)
},
}

var dashboardTagSubCmd = &cobra.Command{
Use: "dashboard [ga,rc] [version]",
Short: "Tag dashboard releases",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("expected at least two arguments: [ga,rc] [version]")
}
rc, err := releaseTypePreRelease(args[0])
if err != nil {
return err
}
tag := args[1]
dashboardRelease, found := rootConfig.Dashboard.Versions[tag]
if !found {
return errors.New("verify your config file, version not found: " + tag)
}
ctx := context.Background()
ghClient := repository.NewGithub(ctx, rootConfig.Auth.GithubToken)
opts := &repository.CreateReleaseOpts{
Tag: tag,
Repo: "dashboard",
Owner: dashboardRelease.DashboardRepoOwner,
Branch: dashboardRelease.ReleaseBranch,
}
return dashboard.CreateRelease(ctx, ghClient, &dashboardRelease, opts, rc)
},
}

func init() {
rootCmd.AddCommand(tagCmd)

tagCmd.AddCommand(k3sTagSubCmd)
tagCmd.AddCommand(rke2TagSubCmd)
tagCmd.AddCommand(rancherTagSubCmd)
tagCmd.AddCommand(systemAgentInstallerK3sTagSubCmd)
tagCmd.AddCommand(uiTagSubCmd)
tagCmd.AddCommand(dashboardTagSubCmd)

// rke2
tagRKE2Flags.AlpineVersion = rke2TagSubCmd.Flags().StringP("alpine-version", "a", "", "Alpine version")
Expand Down
40 changes: 34 additions & 6 deletions cmd/release/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ type RancherRelease struct {
SkipStatusCheck bool `json:"skip_status_check"`
}

type UIRelease struct {
UIRepoOwner string `json:"ui_repo_owner" validate:"required"`
UIRepoName string `json:"ui_repo_name"`
PreviousTag string `json:"previous_tag"`
ReleaseBranch string `json:"release_branch" validate:"required"`
DryRun bool `json:"dry_run"`
}

type DashboardRelease struct {
DashboardRepoOwner string `json:"dashboard_repo_owner" validate:"required"`
DashboardRepoName string `json:"dashboard_repo_name"`
PreviousTag string `json:"previous_tag"`
ReleaseBranch string `json:"release_branch" validate:"required"`
DryRun bool `json:"dry_run"`
}

// RKE2
type RKE2 struct {
Versions []string `json:"versions"`
Expand Down Expand Up @@ -70,6 +86,16 @@ type Rancher struct {
Versions map[string]RancherRelease `json:"versions" validate:"dive"`
}

// UI
type UI struct {
Versions map[string]UIRelease `json:"versions" validate:"dive"`
}

// Dashboard
type Dashboard struct {
Versions map[string]DashboardRelease `json:"versions" validate:"dive"`
}

// Auth
type Auth struct {
GithubToken string `json:"github_token"`
Expand All @@ -78,12 +104,14 @@ type Auth struct {

// Config
type Config struct {
User *User `json:"user"`
K3s *K3s `json:"k3s"`
Rancher *Rancher `json:"rancher"`
RKE2 *RKE2 `json:"rke2"`
Charts *ChartsRelease `json:"charts"`
Auth *Auth `json:"auth"`
User *User `json:"user"`
K3s *K3s `json:"k3s"`
UI *UI `json:"ui"`
Dashboard *Dashboard `json:"dashboard"`
Rancher *Rancher `json:"rancher"`
RKE2 *RKE2 `json:"rke2"`
Charts *ChartsRelease `json:"charts"`
Auth *Auth `json:"auth"`
}

// OpenOnEditor opens the given config file on the user's default text editor.
Expand Down
87 changes: 87 additions & 0 deletions release/dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dashboard

import (
"context"
"errors"
"fmt"
"strconv"
"strings"

"github.com/google/go-github/v39/github"
ecmConfig "github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
"golang.org/x/mod/semver"
)

const (
dashboardOrg = "rancher"
dashboardRepo = "dashboard"
dashboardImagesBaseURL = "https://github.com/" + dashboardOrg + "/" + dashboardRepo + "/releases"
)

func CreateRelease(ctx context.Context, client *github.Client, r *ecmConfig.DashboardRelease, opts *repository.CreateReleaseOpts, rc bool) error {
fmt.Println("validating tag")
if !semver.IsValid(opts.Tag) {
return errors.New("tag isn't a valid semver: " + opts.Tag)
}

if r.DashboardRepoName != "" {
opts.Repo = r.DashboardRepoName
}

latestRC, err := release.LatestRC(ctx, opts.Owner, opts.Repo, opts.Tag, opts.Tag, client)
if err != nil {
return err
}

if rc {
latestRCNumber := 1
if latestRC != nil {
// v2.9.0-rcN
_, trimmedRCNumber, found := strings.Cut(*latestRC, "-rc")
if !found {
return errors.New("failed to parse rc number from " + *latestRC)
}
currentRCNumber, err := strconv.Atoi(trimmedRCNumber)
if err != nil {
return err
}
latestRCNumber = currentRCNumber + 1
} else {
// this means it would be the first RC tag
latestRC = new(string)
*latestRC = opts.Tag + "-rc1"
}
opts.Tag = fmt.Sprintf("%s-rc%d", opts.Tag, latestRCNumber)
}

opts.Name = opts.Tag
opts.Prerelease = true
opts.Draft = !rc
opts.ReleaseNotes = ""

if !rc {
fmt.Printf("release.GenReleaseNotes(ctx, %s, %s, %s, %s, client)", opts.Owner, opts.Repo, opts.Branch, r.PreviousTag)
buff, err := release.GenReleaseNotes(ctx, opts.Owner, opts.Repo, opts.Branch, r.PreviousTag, client)
if err != nil {
return err
}
opts.ReleaseNotes = buff.String()
}

fmt.Printf("create release options: %+v\n", *opts)

if r.DryRun {
fmt.Println("dry run, skipping creating release")
return nil
}

createdRelease, err := repository.CreateRelease(ctx, client, opts)
if err != nil {
return err
}

fmt.Println("release created: " + *createdRelease.HTMLURL)
return nil
}
87 changes: 87 additions & 0 deletions release/ui/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ui

import (
"context"
"errors"
"fmt"
"strconv"
"strings"

"github.com/google/go-github/v39/github"
ecmConfig "github.com/rancher/ecm-distro-tools/cmd/release/config"
"github.com/rancher/ecm-distro-tools/release"
"github.com/rancher/ecm-distro-tools/repository"
"golang.org/x/mod/semver"
)

const (
uiOrg = "rancher"
uiRepo = "ui"
uiImagesBaseURL = "https://github.com/" + uiOrg + "/" + uiRepo + "/releases"
)

func CreateRelease(ctx context.Context, client *github.Client, r *ecmConfig.UIRelease, opts *repository.CreateReleaseOpts, rc bool) error {
fmt.Println("validating tag")
if !semver.IsValid(opts.Tag) {
return errors.New("tag isn't a valid semver: " + opts.Tag)
}

if r.UIRepoName != "" {
opts.Repo = r.UIRepoName
}

latestRC, err := release.LatestRC(ctx, opts.Owner, opts.Repo, opts.Tag, opts.Tag, client)
if err != nil {
return err
}

if rc {
latestRCNumber := 1
if latestRC != nil {
// v2.9.0-rcN
_, trimmedRCNumber, found := strings.Cut(*latestRC, "-rc")
if !found {
return errors.New("failed to parse rc number from " + *latestRC)
}
currentRCNumber, err := strconv.Atoi(trimmedRCNumber)
if err != nil {
return err
}
latestRCNumber = currentRCNumber + 1
} else {
// this means it would be the first RC tag
latestRC = new(string)
*latestRC = opts.Tag + "-rc1"
}
opts.Tag = fmt.Sprintf("%s-rc%d", opts.Tag, latestRCNumber)
}

opts.Name = opts.Tag
opts.Prerelease = true
opts.Draft = !rc
opts.ReleaseNotes = ""

if !rc {
fmt.Printf("release.GenReleaseNotes(ctx, %s, %s, %s, %s, client)", opts.Owner, opts.Repo, opts.Branch, r.PreviousTag)
buff, err := release.GenReleaseNotes(ctx, opts.Owner, opts.Repo, opts.Branch, r.PreviousTag, client)
if err != nil {
return err
}
opts.ReleaseNotes = buff.String()
}

fmt.Printf("create release options: %+v\n", *opts)

if r.DryRun {
fmt.Println("dry run, skipping creating release")
return nil
}

createdRelease, err := repository.CreateRelease(ctx, client, opts)
if err != nil {
return err
}

fmt.Println("release created: " + *createdRelease.HTMLURL)
return nil
}

0 comments on commit 6e81e6d

Please sign in to comment.