-
Notifications
You must be signed in to change notification settings - Fork 26
feat: basic repo-diff #338
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
Open
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18af584
feat(repo): compare RPM repository inventories
01e287e
updated snapshots
d898ec9
Addr pr comments
9a98431
fix(repo): compare shared artifact kinds
8190afb
Fix diff again
5b41332
fix(repo): restore identity-based additions
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
Some comments aren't visible on the classic Files Changed page.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,224 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package repo | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/repo/repocompare" | ||
| "github.com/microsoft/azure-linux-dev-tools/internal/repo/repolayout" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // CompareOptions are the CLI flags for `azldev repo compare`. | ||
| type CompareOptions struct { | ||
| Left string | ||
| Right string | ||
| Arches []string | ||
| } | ||
|
|
||
| func compareOnAppInit(_ *azldev.App, parentCmd *cobra.Command) { | ||
| parentCmd.AddCommand(NewCompareCmd()) | ||
| } | ||
|
|
||
| // NewCompareCmd constructs the `azldev repo compare` command. | ||
| func NewCompareCmd() *cobra.Command { | ||
| var options CompareOptions | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "compare", | ||
| Short: "Compare package inventories in two RPM repo sets", | ||
| Long: `Compare package identities in two configured RPM repo sets. | ||
|
|
||
| The command expands each named [resources.rpm-repo-sets] entry using its selected | ||
| template. The report groups differences by package name and shows summary | ||
| statuses plus the complete left and right NEVR inventories. Package content is | ||
| not compared.`, | ||
| } | ||
|
|
||
| cmd.RunE = azldev.RunFunc(func(env *azldev.Env) (interface{}, error) { | ||
| return RunCompare(env, &options) | ||
| }) | ||
|
|
||
| cmd.Flags().StringVar(&options.Left, "left", "", "left [resources.rpm-repo-sets] name") | ||
| cmd.Flags().StringVar(&options.Right, "right", "", "right [resources.rpm-repo-sets] name") | ||
| cmd.Flags().StringSliceVar(&options.Arches, "arch", repolayout.DefaultArches, | ||
| "comma-separated target architectures") | ||
|
|
||
| for _, name := range []string{"left", "right"} { | ||
| _ = cmd.MarkFlagRequired(name) | ||
| } | ||
|
Tonisal-byte marked this conversation as resolved.
|
||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| // RunCompare loads both repository inventories and returns their package identity differences. | ||
| func RunCompare(env *azldev.Env, options *CompareOptions) ([]repocompare.PackageReport, error) { | ||
| fetcher := &repocompare.HTTPFetcher{Attempts: env.NetworkRetries()} | ||
|
|
||
| return runCompare(env, options, fetcher) | ||
| } | ||
|
|
||
| func runCompare( | ||
| env *azldev.Env, | ||
| options *CompareOptions, | ||
| fetcher repocompare.Fetcher, | ||
| ) ([]repocompare.PackageReport, error) { | ||
| if options.Left == options.Right { | ||
| return nil, errors.New("'--left' and '--right' must name different rpm-repo-sets") | ||
| } | ||
|
|
||
| leftRepositories, err := comparisonRepositories( | ||
| &env.Config().Resources, | ||
| "left", | ||
| options.Left, | ||
| options.Arches, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("resolving left repositories:\n%w", err) | ||
| } | ||
|
|
||
| rightRepositories, err := comparisonRepositories( | ||
| &env.Config().Resources, | ||
| "right", | ||
| options.Right, | ||
| options.Arches, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("resolving right repositories:\n%w", err) | ||
| } | ||
|
|
||
| leftRepositories, rightRepositories = filterToSharedKinds(leftRepositories, rightRepositories) | ||
| if len(leftRepositories) == 0 { | ||
| return nil, errors.New("the selected rpm-repo-sets have no shared artifact kinds") | ||
| } | ||
|
|
||
| leftPackages, err := repocompare.LoadRepositories(env, fetcher, leftRepositories) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("loading left repositories:\n%w", err) | ||
| } | ||
|
|
||
| rightPackages, err := repocompare.LoadRepositories(env, fetcher, rightRepositories) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("loading right repositories:\n%w", err) | ||
| } | ||
|
|
||
| reports, err := repocompare.Compare(leftPackages, rightPackages) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("comparing repository inventories:\n%w", err) | ||
| } | ||
|
|
||
| return reports, nil | ||
| } | ||
|
|
||
| func comparisonRepositories( | ||
| resources *projectconfig.ResourcesConfig, | ||
| side string, | ||
| setName string, | ||
| arches []string, | ||
| ) ([]repocompare.Repository, error) { | ||
| set, ok := resources.RpmRepoSets[setName] | ||
| if !ok { | ||
| return nil, fmt.Errorf("rpm-repo-set %#q is not defined", setName) | ||
| } | ||
|
|
||
| template, err := repolayout.ResolveTemplate(resources.RpmRepoSetTemplates, set.Template) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("rpm-repo-set %#q:\n%w", setName, err) | ||
| } | ||
|
|
||
| if set.DisableSSLVerify { | ||
| slog.Warn("TLS certificate verification is disabled", "rpmRepoSet", setName) | ||
| } | ||
|
|
||
| allowlist := make(map[string]struct{}, len(set.Subrepos)) | ||
| for _, name := range set.Subrepos { | ||
| allowlist[name] = struct{}{} | ||
| } | ||
|
|
||
| expanded := repolayout.ExpandTemplate(set.BaseURI, set.Template, template, arches) | ||
| selected := make([]repolayout.InputRepo, 0, len(expanded)) | ||
|
|
||
| for _, repo := range expanded { | ||
| if len(allowlist) > 0 { | ||
| if _, ok := allowlist[repo.SubrepoName]; !ok { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| if repo.Arch != "" && !archAllowed(set.Arches, repo.Arch) { | ||
| continue | ||
| } | ||
|
|
||
| selected = append(selected, repo) | ||
| } | ||
|
|
||
| selected = repolayout.DedupInputRepos(selected) | ||
| repositories := make([]repocompare.Repository, 0, len(selected)) | ||
|
|
||
| for _, repo := range selected { | ||
| repoID := side + "-" + repo.SubrepoName | ||
| if repo.Arch != "" { | ||
| repoID += "-" + repo.Arch | ||
| } | ||
|
|
||
| repositories = append(repositories, repocompare.Repository{ | ||
| ID: repoID, | ||
| Kind: repo.Kind, | ||
| URL: repo.URL, | ||
| DisableSSLVerify: set.DisableSSLVerify, | ||
| }) | ||
| } | ||
|
|
||
| return repositories, nil | ||
| } | ||
|
|
||
| func archAllowed(allowlist []string, arch string) bool { | ||
| if len(allowlist) == 0 { | ||
| return true | ||
| } | ||
|
|
||
| for _, allowed := range allowlist { | ||
| if allowed == arch { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| func filterToSharedKinds( | ||
| left []repocompare.Repository, | ||
| right []repocompare.Repository, | ||
| ) ([]repocompare.Repository, []repocompare.Repository) { | ||
| leftKinds := make(map[projectconfig.SubrepoKind]struct{}) | ||
| for _, repository := range left { | ||
| leftKinds[repository.Kind] = struct{}{} | ||
| } | ||
|
|
||
| rightKinds := make(map[projectconfig.SubrepoKind]struct{}) | ||
| for _, repository := range right { | ||
| rightKinds[repository.Kind] = struct{}{} | ||
| } | ||
|
|
||
| filter := func( | ||
| repositories []repocompare.Repository, | ||
| otherKinds map[projectconfig.SubrepoKind]struct{}, | ||
| ) []repocompare.Repository { | ||
| result := make([]repocompare.Repository, 0, len(repositories)) | ||
| for _, repository := range repositories { | ||
| if _, ok := otherKinds[repository.Kind]; ok { | ||
| result = append(result, repository) | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| return filter(left, rightKinds), filter(right, leftKinds) | ||
| } | ||
Oops, something went wrong.
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.