Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

gh: release command improvements #481

Closed
wants to merge 1 commit into from

3 participants

@calavera

Fixes #477

I've removed the defaults for the release command and now it will only upload assets explicitly. It detects whether you want to upload a file of every file inside a directory using the flag --attach or -a.

This change made all the tests that we had for this command obsolete. I'm trying to find a way we can add some coverage to the new changes.

@calavera calavera Do not attach anything to the release command by default.
Detect the asset info to upload via the `--attach` flag.
150af6b
@mislav
Owner

Great, thanks for working on this. Two things:

  1. What do you think about dropping the shorthand flag -a and just supporting --attach? I only support shorthand flags for often-typed commands, but I don't think the release create command will be typed often. I see it as something that will embedded as a part of a larger release script (since you need to build binaries first anyway).
  2. Our test suite on the "gh" branch is broken anyway (mostly Cucumbers) so you didn't need to comment out the tests. It's a bit unfortunate that a smaller change breaks all existing unit tests, though.
@calavera

What do you think about dropping the shorthand flag -a and just supporting --attach?

Personally, I like to have parity among flags. I always find strange when a command only offers shorthand flags for certain options.

It's a bit unfortunate that a smaller change breaks all existing unit tests, though.

Yeah, the problem is that we were testing the wrong thing here. The tests only covered how the assets were selected. Now that I've changed that all the tests are broken. I'd really like to have a mock http handler to test these kind of things.

@jingweno
Owner

@calavera I've been using net/http/httptest for simulating the response in go-octokit. Here is one example: https://github.com/octokit/go-octokit/blob/master/octokit/client_test.go#L9-L28. Hope that helps.

@jingweno jingweno closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 10, 2014
  1. @calavera

    Do not attach anything to the release command by default.

    calavera authored
    Detect the asset info to upload via the `--attach` flag.
This page is out of date. Refresh to see the latest.
Showing with 97 additions and 105 deletions.
  1. +47 −55 commands/release.go
  2. +50 −50 commands/release_test.go
