Skip to content

Commit

Permalink
Merge branch 'release/0.10.0'
Browse files Browse the repository at this point in the history
* release/0.10.0:
  (GH-34) Removed gitter-api-pcl and stylecop packages
  (build) Exclude LitJson from analysis
  (GH-34) Switch away from GitterSharp.dll
  (build) Update to latest Cake
  (maint) Add .DS_Store
  (GH-34) Added LitJson
  • Loading branch information
gep13 committed Oct 26, 2018
2 parents d026508 + 4248af5 commit 4d65a20
Show file tree
Hide file tree
Showing 15 changed files with 4,715 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,7 @@ FakesAssemblies/

# Build Related
tools/
BuildArtifacts/
BuildArtifacts/

# Mac
.DS_Store
2 changes: 0 additions & 2 deletions Source/Cake.Gitter/Cake.Gitter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@
<ItemGroup>
<PackageReference Include="Cake.Common" Version="0.28.0" PrivateAssets="All" />
<PackageReference Include="Cake.Core" Version="0.28.0" PrivateAssets="All" />
<PackageReference Include="gitter-api-pcl" Version="0.8.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
</ItemGroup>
</Project>
70 changes: 57 additions & 13 deletions Source/Cake.Gitter/Chat/GitterChatApi.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Cake.Common.Diagnostics;
using Cake.Core;
using GitterSharp.Model.Webhook;
using GitterSharp.Services;
using Cake.Gitter.LitJson;

namespace Cake.Gitter.Chat
{
Expand All @@ -24,11 +27,11 @@ internal static GitterChatMessageResult PostMessage(this ICakeContext context, s
GitterChatMessageResult result;
if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
{
result = PostToIncomingWebHook(context, message, messageSettings);
result = PostToIncomingWebHook(context, message, messageSettings).Result;
}
else
{
result = context.PostToChatApi(message, messageSettings);
result = context.PostToChatApi(message, messageSettings).Result;
}

if (!result.Ok && messageSettings.ThrowOnFail == true)
Expand All @@ -39,35 +42,76 @@ internal static GitterChatMessageResult PostMessage(this ICakeContext context, s
return result;
}

private static GitterChatMessageResult PostToIncomingWebHook(ICakeContext context, string message, GitterChatMessageSettings messageSettings)
private static async Task<GitterChatMessageResult> PostToIncomingWebHook(ICakeContext context, string message, GitterChatMessageSettings messageSettings)
{
context.Verbose("Posting to incoming webhook {0}...", string.Concat(messageSettings.IncomingWebHookUrl.TrimEnd('/').Reverse().SkipWhile(c => c != '/').Reverse()));

var gitterWebHookService = new WebhookService();
var result = gitterWebHookService.PostAsync(messageSettings.IncomingWebHookUrl, message, messageSettings.MessageLevel == GitterMessageLevel.Error ? MessageLevel.Error : MessageLevel.Info);
var httpClient = new HttpClient();

var parsedResult = new GitterChatMessageResult(result.Result, DateTime.UtcNow.ToString("u"), string.Empty);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");

var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"message", message},
{"level", messageSettings.MessageLevel == GitterMessageLevel.Error ? "error" : "info"}
});

var httpResponse = await httpClient.PostAsync(new Uri(messageSettings.IncomingWebHookUrl), content);
var response = await httpResponse.Content.ReadAsStringAsync();
var result = JsonMapper.ToObject(response);

var parsedResult = new GitterChatMessageResult(
httpResponse.IsSuccessStatusCode,
DateTime.UtcNow.ToString("u"),
string.Empty);

context.Debug("Result parsed: {0}", parsedResult);

return parsedResult;
}

private static GitterChatMessageResult PostToChatApi(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
private static async Task<GitterChatMessageResult> PostToChatApi(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
{
if (string.IsNullOrWhiteSpace(messageSettings.Token))
{
throw new NullReferenceException("No authorization token provided.");
}

var gitterApiService = new GitterApiService(messageSettings.Token);
var messageResponse = gitterApiService.SendMessageAsync(messageSettings.RoomId, message);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var parsedResult = new GitterChatMessageResult(!string.IsNullOrWhiteSpace(messageResponse.Result.Id), messageResponse.Result.SentDate.ToString("u"), string.Empty);
if (!string.IsNullOrWhiteSpace(messageSettings.Token))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", messageSettings.Token);
}

string url = $"https://api.gitter.im/v1/rooms/{messageSettings.RoomId}/chatMessages";

var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"text", message}
});

var httpResponse = await httpClient.PostAsync(url, content);
var response = await httpResponse.Content.ReadAsStringAsync();

var result = JsonMapper.ToObject(response);
var parsedResult = new GitterChatMessageResult(
httpResponse.IsSuccessStatusCode,
result.GetString("sent"),
string.Empty);

context.Debug("Result parsed: {0}", parsedResult);

return parsedResult;
}

private static string GetString(this JsonData data, string key)
{
return (data != null && data.Keys.Contains(key))
? (string)data[key]
: null;
}
}
}
}
31 changes: 31 additions & 0 deletions Source/Cake.Gitter/LitJson/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

Thank you for reading this notice. Following the tradition of other public
domain projects, here's a blessing:

May you find forgiveness for yourself and forgive others.
May you experience and share the gift of unconditional love.
May you see light, wherever the illusion of darkness appears.
77 changes: 77 additions & 0 deletions Source/Cake.Gitter/LitJson/IJsonWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#region Header

/*
* IJsonWrapper.cs
* Interface that represents a type capable of handling all kinds of JSON
* data. This is mainly used when mapping objects through JsonMapper, and
* it's implemented by JsonData.
*
* The authors disclaim copyright to this source code. For more details, see
* the COPYING file included with this distribution.
*/

#endregion Header

namespace Cake.Gitter
{
using System.Collections;
using System.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.IO;

namespace LitJson
{
internal enum JsonType
{
None,

Object,
Array,
String,
Int,
Long,
Double,
Boolean
}

internal interface IJsonWrapper : IList, IOrderedDictionary
{
bool IsArray { get; }
bool IsBoolean { get; }
bool IsDouble { get; }
bool IsInt { get; }
bool IsLong { get; }
bool IsObject { get; }
bool IsString { get; }

bool GetBoolean();

double GetDouble();

int GetInt();

JsonType GetJsonType();

long GetLong();

string GetString();

void SetBoolean(bool val);

void SetDouble(double val);

void SetInt(int val);

void SetJsonType(JsonType type);

void SetLong(long val);

void SetString(string val);

string ToJson();

void ToJson(JsonWriter writer);
}
}
}
Loading

0 comments on commit 4d65a20

Please sign in to comment.