Skip to content
Permalink
Browse files

rget: github: publish archives

- Publish archive digests as they are included in the front page of
releases

- Add a dry run flag to publish to aid in testing
  • Loading branch information
philips committed Jul 20, 2019
1 parent f4e88ca commit 4ff2a5ef4343cd31ed55e4539e17ed81e48de401
Showing with 93 additions and 6 deletions.
  1. +30 −6 rget/github/publish-release-sums.go
  2. +21 −0 rgetgithub/rgetgithub.go
  3. +42 −0 rgetgithub/rgetgithub_test.go
@@ -12,6 +12,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/oauth2"

"github.com/merklecounty/rget/rgetgithub"
"github.com/merklecounty/rget/rgethash"
"github.com/merklecounty/rget/rgetwellknown"
)
@@ -24,6 +25,10 @@ var publishReleaseSumsCmd = &cobra.Command{
Run: publishReleaseSumsMain,
}

func init() {
publishReleaseSumsCmd.Flags().BoolP("dry-run", "d", false, "Do not upload file to GitHub")
}

func publishReleaseSumsMain(cmd *cobra.Command, args []string) {
var releases []github.RepositoryRelease

@@ -50,12 +55,20 @@ func publishReleaseSumsMain(cmd *cobra.Command, args []string) {
},
}

tc, err := oauth2ns.AuthenticateUser(conf)
dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
log.Fatal(err)
panic(err)
}

client := github.NewClient(tc.Client)
client := github.NewClient(nil)

if !dryRun {
tc, err := oauth2ns.AuthenticateUser(conf)
if err != nil {
log.Fatal(err)
}
client = github.NewClient(tc.Client)
}

if m["tag"] != "" {
release, _, err := client.Repositories.GetReleaseByTag(ctx, m["org"], m["repo"], m["tag"])
@@ -75,13 +88,24 @@ func publishReleaseSumsMain(cmd *cobra.Command, args []string) {
}
urls.AddURL(*r.ZipballURL)
urls.AddURL(*r.TarballURL)

// Grab the git tag URLs
tu := rgetgithub.ArchiveURLs(m["org"], m["repo"], *r.TagName)
for _, t := range tu {
urls.AddURL(t)
}

sha256sumfile := urls.SHA256SumFile()

content := []byte(sha256sumfile)
uploadSums(client, m["org"], m["repo"], m["tag"], r, content)
}

fmt.Printf("Successfully uploaded https://github.com/%s/%s/releases/download/%s/SHA256SUMS\n", m["org"], m["repo"], m["tag"])
fmt.Printf("generated SHA256SUMS:\n\n%s\n", content)

if !dryRun {
uploadSums(client, m["org"], m["repo"], *r.TagName, r, content)
fmt.Printf("submit the uploaded SHA256SUMS to the public record by running:\n\nrget submit https://github.com/%s/%s/releases/download/%s/SHA256SUMS\n", m["org"], m["repo"], *r.TagName)
}
}

}

@@ -0,0 +1,21 @@
package rgetgithub

import (
"fmt"
"net/url"
)

// ArchiveURLs generates source archive URLs for a GitHub repo tag
// e.g. https://github.com/philips/releases-test/archive/v1.0.zip and
// https://github.com/philips/releases-test/archive/v1.0.tar.gz
func ArchiveURLs(owner, repo, tag string) (urls []string) {
u := url.URL{
Scheme: "https",
Host: "github.com",
Path: fmt.Sprintf("/%s/%s/archive/%s", owner, repo, tag),
}
urls = append(urls, u.String()+".tar.gz")
urls = append(urls, u.String()+".zip")

return
}
@@ -0,0 +1,42 @@
package rgetgithub

import "testing"

func TestArchiveURLs(t *testing.T) {
testCases := []struct {
urls []string
org string
repo string
tag string
}{
{
urls: []string{
"https://github.com/philips/releases-test/archive/v1.0.zip",
"https://github.com/philips/releases-test/archive/v1.0.tar.gz",
},
org: "philips",
repo: "releases-test",
tag: "v1.0",
},
}

for ti, tt := range testCases {
aurls := ArchiveURLs(tt.org, tt.repo, tt.tag)

for _, u := range tt.urls {
if !contains(aurls, u) {
t.Errorf("%d: %v not in %v", ti, u, aurls)
}
}
}

}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

0 comments on commit 4ff2a5e

Please sign in to comment.
You can’t perform that action at this time.