Skip to content

Commit 424f360

Browse files
Merge pull request #9 from simondanielsson/feature/add-refresh-prs
Add refresh PR's endpoint
2 parents 0a47527 + 7a8e6d1 commit 424f360

File tree

9 files changed

+271
-8
lines changed

9 files changed

+271
-8
lines changed

cmd/constants/constants.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,22 @@ package constants
22

33
type ReviewStatus string
44

5+
// Review Status Constants
56
const (
6-
StatusQueued ReviewStatus = "queued"
7+
// StatusQueued indicates that the review is queued.
8+
StatusQueued ReviewStatus = "queued"
9+
// StatusProcessing indicates that the review is in progress.
710
StatusProcessing ReviewStatus = "processing"
8-
StatusAvailable ReviewStatus = "available"
11+
// StatusAvailable indicates that the review is available.
12+
StatusAvailable ReviewStatus = "available"
13+
)
14+
15+
type PRState string
16+
17+
// Pull Request State Constants
18+
const (
19+
// PRStateClosed indicates that the pull request is closed.
20+
PRStateClosed PRState = "closed"
21+
// PRStateOpen indicates that the pull request is open.
22+
PRStateOpen PRState = "open"
923
)

cmd/internal/controllers/reviews_controllers.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,42 @@ func (rc *ReviewsController) GetPullRequests(c *fiber.Ctx) error {
150150
})
151151
}
152152

153-
func (rc *ReviewsController) UpdatePullRequest(c *fiber.Ctx) error {
154-
return c.SendString("UpdatePullRequest")
153+
// generate swagger docs
154+
// @Summary Update pull request
155+
// @Description Update a pull request
156+
// @Tags reviews
157+
// @Security BearerAuth
158+
// @Accept json
159+
// @Produce json
160+
// @Param repositoryID path string true "Repository ID"
161+
// @Success 200 {object} map[string]interface{}
162+
// @Failure 400 {object} map[string]interface{}
163+
// @Failure 500 {object} map[string]interface{}
164+
// @Router /api/v1/repositories/{repositoryID}/pull-requests [put]
165+
func (rc *ReviewsController) RefreshPullRequests(c *fiber.Ctx) error {
166+
repoID, err := utils.ReadUintPathParam(c, "repositoryID")
167+
if err != nil {
168+
return err
169+
}
170+
userID := middlewares.GetUserID(c)
171+
172+
githubClient, ok := c.Locals("githubClient").(utils.GithubClient)
173+
if !ok {
174+
return fiber.NewError(fiber.StatusInternalServerError, "Github client not available")
175+
}
176+
177+
tx := db.GetDBTransaction(c)
178+
ctx := context.Background()
179+
if err := rc.reviewsService.RefreshPullRequests(ctx, tx, githubClient, userID, repoID); err != nil {
180+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
181+
"message": "Could not update pull requests",
182+
"error": err.Error(),
183+
})
184+
}
185+
186+
return c.Status(fiber.StatusOK).JSON(fiber.Map{
187+
"message": "Successfully refreshed pull requests",
188+
})
155189
}
156190

157191
// @Summary Get pull request

cmd/internal/repositories/reviews_repositories.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ func (r *ReviewsRepository) GetPullRequest(tx *gorm.DB, prID uint) (*models.Pull
8181
return &pr, nil
8282
}
8383

