Skip to content

Commit

Permalink
Merge pull request stakwork#1641 from stakwork/feature-add-features-t…
Browse files Browse the repository at this point in the history
…o-workspaces

Feature add features to workspaces
  • Loading branch information
elraphty authored May 12, 2024
2 parents 39b6548 + 356f165 commit 578d5d9
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cypress/e2e/02_repositories.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { User, HostName, Workspaces, Repositories } from '../support/objects/objects';


describe('Create Repositories for Workspace', () => {
it('passes', () => {
cy.upsertlogin(User).then(value => {
for(let i = 0; i <= 1; i++) {
cy.request({
method: 'POST',
url: `${HostName}/workspaces/repositories`,
headers: { 'x-jwt': `${value}` },
body: Repositories[i]
}).its('body').then(body => {
expect(body).to.have.property('name').and.equal(Repositories[i].name.trim());
expect(body).to.have.property('url').and.equal(Repositories[i].url.trim());
});
}
})
})
})


describe('Check Repositories Values', () => {
it('passes', () => {
cy.upsertlogin(User).then(value => {
cy.request({
method: 'GET',
url: `${HostName}/workspaces/repositories/` + Repositories[0].workspace_uuid,
headers: { 'x-jwt': `${ value }` },
body: {}
}).then((resp) => {
expect(resp.status).to.eq(200)
expect(resp.body[0]).to.have.property('name', Repositories[0].name.trim())
expect(resp.body[0]).to.have.property('url', Repositories[0].url.trim())
expect(resp.body[1]).to.have.property('name', Repositories[1].name.trim())
expect(resp.body[1]).to.have.property('url', Repositories[1].url.trim())
})
})
})
})
2 changes: 1 addition & 1 deletion cypress/support/objects/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const Repositories = [
url: ' https://github.com/stakwork/sphinx-tribes-frontend '
},
{
uuid: 'com1t3gn1e4a4qu3tnlg',
uuid: 'com1t3gn1e4a4qu3thss',
workspace_uuid: 'cohob00n1e4808utqel0',
name: ' backend ',
url: ' https://github.com/stakwork/sphinx-tribes '
Expand Down
1 change: 1 addition & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func InitDB() {
db.AutoMigrate(&ConnectionCodes{})
db.AutoMigrate(&BountyRoles{})
db.AutoMigrate(&UserInvoiceData{})
db.AutoMigrate(&WorkspaceRepositories{})
db.AutoMigrate(&WorkspaceFeatures{})

DB.MigrateTablesWithOrgUuid()
Expand Down
2 changes: 2 additions & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ type Database interface {
PersonUniqueNameFromName(name string) (string, error)
ProcessAlerts(p Person)
UserHasAccess(pubKeyFromAuth string, uuid string, role string) bool
CreateWorkspaceRepository(m WorkspaceRepositories) (WorkspaceRepositories, error)
GetWorkspaceRepositorByWorkspaceUuid(uuid string) []WorkspaceRepositories
CreateOrEditFeature(m WorkspaceFeatures) (WorkspaceFeatures, error)
GetFeaturesByWorkspaceUuid(uuid string) []WorkspaceFeatures
GetFeatureByUuid(uuid string) WorkspaceFeatures
Expand Down
12 changes: 12 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ type WorkspaceUsersData struct {
Person
}

type WorkspaceRepositories struct {
ID uint `json:"id"`
Uuid string `json:"uuid"`
WorkspaceUuid string `json:"workspace_uuid"`
Name string `json:"name"`
Url string `json:"url"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
CreatedBy string `json:"created_by"`
UpdatedBy string `json:"updated_by"`
}

type WorkspaceFeatures struct {
ID uint `json:"id"`
Uuid string `json:"uuid"`
Expand Down
24 changes: 24 additions & 0 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/stakwork/sphinx-tribes/utils"
Expand Down Expand Up @@ -57,6 +58,29 @@ func (db database) CreateOrEditWorkspace(m Workspace) (Workspace, error) {
return m, nil
}

func (db database) CreateWorkspaceRepository(m WorkspaceRepositories) (WorkspaceRepositories, error) {
m.Name = strings.TrimSpace(m.Name)
m.Url = strings.TrimSpace(m.Url)

now := time.Now()
m.Updated = &now

if db.db.Model(&m).Where("uuid = ?", m.Uuid).Updates(&m).RowsAffected == 0 {
m.Created = &now
db.db.Create(&m)
}

return m, nil
}

func (db database) GetWorkspaceRepositorByWorkspaceUuid(uuid string) []WorkspaceRepositories {
ms := []WorkspaceRepositories{}

db.db.Model(&WorkspaceRepositories{}).Where("workspace_uuid = ?", uuid).Order("Created").Find(&ms)

return ms
}

func (db database) GetWorkspaceUsers(uuid string) ([]WorkspaceUsersData, error) {
ms := []WorkspaceUsersData{}

Expand Down
57 changes: 57 additions & 0 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,60 @@ func (oh *workspaceHandler) UpdateWorkspace(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(p)
}

func (oh *workspaceHandler) CreateWorkspaceRepository(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

workspaceRepo := db.WorkspaceRepositories{}
body, _ := io.ReadAll(r.Body)
r.Body.Close()
err := json.Unmarshal(body, &workspaceRepo)

if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}

workspaceRepo.CreatedBy = pubKeyFromAuth

// Validate struct data
err = db.Validate.Struct(workspaceRepo)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Error: did not pass validation test : %s", err)
json.NewEncoder(w).Encode(msg)
return
}

p, err := oh.db.CreateWorkspaceRepository(workspaceRepo)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(p)
}

func (oh *workspaceHandler) GetWorkspaceRepositorByWorkspaceUuid(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

uuid := chi.URLParam(r, "uuid")
workspaceFeatures := oh.db.GetWorkspaceRepositorByWorkspaceUuid(uuid)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(workspaceFeatures)
}
104 changes: 104 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions routes/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func WorkspaceRoutes() chi.Router {
r.Post("/mission", workspaceHandlers.UpdateWorkspace)
r.Post("/tactics", workspaceHandlers.UpdateWorkspace)
r.Post("/schematicurl", workspaceHandlers.UpdateWorkspace)

r.Post("/repositories", workspaceHandlers.CreateWorkspaceRepository)
r.Get("/repositories/{uuid}", workspaceHandlers.GetWorkspaceRepositorByWorkspaceUuid)
})
return r
}

0 comments on commit 578d5d9

Please sign in to comment.