Skip to content

Commit

Permalink
feat: add github resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Jun 13, 2022
1 parent c58af0b commit 1b57e43
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 5 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ go 1.18
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/google/go-github/v45 v45.0.0
github.com/hashicorp/go-hclog v1.2.0
github.com/hashicorp/go-plugin v1.4.4
github.com/schollz/progressbar/v3 v3.8.6
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
google.golang.org/grpc v1.46.2
google.golang.org/protobuf v1.28.0
)
Expand All @@ -20,6 +22,7 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand All @@ -45,6 +48,7 @@ require (
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-github/v45 v45.0.0 h1:LU0WBjYidxIVyx7PZeWb+FP4JZJ3Wh3FQgdumnGqiLs=
github.com/google/go-github/v45 v45.0.0/go.mod h1:FObaZJEDSTa/WGCzZ2Z3eoCDXWJKMenWWTrd8jrta28=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -310,6 +314,7 @@ golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -321,6 +326,8 @@ golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 h1:OSnWWcOd/CtWQC2cYSBgbTSJv3ciqd8r54ySIW2y3RE=
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -373,6 +380,7 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
Expand Down Expand Up @@ -445,7 +453,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand All @@ -471,6 +478,7 @@ google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
Expand Down
9 changes: 8 additions & 1 deletion pkg/plugin/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-semantic-release/semantic-release/v2/pkg/config"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver/github"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver/registry"
)

