Repository navigation
ROX-35278: add get-merge-window, list-merge-windows commands #70
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
Draft
tommartensen
wants to merge
4
commits into
main
Choose a base branch
from
tm/merge-window-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ac15fe
add get-merge-window and list-merge-windows commands
tommartensen d9e8842
check-release-pr: fail if the merge window is not open
tommartensen e9091b6
propagate non-404 GetContents errors and reject directory responses
tommartensen 94a5b46
test: lock in merge window output serialization
tommartensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stackrox/relacs/cmd/relacs/common" | ||
| "github.com/stackrox/relacs/pkg/client" | ||
| "github.com/stackrox/relacs/pkg/client/konflux" | ||
| "github.com/stackrox/relacs/pkg/config" | ||
| "github.com/stackrox/relacs/pkg/github" | ||
| ) | ||
|
|
||
| type GetMergeWindowOutput struct { | ||
| ProductStream konflux.ProductStream `json:"product-stream" yaml:"product-stream"` | ||
| Status string `json:"status" yaml:"status"` | ||
| } | ||
|
|
||
| func (o GetMergeWindowOutput) String() string { | ||
| return fmt.Sprintf("%s: %s", o.ProductStream.String(), o.Status) | ||
| } | ||
|
|
||
| var getMergeWindowCmd = &cobra.Command{ | ||
| Use: "get-merge-window", | ||
| Short: "Get the merge window status for a given product stream", | ||
| Long: `Get the merge window status for a given product stream`, | ||
| RunE: common.RunCommand(RunGetMergeWindow), | ||
| } | ||
|
|
||
| func init() { | ||
| getMergeWindowCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") | ||
| getMergeWindowCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") | ||
| getMergeWindowCmd.Flags().String("product-stream", "", "Product stream (required)") | ||
|
|
||
| //nolint:errcheck | ||
| getMergeWindowCmd.MarkFlagRequired("product-stream") | ||
| } | ||
|
|
||
| // RunGetMergeWindow is the main function of this command and exported for testing purposes. | ||
| func RunGetMergeWindow(cmd *cobra.Command, _ []string, c *client.Client) (any, error) { | ||
| ctx := cmd.Context() | ||
| org, _ := cmd.Flags().GetString("org") | ||
| repo, _ := cmd.Flags().GetString("repo") | ||
| productStreamInput, _ := cmd.Flags().GetString("product-stream") | ||
|
|
||
| productStream, err := konflux.NewProductStream(productStreamInput) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| mergeWindowStatus, err := github.GetMergeWindowStatus(ctx, c, org, repo, productStream) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return GetMergeWindowOutput{ | ||
| ProductStream: productStream, | ||
| Status: mergeWindowStatus, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/olekukonko/tablewriter" | ||
| "github.com/olekukonko/tablewriter/renderer" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stackrox/relacs/cmd/relacs/common" | ||
| "github.com/stackrox/relacs/pkg/client" | ||
| "github.com/stackrox/relacs/pkg/client/konflux" | ||
| "github.com/stackrox/relacs/pkg/config" | ||
| "github.com/stackrox/relacs/pkg/github" | ||
| ) | ||
|
|
||
| type ListMergeWindowsOutput struct { | ||
| SupportedProductStreams []konflux.ProductStream `json:"supported-product-streams" yaml:"supported-product-streams"` | ||
| MergeWindowStatuses map[konflux.ProductStream]string `json:"merge-window-statuses" yaml:"merge-window-statuses"` | ||
| } | ||
|
|
||
| func (o ListMergeWindowsOutput) String() string { | ||
| var buf strings.Builder | ||
| fmt.Fprintf(&buf, "# Merge Window Statuses\n\n%s", renderMergeWindowStatusesTable(o)) | ||
| return buf.String() | ||
| } | ||
|
|
||
| func renderMergeWindowStatusesTable(output ListMergeWindowsOutput) string { | ||
| if len(output.SupportedProductStreams) == 0 { | ||
| return " No supported product streams found.\n" | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| table := tablewriter.NewTable(&buf, | ||
| tablewriter.WithHeader([]string{"Product Stream", "Status"}), | ||
| tablewriter.WithRenderer(renderer.NewMarkdown()), | ||
| ) | ||
|
|
||
| for _, productStream := range output.SupportedProductStreams { | ||
| //nolint:errcheck | ||
| table.Append([]string{productStream.String(), output.MergeWindowStatuses[productStream]}) | ||
| } | ||
|
|
||
| //nolint:errcheck | ||
| table.Render() | ||
| return buf.String() | ||
| } | ||
|
|
||
| var listMergeWindowsCmd = &cobra.Command{ | ||
| Use: "list-merge-windows", | ||
| Short: "List merge window statuses for all supported product streams", | ||
| Long: `List merge window statuses for all supported product streams in different formats`, | ||
| RunE: common.RunCommand(RunListMergeWindows), | ||
| } | ||
|
|
||
| func init() { | ||
| listMergeWindowsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") | ||
| listMergeWindowsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") | ||
| } | ||
|
|
||
| // RunListMergeWindows is the main function of this command and exported for testing purposes. | ||
| func RunListMergeWindows(cmd *cobra.Command, _ []string, c *client.Client) (any, error) { | ||
| ctx := cmd.Context() | ||
| org, _ := cmd.Flags().GetString("org") | ||
| repo, _ := cmd.Flags().GetString("repo") | ||
|
|
||
| supportedProductStreams, err := c.GetKonflux().GetActiveProductStreams(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| mergeWindowStatuses := map[konflux.ProductStream]string{} | ||
| for _, productStream := range supportedProductStreams { | ||
| mergeWindowStatus, err := github.GetMergeWindowStatus(ctx, c, org, repo, productStream) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| mergeWindowStatuses[productStream] = mergeWindowStatus | ||
| } | ||
|
|
||
| return ListMergeWindowsOutput{ | ||
| SupportedProductStreams: supportedProductStreams, | ||
| MergeWindowStatuses: mergeWindowStatuses, | ||
| }, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/stackrox/relacs/pkg/client/konflux" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // The merge window output types must serialize product streams as their | ||
| // canonical string (e.g. "4.6"), including as map keys, in both JSON and YAML. | ||
| func TestGetMergeWindowOutputSerialization(t *testing.T) { | ||
| output := GetMergeWindowOutput{ | ||
| ProductStream: konflux.ProductStream{Major: 4, Minor: 6}, | ||
| Status: "OPEN", | ||
| } | ||
|
|
||
| jsonOutput, err := json.Marshal(output) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| wantJSON := `{"product-stream":"4.6","status":"OPEN"}` | ||
| if string(jsonOutput) != wantJSON { | ||
| t.Errorf("json: got %s, want %s", jsonOutput, wantJSON) | ||
| } | ||
|
|
||
| var yamlDecoded map[string]any | ||
| if err := yaml.Unmarshal(mustYAML(t, output), &yamlDecoded); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if yamlDecoded["product-stream"] != "4.6" { | ||
| t.Errorf("yaml: product-stream should be the string 4.6, got %v", yamlDecoded["product-stream"]) | ||
| } | ||
| if yamlDecoded["status"] != "OPEN" { | ||
| t.Errorf("yaml: status should be OPEN, got %v", yamlDecoded["status"]) | ||
| } | ||
| } | ||
|
|
||
| func TestListMergeWindowsOutputSerialization(t *testing.T) { | ||
| productStream := konflux.ProductStream{Major: 4, Minor: 6} | ||
| output := ListMergeWindowsOutput{ | ||
| SupportedProductStreams: []konflux.ProductStream{productStream}, | ||
| MergeWindowStatuses: map[konflux.ProductStream]string{ | ||
| productStream: "OPEN", | ||
| }, | ||
| } | ||
|
|
||
| jsonOutput, err := json.Marshal(output) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| wantJSON := `{"supported-product-streams":["4.6"],"merge-window-statuses":{"4.6":"OPEN"}}` | ||
| if string(jsonOutput) != wantJSON { | ||
| t.Errorf("json: got %s, want %s", jsonOutput, wantJSON) | ||
| } | ||
|
|
||
| var yamlDecoded map[string]any | ||
| if err := yaml.Unmarshal(mustYAML(t, output), &yamlDecoded); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| streams, ok := yamlDecoded["supported-product-streams"].([]any) | ||
| if !ok || len(streams) != 1 || streams[0] != "4.6" { | ||
| t.Errorf("yaml: supported-product-streams should be [4.6], got %v", yamlDecoded["supported-product-streams"]) | ||
| } | ||
| statuses, ok := yamlDecoded["merge-window-statuses"].(map[string]any) | ||
| if !ok || statuses["4.6"] != "OPEN" { | ||
| t.Errorf("yaml: merge-window-statuses should map the string key 4.6 to OPEN, got %v", yamlDecoded["merge-window-statuses"]) | ||
| } | ||
| } | ||
|
|
||
| func mustYAML(t *testing.T, output any) []byte { | ||
| t.Helper() | ||
| yamlOutput, err := yaml.Marshal(output) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return yamlOutput | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package konflux | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "regexp" | ||
| "sort" | ||
| "strconv" | ||
|
|
||
| "github.com/stackrox/relacs/pkg/client/konflux/kubearchive" | ||
| "github.com/stackrox/relacs/pkg/config" | ||
| ) | ||
|
|
||
| // applicationNamePattern matches Konflux application names like acs-4-6. | ||
| var applicationNamePattern = regexp.MustCompile(`^` + config.KonfluxApplicationPrefix + `-(\d+)-(\d+)$`) | ||
|
|
||
| // GetActiveProductStreams returns the product streams that currently have a | ||
| // Konflux application. Applications are the source of truth because in-flight | ||
| // product streams exist as applications before their first release. | ||
| func (c *Client) GetActiveProductStreams(ctx context.Context) ([]ProductStream, error) { | ||
| ka, err := c.getKubearchive() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| applications, err := kubearchive.QueryResources[Application](ctx, ka, "appstudio.redhat.com", "v1alpha1", "applications", kubearchive.QueryOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to retrieve applications: %w", err) | ||
| } | ||
|
|
||
| productStreams := []ProductStream{} | ||
| for _, application := range applications { | ||
| matches := applicationNamePattern.FindStringSubmatch(application.Metadata.Name) | ||
| if matches == nil { | ||
| continue | ||
| } | ||
| major, _ := strconv.Atoi(matches[1]) | ||
| minor, _ := strconv.Atoi(matches[2]) | ||
| productStreams = append(productStreams, ProductStream{Major: major, Minor: minor}) | ||
| } | ||
|
|
||
| sort.Slice(productStreams, func(i, j int) bool { | ||
| if productStreams[i].Major != productStreams[j].Major { | ||
| return productStreams[i].Major < productStreams[j].Major | ||
| } | ||
| return productStreams[i].Minor < productStreams[j].Minor | ||
| }) | ||
|
|
||
| return productStreams, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.