Skip to content

Commit 3c12241

Browse files
zmotsoMykolaMarusenko
authored andcommitted
feat: Implement cache invalidation endpoint (#33)
1 parent 1eb1ec7 commit 3c12241

File tree

14 files changed

+363
-16
lines changed

14 files changed

+363
-16
lines changed

.github/instructions/go-development.instructions.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ applyTo: "**/*.go"
77
- Follow Go best practices and idioms
88
- Use latest Go 1.24 features
99
- Split function parameters into separate lines if they exceed 120 characters
10-
- Use meaningful variable and function names
11-
- Keep functions concise and focused
12-
- Properly handle errors with appropriate context
1310
- Add comments for complex logic, but prefer self-documenting code
1411

1512
## Error Handling

.github/prompts/implement-feature.prompt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ I'll help you implement a new feature in the GitFusion service, following best p
2323
- Identify files that need to be created or modified
2424
- Update OpenAPI `internal/api/oapi.yaml` if API changes are needed and run `make generate` to regenerate code for API stubs and models
2525
- Plan tests to verify the implementation
26+
- At the end of the implementation, run `make lint` to ensure code quality and adherence to project standards and fix any issues reported by the linter
2627

2728
4. **Testing Approach**
2829
- Unit tests for core functionality

internal/api/cache_handler.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/KubeRocketCI/gitfusion/internal/cache"
8+
)
9+
10+
// CacheHandler handles cache management requests.
11+
type CacheHandler struct {
12+
cacheManager *cache.Manager
13+
}
14+
15+
// NewCacheHandler creates a new CacheHandler.
16+
func NewCacheHandler(cacheManager *cache.Manager) *CacheHandler {
17+
return &CacheHandler{
18+
cacheManager: cacheManager,
19+
}
20+
}
21+
22+
// InvalidateCache implements api.StrictServerInterface.
23+
func (c *CacheHandler) InvalidateCache(
24+
ctx context.Context,
25+
request InvalidateCacheRequestObject,
26+
) (InvalidateCacheResponseObject, error) {
27+
endpoint := string(request.Params.Endpoint)
28+
29+
if err := c.cacheManager.InvalidateCache(endpoint); err != nil {
30+
return c.errResponse(err), nil
31+
}
32+
33+
return InvalidateCache200JSONResponse{
34+
Message: fmt.Sprintf("Cache for endpoint '%s' has been successfully invalidated", endpoint),
35+
Endpoint: endpoint,
36+
}, nil
37+
}
38+
39+
// errResponse handles error responses for cache operations.
40+
func (c *CacheHandler) errResponse(err error) InvalidateCacheResponseObject {
41+
if err == nil {
42+
return InvalidateCache200JSONResponse{}
43+
}
44+
45+
// Check if it's an unsupported endpoint error (validation error)
46+
if err.Error() == "unsupported endpoint" {
47+
return InvalidateCache400JSONResponse{
48+
Code: "invalid_endpoint",
49+
Message: err.Error(),
50+
}
51+
}
52+
53+
// Default to 500 internal server error
54+
return InvalidateCache500JSONResponse{
55+
Code: "internal_error",
56+
Message: err.Error(),
57+
}
58+
}

internal/api/oapi.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ paths:
139139
schema:
140140
$ref: '#/components/schemas/Error'
141141

142+
/api/v1/cache/invalidate:
143+
delete:
144+
summary: Invalidate cache for a specific endpoint
145+
operationId: invalidateCache
146+
tags:
147+
- Cache
148+
parameters:
149+
- name: endpoint
150+
in: query
151+
required: true
152+
description: The endpoint name to invalidate cache for (repositories, organizations, branches)
153+
schema:
154+
type: string
155+
enum: [repositories, organizations, branches]
156+
responses:
157+
'200':
158+
description: Cache invalidated successfully
159+
content:
160+
application/json:
161+
schema:
162+
$ref: '#/components/schemas/CacheInvalidationResponse'
163+
'400':
164+
description: Bad request due to invalid parameters.
165+
content:
166+
application/json:
167+
schema:
168+
$ref: '#/components/schemas/Error'
169+
'500':
170+
description: Internal server error.
171+
content:
172+
application/json:
173+
schema:
174+
$ref: '#/components/schemas/Error'
175+
142176
components:
143177
parameters:
144178
repoNameQueryParam:
@@ -300,3 +334,15 @@ components:
300334
$ref: '#/components/schemas/Branch'
301335
required:
302336
- data
337+
CacheInvalidationResponse:
338+
type: object
339+
properties:
340+
message:
341+
type: string
342+
description: A message indicating the result of the cache invalidation
343+
endpoint:
344+
type: string
345+
description: The endpoint for which cache was invalidated
346+
required:
347+
- message
348+
- endpoint