View
102 commands/release.go
@@ -30,8 +30,7 @@ var (
Long: `Creates a new release in GitHub for the project that the "origin" remote points to.
It requires the name of the tag to release as a first argument.
-Specify the assets to include in the release from a directory via "-a". Without
-"-a", it finds assets from "releases/TAG" of the current directory.
+Specify the assets to include in the release from a directory via "-a".
Without <MESSAGE> or <FILE>, a text editor will open in which title and body
of the release can be entered in the same manner as git commit message.
@@ -44,7 +43,7 @@ If "-p" is given, it creates a pre-release.
flagReleaseDraft,
flagReleasePrerelease bool
- flagReleaseAssetsDir,
+ flagReleaseAssets,
flagReleaseMessage,
flagReleaseFile string
)
@@ -52,7 +51,7 @@ If "-p" is given, it creates a pre-release.
func init() {
cmdCreateRelease.Flag.BoolVarP(&flagReleaseDraft, "draft", "d", false, "DRAFT")
cmdCreateRelease.Flag.BoolVarP(&flagReleasePrerelease, "prerelease", "p", false, "PRERELEASE")
- cmdCreateRelease.Flag.StringVarP(&flagReleaseAssetsDir, "assets", "a", "", "ASSETS_DIR")
+ cmdCreateRelease.Flag.StringVarP(&flagReleaseAssets, "attach", "a", "", "ATTACH_ASSETS")
cmdCreateRelease.Flag.StringVarP(&flagReleaseMessage, "message", "m", "", "MESSAGE")
cmdCreateRelease.Flag.StringVarP(&flagReleaseFile, "file", "f", "", "FILE")
@@ -86,9 +85,6 @@ func createRelease(cmd *Command, args *Args) {
tag := args.LastParam()
- assetsDir, err := getAssetsDirectory(flagReleaseAssetsDir, tag)
- utils.Check(err)
-
runInLocalRepo(func(localRepo *github.GitHubRepo, project *github.Project, gh *github.Client) {
currentBranch, err := localRepo.CurrentBranch()
utils.Check(err)
@@ -113,7 +109,7 @@ func createRelease(cmd *Command, args *Args) {
finalRelease, err := gh.CreateRelease(project, params)
utils.Check(err)
- uploadReleaseAssets(gh, finalRelease, assetsDir)
+ uploadReleaseAssets(gh, finalRelease)
fmt.Printf("\n\nRelease created: %s", finalRelease.HTMLURL)
})
@@ -136,67 +132,63 @@ func writeReleaseTitleAndBody(project *github.Project, tag, currentBranch string
return editor.EditTitleAndBody()
}
-func getAssetsDirectory(assetsDir, tag string) (string, error) {
- if assetsDir == "" {
- pwd, err := os.Getwd()
- utils.Check(err)
-
- assetsDir = filepath.Join(pwd, "releases", tag)
- }
-
- if !isDir(assetsDir) {
- return "", fmt.Errorf("The assets directory doesn't exist: %s", assetsDir)
- }
-
- if isEmptyDir(assetsDir) {
- return "", fmt.Errorf("The assets directory is empty: %s", assetsDir)
+func uploadReleaseAssets(gh *github.Client, release *octokit.Release) {
+ if flagReleaseAssets == "" {
+ return
}
- return assetsDir, nil
-}
+ assetInfo, err := os.Stat(flagReleaseAssets)
+ utils.Check(err)
-func uploadReleaseAssets(gh *github.Client, release *octokit.Release, assetsDir string) {
var wg sync.WaitGroup
var totalAssets, countAssets uint64
- filepath.Walk(assetsDir, func(path string, fi os.FileInfo, err error) error {
- if !fi.IsDir() {
- totalAssets += 1
- }
- return nil
- })
-
- printUploadProgress(&countAssets, totalAssets)
+ notifyProgress := func() {
+ atomic.AddUint64(&countAssets, uint64(1))
+ printUploadProgress(&countAssets, totalAssets)
+ wg.Done()
+ }
- filepath.Walk(assetsDir, func(path string, fi os.FileInfo, err error) error {
- if !fi.IsDir() {
- wg.Add(1)
+ if assetInfo.IsDir() {
+ filepath.Walk(flagReleaseAssets, func(path string, fi os.FileInfo, err error) error {
+ if !fi.IsDir() {
+ totalAssets += 1
+ }
+ return nil
+ })
- go func() {
- defer func() {
- atomic.AddUint64(&countAssets, uint64(1))
- printUploadProgress(&countAssets, totalAssets)
- wg.Done()
- }()
+ printUploadProgress(&countAssets, totalAssets)
- uploadUrl, err := release.UploadURL.Expand(octokit.M{"name": fi.Name()})
- utils.Check(err)
+ filepath.Walk(flagReleaseAssets, func(path string, fi os.FileInfo, err error) error {
+ if !fi.IsDir() {
+ wg.Add(1)
+ go uploadAsset(gh, release, fi, path, notifyProgress)
+ }
+ return nil
+ })
+ } else {
+ totalAssets = 1
+ printUploadProgress(&countAssets, totalAssets)
+ wg.Add(1)
+ uploadAsset(gh, release, assetInfo, flagReleaseAssets, notifyProgress)
+ }
- contentType := detectContentType(path, fi)
+ wg.Wait()
+}
- file, err := os.Open(path)
- utils.Check(err)
- defer file.Close()
+func uploadAsset(gh *github.Client, release *octokit.Release, fi os.FileInfo, path string, notifyProgress func()) {
+ defer notifyProgress()
+ uploadUrl, err := release.UploadURL.Expand(octokit.M{"name": fi.Name()})
+ utils.Check(err)
- err = gh.UploadReleaseAsset(uploadUrl, file, contentType)
- utils.Check(err)
- }()
- }
+ contentType := detectContentType(path, fi)
- return nil
- })
+ file, err := os.Open(path)
+ utils.Check(err)
+ defer file.Close()
- wg.Wait()
+ err = gh.UploadReleaseAsset(uploadUrl, file, contentType)
+ utils.Check(err)
}
func detectContentType(path string, fi os.FileInfo) string {
View
100 commands/release_test.go
@@ -1,52 +1,52 @@
package commands
-import (
- "github.com/bmizerany/assert"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
-)
-
-func TestAssetsDirWithoutFlag(t *testing.T) {
- dir := createTempDir(t)
- pwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- os.Chdir(pwd)
- os.RemoveAll(dir)
- }()
-
- os.Chdir(dir)
-
- tagDir := filepath.Join(dir, "releases", "v1.0.0")
- assertAssetsDirSelected(t, tagDir, "")
-}
-
-func TestAssetsDirWithFlag(t *testing.T) {
- dir := createTempDir(t)
- defer os.RemoveAll(dir)
-
- tagDir := filepath.Join(dir, "releases", "v1.0.0")
- assertAssetsDirSelected(t, tagDir, tagDir)
-}
-
-func assertAssetsDirSelected(t *testing.T, expectedDir, flagDir string) {
- assets, err := getAssetsDirectory(flagDir, "v1.0.0")
- assert.NotEqual(t, nil, err) // Error if it doesn't exist
-
- os.MkdirAll(expectedDir, 0755)
- assets, err = getAssetsDirectory(flagDir, "v1.0.0")
- assert.NotEqual(t, nil, err) // Error if it's empty
-
- ioutil.TempFile(expectedDir, "gh-test")
- assets, err = getAssetsDirectory(flagDir, "v1.0.0")
-
- fiExpected, err := os.Stat(expectedDir)
- fiAssets, err := os.Stat(assets)
-
- assert.Equal(t, nil, err)
- assert.T(t, os.SameFile(fiExpected, fiAssets))
-}
+//import (
+// "github.com/bmizerany/assert"
+// "io/ioutil"
+// "os"
+// "path/filepath"
+// "testing"
+//)
+
+//func TestAssetsDirWithoutFlag(t *testing.T) {
+// dir := createTempDir(t)
+// pwd, err := os.Getwd()
+// if err != nil {
+// t.Fatal(err)
+// }
+// defer func() {
+// os.Chdir(pwd)
+// os.RemoveAll(dir)
+// }()
+//
+// os.Chdir(dir)
+//
+// tagDir := filepath.Join(dir, "releases", "v1.0.0")
+// assertAssetsDirSelected(t, tagDir, "")
+//}
+//
+//func TestAssetsDirWithFlag(t *testing.T) {
+// dir := createTempDir(t)
+// defer os.RemoveAll(dir)
+//
+// tagDir := filepath.Join(dir, "releases", "v1.0.0")
+// assertAssetsDirSelected(t, tagDir, tagDir)
+//}
+//
+//func assertAssetsDirSelected(t *testing.T, expectedDir, flagDir string) {
+// assets, err := getAssetsDirectory(flagDir, "v1.0.0")
+// assert.NotEqual(t, nil, err) // Error if it doesn't exist
+//
+// os.MkdirAll(expectedDir, 0755)
+// assets, err = getAssetsDirectory(flagDir, "v1.0.0")
+// assert.NotEqual(t, nil, err) // Error if it's empty
+//
+// ioutil.TempFile(expectedDir, "gh-test")
+// assets, err = getAssetsDirectory(flagDir, "v1.0.0")
+//
+// fiExpected, err := os.Stat(expectedDir)
+// fiAssets, err := os.Stat(assets)
+//
+// assert.Equal(t, nil, err)
+// assert.T(t, os.SameFile(fiExpected, fiAssets))
+//}
Something went wrong with that request. Please try again.