Skip to content

Commit

Permalink
Merge pull request #4 from mona-actions/amenocal/null-body
Browse files Browse the repository at this point in the history
Fix null pointers
  • Loading branch information
amenocal authored Jul 23, 2024
2 parents d88371e + 83199fa commit f048e6d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
33 changes: 28 additions & 5 deletions internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package mapping

import (
"encoding/csv"
"fmt"
"os"
"strings"
"time"

"github.com/google/go-github/v62/github"
"github.com/spf13/viper"
Expand Down Expand Up @@ -34,7 +36,10 @@ func loadHandleMap(filePath string) (map[string]string, error) {
func ModifyReleaseBody(releaseBody *string, filePath string) (*string, error) {
// Modify release body to map new handles and map old urls to new urls

updatedReleaseBody := *releaseBody
updatedReleaseBody := ""
if releaseBody != nil {
updatedReleaseBody = *releaseBody
}

if viper.GetString("SOURCE_HOSTNAME") != "" {
// Replace source hostname with GHEC hostname github.com
Expand All @@ -59,11 +64,29 @@ func ModifyReleaseBody(releaseBody *string, filePath string) (*string, error) {
}

func AddSourceTimeStamps(release *github.RepositoryRelease) (*github.RepositoryRelease, error) {
releaseBody := *release.Body
if release == nil {
return nil, fmt.Errorf("release is nil")
}

releaseBody := ""
if release.Body != nil {
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")
var createdAt, publishedAt string
now := time.Now().Format("January 2, 2006 at 15:04:05 CST")

if release.CreatedAt != nil {
createdAt = release.CreatedAt.Format("January 2, 2006 at 15:04:05 CST")
} else {
createdAt = now
}

if release.PublishedAt != nil {
publishedAt = release.PublishedAt.Format("January 2, 2006 at 15:04:05 CST")
} else {
publishedAt = now
}

// Add source timestamps to release body
releaseBody = releaseBody + "\n\n" + ">Release Originally Created on: " + createdAt + "\n" + "> Release Originally Published on: " + publishedAt
Expand Down
88 changes: 88 additions & 0 deletions internal/mapping/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"reflect"
"strings"
"testing"
"time"

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

Expand Down Expand Up @@ -107,3 +109,89 @@ func TestModifyReleaseBody(t *testing.T) {
t.Errorf("Failed to remove the test file: %v", err)
}
}

func TestModifyReleaseBodyWithNilBody(t *testing.T) {
var releaseBody *string
filePath := "test.csv"

// Create a test CSV file
file, err := os.Create(filePath)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer file.Close()

// Write test data to the CSV file
data := [][]string{
{"source", "target"},
{"naruto", "naruto.uzumaki"},
}
writer := csv.NewWriter(file)
for _, record := range data {
err := writer.Write(record)
if err != nil {
t.Fatalf("Failed to write to test file: %v", err)
}
}
writer.Flush()

// Set up viper configuration
viper.Set("SOURCE_HOSTNAME", "example.com")
viper.Set("SOURCE_ORGANIZATION", "source-org")
viper.Set("TARGET_ORGANIZATION", "target-org")

// Modify the release body
updatedReleaseBody, err := ModifyReleaseBody(releaseBody, filePath)

if err != nil {
t.Errorf("ModifyReleaseBody returned an error: %v", err)
}

if updatedReleaseBody != nil && *updatedReleaseBody != "" {
t.Errorf("Modified release body is not nil")
}

// Clean up the test file
err = os.Remove(filePath)
if err != nil {
t.Errorf("Failed to remove the test file: %v", err)
}
}

func TestAddSourceTimeStampsWithNilBody(t *testing.T) {
release := &github.RepositoryRelease{}
updatedRelease, err := AddSourceTimeStamps(release)
if err != nil {
t.Errorf("AddSourceTimeStamps returned an error: %v", err)
}
if updatedRelease.Body == nil {
t.Errorf("Updated release body is nil")
}
}

func TestAddSourceTimeStampsWithNilCreatedAt(t *testing.T) {
release := &github.RepositoryRelease{
Body: github.String("Test release body"),
}
updatedRelease, err := AddSourceTimeStamps(release)
if err != nil {
t.Errorf("AddSourceTimeStamps returned an error: %v", err)
}
if !strings.Contains(*updatedRelease.Body, "Release Originally Created on:") {
t.Errorf("Updated release body does not contain the expected created at timestamp")
}
}

func TestAddSourceTimeStampsWithNilPublishedAt(t *testing.T) {
release := &github.RepositoryRelease{
Body: github.String("Test release body"),
CreatedAt: &github.Timestamp{Time: time.Now()},
}
updatedRelease, err := AddSourceTimeStamps(release)
if err != nil {
t.Errorf("AddSourceTimeStamps returned an error: %v", err)
}
if !strings.Contains(*updatedRelease.Body, "Release Originally Published on:") {
t.Errorf("Updated release body does not contain the expected published at timestamp")
}
}

0 comments on commit f048e6d

Please sign in to comment.