-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
186 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"VersionAliases": { | ||
"latest": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.5.1" | ||
}, | ||
"v1": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.5.1" | ||
}, | ||
"v1.0": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.0.0" | ||
}, | ||
"v1.1": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.1.3" | ||
}, | ||
"v1.2": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.2.0" | ||
}, | ||
"v1.3": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.3.0" | ||
}, | ||
"v1.4": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.4.5" | ||
}, | ||
"v1.5": { | ||
"Assets": 18, | ||
"TargetVersion": "v1.5.1" | ||
} | ||
} | ||
} |
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
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,131 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/Masterminds/semver" | ||
"github.com/google/go-github/v35/github" | ||
"github.com/hokaccha/go-prettyjson" | ||
"go.uber.org/multierr" | ||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
"moul.io/u" | ||
) | ||
|
||
func doAssetsConfig(ctx context.Context, args []string) error { | ||
if len(args) < 1 { | ||
return flag.ErrHelp | ||
} | ||
paths := u.UniqueStrings(args) | ||
logger.Debug("doAssetsConfig", zap.Any("opts", opts), zap.Strings("project", paths)) | ||
|
||
var errs error | ||
g, ctx := errgroup.WithContext(ctx) | ||
for _, path := range paths { | ||
path := path | ||
g.Go(func() error { | ||
err := doAssetsConfigOnce(ctx, path) | ||
if err != nil { | ||
errs = multierr.Append(errs, fmt.Errorf("%q: %w", path, err)) | ||
} | ||
return nil | ||
}) | ||
} | ||
_ = g.Wait() | ||
return errs | ||
} | ||
|
||
type assetConfigVersion struct { | ||
TargetVersion string `json:",omitempty"` | ||
Assets int `json:",omitempty"` | ||
} | ||
|
||
type assetConfig struct { | ||
VersionAliases map[string]assetConfigVersion | ||
SemverMapping map[string]string `json:",omitempty"` | ||
} | ||
|
||
func doAssetsConfigOnce(_ context.Context, path string) error { | ||
project, err := projectFromPath(path) | ||
if err != nil { | ||
return fmt.Errorf("invalid project: %w", err) | ||
} | ||
|
||
// fetch releases | ||
var releases []*github.RepositoryRelease | ||
{ | ||
client := github.NewClient(nil) | ||
var err error | ||
releases, _, err = client.Repositories.ListReleases(context.Background(), project.Git.RepoOwner, project.Git.RepoName, nil) | ||
if err != nil { | ||
return fmt.Errorf("GH API: list releases: %w", err) | ||
} | ||
} | ||
|
||
// compute config | ||
var config assetConfig | ||
{ | ||
config = assetConfig{ | ||
VersionAliases: make(map[string]assetConfigVersion), | ||
SemverMapping: make(map[string]string), | ||
} | ||
|
||
rawVersions := []string{} | ||
for _, release := range releases { | ||
if release.GetDraft() || release.GetPrerelease() || len(release.Assets) == 0 { | ||
continue | ||
} | ||
rawVersions = append(rawVersions, release.GetTagName()) | ||
} | ||
versions := make([]*semver.Version, 0) | ||
for _, raw := range rawVersions { | ||
version, err := semver.NewVersion(raw) | ||
if err != nil { | ||
logger.Warn("cannot parse version", zap.String("raw", raw), zap.Error(err)) | ||
continue | ||
} | ||
config.SemverMapping[version.String()] = raw | ||
versions = append(versions, version) | ||
} | ||
sort.Sort(semver.Collection(versions)) | ||
|
||
for _, version := range versions { | ||
raw := config.SemverMapping[version.String()] | ||
var release *github.RepositoryRelease | ||
for _, r := range releases { | ||
if r.GetTagName() == raw { | ||
release = r | ||
break | ||
} | ||
} | ||
_ = release | ||
minor := fmt.Sprintf("v%d.%d", version.Major(), version.Minor()) | ||
major := fmt.Sprintf("v%d", version.Major()) | ||
configVersion := assetConfigVersion{ | ||
TargetVersion: raw, | ||
Assets: len(release.Assets), | ||
} | ||
config.VersionAliases[minor] = configVersion | ||
config.VersionAliases[major] = configVersion | ||
config.VersionAliases["latest"] = configVersion | ||
} | ||
} | ||
|
||
// cleanup | ||
{ | ||
config.SemverMapping = nil | ||
} | ||
|
||
{ | ||
s, err := prettyjson.Marshal(config) | ||
if err != nil { | ||
return fmt.Errorf("json marshal error: %w", err) | ||
} | ||
fmt.Println(string(s)) | ||
} | ||
return nil | ||
} |
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 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