Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: 'recursive'

# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x.x
dotnet-version: 9.x.x

# Execute the build
- name: Execute Release Build
Expand All @@ -49,12 +51,14 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: 'recursive'

# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x.x
dotnet-version: 9.x.x

# Execute the tests
- name: Execute Tests
Expand Down Expand Up @@ -92,6 +96,8 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: 'recursive'

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand All @@ -114,7 +120,7 @@ jobs:
- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x.x
dotnet-version: 9.x.x

# Execute the build
- name: Execute Build
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
RUN ls -a

COPY ["src/Nullinside.Cicd.GitHub/Nullinside.Cicd.GitHub.csproj", "Nullinside.Cicd.GitHub/"]
RUN dotnet restore "Nullinside.Cicd.GitHub/Nullinside.Cicd.GitHub.csproj"
COPY . .
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: nullinside-cicd-github
services:
nullinside-cicd-github:
build:
context: .
tags:
- "nullinside-cicd-github:latest"
logging:
driver: loki
options:
loki-url: 'http://192.168.1.4:3100/loki/api/v1/push'
container_name: nullinside-cicd-github
environment:
- GITHUB_PAT
ports:
- 8081:8080
- 8082:8081
restart: unless-stopped
image: nullinside-cicd-github:latest
7 changes: 3 additions & 4 deletions go.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
docker build -t nullinside-cicd-github:latest .
docker container stop nullinside-cicd-github
docker container prune -f
docker run -d --name=nullinside-cicd-github -e GITHUB_PAT=$GITHUB_PAT --restart unless-stopped nullinside-cicd-github:latest
docker compose build
docker compose down
docker compose up -d
10 changes: 10 additions & 0 deletions src/Nullinside.Cicd.GitHub/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
namespace Nullinside.Cicd.GitHub;

/// <summary>
/// Constants used throughout the appliction.
/// </summary>
public static class Constants {
/// <summary>
/// The github project name
/// </summary>
public const string GITHUB_ORG = "nullinside-development-group";

/// <summary>
/// The github project's unique identifier on github.
/// </summary>
public const int GITHUB_PROJECT_NUM = 1;
}
9 changes: 5 additions & 4 deletions src/Nullinside.Cicd.GitHub/Nullinside.Cicd.GitHub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<LangVersion>default</LangVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -18,12 +19,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="log4net" Version="3.0.4" />
<PackageReference Include="Octokit" Version="14.0.0" />
<PackageReference Include="log4net" Version="3.0.4"/>
<PackageReference Include="Octokit" Version="14.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\octokit.graphql.net\src\Octokit.GraphQL\Octokit.GraphQL.csproj" />
<ProjectReference Include="..\octokit.graphql.net\src\Octokit.GraphQL\Octokit.GraphQL.csproj"/>
</ItemGroup>

</Project>
45 changes: 25 additions & 20 deletions src/Nullinside.Cicd.GitHub/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@
.Where(o => null != o)
.ToArray();

var client = new GitHubClient(new ProductHeaderValue("nullinside")) {
Credentials = new Credentials(Environment.GetEnvironmentVariable("GITHUB_PAT"))
};

var graphQl = new Connection(new Octokit.GraphQL.ProductHeaderValue("nullinside"),
Environment.GetEnvironmentVariable("GITHUB_PAT"));

ID projectId = await graphQl.Run(new Query()
.Organization(Constants.GITHUB_ORG)
.ProjectV2(Constants.GITHUB_PROJECT_NUM)
.Select(p => p.Id));

