-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import openshift-pipelines/operator-tooling in…
… so that it is encapsulated with the operator's code. Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
- Loading branch information
1 parent
8a77ead
commit bd5d238
Showing
205 changed files
with
31,857 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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,64 @@ | ||
// Package main is the main package :D | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var bugfix bool | ||
|
||
func BumpCommand(ioStreams *cli.IOStreams) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "bump", | ||
Short: "Bump components version", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("Requires 1 argument") | ||
} | ||
filename := args[0] | ||
return bump(filename, bugfix) | ||
}, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
} | ||
cmd.Flags().BoolVar(&bugfix, "bugfix", false, "Only update bugfix versions of components") | ||
return cmd | ||
} | ||
|
||
func bump(filename string, bugfix bool) error { | ||
newComponents := map[string]component{} | ||
components, err := ReadCompoments(filename) | ||
if err != nil { | ||
return err | ||
} | ||
for name, component := range components { | ||
newComponent, err := bumpComponent(name, component, bugfix) | ||
if err != nil { | ||
return err | ||
} | ||
newComponents[name] = newComponent | ||
} | ||
return writeComponents(filename, newComponents) | ||
} | ||
|
||
func bumpComponent(name string, c component, bugfix bool) (component, error) { | ||
newVersion := c.Version | ||
newerVersions, err := checkComponentNewerVersions(c, bugfix) | ||
if err != nil { | ||
return component{}, err | ||
} | ||
if len(newerVersions) > 0 { | ||
// Get the latest one | ||
sort.Sort(newerVersions) // sort just in case | ||
newVersion = "v" + newerVersions[len(newerVersions)-1].String() | ||
} | ||
return component{ | ||
Github: c.Github, | ||
Version: newVersion, | ||
}, nil | ||
} |
This file contains 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,123 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"sort" | ||
|
||
"github.com/Masterminds/semver" | ||
"github.com/cli/go-gh" | ||
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func CheckCommand(ioStreams *cli.IOStreams) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "check", | ||
Short: "Check component versions (and if there is an upgrade needed)", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("Requires 1 argument") | ||
} | ||
filename := args[0] | ||
return check(filename, bugfix, ioStreams.Out) | ||
}, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
} | ||
cmd.Flags().BoolVar(&bugfix, "bugfix", false, "Only update bugfix versions of components") | ||
return cmd | ||
} | ||
|
||
func check(filename string, bugfix bool, out io.Writer) error { | ||
components, err := ReadCompoments(filename) | ||
if err != nil { | ||
return err | ||
} | ||
g, ctx := errgroup.WithContext(context.Background()) | ||
for name, component := range components { | ||
// Force scope | ||
name := name | ||
component := component | ||
|
||
g.Go(func() error { | ||
return checkComponent(ctx, name, component, bugfix, out) | ||
}) | ||
} | ||
return g.Wait() | ||
} | ||
|
||
func checkComponent(ctx context.Context, name string, component component, bugfix bool, out io.Writer) error { | ||
newerVersion, err := checkComponentNewerVersions(component, bugfix) | ||
if err != nil { | ||
return err | ||
} | ||
if len(newerVersion) > 0 { | ||
fmt.Fprintf(out, "%s: %v\n", name, newerVersion) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func checkComponentNewerVersions(component component, bugfix bool) (semver.Collection, error) { | ||
sVersions, err := fetchVersions(component.Github) | ||
if err != nil { | ||
return nil, err | ||
} | ||
currentVersion, err := semver.NewVersion(component.Version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newerVersion, err := getNewerVersion(currentVersion, sVersions, bugfix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newerVersion, nil | ||
} | ||
|
||
func fetchVersions(github string) (semver.Collection, error) { | ||
client, err := gh.RESTClient(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
versions := []struct { | ||
Name string | ||
TagName string `json:"tag_name"` | ||
}{} | ||
err = client.Get(fmt.Sprintf("repos/%s/releases", github), &versions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sVersions := semver.Collection([]*semver.Version{}) | ||
for _, v := range versions { | ||
sVersion, err := semver.NewVersion(v.TagName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sVersions = append(sVersions, sVersion) | ||
} | ||
sort.Sort(sVersions) | ||
return sVersions, nil | ||
} | ||
|
||
func getNewerVersion(currentVersion *semver.Version, versions []*semver.Version, bugfix bool) (semver.Collection, error) { | ||
constraint := fmt.Sprintf("> %s", currentVersion) | ||
if bugfix { | ||
nextMinorVersion := currentVersion.IncMinor() | ||
constraint = fmt.Sprintf("> %s, < %s", currentVersion, nextMinorVersion.String()) | ||
} | ||
c, err := semver.NewConstraint(constraint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newerVersion := semver.Collection([]*semver.Version{}) | ||
for _, sv := range versions { | ||
if c.Check(sv) { | ||
newerVersion = append(newerVersion, sv) | ||
} | ||
} | ||
return newerVersion, nil | ||
} |
This file contains 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,69 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli" | ||
"github.com/spf13/cobra" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type component struct { | ||
Github string `json:"github"` | ||
Version string `json:"version"` | ||
} | ||
|
||
func ReadCompoments(filename string) (map[string]component, error) { | ||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
components := map[string]component{} | ||
if err := yaml.Unmarshal(data, &components); err != nil { | ||
return nil, err | ||
} | ||
return components, nil | ||
} | ||
|
||
func writeComponents(filename string, components map[string]component) error { | ||
data, err := yaml.Marshal(components) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(filename, data, 0o644) | ||
} | ||
|
||
func ComponentVersionCommand(ioStreams *cli.IOStreams) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "component-version", | ||
Short: "Prints the version of a component from a components file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 2 { | ||
return fmt.Errorf("Requires at least 2 argument") | ||
} | ||
filename := args[0] | ||
return componentVersion(filename, args[1:], ioStreams.Out) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func componentVersion(filename string, args []string, out io.Writer) error { | ||
if len(args) == 0 || len(args) > 1 { | ||
return fmt.Errorf("Need one and only one argument, the component name") | ||
} | ||
component := args[0] | ||
components, err := ReadCompoments(filename) | ||
if err != nil { | ||
return err | ||
} | ||
c, ok := components[component] | ||
if !ok { | ||
return fmt.Errorf("Component %s not found", component) | ||
} | ||
fmt.Fprint(out, c.Version) | ||
return nil | ||
} |
This file contains 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,30 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func main() { | ||
cmd := &cobra.Command{ | ||
Use: "operator-tool", | ||
Short: "Tooling to manage the operator", | ||
Long: `This is a tool to help maintaining this operator`, | ||
SilenceUsage: true, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
} | ||
|
||
ioStreams := cli.NewIOStreams() | ||
|
||
cmd.AddCommand(BumpCommand(ioStreams)) | ||
cmd.AddCommand(CheckCommand(ioStreams)) | ||
cmd.AddCommand(ComponentVersionCommand(ioStreams)) | ||
|
||
if err := cmd.Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
Oops, something went wrong.