Skip to content

Commit

Permalink
feat(repo): Add Name History field to Repo type (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Dec 29, 2021
1 parent 3cfb59b commit a10f36c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
6 changes: 6 additions & 0 deletions constants/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ const (

// EventComment defines the event type for comments added to a pull request.
EventComment = "comment"

// EventRepositoryRename defines the event type for a repo being renamed.
EventRepositoryRename = "repositoryRename"

// EventRepository defines the general event type for repo management.
EventRepository = "repository"
)
8 changes: 8 additions & 0 deletions database/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Repo struct {
AllowTag sql.NullBool `sql:"allow_tag"`
AllowComment sql.NullBool `sql:"allow_comment"`
PipelineType sql.NullString `sql:"pipeline_type"`
PreviousName sql.NullString `sql:"previous_name"`
}

// Decrypt will manipulate the existing repo hash by
Expand Down Expand Up @@ -181,6 +182,11 @@ func (r *Repo) Nullify() *Repo {
r.PipelineType.Valid = false
}

// check if the PreviousName field should be false
if len(r.PreviousName.String) == 0 {
r.PreviousName.Valid = false
}

return r
}

Expand Down Expand Up @@ -210,6 +216,7 @@ func (r *Repo) ToLibrary() *library.Repo {
repo.SetAllowTag(r.AllowTag.Bool)
repo.SetAllowComment(r.AllowComment.Bool)
repo.SetPipelineType(r.PipelineType.String)
repo.SetPreviousName(r.PreviousName.String)

return repo
}
Expand Down Expand Up @@ -288,6 +295,7 @@ func RepoFromLibrary(r *library.Repo) *Repo {
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true},
PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true},
PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true},
}

return repo.Nullify()
Expand Down
3 changes: 3 additions & 0 deletions database/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) {
want.SetAllowTag(false)
want.SetAllowComment(false)
want.SetPipelineType("yaml")
want.SetPreviousName("oldName")

// run test
got := testRepo().ToLibrary()
Expand Down Expand Up @@ -306,6 +307,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) {
r.SetAllowTag(false)
r.SetAllowComment(false)
r.SetPipelineType("yaml")
r.SetPreviousName("oldName")

want := testRepo()

Expand Down Expand Up @@ -342,5 +344,6 @@ func testRepo() *Repo {
AllowTag: sql.NullBool{Bool: false, Valid: true},
AllowComment: sql.NullBool{Bool: false, Valid: true},
PipelineType: sql.NullString{String: "yaml", Valid: true},
PreviousName: sql.NullString{String: "oldName", Valid: true},
}
}
33 changes: 32 additions & 1 deletion library/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package library

import "fmt"
import (
"fmt"
)

// Repo is the library representation of a repo.
//
Expand All @@ -31,6 +33,7 @@ type Repo struct {
AllowTag *bool `json:"allow_tag,omitempty"`
AllowComment *bool `json:"allow_comment,omitempty"`
PipelineType *string `json:"pipeline_type,omitempty"`
PreviousName *string `json:"previous_name,omitempty"`
}

// Environment returns a list of environment variables
Expand Down Expand Up @@ -348,6 +351,19 @@ func (r *Repo) GetPipelineType() string {
return *r.PipelineType
}

// GetPreviousName returns the PreviousName field.
//
// When the provided Repo type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (r *Repo) GetPreviousName() string {
// return zero value if Repo type or PreviousName field is nil
if r == nil || r.PreviousName == nil {
return ""
}

return *r.PreviousName
}

// SetID sets the ID field.
//
// When the provided Repo type is nil, it
Expand Down Expand Up @@ -621,6 +637,19 @@ func (r *Repo) SetPipelineType(v string) {
r.PipelineType = &v
}

// SetPreviousName sets the PreviousName field.
//
// When the provided Repo type is nil, it
// will set nothing and immediately return.
func (r *Repo) SetPreviousName(v string) {
// return if Repo type is nil
if r == nil {
return
}

r.PreviousName = &v
}

// String implements the Stringer interface for the Repo type.
func (r *Repo) String() string {
return fmt.Sprintf(`{
Expand All @@ -644,6 +673,7 @@ func (r *Repo) String() string {
UserID: %d
Visibility: %s,
PipelineType: %s,
PreviousName: %s,
}`,
r.GetActive(),
r.GetAllowComment(),
Expand All @@ -665,5 +695,6 @@ func (r *Repo) String() string {
r.GetUserID(),
r.GetVisibility(),
r.GetPipelineType(),
r.GetPreviousName(),
)
}
12 changes: 12 additions & 0 deletions library/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func TestLibrary_Repo_Getters(t *testing.T) {
if test.repo.GetPipelineType() != test.want.GetPipelineType() {
t.Errorf("GetPipelineType is %v, want %v", test.repo.GetPipelineType(), test.want.GetPipelineType())
}

if !reflect.DeepEqual(test.repo.GetPreviousName(), test.want.GetPreviousName()) {
t.Errorf("GetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName())
}
}
}

Expand Down Expand Up @@ -198,6 +202,7 @@ func TestLibrary_Repo_Setters(t *testing.T) {
test.repo.SetAllowTag(test.want.GetAllowTag())
test.repo.SetAllowComment(test.want.GetAllowComment())
test.repo.SetPipelineType(test.want.GetPipelineType())
test.repo.SetPreviousName(test.want.GetPreviousName())

if test.repo.GetID() != test.want.GetID() {
t.Errorf("SetID is %v, want %v", test.repo.GetID(), test.want.GetID())
Expand Down Expand Up @@ -278,6 +283,10 @@ func TestLibrary_Repo_Setters(t *testing.T) {
if test.repo.GetPipelineType() != test.want.GetPipelineType() {
t.Errorf("SetPipelineType is %v, want %v", test.repo.GetPipelineType(), test.want.GetPipelineType())
}

if !reflect.DeepEqual(test.repo.GetPreviousName(), test.want.GetPreviousName()) {
t.Errorf("SetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName())
}
}
}

Expand Down Expand Up @@ -306,6 +315,7 @@ func TestLibrary_Repo_String(t *testing.T) {
UserID: %d
Visibility: %s,
PipelineType: %s,
PreviousName: %s,
}`,
r.GetActive(),
r.GetAllowComment(),
Expand All @@ -327,6 +337,7 @@ func TestLibrary_Repo_String(t *testing.T) {
r.GetUserID(),
r.GetVisibility(),
r.GetPipelineType(),
r.GetPreviousName(),
)

// run test
Expand Down Expand Up @@ -361,6 +372,7 @@ func testRepo() *Repo {
r.SetAllowTag(false)
r.SetAllowComment(false)
r.SetPipelineType("")
r.SetPreviousName("")

return r
}

0 comments on commit a10f36c

Please sign in to comment.