Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- feature/*
- hotfix/*
- release/*
- master
tags:
- v*
pull_request:
Expand All @@ -14,14 +14,9 @@ jobs:
runs-on: windows-latest
name: Dotnet build
steps:
- uses: actions/checkout@master

- name: Branchy Branchy
run: |
git checkout master
echo "${{ github.ref }}" | sed "s/refs\/\(heads\|tags\)\///" | sed "s/refs\/pull\/\([0-9]*\)\/merge/refs\/remotes\/pull\/\1\/merge/" | xargs git checkout
git checkout ${{ github.sha }}
shell: cmd
- uses: actions/checkout@v2
- run: |
git fetch --prune --unshallow
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
Expand All @@ -31,11 +26,10 @@ jobs:
shell: pwsh
id: build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NUGET_TOKEN: ${{ secrets.NugetKey }}
- name: Add artifacts
uses: actions/upload-artifact@v1
with:
name: ${{ steps.build.outputs.nupkg_name }}
path: ${{ steps.build.outputs.nupkg }}

21 changes: 12 additions & 9 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ var configuration = Argument("configuration", "Release");
///////////////////////////////////////////////////////////////////////////////

var isGitHubAction = !string.IsNullOrWhiteSpace(EnvironmentVariable("GITHUB_ACTION"));
var githubEventName = EnvironmentVariable("GITHUB_EVENT_NAME");
var githubRef = EnvironmentVariable("GITHUB_REF");
var githubBaseRef = EnvironmentVariable("GITHUB_BASE_REF");
var isPullRequest = githubEventName == "pull_request";
var isTag = githubEventName == "push" && githubRef.StartsWith("refs/tags/v");

Information($"EventName: {githubEventName}, Ref: {githubRef}, BaseRef: {githubBaseRef}, IsTag: {isTag}, IsGithubAction: {isGitHubAction}");

GitVersion gitVersion = null;
Task("Default")
.IsDependentOn("Build")
Expand Down Expand Up @@ -90,8 +98,9 @@ Task("DeployGPR")
.IsDependentOn("Pack")
.Does(() =>
{
if(isGitHubAction)
if(isGitHubAction && !isPullRequest)
{
Information($"DeployGPR. IsGitHubAction: {isGitHubAction}, IsPullRequest: {isPullRequest}");
var settings = new NuGetSourcesSettings
{
UserName = "mrb0nj",
Expand All @@ -106,23 +115,17 @@ Task("DeployGPR")
}
else
{
Information($"Skipping DeployGPR. IsGitHubAction: {isGitHubAction}");
Information($"Skipping DeployGPR. IsGitHubAction: {isGitHubAction}, IsPullRequest: {isPullRequest}");
}
});

Task("DeployNuGet")
.IsDependentOn("Pack")
.Does(() =>
{
var githubEventName = EnvironmentVariable("GITHUB_EVENT_NAME");
var githubRef = EnvironmentVariable("GITHUB_REF");
var githubBaseRef = EnvironmentVariable("GITHUB_BASE_REF");
var isTag = githubEventName == "push" && githubRef.StartsWith("refs/tags/v") && githubBaseRef == "refs/heads/master";
Information($"EventName: {githubEventName}, Ref: {githubRef}, BaseRef: {githubBaseRef}, IsTag: {isTag}");


if(isGitHubAction && isTag)
{
Information($"DeployNuget. IsGitHubAction: {isGitHubAction}, IsTag: {isTag}");
var settings = new NuGetPushSettings
{
Source = "https://api.nuget.org/v3/index.json",
Expand Down
27 changes: 24 additions & 3 deletions src/Slack.Webhooks/SlackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
Expand All @@ -27,11 +28,20 @@ public class SlackClient : ISlackClient, IDisposable
/// </summary>
internal int TimeoutMs { get { return _timeout * 1000; } }

public SlackClient(string webhookUrl, int timeout = 100, HttpClient httpClient = null)
public SlackClient(string webhookUrl = null, int timeout = 100, HttpClient httpClient = null, string authorizationToken = null)
{
_httpClient = httpClient ?? new HttpClient();
if (!Uri.TryCreate(webhookUrl, UriKind.Absolute, out _webhookUri))
throw new ArgumentException("Please enter a valid webhook url");
if (!string.IsNullOrWhiteSpace(authorizationToken))
{
_webhookUri = new Uri("https://slack.com/api/chat.postMessage");
_httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse($"Bearer {authorizationToken}");
}
else
{
if (!Uri.TryCreate(webhookUrl, UriKind.Absolute, out _webhookUri))
throw new ArgumentException("Please enter a valid webhook url");
}

_timeout = timeout;
}

Expand Down Expand Up @@ -65,6 +75,17 @@ public async Task<bool> PostAsync(SlackMessage slackMessage)
return content.Equals(POST_SUCCESS, StringComparison.OrdinalIgnoreCase);
}
}

public async Task<SlackResponse> PostAsAppAsync(SlackMessage slackMessage)
{
using (var request = new HttpRequestMessage(HttpMethod.Post, _webhookUri))
{
request.Content = new StringContent(slackMessage.AsJson(), System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SlackResponse>(content);
}
}

/// <summary>
/// Deserialize SlackMessage from a JSON string
Expand Down
40 changes: 40 additions & 0 deletions src/Slack.Webhooks/SlackResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;

namespace Slack.Webhooks
{
public class SlackResponse
{
[JsonProperty(PropertyName = "ok")]
public bool Ok { get; set; }

[JsonProperty(PropertyName = "channel")]
public string Channel { get; set; }

[JsonProperty(PropertyName = "ts")]
public string ThreadId { get; set; }

[JsonProperty(PropertyName = "message")]
public Message Message { get; set; }
}

public class Message
{
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }

[JsonProperty(PropertyName = "subtype")]
public string SubType { get; set; }

[JsonProperty(PropertyName = "text")]
public string Text { get; set; }

[JsonProperty(PropertyName = "ts")]
public string ThreadId { get; set; }

[JsonProperty(PropertyName = "username")]
public string Username { get; set; }

[JsonProperty(PropertyName = "bot_id")]
public string BotId { get; set; }
}
}