Skip to content

Commit

Permalink
Merge pull request #1471 from Catel/GitHubSync-20191106-164237
Browse files Browse the repository at this point in the history
GitHubSync update
  • Loading branch information
GeertvanHorrik committed Nov 6, 2019
2 parents 25900fa + 4cf6899 commit 9de80d3
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 4 deletions.
72 changes: 72 additions & 0 deletions deployment/cake/sourcecontrol-github.cake
@@ -0,0 +1,72 @@
#addin "nuget:?package=Cake.GitHub&version=0.1.0"
#addin "nuget:?package=Octokit&version=0.36.0"

//-------------------------------------------------------------

public class GitHubSourceControl : ISourceControl
{
public GitHubSourceControl(BuildContext buildContext)
{
BuildContext = buildContext;

UserName = buildContext.BuildServer.GetVariable("GitHubUserName", buildContext.General.Repository.Username, showValue: true);
ApiKey = buildContext.BuildServer.GetVariable("GitHubApiKey", buildContext.General.Repository.Password, showValue: false);
OwnerName = buildContext.BuildServer.GetVariable("GitHubOwnerName", buildContext.General.Copyright.Company, showValue: true);
ProjectName = buildContext.BuildServer.GetVariable("GitHubProjectName", buildContext.General.Solution.Name, showValue: true);

if (!string.IsNullOrWhiteSpace(UserName) &&
!string.IsNullOrWhiteSpace(ApiKey) &&
!string.IsNullOrWhiteSpace(OwnerName) &&
!string.IsNullOrWhiteSpace(ProjectName))
{
IsAvailable = true;
}
}

public BuildContext BuildContext { get; private set; }

public string UserName { get; set; }
public string ApiKey { get; set; }
public string OwnerName { get; set; }
public string ProjectName { get; set; }

public string OwnerAndProjectName
{
get { return $"{OwnerName}/{ProjectName}"; }
}

public bool IsAvailable { get; private set; }

public async Task MarkBuildAsPendingAsync(string context, string description)
{
UpdateStatus(GitHubStatusState.Pending, context, description);
}

public async Task MarkBuildAsFailedAsync(string context, string description)
{
UpdateStatus(GitHubStatusState.Failure, context, description);
}

public async Task MarkBuildAsSucceededAsync(string context, string description)
{
UpdateStatus(GitHubStatusState.Success, context, description);
}

private void UpdateStatus(GitHubStatusState state, string context, string description)
{
if (!IsAvailable)
{
return;
}

var commitSha = BuildContext.General.Repository.CommitId;

BuildContext.CakeContext.GitHubStatus(UserName, ApiKey, OwnerName, ProjectName, commitSha, new GitHubStatusSettings
{
State = state,
TargetUrl = null,// "url-to-build-server",
Description = description,
Context = $"Cake - {context}"
});
}
}
63 changes: 63 additions & 0 deletions deployment/cake/sourcecontrol.cake
@@ -0,0 +1,63 @@
// Customize this file when using a different source controls
#l "sourcecontrol-github.cake"

//-------------------------------------------------------------

public interface ISourceControl
{
Task MarkBuildAsPendingAsync(string context, string description);
Task MarkBuildAsFailedAsync(string context, string description);
Task MarkBuildAsSucceededAsync(string context, string description);
}

//-------------------------------------------------------------

public class SourceControlIntegration : IntegrationBase
{
private readonly List<ISourceControl> _sourceControls = new List<ISourceControl>();

public SourceControlIntegration(BuildContext buildContext)
: base(buildContext)
{
_sourceControls.Add(new GitHubSourceControl(buildContext));
}

public async Task MarkBuildAsPendingAsync(string context, string description = null)
{
BuildContext.CakeContext.LogSeparator("Marking build as pending");

context = context ?? "default";
description = description ?? "Build pending";

foreach (var sourceControl in _sourceControls)
{
await sourceControl.MarkBuildAsPendingAsync(context, description);
}
}

public async Task MarkBuildAsFailedAsync(string context, string description = null)
{
BuildContext.CakeContext.LogSeparator("Marking build as failed");

context = context ?? "default";
description = description ?? "Build failed";

foreach (var sourceControl in _sourceControls)
{
await sourceControl.MarkBuildAsFailedAsync(context, description);
}
}

public async Task MarkBuildAsSucceededAsync(string context, string description = null)
{
BuildContext.CakeContext.LogSeparator("Marking build as succeeded");

context = context ?? "default";
description = description ?? "Build succeeded";

foreach (var sourceControl in _sourceControls)
{
await sourceControl.MarkBuildAsSucceededAsync(context, description);
}
}
}
45 changes: 43 additions & 2 deletions deployment/cake/tasks.cake
Expand Up @@ -2,6 +2,7 @@
#l "lib-nuget.cake"
#l "lib-sourcelink.cake"
#l "issuetrackers.cake"
#l "sourcecontrol.cake"
#l "notifications.cake"
#l "generic-tasks.cake"
#l "apps-uwp-tasks.cake"
Expand Down Expand Up @@ -68,6 +69,7 @@ public class BuildContext : BuildContextBase
public BuildServerIntegration BuildServer { get; set; }
public IssueTrackerIntegration IssueTracker { get; set; }
public NotificationsIntegration Notifications { get; set; }
public SourceControlIntegration SourceControl { get; set; }
public OctopusDeployIntegration OctopusDeploy { get; set; }

