• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

goto / shield / 9364946062

04 Jun 2024 09:49AM UTC coverage: 60.159% (+0.5%) from 59.644%
9364946062

Pull #62

github

ishanarya0
fix: test.yaml
Pull Request #62: feat: add activities table partitioning

5457 of 9071 relevant lines covered (60.16%)

11.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
/core/servicedata/service.go
1
package servicedata
2

3
import (
4
        "context"
5

6
        "github.com/goto/shield/core/project"
7
        "github.com/goto/shield/core/relation"
8
        "github.com/goto/shield/core/resource"
9
        "github.com/goto/shield/core/user"
10
        "github.com/goto/shield/internal/schema"
11
)
12

13
const keyNamespace = "shield/servicedata_key"
14

15
type ResourceService interface {
16
        Create(ctx context.Context, res resource.Resource) (resource.Resource, error)
17
}
18

19
type RelationService interface {
20
        Create(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error)
21
}
22

23
type ProjectService interface {
24
        Get(ctx context.Context, idOrSlug string) (project.Project, error)
25
}
26

27
type UserService interface {
28
        FetchCurrentUser(ctx context.Context) (user.User, error)
29
}
30

31
type Service struct {
32
        repository      Repository
33
        resourceService ResourceService
34
        relationService RelationService
35
        projectService  ProjectService
36
        userService     UserService
37
}
38

39
func NewService(repository Repository, resourceService ResourceService, relationService RelationService, projectService ProjectService, userService UserService) *Service {
40
        return &Service{
41
                repository:      repository,
42
                resourceService: resourceService,
43
                relationService: relationService,
44
                projectService:  projectService,
45
                userService:     userService,
46
        }
47
}
48

49
func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) {
50
        // check if key contains ':'
51
        if key.Key == "" {
52
                return Key{}, ErrInvalidDetail
22✔
53
        }
22✔
54

22✔
55
        // fetch current user
22✔
56
        currentUser, err := s.userService.FetchCurrentUser(ctx)
22✔
57
        if err != nil {
22✔
58
                return Key{}, err
22✔
59
        }
22✔
60

22✔
61
        // Get Project
62
        project, err := s.projectService.Get(ctx, key.ProjectID)
8✔
63
        if err != nil {
8✔
64
                return Key{}, err
9✔
65
        }
1✔
66
        key.ProjectID = project.ID
1✔
67
        key.ProjectSlug = project.Slug
68

69
        // create URN
7✔
70
        key.URN = key.CreateURN()
9✔
71

2✔
72
        // Transaction for postgres repository
2✔
73
        // TODO find way to use transaction for spicedb
74
        ctx = s.repository.WithTransaction(ctx)
75

5✔
76
        // insert the service data key
6✔
77
        resource, err := s.resourceService.Create(ctx, resource.Resource{
1✔
78
                Name:        key.URN,
1✔
79
                NamespaceID: keyNamespace,
4✔
80
                ProjectID:   key.ProjectID,
4✔
81
                UserID:      currentUser.ID,
4✔
82
        })
4✔
83
        if err != nil {
4✔
84
                if err := s.repository.Rollback(ctx, err); err != nil {
4✔
85
                        return Key{}, err
4✔
86
                }
4✔
87
                return Key{}, err
4✔
88
        }
4✔
89
        key.ResourceID = resource.Idxa
4✔
90

4✔
91
        // insert service data key to the servicedata_keys table
4✔
92
        createdServiceDataKey, err := s.repository.CreateKey(ctx, key)
4✔
93
        if err != nil {
4✔
94
                if err := s.repository.Rollback(ctx, err); err != nil {
4✔
95
                        return Key{}, err
4✔
96
                }
5✔
97
                return Key{}, err
1✔
98
        }
×
99

×
100
        // create relation
1✔
101
        _, err = s.relationService.Create(ctx, relation.RelationV2{
102
                Object: relation.Object{
3✔
103
                        ID:          resource.Idxa,
3✔
104
                        NamespaceID: schema.ServiceDataKeyNamespace,
3✔
105
                },
3✔
106
                Subject: relation.Subject{
4✔
107
                        ID:        currentUser.ID,
1✔
108
                        RoleID:    schema.OwnerRole,
×
109
                        Namespace: schema.UserPrincipal,
×
110
                },
1✔
111
        })
112
        if err != nil {
113
                if err := s.repository.Rollback(ctx, err); err != nil {
114
                        return Key{}, err
2✔
115
                }
2✔
116
                return Key{}, err
2✔
117
        }
2✔
118

2✔
119
        if err := s.repository.Commit(ctx); err != nil {
2✔
120
                return Key{}, err
2✔
121
        }
2✔
122

2✔
123
        return createdServiceDataKey, nil
2✔
124
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc