func newDraftRepositoryRelease(id int, version string) github.RepositoryRelease { return github.RepositoryRelease{ TagName: github.String(version), Draft: github.Bool(true), ID: github.Int(id), } }
func newDraftRepositoryRelease(version string) github.RepositoryRelease { draft := true return github.RepositoryRelease{ TagName: github.String(version), Draft: &draft, } }
func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, error) { params := request.Params name, err := c.fileContents(filepath.Join(sourceDir, request.Params.NamePath)) if err != nil { return OutResponse{}, err } tag, err := c.fileContents(filepath.Join(sourceDir, request.Params.TagPath)) if err != nil { return OutResponse{}, err } body := "" if request.Params.BodyPath != "" { body, err = c.fileContents(filepath.Join(sourceDir, request.Params.BodyPath)) if err != nil { return OutResponse{}, err } } targetCommitish := "" if request.Params.CommitishPath != "" { targetCommitish, err = c.fileContents(filepath.Join(sourceDir, request.Params.CommitishPath)) if err != nil { return OutResponse{}, err } } draft := request.Params.Draft release := &github.RepositoryRelease{ Name: github.String(name), TagName: github.String(tag), Body: github.String(body), Draft: github.Bool(draft), TargetCommitish: github.String(targetCommitish), } existingReleases, err := c.github.ListReleases() if err != nil { return OutResponse{}, err } var existingRelease *github.RepositoryRelease for _, e := range existingReleases { if e.TagName != nil && *e.TagName == tag { existingRelease = &e break } } if existingRelease != nil { existingRelease.Name = github.String(name) existingRelease.Body = github.String(body) existingRelease.TargetCommitish = github.String(targetCommitish) for _, asset := range existingRelease.Assets { fmt.Fprintf(c.writer, "clearing existing asset: %s\n", *asset.Name) err := c.github.DeleteReleaseAsset(asset) if err != nil { return OutResponse{}, err } } fmt.Fprintf(c.writer, "updating release %s\n", name) release, err = c.github.UpdateRelease(*existingRelease) } else { fmt.Fprintf(c.writer, "creating release %s\n", name) release, err = c.github.CreateRelease(*release) } if err != nil { return OutResponse{}, err } for _, fileGlob := range params.Globs { matches, err := filepath.Glob(filepath.Join(sourceDir, fileGlob)) if err != nil { return OutResponse{}, err } if len(matches) == 0 { return OutResponse{}, fmt.Errorf("could not find file that matches glob '%s'", fileGlob) } for _, filePath := range matches { file, err := os.Open(filePath) if err != nil { return OutResponse{}, err } fmt.Fprintf(c.writer, "uploading %s\n", filePath) name := filepath.Base(filePath) err = c.github.UploadReleaseAsset(*release, name, file) if err != nil { return OutResponse{}, err } file.Close() } } return OutResponse{ Version: versionFromDraft(release), Metadata: metadataFromRelease(release), }, nil }
response resource.OutResponse ) BeforeEach(func() { var err error githubClient = &fakes.FakeGitHub{} command = resource.NewOutCommand(githubClient, ioutil.Discard) sourcesDir, err = ioutil.TempDir("", "github-release") Ω(err).ShouldNot(HaveOccurred()) githubClient.CreateReleaseStub = func(gh github.RepositoryRelease) (*github.RepositoryRelease, error) { createdRel := gh createdRel.ID = github.Int(112) createdRel.HTMLURL = github.String("http://google.com") createdRel.Name = github.String("release-name") createdRel.Body = github.String("*markdown*") return &createdRel, nil } githubClient.UpdateReleaseStub = func(gh github.RepositoryRelease) (*github.RepositoryRelease, error) { return &gh, nil } }) JustBeforeEach(func() { var err error response, err = command.Run(sourcesDir, request) Ω(err).ShouldNot(HaveOccurred()) })
destDir = filepath.Join(tmpDir, "destination") githubClient.DownloadReleaseAssetReturns(ioutil.NopCloser(bytes.NewBufferString("some-content")), nil) inRequest = resource.InRequest{} }) AfterEach(func() { Ω(os.RemoveAll(tmpDir)).Should(Succeed()) }) buildRelease := func(id int, tag string) *github.RepositoryRelease { return &github.RepositoryRelease{ ID: github.Int(id), TagName: github.String(tag), HTMLURL: github.String("http://google.com"), Name: github.String("release-name"), Body: github.String("*markdown*"), } } buildAsset := func(id int, name string) github.ReleaseAsset { return github.ReleaseAsset{ ID: github.Int(id), Name: &name, } } Context("when there is a tagged release", func() { Context("when a present version is specified", func() {