// Contexts
Expand Down Expand Up @@ -128,6 +130,7 @@ Setup<BuildContext>(setupContext =>
buildContext.IssueTracker = new IssueTrackerIntegration(buildContext);
buildContext.Notifications = new NotificationsIntegration(buildContext);
buildContext.OctopusDeploy = new OctopusDeployIntegration(buildContext);
buildContext.SourceControl = new SourceControlIntegration(buildContext);

setupContext.LogSeparator("Validating build context");

Expand Down Expand Up @@ -219,6 +222,8 @@ Task("Build")
.IsDependentOn("CleanupCode")
.Does<BuildContext>(async buildContext =>
{
await buildContext.SourceControl.MarkBuildAsPendingAsync("Build");
var sonarUrl = buildContext.General.SonarQube.Url;
var enableSonar = !buildContext.General.SonarQube.IsDisabled &&
Expand Down Expand Up @@ -333,20 +338,38 @@ Task("Build")
}
BuildTestProjects(buildContext);
await buildContext.SourceControl.MarkBuildAsSucceededAsync("Build");
})
.OnError<BuildContext>(async (ex, buildContext) =>
{
await buildContext.SourceControl.MarkBuildAsFailedAsync("Build");
throw ex;
});

//-------------------------------------------------------------

Task("Test")
// Note: no dependency on 'build' since we might have already built the solution
.Does<BuildContext>(buildContext =>
{
.Does<BuildContext>(async buildContext =>
{
await buildContext.SourceControl.MarkBuildAsPendingAsync("Test");
foreach (var testProject in buildContext.Tests.Items)
{
buildContext.CakeContext.LogSeparator("Running tests for '{0}'", testProject);
RunUnitTests(buildContext, testProject);
}
await buildContext.SourceControl.MarkBuildAsSucceededAsync("Test");
})
.OnError<BuildContext>(async (ex, buildContext) =>
{
await buildContext.SourceControl.MarkBuildAsFailedAsync("Test");
throw ex;
});

//-------------------------------------------------------------
Expand Down Expand Up @@ -509,6 +532,24 @@ Task("TestNotifications")
await buildContext.Notifications.NotifyErrorAsync("MyProject", "This is an error");
});

//-------------------------------------------------------------

Task("TestSourceControl")
.Does<BuildContext>(async buildContext =>
{
await buildContext.SourceControl.MarkBuildAsPendingAsync("Build");
await System.Threading.Tasks.Task.Delay(5 * 1000);
await buildContext.SourceControl.MarkBuildAsSucceededAsync("Build");
await buildContext.SourceControl.MarkBuildAsPendingAsync("Test");
await System.Threading.Tasks.Task.Delay(5 * 1000);
await buildContext.SourceControl.MarkBuildAsSucceededAsync("Test");
});

//-------------------------------------------------------------
// ACTUAL RUNNER - MUST BE DEFINED AT THE BOTTOM
//-------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.build.shared.implicit.props
Expand Up @@ -31,7 +31,7 @@

<!-- CSharp language -->
<PropertyGroup>
<LangVersion>7.3</LangVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

<!-- Enforce portable pdb format -->
Expand Down
2 changes: 1 addition & 1 deletion src/global.json
@@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "2.0.41"
"MSBuild.Sdk.Extras": "2.0.54"
}
}

0 comments on commit 9de80d3

Please sign in to comment.