Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1471 from Catel/GitHubSync-20191106-164237
GitHubSync update
- Loading branch information
Showing
5 changed files
with
180 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}" | ||
}); | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"msbuild-sdks": { | ||
"MSBuild.Sdk.Extras": "2.0.41" | ||
"MSBuild.Sdk.Extras": "2.0.54" | ||
} | ||
} |