Skip to content

Commit

Permalink
Upgrade to dotnet 9 and latest Nuget packages (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real authored Feb 1, 2025
1 parent e64bdaa commit 3e3529e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 94 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 as builder
FROM mcr.microsoft.com/dotnet/sdk:9.0 as builder
ARG TARGETPLATFORM
ARG BUILDPLATFORM

# Copy the source to docker container
COPY ./src /usr/src
RUN dotnet publish -c Release /usr/src/netdaemonbot.csproj -o "/publish"

FROM mcr.microsoft.com/dotnet/aspnet:8.0
FROM mcr.microsoft.com/dotnet/aspnet:9.0
# RUN mkdir /app
# Set the working directory to /app
WORKDIR /app

COPY --from=builder /publish .
# Start the application
ENTRYPOINT ["dotnet", "netdaemonbot.dll"]
ENTRYPOINT ["dotnet", "netdaemonbot.dll"]
18 changes: 7 additions & 11 deletions src/BotParser.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Text.RegularExpressions;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using netdaemonbot;

namespace netdaemonbot;

Expand Down Expand Up @@ -80,8 +76,8 @@ public class BotParser : IMessage

#region -- Parse expressions --

static Regex _exCommand = new Regex(@"(<@!\d+>)*\s*(?'command'\w+)\s*(?'argument'.*)");
static Regex _exQuery = new Regex(@"(<@!\d+>)*\s*(?'query'.+)\?");
static readonly Regex _exCommand = new(@"(<@!\d+>)*\s*(?'command'\w+)\s*(?'argument'.*)");
static readonly Regex _exQuery = new(@"(<@!\d+>)*\s*(?'query'.+)\?");

#endregion

Expand All @@ -97,9 +93,9 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,
{
// Parse queries
Match? matchQuery = _exQuery.Matches(message).FirstOrDefault();
if (matchQuery is object)
if (matchQuery is not null)
{
foreach (Group? group in matchQuery.Groups)
foreach (Group? group in matchQuery.Groups.Cast<Group?>())
{
if (group?.Name == "query")
Query = string.IsNullOrEmpty(group.Value) ? null : group.Value;
Expand All @@ -110,9 +106,9 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,
}

Match? match = _exCommand.Matches(message).FirstOrDefault();
if (match is object)
if (match is not null)
{
foreach (Group? group in match.Groups)
foreach (Group? group in match.Groups.Cast<Group?>())
{
if (group?.Name == "command")
Command = string.IsNullOrEmpty(group.Value) ? null : group.Value.ToLowerInvariant();
Expand All @@ -122,4 +118,4 @@ public BotParser(string message, bool botMentioned, IEnumerable<string>? roles,

}
}
}
}
4 changes: 2 additions & 2 deletions src/BotResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class BotResult
/// <typeparam name="field">Field name</typeparam>
/// <typeparam name="text">Text in field</typeparam>
/// <returns></returns>
public List<(string field, string text)> Fields { get; set; } = new List<(string, string)>();
public List<(string field, string text)> Fields { get; set; } = [];

}
}
5 changes: 1 addition & 4 deletions src/BotService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Reflection;
using System.Text;
using DSharpPlus;
using DSharpPlus.Entities;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace netdaemonbot;

Expand Down Expand Up @@ -122,4 +119,4 @@ public Task StopAsync(CancellationToken cancellationToken)
logger.LogInformation("Stopping BotService...");
return Task.CompletedTask;
}
}
}
75 changes: 44 additions & 31 deletions src/Plugins/AlgoliaSearch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


using System.Text;
using System.Text;
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;
using netdaemonbot;

/// <summary>
Expand All @@ -18,11 +17,12 @@ public class AlgoliaPlugin : IBotPlugin
private int _orderOfProcessingMessages = 0;
private ILogger _logger;
SearchClient _algoliaSearchClient;
SearchIndex _algoliaIndex;
/*SearchIndex _algoliaIndex;*/
private readonly string _searchCommand;
private readonly bool _isDefaultSearch;
private readonly string _searchCommandHelp;
private readonly string _searchDescriptionHelp;
private readonly string _indexName;

