Skip to content

Commit 76bb2dc

Browse files
committed
workflows updated, Iinitializer setup fixed for dynamic additions using scutor
1 parent 7f7daf3 commit 76bb2dc

File tree

8 files changed

+74
-31
lines changed

8 files changed

+74
-31
lines changed

.github/dependabot.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
version: 2
77
updates:
8-
# Enable version updates for Docker
8+
- package-ecosystem: "nuget"
9+
directory: "/src/RandomAPI"
10+
schedule:
11+
interval: "weekly"
12+
913
- package-ecosystem: "docker"
10-
# Look for a `Dockerfile` in the src/randomdapi directory
11-
directory: "src/RandomAPI/"
12-
# Check for updates once a week
14+
directory: "/src/RandomAPI"
1315
schedule:
1416
interval: "weekly"
1517

16-
# Enable version updates for GitHub Actions
1718
- package-ecosystem: "github-actions"
18-
# Workflow files stored in the default location of `.github/workflows`
19-
# You don't need to specify `/.github/workflows` for `directory`. You can use
19+
directory: "/"
20+
schedule:
21+
interval: "weekly"

.github/workflows/CodeQL_PR_Analysis.yml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,51 @@ name: "CodeQL PR Analysis"
22

33
on:
44
pull_request:
5+
# Triggers analysis on pull requests targeting the main branch
56
branches: [ main ]
6-
7+
push:
8+
# Also good practice to run on pushes to main for full analysis
9+
branches: [ main ]
10+
711
jobs:
812
analyze:
913
name: Analyze code with CodeQL
14+
# Use newer, stable runner
1015
runs-on: ubuntu-latest
16+
17+
# Permission setup is highly recommended for security analysis
18+
permissions:
19+
security-events: write
20+
actions: read
21+
contents: read
1122

1223
steps:
1324
- name: Checkout repository
14-
uses: actions/checkout@v3
25+
# Use the latest stable version
26+
uses: actions/checkout@v4
27+
with:
28+
# Important for CodeQL to fetch history
29+
fetch-depth: 0
30+
31+
# NEW: Setup the .NET SDK environment
32+
- name: Setup .NET
33+
uses: actions/setup-dotnet@v4
34+
with:
35+
# Assuming you are targeting .NET 8, adjust if needed
36+
dotnet-version: '8.0.x'
1537

1638
- name: Initialize CodeQL
17-
uses: github/codeql-action/init@v2
39+
uses: github/codeql-action/init@v3
1840
with:
1941
languages: csharp
2042
# Using default queries (security + quality)
21-
# You can also specify custom queries if needed
2243

23-
- name: Autobuild
24-
uses: github/codeql-action/autobuild@v2
44+
# FIX: Removed 'github/codeql-action/autobuild'.
45+
# C# projects must be built explicitly so CodeQL can monitor the compiler output.
46+
- name: Build Project
47+
# Run dotnet build on your project directory to compile the code
48+
# Based on your Dependabot config, the project is in 'src/RandomAPI'
49+
run: dotnet build src/RandomAPI/RandomAPI.csproj
2550

2651
- name: Perform CodeQL Analysis
27-
uses: github/codeql-action/analyze@v2
52+
uses: github/codeql-action/analyze@v3

src/RandomAPI/Program.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
DBInitialization.CONNECTIONSTRING
1717
));
1818

19+
#region Add Services
1920
//webhook
2021
builder.Services.AddSingleton<IWebhookService, WebhookService>();
2122

@@ -24,18 +25,33 @@
2425

2526
//db
2627
builder.Services.AddScoped<IEventRepository, EventRepository>();
28+
builder.Services.AddScoped<IWebhookRepository, WebhookRepository>();
2729

30+
#endregion
31+
32+
#region Initialization
33+
//scanner for initializations
34+
builder.Services.Scan(scan => scan
35+
.FromAssemblyOf<IInitializer>()
36+
.AddClasses(c => c.AssignableTo<IInitializer>())
37+
.As<IInitializer>()
38+
.WithScopedLifetime()
39+
);
2840

2941
var app = builder.Build();
30-
await DBInitialization.EnsureDb(app.Services);
3142
//the the end, init the dbs
3243
using (var scope = app.Services.CreateScope())
3344
{
3445
IServiceProvider? serviceProvider = scope.ServiceProvider;
3546
IEnumerable<IInitializer>? initializers = serviceProvider.GetServices<IInitializer>();
47+
if (!initializers.Any())
48+
{
49+
Console.WriteLine("Warning: No services implementing IInitializer were found.");
50+
}
3651
IEnumerable<Task>? initializationTasks = initializers.Select(i => i.InitializeAsync());
3752
await Task.WhenAll(initializationTasks);
3853
}
54+
#endregion
3955

