Skip to content

Commit

Permalink
Merge pull request stakwork#2157 from stakwork/feat/update_tickets_de…
Browse files Browse the repository at this point in the history
…fault

Feat: Added tickets group update to default, and wrote test
  • Loading branch information
elraphty authored Dec 11, 2024
2 parents 0c6c436 + e774da9 commit 82a436f
Show file tree
Hide file tree
Showing 6 changed files with 724 additions and 388 deletions.
20 changes: 20 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"fmt"
"log"
"os"

"github.com/rs/xid"
Expand Down Expand Up @@ -456,3 +457,22 @@ func (db database) UserHasManageBountyRoles(pubKeyFromAuth string, uuid string)
}
return true
}

func (db database) ProcessUpdateTicketsWithoutGroup() {
// get all tickets without group
tickets, err := db.GetTicketsWithoutGroup()

if err != nil {
log.Printf("Error getting tickets without group: %v", err)
return
}

// update each ticket with group uuid
for _, ticket := range tickets {
fmt.Println("ticket from process", ticket)
err := db.UpdateTicketsWithoutGroup(ticket)
if err != nil {
log.Printf("Error updating ticket: %v", err)
}
}
}
96 changes: 96 additions & 0 deletions db/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package db

import (
"fmt"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -152,3 +154,97 @@ func TestUserHasManageBountyRoles(t *testing.T) {
assert.False(t, result, "Expected UserHasManageBountyRoles to return false for user without all bounty roles")
})
}

func TestProcessUpdateTicketsWithoutGroup(t *testing.T) {
InitTestDB()

// create person
now := time.Now()

person := Person{
Uuid: uuid.New().String(),
OwnerPubKey: "testfeaturepubkeyProcess",
OwnerAlias: "testfeaturealiasProcess",
Description: "testfeaturedescriptionProcess",
Created: &now,
Updated: &now,
Deleted: false,
}

// create person
TestDB.CreateOrEditPerson(person)

workspace := Workspace{
Uuid: uuid.New().String(),
Name: "Test tickets process space",
Created: &now,
Updated: &now,
}

// create workspace
TestDB.CreateOrEditWorkspace(workspace)

workspaceFeatures := WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test process feature",
Brief: "test get process brief",
Requirements: "Test get process requirements",
Architecture: "Test get process architecture",
Url: "Test get process url",
Priority: 1,
Created: &now,
Updated: &now,
CreatedBy: "test",
UpdatedBy: "test",
}

// create WorkspaceFeatures
TestDB.CreateOrEditFeature(workspaceFeatures)

featurePhase := FeaturePhase{
Uuid: uuid.New().String(),
FeatureUuid: workspaceFeatures.Uuid,
Name: "test get process feature phase",
Priority: 1,
Created: &now,
Updated: &now,
}

// create FeaturePhase
TestDB.CreateOrEditFeaturePhase(featurePhase)

ticket := Tickets{
UUID: uuid.New(),
FeatureUUID: workspaceFeatures.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test get process ticket",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket)

// process update tickets without group
TestDB.ProcessUpdateTicketsWithoutGroup()

// get tickets without group and assert that there is 0
tickets, err := TestDB.GetTicketsWithoutGroup()
assert.NoError(t, err)
assert.Equal(t, 0, len(tickets))

// get ticket and assert that the ticket group is the same as the ticket uuid
ticket, err = TestDB.GetTicket(ticket.UUID.String())

fmt.Println("tickets", tickets)

ticketUuid := ticket.UUID
ticketAuthorID := "12345"
ticketAuthor := Author("HUMAN")

assert.NoError(t, err)
assert.Equal(t, ticket.TicketGroup, &ticketUuid)
assert.Equal(t, ticket.AuthorID, &ticketAuthorID)
assert.Equal(t, ticket.Author, &ticketAuthor)
}
3 changes: 3 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,7 @@ type Database interface {
GetCodeGraphsByWorkspaceUuid(workspace_uuid string) ([]WorkspaceCodeGraph, error)
CreateOrEditCodeGraph(m WorkspaceCodeGraph) (WorkspaceCodeGraph, error)
DeleteCodeGraph(workspace_uuid string, uuid string) error
GetTicketsWithoutGroup() ([]Tickets, error)
UpdateTicketsWithoutGroup(ticket Tickets) error
ProcessUpdateTicketsWithoutGroup()
}
41 changes: 41 additions & 0 deletions db/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,44 @@ func (db database) GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) (

return tickets, nil
}

func (db database) GetTicketsWithoutGroup() ([]Tickets, error) {
var tickets []Tickets

result := db.db.
Where("ticket_group IS NULL OR ticket_group = ?", uuid.Nil).Find(&tickets)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch tickets: %w", result.Error)
}

if result.RowsAffected == 0 {
return []Tickets{}, nil
}

return tickets, nil
}

func (db database) UpdateTicketsWithoutGroup(ticket Tickets) error {
data := map[string]interface{}{}

data["ticket_group"] = ticket.UUID

if ticket.AuthorID == nil {
data["author_id"] = "12345"
}

if ticket.Author == nil {
data["author"] = "HUMAN"
}

fmt.Println("data ===", data)

result := db.db.Model(&Tickets{}).Where("uuid = ?", ticket.UUID).Updates(data)

if result.Error != nil {
return fmt.Errorf("failed to update ticket: %w", result.Error)
}

return nil
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ import (
)

func main() {
var err error

err = godotenv.Load()
if err != nil {
if err := godotenv.Load(); err != nil {
fmt.Println("no .env file")
}

db.InitDB()
db.InitRedis()
db.InitCache()
db.InitRoles()
db.DB.ProcessUpdateTicketsWithoutGroup()

// Config has to be inited before JWT, if not it will lead to NO JWT error
config.InitConfig()
auth.InitJwt()
Expand Down
Loading

0 comments on commit 82a436f

Please sign in to comment.