Skip to content

Commit

Permalink
feat: add assets-config
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed May 30, 2021
1 parent 6d7ad18 commit cf99a63
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 3 deletions.
36 changes: 36 additions & 0 deletions .github/assets-config.json
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"
}
}
}
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ generate: install
echo 'foo@bar:~$$ repoman info .' > .tmp/example-info.txt
repoman info . >> .tmp/example-info.txt

repoman assets-config . > .github/assets-config.json

embedmd -w README.md
rm -rf .tmp
.PHONY: generate
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ foo@bar:~$ repoman info .
{
"Git": {
"CloneURL": "git@github.com:moul/repoman",
"CurrentBranch": "master",
"CurrentBranch": "dev/moul/assets-config",
"HTMLURL": "https://github.com/moul/repoman",
"InMainBranch": true,
"InMainBranch": false,
"IsDirty": null,
"MainBranch": "master",
"Metadata": {
Expand Down Expand Up @@ -63,6 +63,7 @@ SUBCOMMANDS
maintenance perform various maintenance tasks (write)
version show version and build info
template-post-clone replace template
assets-config generate a configuration for assets

FLAGS
-v false verbose mode
Expand Down
131 changes: 131 additions & 0 deletions assets-config.go
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
}

// print
{
s, err := prettyjson.Marshal(config)
if err != nil {
return fmt.Errorf("json marshal error: %w", err)
}
fmt.Println(string(s))
}
return nil
}
2 changes: 2 additions & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ var (
infoFs = flag.NewFlagSet("doctor", flag.ExitOnError)
maintenanceFs = flag.NewFlagSet("maintenance", flag.ExitOnError)
versionFs = flag.NewFlagSet("version", flag.ExitOnError)
templatePostCloneFs = flag.NewFlagSet("tmeplate-post-clone", flag.ExitOnError)
templatePostCloneFs = flag.NewFlagSet("template-post-clone", flag.ExitOnError)
assetsConfigFs = flag.NewFlagSet("assets-config", flag.ExitOnError)
opts Opts

logger *zap.Logger
Expand Down Expand Up @@ -94,6 +95,7 @@ func run(args []string) error {
{Name: "maintenance", Exec: doMaintenance, FlagSet: maintenanceFs, ShortHelp: "perform various maintenance tasks (write)", ShortUsage: "maintenance [opts] <path...>"},
{Name: "version", Exec: doVersion, FlagSet: versionFs, ShortHelp: "show version and build info", ShortUsage: "version"},
{Name: "template-post-clone", Exec: doTemplatePostClone, FlagSet: templatePostCloneFs, ShortHelp: "replace template", ShortUsage: "template-post-clone [opts] <path...>"},
{Name: "assets-config", Exec: doAssetsConfig, FlagSet: assetsConfigFs, ShortHelp: "generate a configuration for assets", ShortUsage: "assets-config [opts] <path...>"},
},
Exec: func(ctx context.Context, args []string) error {
return flag.ErrHelp
Expand Down

0 comments on commit cf99a63

Please sign in to comment.