/// <summary>
/// Constructor
Expand All @@ -44,7 +44,7 @@ public AlgoliaPlugin(
{
_ = appId ?? throw new ArgumentNullException(nameof(appId));
_ = apiKey ?? throw new ArgumentNullException(nameof(apiKey));
_ = indexName ?? throw new ArgumentNullException(nameof(apiKey));
_indexName = indexName ?? throw new ArgumentNullException(nameof(apiKey));

_searchCommand = searchCommand;
_isDefaultSearch = searchCommand == "search" ? true : false;
Expand All @@ -54,7 +54,8 @@ public AlgoliaPlugin(

_logger = loggerFactory.CreateLogger<AlgoliaPlugin>();
_algoliaSearchClient = new SearchClient(appId, apiKey);
_algoliaIndex = _algoliaSearchClient.InitIndex(indexName);
/*_algoliaIndex = _algoliaSearchClient.InitIndex(indexName);*/

_orderOfProcessingMessages = order;
}

Expand Down Expand Up @@ -126,43 +127,55 @@ private async Task<BotResult> QueryMessage(string query)
var returnList = new List<(string, string)>();
try
{
var query = new Algolia.Search.Models.Search.Query(userQuery);
// query.
var result = await _algoliaIndex.SearchAsync<Hit>(query);
var query = new SearchQuery(
new SearchForHits
{
IndexName = _indexName,
Query = userQuery,
HitsPerPage = 10,
});
var result = await _algoliaSearchClient.SearchAsync<Hit>(
new SearchMethodParams
{
Requests = [query]
});

foreach (var hit in result.Hits)
foreach (var res in result.Results)
{
if (hit.hierarchy is object && hit.url is object)
foreach (var hit in res.AsSearchResponse().Hits)
{
bool isIndexedWrong = false;

var caption = hit.anchor;
for (int i = 5; i >= 0; i--)
if (hit.hierarchy is not null && hit.url is not null)
{
var lvl = $"lvl{i}";
if (hit.hierarchy.ContainsKey(lvl) && hit.hierarchy[lvl] is object)
bool isIndexedWrong = false;

var caption = hit.anchor;
for (int i = 5; i >= 0; i--)
{
caption = hit.hierarchy[lvl];
var lvl = $"lvl{i}";
if (hit.hierarchy.TryGetValue(lvl, out string? value) && value is not null)
{
caption = value;

if (caption.StartsWith("«") || caption.EndsWith("»"))
isIndexedWrong = true; // It has indexed the arrow texts
if (caption.StartsWith("«") || caption.EndsWith("»"))
isIndexedWrong = true; // It has indexed the arrow texts

break;
break;
}
}
}

if (hit.url.EndsWith("#__docusaurus"))
{
hit.url = hit.url[..^14];
}
if (hit.url.EndsWith("#__docusaurus"))
{
hit.url = hit.url[..^14];
}

// Compensate for fauly indexed pages
if (!isIndexedWrong)
returnList.Add((caption!, hit.url));
// Compensate for fauly indexed pages
if (!isIndexedWrong)
returnList.Add((caption!, hit.url));
}
}
}
}
catch (System.Exception e)
catch (Exception e)
{
_logger.LogError(e, "Ops, something went wrong in Algolia search!");
}
Expand All @@ -178,4 +191,4 @@ public class Hit
public string? anchor { get; set; }
public string? url { get; set; }
public Dictionary<string, string>? hierarchy { get; set; }
}
}
44 changes: 19 additions & 25 deletions src/Plugins/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GithubPlugin : IBotPlugin
private readonly GitHubClient _client;
private readonly int _order;

