Skip to content

Commit

Permalink
(cake-contrib#510) post to mastodon on successful publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-a authored and gep13 committed Aug 1, 2024
1 parent 710b4d1 commit 565c705
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions Source/Cake.Recipe/Content/addins.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions Source/Cake.Recipe/Content/credentials.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
8 changes: 7 additions & 1 deletion Source/Cake.Recipe/Content/environment.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand All @@ -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";
}
}
43 changes: 43 additions & 0 deletions Source/Cake.Recipe/Content/mastodon.cake
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
12 changes: 11 additions & 1 deletion Source/Cake.Recipe/Content/parameters.cake
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ public static class BuildParameters
List<PackageSourceData> packageSourceDatas = null,
PlatformFamily preferredBuildAgentOperatingSystem = PlatformFamily.Windows,
BuildProviderType preferredBuildProviderType = BuildProviderType.AppVeyor,
Func<BuildVersion, object[]> messageArguments = null
Func<BuildVersion, object[]> messageArguments = null,
string mastodonMessage = null,
bool shouldPostToMastodon = true
)
{
if (context == null)
Expand Down Expand Up @@ -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))
Expand Down
18 changes: 18 additions & 0 deletions docs/input/docs/fundamentals/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions docs/input/docs/fundamentals/set-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions docs/input/docs/fundamentals/set-variable-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 565c705

Please sign in to comment.