Skip to content

Commit

Permalink
feat: check for existing publishes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert committed May 31, 2024
1 parent c8979f7 commit 233cbaa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sdk-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
run: npm publish --access public
- id: publish-event
uses: speakeasy-api/sdk-generation-action@v15
uses: speakeasy-api/sdk-generation-action@v15.11.0
if: always()
with:
github_access_token: ${{ secrets.github_access_token }}
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ outputs:
description: "The directory the SDK target was generated to"
runs:
using: "docker"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15.11.0"
env:
SPEAKEASY_API_KEY: ${{ inputs.speakeasy_api_key }}
SPEAKEASY_SERVER_URL: ${{ inputs.speakeasy_server_url }}
Expand Down
9 changes: 8 additions & 1 deletion internal/actions/publishEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

func PublishEvent() error {
if _, err := initAction(); err != nil {
g, err := initAction()
if err != nil {
return err
}

Expand All @@ -39,6 +40,12 @@ func PublishEvent() error {

version := processLockFile(*loadedCfg.LockFile, event)

if !strings.Contains(strings.ToLower(os.Getenv("GH_ACTION_RESULT")), "success") {
if err = g.SetReleaseToPublished(version); err != nil {
fmt.Println("Failed to set release to published %w", err)
}
}

var processingErr error
switch os.Getenv("INPUT_REGISTRY_NAME") {
case "pypi":
Expand Down
38 changes: 33 additions & 5 deletions internal/git/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/google/go-github/v54/github"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
Expand All @@ -16,6 +17,31 @@ import (
//go:embed goreleaser.yml
var tfGoReleaserConfig string

func (g *Git) SetReleaseToPublished(version string) error {
if g.repo == nil {
return fmt.Errorf("repo not cloned")
}
tag := "v" + version

release, _, err := g.client.Repositories.GetReleaseByTag(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), tag)
if err != nil {
return fmt.Errorf("failed to get release for tag %s: %w", tag, err)
}

if release != nil && release.ID != nil {
if release.Body != nil && !strings.Contains(*release.Body, "Publishing Completed") {
body := *release.Body + "\n\nPublishing Completed"
release.Body = &body
}

if _, _, err = g.client.Repositories.EditRelease(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), *release.ID, release); err != nil {
return fmt.Errorf("failed to add to release body for tag %s: %w", tag, err)
}
}

return nil
}

func (g *Git) CreateRelease(releaseInfo releases.ReleasesInfo, outputs map[string]string) error {
if g.repo == nil {
return fmt.Errorf("repo not cloned")
Expand Down Expand Up @@ -70,12 +96,14 @@ func (g *Git) CreateRelease(releaseInfo releases.ReleasesInfo, outputs map[strin
})
if err != nil {
if release, _, err := g.client.Repositories.GetReleaseByTag(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), *tagName); err == nil && release != nil {
fmt.Println(fmt.Sprintf("a github release with tag %s already existing ... skipping publishing", *tagName))
fmt.Println(fmt.Sprintf("to publish this version again delete the github tag and release"))
if _, ok := outputs[fmt.Sprintf("publish_%s", lang)]; ok {
outputs[fmt.Sprintf("publish_%s", lang)] = "false"
if release.Body != nil && strings.Contains(*release.Body, "Publishing Completed") {
fmt.Println(fmt.Sprintf("a github release with tag %s already existing ... skipping publishing", *tagName))
fmt.Println(fmt.Sprintf("to publish this version again delete the github tag and release"))
if _, ok := outputs[fmt.Sprintf("publish_%s", lang)]; ok {
outputs[fmt.Sprintf("publish_%s", lang)] = "false"
}
}

// TODO: Consider deleting and recreating the release if we are moving forward with publishing
return nil
}

Expand Down

0 comments on commit 233cbaa

Please sign in to comment.