static Regex _exIssueParsing = new Regex(@"\s*(?'type'\w+)\s*(?'topic'.*)");
static readonly Regex _exIssueParsing = new(@"\s*(?'type'\w+)\s*(?'topic'.*)");

// private readonly bool _isAuthenticated;
public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken = null)
Expand All @@ -32,20 +32,19 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
if (string.IsNullOrEmpty(token) == false)
{
_client.Credentials = new Credentials(token);
// _isAuthenticated = true;
}
}

public int Order => _order;

public IEnumerable<(string, string?)>? GetCommandsAndDecriptions()
{
return new List<(string, string?)>
{
return
[
("latest", "get latest release notes"),
("issues", "get latest (max 10) reported issues"),
("issue", "adds issues fast, enter command `issue` for additional options"),
};
];
}

public async Task<BotResult?> HandleMessage(IMessage message)
Expand All @@ -67,7 +66,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
return await AddIssueInRepo(message);
}
}
catch (System.Exception e)
catch (Exception e)
{
_logger.LogError(e, "Failed to handle message");
}
Expand All @@ -92,11 +91,11 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
}

Match? match = _exIssueParsing.Matches(message.CommandArgs).FirstOrDefault();
if (match is object)
if (match is not null)
{
string? command = null, title = null;

foreach (Group? group in match.Groups)
foreach (Group? group in match.Groups.Cast<Group?>())
{
if (group?.Name == "type")
command = string.IsNullOrEmpty(group.Value) ? null : group.Value.ToLowerInvariant();
Expand All @@ -109,18 +108,13 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
return GetIssueMissingTitleHelpMessage();
}

switch (command)
return command switch
{
case "docs":
return await AddDocsIssue(title, message.User);
case "feature":
return await AddDaemonIssue("feature", title, message.User);
case "bug":
return await AddDaemonIssue("bug", title, message.User);
default:
return UnKnownIssueCommand();
}

"docs" => await AddDocsIssue(title, message.User),
"feature" => await AddDaemonIssue("feature", title, message.User),
"bug" => await AddDaemonIssue("bug", title, message.User),
_ => UnKnownIssueCommand(),
};
;

}
Expand All @@ -145,7 +139,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
_ => null
};

if (label is object)
if (label is not null)
createIssue.Labels.Add(label);

var body = type switch
Expand All @@ -155,7 +149,7 @@ public GithubPlugin(ILoggerFactory loggerFactory, int order, string? githubToken
_ => null
};

if (body is object)
if (body is not null)
createIssue.Body = body;

var issue = await _client.Issue.Create("net-daemon", "netdaemon", createIssue);
Expand Down Expand Up @@ -244,7 +238,7 @@ private BotResult GetIssueHelpMessage()
if (releases.Count == 0)
return null;

var release = releases.First();
var release = releases[0];

var result = new BotResult() { Title = $"Latest release version {release.TagName}", Text = release.Body };

Expand Down Expand Up @@ -292,7 +286,7 @@ private BotResult GetIssueHelpMessage()
return result;
}

private string featureTemplate = @"
private readonly string featureTemplate = @"
<!--
Please describe the feature you want from a usage perspective.
-->
Expand All @@ -312,7 +306,7 @@ Please describe the feature you want from a usage perspective.
";

private string docsTemplate = @"
private readonly string docsTemplate = @"
<!--
Please describe what suggestions or issues you have for the docs.
-->
Expand All @@ -323,7 +317,7 @@ Please describe the feature you want from a usage perspective.
";

private string issueTemplate = @"
private readonly string issueTemplate = @"
<!-- READ THIS FIRST:
- If you need additional help with this template, please refer to https://netdaemon.xtz/help/reporting_issues/
- Make sure you are running the latest version of NetDaemon before reporting an issue: https://github.com/net-daemon/netdaemon/releases
Expand Down
Loading

0 comments on commit 3e3529e

Please sign in to comment.