-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets-config.go
137 lines (124 loc) · 3.3 KB
/
assets-config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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 {
logger.Debug("ignoring release",
zap.String("version", release.GetTagName()),
zap.Bool("draft", release.GetDraft()),
zap.Bool("prerelease", release.GetPrerelease()),
zap.Int("assets", len(release.Assets)),
)
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
}