Skip to content

Commit da94fc6

Browse files
committed
GetOpenPrs in progress
1 parent 778910e commit da94fc6

File tree

4 files changed

+691
-96
lines changed

4 files changed

+691
-96
lines changed

NotificationsService/NotificationsService/Controllers/PrController.cs

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using Microsoft.Extensions.Logging;
44
using System.Security.Cryptography.Xml;
55
using NotificationsService.Services;
6+
using NotificationsService.Models;
7+
using Amazon.DynamoDBv2.Model;
68

79
namespace NotificationsService.Services
810
{
@@ -29,7 +31,7 @@ public IActionResult GetHealth()
2931
public IActionResult ListRoutes()
3032
{
3133
return Ok(new[] {
32-
"GET /api/pr",
34+
"GET /api/pr/openprs",
3335
"GET /api/pr/health",
3436
"GET /api/pr/details?id=&repo=",
3537
"GET /api/pr/review?prNumber=",
@@ -38,12 +40,49 @@ public IActionResult ListRoutes()
3840
});
3941
}
4042

41-
[HttpGet]
42-
public IActionResult GetOpenPrs()
43+
[HttpGet("openprs")]
44+
public async Task<IActionResult> GetOpenPrs()
4345
{
44-
_logger.LogInformation("Received request to get open PRs.");
45-
var prs = _prService.GetAllPrs();
46-
return Ok(prs);
46+
_logger.LogInformation("Received request to get all open PRs.");
47+
try
48+
{
49+
var dynamoService = new DynamoService(_logger);
50+
_logger.LogInformation("Received request to get PR Details.");
51+
var allReviews = await dynamoService.GetAllReviewsAsync();
52+
// Create a list to hold the processed reviews
53+
var processedReviews = new List<object>();
54+
// Process each review item using the shared parsing method
55+
foreach (var prItem in allReviews)
56+
{
57+
try
58+
{
59+
var reviewDetails = GetPrItemDetails(prItem);
60+
if (reviewDetails != null)
61+
{
62+
processedReviews.Add(reviewDetails);
63+
_logger.LogDebug($"Processed review with ID: {((dynamic)reviewDetails).id}");
64+
}
65+
else
66+
{
67+
_logger.LogWarning("Skipping review item with missing required fields");
68+
}
69+
}
70+
catch (Exception ex)
71+
{
72+
_logger.LogError(ex, "Error processing individual review item");
73+
// Continue with next item rather than failing the entire request
74+
}
75+
}
76+
77+
_logger.LogInformation($"Successfully processed {processedReviews.Count} reviews");
78+
return Ok(processedReviews);
79+
}
80+
catch (Exception ex)
81+
{
82+
_logger.LogError(ex, $"Caught exception in GetDetails().");
83+
}
84+
return NotFound();
85+
4786
}
4887

4988
// 1. Get PR Details (already assumed)
@@ -81,16 +120,6 @@ public async Task<IActionResult> GetDetails([FromQuery] int id, string repo)
81120
_logger.LogError(ex, $"Caught exception in GetDetails().");
82121
}
83122
return NotFound();
84-
//var detail = new
85-
//{
86-
// id = id,
87-
// title = $"Pull Request #{id} Title Example",
88-
// status = (id % 2 == 0) ? "Open" : "Closed",
89-
// author = "John Doe",
90-
// date = "2024-04-28"
91-
//};
92-
93-
//return Ok(detail);
94123
}
95124

96125
// 2. Get PR Review text
@@ -108,8 +137,8 @@ public IActionResult GetReview([FromQuery] int prNumber)
108137
[HttpPost("feedback")]
109138
public IActionResult PostFeedback([FromBody] FeedbackRequest request)
110139
{
111-
// You could log/store this feedback if needed
112-
Console.WriteLine($"Received feedback: PR#{request.PrNumber}, Vote: {request.Vote}");
140+
_logger.LogInformation($"Received feedback: PR#{request.prNumber}, Vote: {request.vote}");
141+
//Console.WriteLine($"Received feedback: PR#{request.PrNumber}, Vote: {request.Vote}");
113142

114143
return Ok(new { message = "Feedback received" });
115144
}
@@ -118,24 +147,45 @@ public IActionResult PostFeedback([FromBody] FeedbackRequest request)
118147
[HttpPost("decision")]
119148
public IActionResult PostDecision([FromBody] DecisionRequest request)
120149
{
121-
// You could process/store this action if needed
122-
Console.WriteLine($"Received decision: PR#{request.PrNumber}, Decision: {request.Decision}");
123-
150+
_logger.LogInformation($"Received decision: PR#{request.prNumber}, Decision: {request.decision}");
151+
// call the git service to submit the decision to Git
124152
return Ok(new { message = "Decision received" });
125153
}
154+
public object GetPrItemDetails(Dictionary<string, AttributeValue> prItem)
155+
{
156+
if (prItem == null || !prItem.ContainsKey("metadata") || !prItem.ContainsKey("reviewTitle") || !prItem.ContainsKey("review"))
157+
{
158+
return null;
159+
}
160+
161+
var metadataMap = prItem["metadata"].M;
162+
163+
return new
164+
{
165+
id = metadataMap.ContainsKey("pr_number") ? metadataMap["pr_number"].S : "Unknown",
166+
title = prItem["reviewTitle"].S,
167+
author = metadataMap.ContainsKey("user_login") ? metadataMap["user_login"].S : "Unknown",
168+
date = metadataMap.ContainsKey("created_at") ? metadataMap["created_at"].S : "Unknown",
169+
status = metadataMap.ContainsKey("pr_state") ? metadataMap["pr_state"].S : "Unknown",
170+
prurl = metadataMap.ContainsKey("html_url") ? metadataMap["html_url"].S : "Unknown",
171+
repo = metadataMap.ContainsKey("repo") ? metadataMap["repo"].S : "Unknown",
172+
review = prItem["review"].S
173+
};
174+
}
175+
126176
}
127177

128178
// Request DTOs
129179
public class FeedbackRequest
130180
{
131-
public int PrNumber { get; set; }
132-
public string Vote { get; set; } // \"up\" or \"down\"
181+
public int prNumber { get; set; }
182+
public string vote { get; set; } // \"up\" or \"down\"
133183
}
134184

135185
public class DecisionRequest
136186
{
137-
public int PrNumber { get; set; }
138-
public string Decision { get; set; } // \"Approve\" or \"Request Changes\"
187+
public int prNumber { get; set; }
188+
public string decision { get; set; } // \"Approve\" or \"Request Changes\"
139189
}
140190
}
141191

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NotificationsService.Services
2+
{
3+
public class GitService
4+
{
5+
}
6+
}

0 commit comments

Comments
 (0)