Skip to content

Commit

Permalink
Merge pull request #64 from nowsprinting/fix/reporter
Browse files Browse the repository at this point in the history
Not to send body when stack-trace is empty
  • Loading branch information
Kuniwak authored Jun 4, 2024
2 parents aaf61cf + 4f73527 commit 527a80d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
3 changes: 0 additions & 3 deletions Runtime/Reporters/AbstractReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Threading;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Settings;
using UnityEngine;

namespace DeNA.Anjin.Reporters
Expand All @@ -16,15 +15,13 @@ public abstract class AbstractReporter : ScriptableObject
/// <summary>
/// Post report log message, stacktrace and screenshot
/// </summary>
/// <param name="settings">Autopilot settings</param>
/// <param name="logString">Log message</param>
/// <param name="stackTrace">Stack trace</param>
/// <param name="type">Log message type</param>
/// <param name="withScreenshot">With screenshot</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns></returns>
public abstract UniTask PostReportAsync(
AutopilotSettings settings,
string logString,
string stackTrace,
LogType type,
Expand Down
4 changes: 1 addition & 3 deletions Runtime/Reporters/CompositeReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Threading;
using Cysharp.Threading.Tasks;
using DeNA.Anjin.Settings;
using UnityEngine;

namespace DeNA.Anjin.Reporters
Expand All @@ -24,7 +23,6 @@ public class CompositeReporter : AbstractReporter

/// <inheritdoc />
public override async UniTask PostReportAsync(
AutopilotSettings settings,
string logString,
string stackTrace,
LogType type,
Expand All @@ -36,7 +34,7 @@ await UniTask.WhenAll(
reporters
.Where(r => r != this && r != null)
.Select(
r => r.PostReportAsync(settings, logString, stackTrace, type, withScreenshot, cancellationToken)
r => r.PostReportAsync(logString, stackTrace, type, withScreenshot, cancellationToken)
)
);
}
Expand Down
12 changes: 8 additions & 4 deletions Runtime/Reporters/SlackMessageSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async UniTask Send(
if (withScreenshot)
{
var coroutineRunner = new GameObject().AddComponent<CoroutineRunner>();
await UniTask.WaitForEndOfFrame(coroutineRunner);
await UniTask.WaitForEndOfFrame(coroutineRunner, cancellationToken);
Object.Destroy(coroutineRunner);

var screenshot = ScreenCapture.CaptureScreenshotAsTexture();
Expand All @@ -107,16 +107,20 @@ public async UniTask Send(
slackToken,
slackChannel,
withoutAlpha.EncodeToPNG(),
postTitleTask.Ts
postTitleTask.Ts,
cancellationToken
);
if (!postScreenshotTask.Success)
{
return;
}
}

var body = Body(logString, stackTrace);
await _slackAPI.Post(slackToken, slackChannel, body, postTitleTask.Ts);
if (stackTrace != null && stackTrace.Length > 0)
{
var body = Body(logString, stackTrace);
await _slackAPI.Post(slackToken, slackChannel, body, postTitleTask.Ts, cancellationToken);
}
}


Expand Down
26 changes: 20 additions & 6 deletions Runtime/Reporters/SlackReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,25 @@ public class SlackReporter : AbstractReporter

/// <inheritdoc />
public override async UniTask PostReportAsync(
AutopilotSettings settings,
string logString,
string stackTrace,
LogType type,
bool withScreenshot,
CancellationToken cancellationToken = default
)
{
OverwriteByCommandlineArguments();

// NOTE: In _sender.send, switch the execution thread to the main thread, so UniTask.WhenAll is meaningless.
foreach (var slackChannel in (string.IsNullOrEmpty(settings.slackChannels)
? slackChannels
: settings.slackChannels).Split(','))
foreach (var slackChannel in slackChannels.Split(','))
{
if (cancellationToken.IsCancellationRequested)
{
return;
}

await _sender.Send(
string.IsNullOrEmpty(settings.slackToken) ? slackToken : settings.slackToken,
slackToken,
slackChannel,
mentionSubTeamIDs.Split(','),
addHereInSlackMessage,
Expand All @@ -68,5 +67,20 @@ await _sender.Send(
);
}
}

private void OverwriteByCommandlineArguments()
{
var args = new Arguments();

if (args.SlackToken.IsCaptured())
{
slackToken = args.SlackToken.Value();
}

if (args.SlackChannels.IsCaptured())
{
slackChannels = args.SlackChannels.Value();
}
}
}
}
4 changes: 0 additions & 4 deletions Runtime/Settings/AutopilotSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,12 @@ public void OverrideByCommandLineArguments(Arguments args)

if (args.SlackToken.IsCaptured())
{
#pragma warning disable CS0618 // Type or member is obsolete
slackToken = args.SlackToken.Value();
#pragma warning restore CS0618 // Type or member is obsolete
}

if (args.SlackChannels.IsCaptured())
{
#pragma warning disable CS0618 // Type or member is obsolete
slackChannels = args.SlackChannels.Value();
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Utilities/LogMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async void HandleLog(string logString, string stackTrace, LogType type)

if (_reporter != null)
{
await _reporter.PostReportAsync(_settings, logString, stackTrace, type, true);
await _reporter.PostReportAsync(logString, stackTrace, type, true);
}

var autopilot = Object.FindObjectOfType<Autopilot>();
Expand Down
2 changes: 0 additions & 2 deletions Tests/Runtime/TestDoubles/SpyReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class SpyReporter : AbstractReporter
public List<Dictionary<string, string>> Arguments { get; } = new List<Dictionary<string, string>>();

public override async UniTask PostReportAsync(
AutopilotSettings settings,
string logString,
string stackTrace,
LogType type,
Expand All @@ -30,7 +29,6 @@ public override async UniTask PostReportAsync(
Debug.Log("Reporter called");
Arguments.Add(new Dictionary<string, string>
{
{"settings", settings.ToString()},
{"logString", logString},
{"stackTrace", stackTrace},
{"type", type.ToString()},
Expand Down

0 comments on commit 527a80d

Please sign in to comment.