IReadOnlyList<Repository>? repository = await client.Repository.GetAllForOrg(Constants.GITHUB_ORG);
foreach (Repository repo in repository) {
if (repo.Private) {
continue;
}

foreach (IRepoRule? rule in rules) {
await rule.Handle(client, graphQl, projectId, repo);
while (true) {
var client = new GitHubClient(new ProductHeaderValue("nullinside")) {
Credentials = new Credentials(Environment.GetEnvironmentVariable("GITHUB_PAT"))
};

var graphQl = new Connection(new Octokit.GraphQL.ProductHeaderValue("nullinside"),
Environment.GetEnvironmentVariable("GITHUB_PAT"));

ID projectId = await graphQl.Run(new Query()
.Organization(Constants.GITHUB_ORG)
.ProjectV2(Constants.GITHUB_PROJECT_NUM)
.Select(p => p.Id));

IReadOnlyList<Repository>? repository = await client.Repository.GetAllForOrg(Constants.GITHUB_ORG);
foreach (Repository repo in repository) {
if (repo.Private) {
continue;
}

foreach (IRepoRule? rule in rules) {
await rule!.Handle(client, graphQl, projectId, repo);
}
}

Console.WriteLine("Waiting for next execution time...");
Task.WaitAll(Task.Delay(TimeSpan.FromMinutes(5)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

namespace Nullinside.Cicd.GitHub.Rule;

/// <summary>
/// Handles associating issues with the project automatically.
/// </summary>
public class AssociateIssuesWithProject : IRepoRule {
/// <inheritdoc />
public async Task Handle(GitHubClient client, Connection graphQl, ID projectId, Repository repo) {
if (!repo.HasIssues) {
return;
Expand Down
14 changes: 9 additions & 5 deletions src/Nullinside.Cicd.GitHub/Rule/CreateRulesets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

namespace Nullinside.Cicd.GitHub.Rule;

/// <summary>
/// Creates the branch merging rules based on the code bases' language.
/// </summary>
public class CreateRulesets : IRepoRule {
/// <inheritdoc />
public async Task Handle(GitHubClient client, Connection graphQl, ID projectId, Repository repo) {
// This currently doesn't run properly. You get an error about not specifying multiple Parameters on the status
// checks. Waiting on an update from the source library to not include nulls in the compiled query.
Expand All @@ -18,19 +22,19 @@ public async Task Handle(GitHubClient client, Connection graphQl, ID projectId,
.Rulesets()
.AllPages()
.Select(i => i.Name));

string? expectedRuleset =
rulesets.FirstOrDefault(r => "main".Equals(r, StringComparison.InvariantCultureIgnoreCase));
if (null != expectedRuleset) {
return;
}

ID id = await graphQl.Run(new Query()
.Repository(repo.Name, Constants.GITHUB_ORG)
.Select(i => i.Id));

Console.WriteLine($"{repo.Name}: Creating default ruleset");
StatusCheckConfigurationInput[] statusChecks = null;
StatusCheckConfigurationInput[]? statusChecks = null;
if ("Typescript".Equals(repo.Language, StringComparison.InvariantCultureIgnoreCase)) {
statusChecks = new[] {
new StatusCheckConfigurationInput {
Expand Down Expand Up @@ -90,7 +94,7 @@ public async Task Handle(GitHubClient client, Connection graphQl, ID projectId,
}
});
}

await graphQl.Run(new Mutation()
.CreateRepositoryRuleset(new Arg<CreateRepositoryRulesetInput>(new CreateRepositoryRulesetInput {
SourceId = id,
Expand Down
11 changes: 11 additions & 0 deletions src/Nullinside.Cicd.GitHub/Rule/IRepoRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@

namespace Nullinside.Cicd.GitHub.Rule;

/// <summary>
/// Contracts for executing code against a project.
/// </summary>
public interface IRepoRule {
/// <summary>
/// Handles performing the rule updates.
/// </summary>
/// <param name="client">The github rest api client.</param>
/// <param name="graphQl">The github graphql client.</param>
/// <param name="projectId">The unique identifier of the project.</param>
/// <param name="repo">The git repo being updated.</param>
/// <returns></returns>
Task Handle(GitHubClient client, Connection graphQl, ID projectId, Repository repo);
}
Loading