internal/api/server.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55

6+
"github.com/KubeRocketCI/gitfusion/internal/cache"
67
"github.com/KubeRocketCI/gitfusion/internal/services/branches"
78
"github.com/KubeRocketCI/gitfusion/internal/services/krci"
89
"github.com/KubeRocketCI/gitfusion/internal/services/organizations"
@@ -22,18 +23,21 @@ type Server struct {
2223
repositoryHandler *RepositoryHandler
2324
organizationHandler *OrganizationHandler
2425
branchHandler *BranchHandler
26+
cacheHandler *CacheHandler
2527
}
2628

2729
// NewServer creates a new Server instance.
2830
func NewServer(
2931
repositoryHandler *RepositoryHandler,
3032
organizationHandler *OrganizationHandler,
3133
branchHandler *BranchHandler,
34+
cacheHandler *CacheHandler,
3235
) *Server {
3336
return &Server{
3437
repositoryHandler: repositoryHandler,
3538
organizationHandler: organizationHandler,
3639
branchHandler: branchHandler,
40+
cacheHandler: cacheHandler,
3741
}
3842
}
3943

@@ -69,6 +73,14 @@ func (s *Server) ListBranches(
6973
return s.branchHandler.ListBranches(ctx, request)
7074
}
7175

76+
// InvalidateCache implements StrictServerInterface.
77+
func (s *Server) InvalidateCache(
78+
ctx context.Context,
79+
request InvalidateCacheRequestObject,
80+
) (InvalidateCacheResponseObject, error) {
81+
return s.cacheHandler.InvalidateCache(ctx, request)
82+
}
83+
7284
func BuildHandler(conf Config) (ServerInterface, error) {
7385
k8sCl, err := initk8sClient()
7486
if err != nil {
@@ -77,27 +89,33 @@ func BuildHandler(conf Config) (ServerInterface, error) {
7789

7890
gitServerService := krci.NewGitServerService(k8sCl, conf.Namespace)
7991

80-
repoSvc := repositories.NewRepositoriesService(
81-
repositories.NewMultiProviderRepositoryService(),
82-
gitServerService,
83-
)
84-
orgSvc := organizations.NewOrganizationsService(
85-
organizations.NewMultiProviderOrganizationsService(
86-
gitServerService,
87-
),
88-
gitServerService,
89-
)
90-
branchesSvc := branches.NewBranchesService(
91-
branches.NewMultiProviderBranchesService(),
92-
gitServerService,
92+
// Create multi-provider services
93+
repoMultiProvider := repositories.NewMultiProviderRepositoryService()
94+
orgMultiProvider := organizations.NewMultiProviderOrganizationsService(gitServerService)
95+
branchesMultiProvider := branches.NewMultiProviderBranchesService()
96+
97+
// Create high-level services
98+
repoSvc := repositories.NewRepositoriesService(repoMultiProvider, gitServerService)
99+
orgSvc := organizations.NewOrganizationsService(orgMultiProvider, gitServerService)
100+
branchesSvc := branches.NewBranchesService(branchesMultiProvider, gitServerService)
101+
102+
// Create cache manager with access to all cache instances
103+
cacheManager := cache.NewManager(
104+
repoSvc.GetProvider().GetCache(),
105+
orgSvc.GetProvider().GetCache(),
106+
branchesSvc.GetProvider().GetCache(),
93107
)
108+
109+
// Create handlers
94110
branchHandler := NewBranchHandler(branchesSvc)
111+
cacheHandler := NewCacheHandler(cacheManager)
95112

96113
return NewStrictHandlerWithOptions(
97114
NewServer(
98115
NewRepositoryHandler(repoSvc),
99116
NewOrganizationHandler(orgSvc),
100117
branchHandler,
118+
cacheHandler,
101119
),
102120
[]StrictMiddlewareFunc{},
103121
StrictHTTPServerOptions{},

internal/api/server_gen.go

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)