-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create initial DataSync project * Move GithubPushWebHookPayload model * Initial app working with hard coded commit * Getting commits for each file * Note * DataSync working * Adding auth to GitHub api calls * Adding GitHub PAT env var * Change order of param * Underscores rather than : * Add incremental to deployment mode * Remove unique name as a param Build it from RG name * Remove hyphens from unique name * Revert location * Output uniqueName * hard code name * Remove hyphen from RG name * Use params with defaults * Capitalisation * Revert to V1 * Passing unique label as param * Build params file * Remove at * toLower and Label rather than name * Label rather than name * New RG name and unique label * Fail on error * Updated params for both dev and prod * Adding manual depends * Remove depends * new label * use consistent label for prod
- Loading branch information
1 parent
24792c2
commit e7468f4
Showing
16 changed files
with
279 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoFixture" Version="4.18.1" /> | ||
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" /> | ||
<ProjectReference Include="..\Domain\Domain.csproj" /> | ||
<PackageReference Include="Moq" Version="4.20.72" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.development.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System.Net.Http.Headers; | ||
using System.Runtime.CompilerServices; | ||
using Domain.Models; | ||
using System.Text; | ||
using System.Text.Json; | ||
using AutoFixture; | ||
using AutoFixture.AutoMoq; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace DataSync; | ||
|
||
class Program | ||
{ | ||
static async Task Main(string[] args) | ||
{ | ||
// Setup configuration | ||
var builder = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) // Set the base path | ||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Load default appsettings.json | ||
.AddJsonFile("appsettings.development.json", optional: true, reloadOnChange: true) // Load development-specific settings | ||
.AddEnvironmentVariables(); // Optionally add environment variables | ||
IConfiguration configuration = builder.Build(); | ||
var functionUrl = configuration["FunctionUrl"]!; | ||
var gitHubPat = configuration["GitHubPAT"]!; | ||
|
||
// Get GH blogs | ||
var files = await GetGithubFiles("martinkearn", "Content", "Blogs", gitHubPat); // These values ARE case senitive | ||
Console.WriteLine($"Got {files.Count} files from GitHub"); | ||
foreach (var file in files) | ||
{ | ||
Console.WriteLine($"Processing File: {file.Path}"); | ||
|
||
// Get Commit | ||
var commit = await GetGithubLastCommit("martinkearn", "Content", file.Path, gitHubPat); | ||
|
||
// Create Fixture | ||
var fixture = CreateFixture($"Updated {file.Path}", commit.Url, file.Path); | ||
|
||
// Send to Function | ||
if (functionUrl != null) await CallFunction(functionUrl, fixture); | ||
|
||
await Task.Delay(10000); // Pause for 10 seconds | ||
|
||
Console.WriteLine($"Processed File: {file.Path}"); | ||
|
||
//Console.WriteLine("Press any key to continue..."); | ||
//Console.ReadKey(); // Waits for the user to press any key | ||
|
||
Console.WriteLine(""); | ||
|
||
} | ||
|
||
Console.WriteLine("COMPLETED"); | ||
} | ||
|
||
|
||
private static GithubPushWebhookPayload CreateFixture(string message, string commitUrl, string modifiedPath) | ||
{ | ||
var fixture = new Fixture().Customize(new AutoMoqCustomization()); | ||
var ghWh = fixture.Create<GithubPushWebhookPayload>(); | ||
ghWh.Repository.Name = "Content"; | ||
ghWh.HeadCommit.Message = message; | ||
ghWh.HeadCommit.Url = commitUrl; | ||
ghWh.HeadCommit.Timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz"); | ||
ghWh.HeadCommit.Author = new Author() | ||
{ | ||
Name = "Martin Kearn", | ||
Email = "martin.kearn@microsoft.com", | ||
Username = "martinkearn" | ||
}; | ||
ghWh.HeadCommit.Added = []; | ||
ghWh.HeadCommit.Removed = []; | ||
ghWh.HeadCommit.Modified = [modifiedPath]; | ||
var commit = new Commit() | ||
{ | ||
Id = ghWh.HeadCommit.Id, | ||
TreeId = ghWh.HeadCommit.TreeId, | ||
Distinct = ghWh.HeadCommit.Distinct, | ||
Message = ghWh.HeadCommit.Message, | ||
Timestamp = Convert.ToDateTime(ghWh.HeadCommit.Timestamp), | ||
Url = ghWh.HeadCommit.Url, | ||
Author = ghWh.HeadCommit.Author, | ||
Committer = ghWh.HeadCommit.Committer, | ||
Added = [], | ||
Removed = [], | ||
Modified = ghWh.HeadCommit.Modified | ||
}; | ||
ghWh.Commits = | ||
[ | ||
commit | ||
]; | ||
|
||
return ghWh; | ||
} | ||
|
||
private static async Task<List<GithubFile>> GetGithubFiles(string repoOwner, string repoName, string folderPath, string pat) | ||
{ | ||
var url = $"https://api.github.com/repos/{repoOwner}/{repoName}/contents/{folderPath}"; | ||
using var client = new HttpClient(); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", pat); | ||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; dotnet)"); //(GitHub requires this) | ||
var response = await client.GetAsync(url); | ||
response.EnsureSuccessStatusCode(); | ||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
var files = JsonSerializer.Deserialize<GithubFile[]>(responseBody); | ||
|
||
return files!.ToList(); | ||
} | ||
|
||
private static async Task<Commit> GetGithubLastCommit(string repoOwner, string repoName, string filePath, string pat) | ||
{ | ||
var url = $"https://api.github.com/repos/{repoOwner}/{repoName}/commits?path={filePath}&sha=master"; | ||
using var client = new HttpClient(); | ||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", pat); | ||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; dotnet)"); //(GitHub requires this) | ||
var response = await client.GetAsync(url); | ||
response.EnsureSuccessStatusCode(); | ||
var responseBody = await response.Content.ReadAsStringAsync(); | ||
var commits = JsonSerializer.Deserialize<Commit[]>(responseBody); | ||
|
||
return commits.FirstOrDefault(); | ||
} | ||
|
||
private static async Task CallFunction(string functionUrl, GithubPushWebhookPayload data) | ||
{ | ||
using var client = new HttpClient(); | ||
var jsonData = JsonSerializer.Serialize(data); | ||
var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); | ||
var response = await client.PostAsync(functionUrl, content); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var result = await response.Content.ReadAsStringAsync(); | ||
Console.WriteLine("Response received successfully:"); | ||
Console.WriteLine(result); | ||
} | ||
else | ||
{ | ||
Console.WriteLine($"Failed to send POST request. Status Code: {response.StatusCode}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"FunctionUrl": "", | ||
"GitHubPAT": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,5 @@ | |
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Interfaces\" /> | ||
<Folder Include="Models\" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Domain.Models | ||
{ | ||
/// <summary> | ||
/// Used to strongly type the "GithubConfiguration" appsettings section | ||
/// </summary> | ||
public class GithubConfiguration | ||
{ | ||
/// <summary> | ||
/// PAT for acessing API. | ||
/// </summary> | ||
public string Pat { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Domain.Models; | ||
|
||
public class GithubFile | ||
{ | ||
[JsonPropertyName("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonPropertyName("path")] | ||
public string Path { get; set; } | ||
|
||
[JsonPropertyName("type")] | ||
public string Type { get; set; } | ||
|
||
[JsonPropertyName("download_url")] | ||
public string DownloadUrl { get; set; } | ||
} |
Oops, something went wrong.