Skip to content

Commit

Permalink
Merge pull request #2 from mona-actions/amenocal/diff-releases
Browse files Browse the repository at this point in the history
Feat: skips existing releases, adds source timestamps
  • Loading branch information
amenocal authored Jul 11, 2024
2 parents 4752227 + e4cb519 commit aa71c45
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.tar.gz
*.zip
*.json
/tmp

# Test binary, built with `go test -c`
*.test
Expand Down
6 changes: 5 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ func CreateRelease(release *github.RepositoryRelease) (*github.RepositoryRelease
ctx := context.WithValue(context.Background(), github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true)
newRelease, _, err := client.Repositories.CreateRelease(ctx, viper.Get("TARGET_ORGANIZATION").(string), viper.Get("REPOSITORY").(string), release)
if err != nil {
return nil, err
if strings.Contains(err.Error(), "already_exists") {
return nil, fmt.Errorf("release already exists: %v", release.GetName())
} else {
return nil, err
}
}

return newRelease, nil
Expand Down
16 changes: 16 additions & 0 deletions internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/google/go-github/v62/github"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -56,3 +57,18 @@ func ModifyReleaseBody(releaseBody *string, filePath string) (*string, error) {

return &updatedReleaseBody, nil
}

func AddSourceTimeStamps(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
releaseBody := *release.Body

// Format the timestamps
createdAt := release.CreatedAt.Format("January 2, 2006 at 15:04:05 CST")
publishedAt := release.PublishedAt.Format("January 2, 2006 at 15:04:05 CST")

// Add source timestamps to release body
releaseBody = releaseBody + "\n\n" + ">Release Originally Created on: " + createdAt + "\n" + "> Release Originally Published on: " + publishedAt

release.Body = &releaseBody

return release, nil
}
23 changes: 17 additions & 6 deletions pkg/sync/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sync

import (
"strings"

"github.com/mona-actions/gh-migrate-releases/internal/api"
"github.com/mona-actions/gh-migrate-releases/internal/mapping"
"github.com/pterm/pterm"
Expand All @@ -9,33 +11,42 @@ import (

func SyncReleases() {
// Get all releases from source repository
fetchReleasesSpinner, _ := pterm.DefaultSpinner.Start("Fetching releases from repository...")
fetchReleasesSpinner, _ := pterm.DefaultSpinner.Start("Fetching releases from repository: ", viper.GetString("REPOSITORY"))
releases, err := api.GetSourceRepositoryReleases()
if err != nil {
pterm.Error.Printf("Error getting releases: %v", err)
fetchReleasesSpinner.Fail()
}
fetchReleasesSpinner.UpdateText("Releases fetched successfully!")
fetchReleasesSpinner.Success()

// Create releases in target repository
createReleasesSpinner, _ := pterm.DefaultSpinner.Start("Creating releases in target repository...")
createReleasesSpinner, _ := pterm.DefaultSpinner.Start("Creating releases in target repository...", viper.GetString("REPOSITORY"))

//loop through each release and create it in the target repository
for _, release := range releases {
createReleasesSpinner.UpdateText("Creating release: " + release.GetName())

// Modify release body to map new handles and map old urls to new urls
release, err := mapping.AddSourceTimeStamps(release)
if err != nil {
pterm.Warning.Printf("Error adding source timestamps: %v", err)
}
release.Body, err = mapping.ModifyReleaseBody(release.Body, viper.GetString("MAPPING_FILE"))
if err != nil {
pterm.Error.Printf("Error modifying release body: %v", err)
pterm.Warning.Printf("Error modifying release body: %v", err)
}
// Create release api call
newRelease, err := api.CreateRelease(release)
if err != nil {
createReleasesSpinner.Fail()
pterm.Fatal.Printf("Error creating release: %v", err)
if strings.Contains(err.Error(), "already exists") {
pterm.Info.Printf("Release already exists: %v... skipping", release.GetName())
continue
} else {
createReleasesSpinner.Fail()
pterm.Fatal.Printf("Error creating release: %v", err)
}
}
createReleasesSpinner.UpdateText("Downloading assets...")
// Download assets from source repository and upload to target repository
for _, asset := range release.Assets {

Expand Down

0 comments on commit aa71c45

Please sign in to comment.