Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't depend on releases file for direct mode publishing #171

Merged
merged 8 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions internal/actions/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ func Release() error {

dir := "."
usingReleasesMd := false
// TODO: Maybe instead check for workflow dispatch event
providesExplicitTarget := environment.SpecifiedTarget() != ""
if providesExplicitTarget {
var providesExplicitTarget bool
if specificTarget := environment.SpecifiedTarget(); specificTarget != "" {
workflow, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return err
}
if target, ok := workflow.Targets[environment.SpecifiedTarget()]; ok && target.Output != nil {
dir = strings.TrimPrefix(*target.Output, "./")
if target, ok := workflow.Targets[specificTarget]; ok {
if target.Output != nil {
dir = strings.TrimPrefix(*target.Output, "./")
}

providesExplicitTarget = true
dir = filepath.Join(environment.GetWorkingDirectory(), dir)
}
dir = filepath.Join(environment.GetWorkingDirectory(), dir)
}

if !providesExplicitTarget {
Expand Down Expand Up @@ -97,10 +100,8 @@ func Release() error {
return err
}

if environment.CreateGitRelease() {
if err := g.CreateRelease(*latestRelease, outputs); err != nil {
return err
}
if err := g.CreateRelease(*latestRelease, outputs); err != nil {
return err
}

if err = setOutputs(outputs); err != nil {
Expand Down
27 changes: 13 additions & 14 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ func RunWorkflow() error {

anythingRegenerated := false

var releaseInfo releases.ReleasesInfo
if runRes.GenInfo != nil {
docVersion := runRes.GenInfo.OpenAPIDocVersion
speakeasyVersion := runRes.GenInfo.SpeakeasyVersion

releaseInfo := releases.ReleasesInfo{
releaseInfo = releases.ReleasesInfo{
ReleaseTitle: environment.GetInvokeTime().Format("2006-01-02 15:04:05"),
DocVersion: docVersion,
SpeakeasyVersion: speakeasyVersion,
Expand Down Expand Up @@ -182,6 +183,7 @@ func RunWorkflow() error {
LintingReportURL: runRes.LintingReportURL,
ChangesReportURL: runRes.ChangesReportURL,
OpenAPIChangeSummary: runRes.OpenAPIChangeSummary,
currentRelease: &releaseInfo,
}); err != nil {
return err
}
Expand All @@ -206,6 +208,7 @@ type finalizeInputs struct {
ChangesReportURL string
OpenAPIChangeSummary string
VersioningReport *versioning.MergedVersionReport
currentRelease *releases.ReleasesInfo
}

// Sets outputs and creates or adds releases info
Expand Down Expand Up @@ -235,17 +238,9 @@ func finalize(inputs finalizeInputs) error {
return err
}

var releaseInfo *releases.ReleasesInfo
if !inputs.SourcesOnly {
releaseInfo, err = getReleasesInfo()
if err != nil {
return err
}
}

if err := inputs.Git.CreateOrUpdatePR(git.PRInfo{
BranchName: branchName,
ReleaseInfo: releaseInfo,
ReleaseInfo: inputs.currentRelease,
PreviousGenVersion: inputs.Outputs["previous_gen_version"],
PR: pr,
SourceGeneration: inputs.SourcesOnly,
Expand All @@ -259,9 +254,13 @@ func finalize(inputs finalizeInputs) error {
case environment.ModeDirect:
var releaseInfo *releases.ReleasesInfo
if !inputs.SourcesOnly {
releaseInfo, err = getReleasesInfo()
if err != nil {
return err
releaseInfo = inputs.currentRelease
// We still read from releases info for terraform generations since they use the goreleaser
if inputs.Outputs["terraform_regenerated"] == "true" {
releaseInfo, err = getReleasesInfo()
if err != nil {
return err
}
}
}

Expand All @@ -270,7 +269,7 @@ func finalize(inputs finalizeInputs) error {
return err
}

if !inputs.SourcesOnly && environment.CreateGitRelease() {
if !inputs.SourcesOnly {
if err := inputs.Git.CreateRelease(*releaseInfo, inputs.Outputs); err != nil {
return err
}
Expand Down
12 changes: 0 additions & 12 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ func IsDocsGeneration() bool {
return strings.Contains(languages, "docs")
}

func CreateGitRelease() bool {
ryan-timothy-albert marked this conversation as resolved.
Show resolved Hide resolved
return IsLanguagePublished("php") || IsLanguagePublished("terraform") || IsLanguagePublished("swift")
}

func GetAccessToken() string {
return os.Getenv("INPUT_GITHUB_ACCESS_TOKEN")
}
Expand All @@ -209,14 +205,6 @@ func GetInvokeTime() time.Time {
return invokeTime
}

func IsLanguagePublished(lang string) bool {
if lang == "go" || lang == "swift" {
return true
}

return os.Getenv(fmt.Sprintf("INPUT_PUBLISH_%s", strings.ToUpper(lang))) == "true"
}

func GetOpenAPIDocAuthHeader() string {
return os.Getenv("INPUT_OPENAPI_DOC_AUTH_HEADER")
}
Expand Down
12 changes: 7 additions & 5 deletions internal/git/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
//go:embed goreleaser.yml
var tfGoReleaserConfig string

const PublishingCompletedString = "Publishing Completed"

func (g *Git) SetReleaseToPublished(version, directory string) error {
if g.repo == nil {
return fmt.Errorf("repo not cloned")
Expand All @@ -33,8 +35,8 @@ func (g *Git) SetReleaseToPublished(version, directory string) error {
}

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

Expand Down Expand Up @@ -101,9 +103,9 @@ 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 {
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 release.Body != nil && strings.Contains(*release.Body, PublishingCompletedString) {
fmt.Println(fmt.Sprintf("a github release with tag %s has already been published ... skipping publishing", *tagName))
fmt.Println(fmt.Sprintf("to publish this version again please check with your package managed delete the github tag and release"))
if _, ok := outputs[fmt.Sprintf("publish_%s", lang)]; ok {
outputs[fmt.Sprintf("publish_%s", lang)] = "false"
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/releases/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"strings"

config "github.com/speakeasy-api/sdk-gen-config"
Expand Down Expand Up @@ -185,12 +184,6 @@ func GetReleaseInfoFromGenerationFiles(path string) (*ReleasesInfo, error) {
}

for lang, info := range cfgFile.Languages {
packageName := utils.GetPackageName(lang, &info)
// swift and go expects specific package formatting when writing a github release
if path != "" && path != "." && slices.Contains([]string{"go", "swift"}, lang) {
packageName = fmt.Sprintf("%s/%s", packageName, strings.TrimPrefix(path, "./"))
}

releaseInfo.Languages[lang] = LanguageReleaseInfo{
PackageName: utils.GetPackageName(lang, &info),
Version: lockFile.Management.ReleaseVersion,
Expand Down
Loading