4056
// Configure the HTTP request pipeline.
4157
if (app.Environment.IsDevelopment())

src/RandomAPI/RandomAPI.csproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
<ItemGroup>
1212
<PackageReference Include="Dapper" Version="2.1.66" />
1313
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.0" />
14-
<PackageReference
15-
Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets"
16-
Version="1.22.1"
17-
/>
14+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.22.1" />
1815
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
16+
<PackageReference Include="Scrutor" Version="7.0.0" />
1917
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
2018
</ItemGroup>
2119
</Project>

src/RandomAPI/Repository/EventRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace RandomAPI.Repository
77
{
8-
public class EventRepository : IEventRepository
8+
public class EventRepository : IEventRepository, IInitializer
99
{
1010
private readonly IDbConnection _db;
1111
private readonly ILogger<EventRepository> _logger;

src/RandomAPI/Repository/IEventRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RandomAPI.Repository
55
/// <summary>
66
/// Defines the contract for persistence operations related to the Event model.
77
/// </summary>
8-
public interface IEventRepository : IInitializer
8+
public interface IEventRepository
99
{
1010
/// <summary>
1111
/// Attempts to add a new Event record to the database.

src/RandomAPI/Repository/IWebhookRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RandomAPI.Repository
55
/// <summary>
66
/// Defines the contract for persistence operations related to WebhookUrl models.
77
/// </summary>
8-
public interface IWebhookRepository : IInitializer
8+
public interface IWebhookRepository
99
{
1010
Task<IEnumerable<WebhookUrl>> GetAllUrlsAsync();
1111
Task AddUrlAsync(string url);

src/RandomAPI/Repository/WebhookRepository.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using RandomAPI.Models;
1+
using Dapper;
2+
using RandomAPI.Models;
3+
using System.Data;
24

35
namespace RandomAPI.Repository
46
{
57

68
/// <summary>
79
/// Implements CRUD operations for Webhook URLs using Dapper and the provided DatabaseService.
810
/// </summary>
9-
public class WebhookRepository : IWebhookRepository
11+
public class WebhookRepository : IWebhookRepository, IInitializer
1012
{
11-
private readonly DatabaseService _dbService;
13+
private readonly IDbConnection _db;
1214

13-
public WebhookRepository(DatabaseService dbService)
15+
public WebhookRepository(IDbConnection dbService)
1416
{
15-
_dbService = dbService;
17+
_db = dbService;
1618
}
1719

1820
/// <summary>
@@ -27,7 +29,7 @@ CREATE TABLE IF NOT EXISTS WebhookUrls (
2729
Url TEXT NOT NULL UNIQUE
2830
);";
2931

30-
await _dbService.ExecuteAsync(sql);
32+
await _db.ExecuteAsync(sql);
3133
}
3234

3335
/// <summary>
@@ -38,7 +40,7 @@ public async Task<IEnumerable<WebhookUrl>> GetAllUrlsAsync()
3840
{
3941
const string sql = "SELECT Id, Url FROM WebhookUrls ORDER BY Id;";
4042
// Dapper maps the columns to the WebhookUrl model properties
41-
return await _dbService.QueryAsync<WebhookUrl>(sql);
43+
return await _db.QueryAsync<WebhookUrl>(sql);
4244
}
4345

4446
/// <summary>
@@ -49,7 +51,7 @@ public async Task AddUrlAsync(string url)
4951
{
5052
// SQLITE specific command to ignore unique constraint errors if URL already exists
5153
const string sql = "INSERT OR IGNORE INTO WebhookUrls (Url) VALUES (@Url);";
52-
await _dbService.ExecuteAsync(sql, new { Url = url });
54+
await _db.ExecuteAsync(sql, new { Url = url });
5355
}
5456

5557
/// <summary>
@@ -60,7 +62,7 @@ public async Task AddUrlAsync(string url)
6062
public async Task<int> DeleteUrlAsync(string url)
6163
{
6264
const string sql = "DELETE FROM WebhookUrls WHERE Url = @Url;";
63-
return await _dbService.ExecuteAsync(sql, new { Url = url });
65+
return await _db.ExecuteAsync(sql, new { Url = url });
6466
}
6567
}
6668
}

0 commit comments

Comments
 (0)