Skip to content

Commit

Permalink
Merge pull request #10 from carolynvs/publish-fixes
Browse files Browse the repository at this point in the history
Fix publish of checksum files
  • Loading branch information
carolynvs authored Apr 21, 2022
2 parents c52bd9a + 87d5fcb commit 5640afe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
57 changes: 31 additions & 26 deletions releases/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,39 +183,43 @@ func GeneratePluginFeed() error {
// AddFilesToRelease uploads the files in the specified directory to a GitHub release.
// If the release does not exist already, it will be created with empty release notes.
func AddFilesToRelease(repo string, tag string, dir string) {
files, err := getReleaseAssets(dir)
mgx.Must(err)

if !releaseExists(repo, tag) {
// Mark canary releases as a draft
draft := ""
if strings.HasPrefix(tag, "canary") {
draft = "-p"
}

// Create the GH release
must.RunV("gh", "release", "create", "-R", repo, tag, "--notes=", draft)
}

// Upload the release assets and overwrite existing assets
must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV()
}

func getReleaseAssets(dir string) ([]string, error) {
files := listFiles(dir)

checksumFiles := make([]string, len(files))
for i, file := range files {
var releaseFiles []string
for _, file := range files {
checksumFile, added := AddChecksumExt(file)
if !added {
checksumFiles[i] = file
// This is a checksum file, skip
continue
}

err := createChecksumFile(file, checksumFile)
if err != nil {
mgx.Must(fmt.Errorf("failed to generate checksum file for asset %s: %w", file, err))
return nil, fmt.Errorf("failed to generate checksum file for asset %s: %w", file, err)
}
checksumFiles[i] = checksumFile

}

files = append(files, checksumFiles...)

// Mark canary releases as a draft
draft := ""
if strings.HasPrefix(tag, "canary") {
draft = "-p"
}

if releaseExists(repo, tag) {
must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV()
} else {
must.Command("gh", "release", "create", "-R", repo, "-t", tag, "--notes=", draft, tag).
CollapseArgs().Args(files...).RunV()
releaseFiles = append(releaseFiles, file, checksumFile)
}
return releaseFiles, nil
}

func releaseExists(repo string, version string) bool {
Expand Down Expand Up @@ -247,7 +251,7 @@ func AddChecksumExt(path string) (string, bool) {
func GenerateChecksum(data io.Reader, path string) (string, error) {
hash := sha256.New()
if _, err := io.Copy(hash, data); err != nil {
return "", err
return "", fmt.Errorf("error generating checksum for %s: %w", path, err)
}
sum := hash.Sum(nil)

Expand All @@ -263,7 +267,7 @@ func AppendDataPath(data []byte, path string) string {
func createChecksumFile(contentPath string, checksumFile string) error {
data, err := os.Open(contentPath)
if err != nil {
return err
return fmt.Errorf("error reading release asset %s: %w", contentPath, err)
}
defer data.Close()

Expand All @@ -274,10 +278,11 @@ func createChecksumFile(contentPath string, checksumFile string) error {

f, err := os.Create(checksumFile)
if err != nil {
return err
return fmt.Errorf("error creating checksum file %s: %w", checksumFile, err)
}
defer f.Close()
if _, err := f.WriteString(sum); err != nil {
return err
return fmt.Errorf("error writing checksum file %s: %w", checksumFile, err)
}

return nil
Expand Down
28 changes: 28 additions & 0 deletions releases/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package releases
import (
"crypto/rand"
"encoding/hex"
"github.com/carolynvs/magex/mgx"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -14,6 +15,33 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetReleaseAssets(t *testing.T) {
tmp, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err)
defer os.RemoveAll(tmp)

mgx.Must(shx.Copy("testdata/mixins/v1.2.3/*", tmp, shx.CopyRecursive))

gotFiles, err := getReleaseAssets(tmp)
require.NoError(t, err)

wantFiles := []string{
filepath.Join(tmp, "mymixin-darwin-amd64"),
filepath.Join(tmp, "mymixin-darwin-amd64.sha256sum"),
filepath.Join(tmp, "mymixin-linux-amd64"),
filepath.Join(tmp, "mymixin-linux-amd64.sha256sum"),
filepath.Join(tmp, "mymixin-windows-amd64.exe"),
filepath.Join(tmp, "mymixin-windows-amd64.exe.sha256sum"),
}
assert.Equal(t, wantFiles, gotFiles)

// Read the existing checksum file with stale contents, and ensure it was updated
gotChecksum, err := ioutil.ReadFile(filepath.Join(tmp, "mymixin-darwin-amd64.sha256sum"))
require.NoError(t, err)
wantCheckSum := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 mymixin-darwin-amd64"
assert.Equal(t, wantCheckSum, string(gotChecksum))
}

func TestAddChecksumExt(t *testing.T) {
tests := []struct {
input string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bad checksum file contents, should be overwritten

0 comments on commit 5640afe

Please sign in to comment.