Expand All @@ -19,6 +20,9 @@ func loadResolvers(resolvers ...resolver.Resolver) (map[string]resolver.Resolver
resolversMap := make(map[string]resolver.Resolver)
for _, r := range resolvers {
for _, name := range r.Names() {
if name == "default" {
return nil, fmt.Errorf("resolver name default is reserved")
}
if _, ok := resolversMap[name]; ok {
return nil, fmt.Errorf("resolver %s already exists", name)
}
Expand All @@ -29,10 +33,13 @@ func loadResolvers(resolvers ...resolver.Resolver) (map[string]resolver.Resolver
}

func New(config *config.Config) (*Discovery, error) {
resolvers, err := loadResolvers(registry.NewResolver())
resolvers, err := loadResolvers(registry.NewResolver(), github.NewResolver())
if err != nil {
return nil, err
}
// use the registry resolver as default
resolvers["default"] = resolvers["registry"]

return &Discovery{
config: config,
resolvers: resolvers,
Expand Down
160 changes: 160 additions & 0 deletions pkg/plugin/discovery/resolver/github/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package github

import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"runtime"
"sort"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin/discovery/resolver"
"github.com/google/go-github/v45/github"
"golang.org/x/oauth2"
)

type GitHubResolver struct {
ghClient *github.Client
}

func NewResolver() *GitHubResolver {
var tc *http.Client
if ghToken := os.Getenv("GITHUB_TOKEN"); ghToken != "" {
tc = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: ghToken}))
}
return &GitHubResolver{
ghClient: github.NewClient(tc),
}
}

func (g *GitHubResolver) githubReleaseToDownloadInfo(repoOwner, repoName string, release *github.RepositoryRelease) (*resolver.PluginDownloadInfo, error) {
var checksumAsset *github.ReleaseAsset
var pluginAsset *github.ReleaseAsset
osArchRe := regexp.MustCompile("(?i)" + runtime.GOOS + "(_|-)" + runtime.GOARCH)
for _, asset := range release.Assets {
assetName := asset.GetName()
if checksumAsset == nil && asset.GetSize() <= 4096 && strings.Contains(strings.ToLower(assetName), "checksum") {
checksumAsset = asset
}
if pluginAsset == nil && osArchRe.MatchString(assetName) {
pluginAsset = asset
}
}

if pluginAsset == nil {
return nil, fmt.Errorf("no matching plugin binary was found for %s/%s", runtime.GOOS, runtime.GOARCH)
}

foundChecksum := ""
if checksumAsset != nil {
checksumDownload, _, err := g.ghClient.Repositories.DownloadReleaseAsset(context.Background(), repoOwner, repoName, checksumAsset.GetID(), http.DefaultClient)
if err != nil {
return nil, err
}
checksumData, err := ioutil.ReadAll(checksumDownload)
checksumDownload.Close()
if err != nil {
return nil, err
}
for _, l := range strings.Split(string(checksumData), "\n") {
sl := strings.Split(l, " ")
if len(sl) < 3 {
continue
}
if sl[2] == pluginAsset.GetName() {
foundChecksum = sl[0]
}
}
}

return &resolver.PluginDownloadInfo{
URL: pluginAsset.GetBrowserDownloadURL(),
Checksum: foundChecksum,
FileName: pluginAsset.GetName(),
Version: strings.TrimLeft(release.GetTagName(), "v"),
}, nil
}

type ghRelease struct {
version *semver.Version
release *github.RepositoryRelease
}

type ghReleases []*ghRelease

func (gr ghReleases) Len() int { return len(gr) }
func (gr ghReleases) Less(i, j int) bool { return gr[j].version.LessThan(gr[i].version) }
func (gr ghReleases) Swap(i, j int) { gr[i], gr[j] = gr[j], gr[i] }

func (g *GitHubResolver) getAllValidGitHubReleases(repoOwner, repoName string) (ghReleases, error) {
ret := make(ghReleases, 0)
opts := &github.ListOptions{Page: 1, PerPage: 100}
for {
releases, resp, err := g.ghClient.Repositories.ListReleases(context.Background(), repoOwner, repoName, opts)
if err != nil {
return nil, err
}
for _, release := range releases {
if release.GetDraft() {
continue
}

if semverTag, err := semver.NewVersion(release.GetTagName()); err == nil {
ret = append(ret, &ghRelease{version: semverTag, release: release})
}
}

if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
sort.Sort(ret)
return ret, nil
}

func (g *GitHubResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
if pluginInfo.RepoSlug == "" {
pluginInfo.RepoSlug = knownPlugins[pluginInfo.ShortNormalizedName]
}
if pluginInfo.RepoSlug == "" {
return nil, fmt.Errorf("repo slug not found")
}
repoOwner, repoName, _ := strings.Cut(pluginInfo.RepoSlug, "/")

var foundRelease *github.RepositoryRelease
if pluginInfo.Constraint == nil {
release, _, err := g.ghClient.Repositories.GetLatestRelease(context.Background(), repoOwner, repoName)
if err != nil {
return nil, err
}
foundRelease = release
} else {
validReleases, err := g.getAllValidGitHubReleases(repoOwner, repoName)
if err != nil {
return nil, err
}
for _, release := range validReleases {
if pluginInfo.Constraint.Check(release.version) {
foundRelease = release.release
break
}
}
}

if foundRelease == nil {
return nil, fmt.Errorf("no matching release was found")
}

di, err := g.githubReleaseToDownloadInfo(repoOwner, repoName, foundRelease)
return di, err
}

func (g *GitHubResolver) Names() []string {
return []string{"github", "gh"}
}
17 changes: 17 additions & 0 deletions pkg/plugin/discovery/resolver/github/known_plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package github

var knownPlugins = map[string]string{
"provider-github": "go-semantic-release/provider-github",
"provider-gitlab": "go-semantic-release/provider-gitlab",
"changelog-generator-default": "go-semantic-release/changelog-generator-default",
"commit-analyzer-default": "go-semantic-release/commit-analyzer-cz",
"condition-default": "go-semantic-release/condition-default",
"condition-github": "go-semantic-release/condition-github",
"condition-gitlab": "go-semantic-release/condition-gitlab",
"files-updater-npm": "go-semantic-release/files-updater-npm",
"provider-git": "go-semantic-release/provider-git",
"condition-bitbucket": "go-semantic-release/condition-bitbucket",
"files-updater-helm": "go-semantic-release/files-updater-helm",
"hooks-goreleaser": "go-semantic-release/hooks-goreleaser",
"hooks-npm-binary-releaser": "go-semantic-release/hooks-npm-binary-releaser",
}
4 changes: 2 additions & 2 deletions pkg/plugin/discovery/resolver/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewResolver() *RegistryResolver {
return &RegistryResolver{}
}

func (r RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
func (r *RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolver.PluginDownloadInfo, error) {
pluginApiRes, err := getPluginInfo(pluginInfo.NormalizedName)
if err != nil {
return nil, err
Expand Down Expand Up @@ -62,5 +62,5 @@ func (r RegistryResolver) ResolvePlugin(pluginInfo *plugin.PluginInfo) (*resolve
}

func (r *RegistryResolver) Names() []string {
return []string{"default", "registry"}
return []string{"registry"}
}

0 comments on commit 1b57e43

Please sign in to comment.