84+
func (r *ReviewsRepository) UpdatePullRequestStatuses(tx *gorm.DB, prs []*models.PullRequest) error {
85+
if len(prs) == 0 {
86+
return nil
87+
}
88+
for _, pr := range prs {
89+
if err := tx.Model(&models.PullRequest{}).Where("id = ?", pr.ID).Updates(models.PullRequest{State: pr.State}).Error; err != nil {
90+
return fmt.Errorf("failed to update pull requests: %v", err)
91+
}
92+
}
93+
94+
return nil
95+
}
96+
8497
// GetReviews returns all reviews for a pull request
8598
func (r *ReviewsRepository) GetReviews(tx *gorm.DB, repoID, prID uint) ([]*models.Review, error) {
8699
var reviews []*models.Review

cmd/internal/routes/reviews_routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func RegisterReviewsRoutes(apiV1 fiber.Router, reviewsController *controllers.Re
1414
router.Get(":repositoryID", reviewsController.GetRepository)
1515

1616
router.Get("/:repositoryID/pull-requests", reviewsController.GetPullRequests)
17-
router.Put("/:repositoryID/pull-requests/:prID", reviewsController.UpdatePullRequest)
17+
router.Put("/:repositoryID/pull-requests", reviewsController.RefreshPullRequests)
1818
router.Get("/:repositoryID/pull-requests/:prID", reviewsController.GetPullRequest)
1919

2020
router.Get("/:repositoryID/pull-requests/:prID/reviews", reviewsController.GetReviews)

cmd/internal/services/reviews_services.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,75 @@ func (rs *ReviewsService) GetRepository(tx *gorm.DB, repoID uint) (*responses.Ge
9797
return response, nil
9898
}
9999

100+
func (rs *ReviewsService) RefreshPullRequests(ctx context.Context, tx *gorm.DB, githubClient utils.GithubClient, userID, repoID uint) error {
101+
repository, err := rs.reviewsRepository.GetRepository(tx, repoID)
102+
if err != nil {
103+
return err
104+
}
105+
106+
existingPRs, err := rs.reviewsRepository.GetPullRequests(tx, userID, repoID)
107+
if err != nil {
108+
return err
109+
}
110+
111+
currentOpenPRs, err := githubClient.ListPullRequests(ctx, repository.Name, repository.Owner, userID)
112+
if err != nil {
113+
return err
114+
}
115+
116+
// find all new open PRs that were added since last refresh
117+
var newPRs []*models.PullRequest
118+
for _, pr := range currentOpenPRs {
119+
found := false
120+
for _, existingPR := range existingPRs {
121+
if pr.Number == existingPR.Number {
122+
found = true
123+
break
124+
}
125+
}
126+
if !found {
127+
newPRs = append(newPRs, &models.PullRequest{
128+
RepositoryID: repoID,
129+
Number: pr.Number,
130+
Title: pr.Title,
131+
URL: pr.URL,
132+
State: pr.State,
133+
LastCommit: pr.LastCommit,
134+
})
135+
}
136+
}
137+
log.Printf("Found %d new PRs\n", len(newPRs))
138+
139+
// find PRs that are no longer open
140+
var closedPRs []*models.PullRequest
141+
for _, existingPR := range existingPRs {
142+
if existingPR.State == constants.PRStateClosed {
143+
continue
144+
}
145+
found := false
146+
for _, pr := range currentOpenPRs {
147+
if pr.Number == existingPR.Number {
148+
found = true
149+
break
150+
}
151+
}
152+
if !found {
153+
existingPR.State = constants.PRStateClosed
154+
closedPRs = append(closedPRs, existingPR)
155+
}
156+
}
157+
log.Printf("Found %d now closed PRs\n", len(closedPRs))
158+
159+
if err := rs.reviewsRepository.CreatePullRequests(tx, newPRs); err != nil {
160+
return err
161+
}
162+
if err := rs.reviewsRepository.UpdatePullRequestStatuses(tx, closedPRs); err != nil {
163+
return err
164+
}
165+
166+
return nil
167+
}
168+
100169
func (rs *ReviewsService) findPullRequests(ctx context.Context, githubClient utils.GithubClient, repo *models.Repository, userID uint) ([]*models.PullRequest, error) {
101170
fetched_prs, err := githubClient.ListPullRequests(ctx, repo.Name, repo.Owner, userID)
102171
if err != nil {

docs/docs.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,56 @@ const docTemplate = `{
340340
}
341341
}
342342
}
343+
},
344+
"put": {
345+
"security": [
346+
{
347+
"BearerAuth": []
348+
}
349+
],
350+
"description": "Update a pull request",
351+
"consumes": [
352+
"application/json"
353+
],
354+
"produces": [
355+
"application/json"
356+
],
357+
"tags": [
358+
"reviews"
359+
],
360+
"summary": "Update pull request",
361+
"parameters": [
362+
{
363+
"type": "string",
364+
"description": "Repository ID",
365+
"name": "repositoryID",
366+
"in": "path",
367+
"required": true
368+
}
369+
],
370+
"responses": {
371+
"200": {
372+
"description": "OK",
373+
"schema": {
374+
"type": "object",
375+
"additionalProperties": true
376+
}
377+
},
378+
"400": {
379+
"description": "Bad Request",
380+
"schema": {
381+
"type": "object",
382+
"additionalProperties": true
383+
}
384+
},
385+
"500": {
386+
"description": "Internal Server Error",
387+
"schema": {
388+
"type": "object",
389+
"additionalProperties": true
390+
}
391+
}
392+
}
343393
}
344394
},
345395
"/api/v1/repositories/{repositoryID}/pull-requests/{prID}": {

docs/swagger.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,56 @@
332332
}
333333
}
334334
}
335+
},
336+
"put": {
337+
"security": [
338+
{
339+
"BearerAuth": []
340+
}
341+
],
342+
"description": "Update a pull request",
343+
"consumes": [
344+
"application/json"
345+
],
346+
"produces": [
347+
"application/json"
348+
],
349+
"tags": [
350+
"reviews"
351+
],
352+
"summary": "Update pull request",
353+
"parameters": [
354+
{
355+
"type": "string",
356+
"description": "Repository ID",
357+
"name": "repositoryID",
358+
"in": "path",
359+
"required": true
360+
}
361+
],
362+
"responses": {
363+
"200": {
364+
"description": "OK",
365+
"schema": {
366+
"type": "object",
367+
"additionalProperties": true
368+
}
369+
},
370+
"400": {
371+
"description": "Bad Request",
372+
"schema": {
373+
"type": "object",
374+
"additionalProperties": true
375+
}
376+
},
377+
"500": {
378+
"description": "Internal Server Error",
379+
"schema": {
380+
"type": "object",
381+
"additionalProperties": true
382+
}
383+
}
384+
}
335385
}
336386
},
337387
"/api/v1/repositories/{repositoryID}/pull-requests/{prID}": {

docs/swagger.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,39 @@ paths:
272272
summary: Get pull requests
273273
tags:
274274
- reviews
275+
put:
276+
consumes:
277+
- application/json
278+
description: Update a pull request
279+
parameters:
280+
- description: Repository ID
281+
in: path
282+
name: repositoryID
283+
required: true
284+
type: string
285+
produces:
286+
- application/json
287+
responses:
288+
"200":
289+
description: OK
290+
schema:
291+
additionalProperties: true
292+
type: object
293+
"400":
294+
description: Bad Request
295+
schema:
296+
additionalProperties: true
297+
type: object
298+
"500":
299+
description: Internal Server Error
300+
schema:
301+
additionalProperties: true
302+
type: object
303+
security:
304+
- BearerAuth: []
305+
summary: Update pull request
306+
tags:
307+
- reviews
275308
/api/v1/repositories/{repositoryID}/pull-requests/{prID}:
276309
get:
277310
consumes:

pkg/utils/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ func (c *GithubClient) ListPullRequests(ctx context.Context, repoName, repoOwner
8282

8383
var prs []*GithubPullRequest
8484
for {
85-
fetched_prs, resp, err := c.client.PullRequests.List(ctx, repoOwner, repoName, opts)
85+
fetchedPRs, resp, err := c.client.PullRequests.List(ctx, repoOwner, repoName, opts)
8686
if err != nil {
8787
if r, _ := err.(*github.ErrorResponse); r.Response.StatusCode == 404 {
88-
fetched_prs = []*github.PullRequest{}
88+
fetchedPRs = []*github.PullRequest{}
8989
} else {
9090
return nil, err
9191
}
9292
}
9393

94-
for _, pr := range fetched_prs {
94+
for _, pr := range fetchedPRs {
9595
number := *pr.Number
9696
if number < 0 {
9797
number = 0

0 commit comments

Comments
 (0)