From 565c705b6a60edf31a2083f981866abf5116c3cc Mon Sep 17 00:00:00 2001 From: Nils Andresen Date: Sun, 5 Feb 2023 23:59:25 +0100 Subject: [PATCH] (#510) post to mastodon on successful publishing --- Source/Cake.Recipe/Content/addins.cake | 1 + Source/Cake.Recipe/Content/credentials.cake | 19 ++++++++ Source/Cake.Recipe/Content/environment.cake | 8 +++- Source/Cake.Recipe/Content/mastodon.cake | 43 +++++++++++++++++++ Source/Cake.Recipe/Content/parameters.cake | 12 +++++- .../fundamentals/environment-variables.md | 18 ++++++++ .../input/docs/fundamentals/set-parameters.md | 23 ++++++++++ .../docs/fundamentals/set-variable-names.md | 8 ++++ 8 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 Source/Cake.Recipe/Content/mastodon.cake diff --git a/Source/Cake.Recipe/Content/addins.cake b/Source/Cake.Recipe/Content/addins.cake index 9ffc33a9..4c6bedab 100644 --- a/Source/Cake.Recipe/Content/addins.cake +++ b/Source/Cake.Recipe/Content/addins.cake @@ -14,6 +14,7 @@ #addin nuget:?package=Cake.Transifex&version=1.0.1 #addin nuget:?package=Cake.Twitter&version=2.0.0 #addin nuget:?package=Cake.Wyam&version=2.2.13 +#addin nuget:?package=Cake.Mastodon&version=1.1.0 #load nuget:?package=Cake.Issues.Recipe&version=1.3.2 diff --git a/Source/Cake.Recipe/Content/credentials.cake b/Source/Cake.Recipe/Content/credentials.cake index d81faec4..15a7f6ea 100644 --- a/Source/Cake.Recipe/Content/credentials.cake +++ b/Source/Cake.Recipe/Content/credentials.cake @@ -131,6 +131,18 @@ public class WyamCredentials } } +public class MastodonCredentials +{ + public string AccessToken { get; private set; } + public string InstanceUrl { get; private set; } + + public MastodonCredentials(string accessToken, string instanceUrl) + { + AccessToken = accessToken; + InstanceUrl = instanceUrl; + } +} + public static GitHubCredentials GetGitHubCredentials(ICakeContext context) { string token = null; @@ -211,3 +223,10 @@ public static WyamCredentials GetWyamCredentials(ICakeContext context) context.EnvironmentVariable(Environment.WyamDeployRemoteVariable), context.EnvironmentVariable(Environment.WyamDeployBranchVariable)); } + +public static MastodonCredentials GetMastodonCredentials(ICakeContext context) +{ + return new MastodonCredentials( + context.EnvironmentVariable(Environment.MastodonAccessTokenVariable), + context.EnvironmentVariable(Environment.MastodonInstanceUrlVariable)); +} \ No newline at end of file diff --git a/Source/Cake.Recipe/Content/environment.cake b/Source/Cake.Recipe/Content/environment.cake index e6d40d41..bc13976a 100644 --- a/Source/Cake.Recipe/Content/environment.cake +++ b/Source/Cake.Recipe/Content/environment.cake @@ -20,6 +20,8 @@ public static class Environment public static string WyamAccessTokenVariable { get; private set; } public static string WyamDeployRemoteVariable { get; private set; } public static string WyamDeployBranchVariable { get; private set; } + public static string MastodonAccessTokenVariable { get; private set; } + public static string MastodonInstanceUrlVariable { get; private set; } public static void SetVariableNames( string githubTokenVariable = null, @@ -41,7 +43,9 @@ public static class Environment string transifexApiTokenVariable = null, string wyamAccessTokenVariable = null, string wyamDeployRemoteVariable = null, - string wyamDeployBranchVariable = null) + string wyamDeployBranchVariable = null, + string mastodonAccessTokenVariable = null, + string mastodonInstanceUrlVariable = null) { GithubTokenVariable = githubTokenVariable ?? "GITHUB_PAT"; SlackTokenVariable = slackTokenVariable ?? "SLACK_TOKEN"; @@ -63,5 +67,7 @@ public static class Environment WyamAccessTokenVariable = wyamAccessTokenVariable ?? "WYAM_ACCESS_TOKEN"; WyamDeployRemoteVariable = wyamDeployRemoteVariable ?? "WYAM_DEPLOY_REMOTE"; WyamDeployBranchVariable = wyamDeployBranchVariable ?? "WYAM_DEPLOY_BRANCH"; + MastodonAccessTokenVariable = mastodonAccessTokenVariable ?? "MASTODON_ACCESS_TOKEN"; + MastodonInstanceUrlVariable = mastodonInstanceUrlVariable ?? "MASTODON_INSTANCE_URL"; } } diff --git a/Source/Cake.Recipe/Content/mastodon.cake b/Source/Cake.Recipe/Content/mastodon.cake new file mode 100644 index 00000000..39be965b --- /dev/null +++ b/Source/Cake.Recipe/Content/mastodon.cake @@ -0,0 +1,43 @@ +public class MastodonReporter : SuccessReporter +{ + private MastodonCredentials _credentials; + private string _messageTemplate; + + public MastodonReporter(MastodonCredentials credentials, string messageTemplate) + { + _credentials = credentials; + _messageTemplate = messageTemplate; + } + + public override string Name { get; } = "Mastodon"; + + public override bool CanBeUsed + { + get => !string.IsNullOrEmpty(_credentials.AccessToken) && + !string.IsNullOrEmpty(_credentials.InstanceUrl); + } + + + public override void ReportSuccess(ICakeContext context, BuildVersion buildVersion) + { + try + { + context.Information("Sending message to Mastodon..."); + + var messageArguments = BuildParameters.MessageArguments(buildVersion); + var message = string.Format(_messageTemplate, messageArguments); + var idempotencyKey = Guid.NewGuid().ToString("d"); + + context.MastodonSendToot(_credentials.InstanceUrl, + _credentials.AccessToken, + message, + idempotencyKey); + + context.Information("Message successfully sent."); + } + catch(Exception ex) + { + context.Error("{0}", ex); + } + } +} diff --git a/Source/Cake.Recipe/Content/parameters.cake b/Source/Cake.Recipe/Content/parameters.cake index 4ca34091..c2161b9f 100644 --- a/Source/Cake.Recipe/Content/parameters.cake +++ b/Source/Cake.Recipe/Content/parameters.cake @@ -329,7 +329,9 @@ public static class BuildParameters List packageSourceDatas = null, PlatformFamily preferredBuildAgentOperatingSystem = PlatformFamily.Windows, BuildProviderType preferredBuildProviderType = BuildProviderType.AppVeyor, - Func messageArguments = null + Func messageArguments = null, + string mastodonMessage = null, + bool shouldPostToMastodon = true ) { if (context == null) @@ -640,6 +642,14 @@ public static class BuildParameters ShouldBeUsed = shouldPostToSlack } ); + SuccessReporters.Add( + new MastodonReporter( + GetMastodonCredentials(context), + mastodonMessage ?? StandardMessage) + { + ShouldBeUsed = shouldPostToMastodon + } + ); var eMailReporter = new EmailReporter( GetEmailCredentials(context)) diff --git a/docs/input/docs/fundamentals/environment-variables.md b/docs/input/docs/fundamentals/environment-variables.md index 52af7c5e..114f5d2d 100644 --- a/docs/input/docs/fundamentals/environment-variables.md +++ b/docs/input/docs/fundamentals/environment-variables.md @@ -130,6 +130,24 @@ The username that should be used for authenticating to the SMTP server. The password that should be used for authenticating to the SMTP server. +## Mastodon + +When a successful release build has been completed, Cake.Recipe can be configured to send out a notification (with configurable message) to the fediverse via Mastodon. There are two required environment variables that needs to be set to make this happen. Further information about find this information can be found in the Cake.Mastodon [documentation](https://github.com/cake-contrib/Cake.Mastodon/blob/master/README.md#usage). + +:::{.alert .alert-info} +**NOTE:** + +In addition to these environment variables being present, and correct, the control variable [shouldPostToMastodon](./set-parameters#shouldPostToMastodon) also needs to be set to true. The default value for this parameter is true. +::: + +### MASTODON_ACCESS_TOKEN + +The Access Token for the Mastodon application that is going to be used to send the toot. + +### MASTODON_INSTANCE_URL + +The URL to the Mastodon instance, where the application was registered. + ## AppVeyor More information about what this is used for can be found in the [clean AppVeyor build cache](../usage/cleaning-cache) documentation. diff --git a/docs/input/docs/fundamentals/set-parameters.md b/docs/input/docs/fundamentals/set-parameters.md index 3b2c614d..74ce6788 100644 --- a/docs/input/docs/fundamentals/set-parameters.md +++ b/docs/input/docs/fundamentals/set-parameters.md @@ -225,6 +225,17 @@ Type: `bool` Default Value: +```csharp +true +``` +### shouldPostToMastodon + +This is used as a final control variable for whether or not notification messages should be posted to the fediverse via Mastodon when the a final release build (i.e. a tagged build) completes. + +Type: `bool` + +Default Value: + ```csharp true ``` @@ -518,6 +529,18 @@ Default Value: Version {0} of the {1} Addin has just been released, this will be available here https://www.nuget.org/packages/{1}, once package indexing is complete." ``` +### mastodonMessage + +This is the message that is sent to the fediverse via Mastodon at the end of a tagged build. This is formatted with the calculated version number, as well as the Title parameter. + +Type: `string` + +Default Value: + +```csharp +Version {0} of the {1} Addin has just been released, this will be available here https://www.nuget.org/packages/{1}, once package indexing is complete." +``` + ### wyamRootDirectoryPath This is the directory that stores the documentation files that will be passed to the Wyam tool. diff --git a/docs/input/docs/fundamentals/set-variable-names.md b/docs/input/docs/fundamentals/set-variable-names.md index 661ec717..9f6e869b 100644 --- a/docs/input/docs/fundamentals/set-variable-names.md +++ b/docs/input/docs/fundamentals/set-variable-names.md @@ -83,6 +83,14 @@ Default value: `EMAIL_USERNAME` Default value: `EMAIL_PASSWORD` +### mastodonAccessTokenVariable + +Default value: `MASTODON_ACCESS_TOKEN` + +### mastodonInstanceUrlVariable + +Default value: `MASTODON_INSTANCE_URL` + ### appVeyorApiTokenVariable Default value: `APPVEYOR_API_TOKEN`