Skip to content

Commit

Permalink
better homepage for builddep
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed May 10, 2023
1 parent e138a49 commit 1a87408
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/bindown/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (c *dependencyAddByGithubReleaseCmd) Run(ctx *runContext) error {
default:
return fmt.Errorf(`invalid release URL or "owner/repo(@tag)"`)
}
urls, releaseVer, repoDesc, err := builddep.QueryGitHubRelease(ctx, fmt.Sprintf("%s/%s", owner, repo), tag, c.GithubToken)
urls, releaseVer, repoPage, repoDesc, err := builddep.QueryGitHubRelease(ctx, fmt.Sprintf("%s/%s", owner, repo), tag, c.GithubToken)
if err != nil {
return err
}
Expand All @@ -288,7 +288,7 @@ func (c *dependencyAddByGithubReleaseCmd) Run(ctx *runContext) error {
}
homepage := c.Homepage
if homepage == "" {
homepage = fmt.Sprintf("https://github.com/%s/%s", owner, repo)
homepage = repoPage
}
description := c.Description
if description == "" {
Expand Down
14 changes: 9 additions & 5 deletions internal/builddep/builddep.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,28 +1097,32 @@ func (g *depGroup) fileAllowed(f *dlFile, binName string) bool {
return true
}

func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []string, version, description string, _ error) {
func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []string, version, homepage, description string, _ error) {
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: tkn},
)))
splitRepo := strings.Split(repo, "/")
orgName, repoName := splitRepo[0], splitRepo[1]
repoResp, _, err := client.Repositories.Get(ctx, orgName, repoName)
if err != nil {
return nil, "", "", err
return nil, "", "", "", err
}
description = repoResp.GetDescription()
homepage = repoResp.GetHomepage()
if homepage == "" {
homepage = repoResp.GetHTMLURL()
}
var release *github.RepositoryRelease
if tag == "" {
release, _, err = client.Repositories.GetLatestRelease(ctx, orgName, repoName)
if err != nil {
return nil, "", "", err
return nil, "", "", "", err
}
tag = release.GetTagName()
} else {
release, _, err = client.Repositories.GetReleaseByTag(ctx, orgName, repoName, tag)
if err != nil {
return nil, "", "", err
return nil, "", "", "", err
}
}

Expand All @@ -1134,5 +1138,5 @@ func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []stri
for _, asset := range release.Assets {
urls = append(urls, asset.GetBrowserDownloadURL())
}
return urls, version, description, nil
return urls, version, homepage, description, nil
}
9 changes: 5 additions & 4 deletions internal/builddep/builddep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_sanity(t *testing.T) {
if tkn == "" {
t.Skip("GITHUB_TOKEN not set")
}
urls, version, description, err := QueryGitHubRelease(ctx, "willabides/bindown", "v3.16.1", tkn)
urls, version, homepage, description, err := QueryGitHubRelease(ctx, "willabides/bindown", "v3.16.1", tkn)
require.NoError(t, err)
require.Equal(t, "3.16.1", version)
require.Equal(t, 18, len(urls))
Expand All @@ -41,7 +41,7 @@ systems:
var cfg bindown.Config
err = yaml.Unmarshal([]byte(initialConfig), &cfg)
require.NoError(t, err)
err = addDependency(ctx, &cfg, "bindown", version, "", description, urls, selectFirstCandidate)
err = addDependency(ctx, &cfg, "bindown", version, homepage, description, urls, selectFirstCandidate)
require.NoError(t, err)
got, err := yaml.Marshal(&cfg)
require.NoError(t, err)
Expand All @@ -59,6 +59,7 @@ dependencies:
version: 3.16.1
templates:
bindown:
homepage: https://github.com/WillAbides/bindown
url: https://github.com/WillAbides/bindown/releases/download/v{{.version}}/bindown_{{.version}}_{{.os}}_{{.arch}}{{.urlSuffix}}
archive_path: bindown{{.archivePathSuffix}}
bin: bindown
Expand Down Expand Up @@ -93,10 +94,10 @@ func adhocRelease(t *testing.T, repo, tag string) {
if tkn == "" {
t.Skip("GITHUB_TOKEN not set")
}
urls, version, description, err := QueryGitHubRelease(ctx, repo, tag, tkn)
urls, version, homepage, description, err := QueryGitHubRelease(ctx, repo, tag, tkn)
require.NoError(t, err)
var cfg bindown.Config
err = addDependency(ctx, &cfg, "x", version, "", description, urls, selectFirstCandidate)
err = addDependency(ctx, &cfg, "x", version, homepage, description, urls, selectFirstCandidate)
require.NoError(t, err)
got, err := yaml.Marshal(&cfg)
require.NoError(t, err)
Expand Down

0 comments on commit 1a87408

Please sign in to comment.