From 0e67d8cf1cf51c5ed74275b6844d2bb34d133f7b Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Fri, 26 Jul 2024 22:21:13 +1000 Subject: [PATCH 01/42] Remove security protocol usage --- GmailAPIHelper/GmailHelper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 8f774fe..ab7e373 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -141,7 +141,6 @@ public static GmailService GetGmailService(string applicationName, TokenPathType new FileDataStore(credPath, true)).Result; } //Create Gmail API service. - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; var service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer = credential, From f5bf2dd99c1f4230dd1308b2b913273b90949b95 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Fri, 26 Jul 2024 23:03:56 +1000 Subject: [PATCH 02/42] Remove security protocol usage --- GmailAPIHelper/GmailHelper.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index ab7e373..ee6ce3c 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -8,7 +8,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; From 817d42b6f5126622f5a54d0b309355cc7285c5dc Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Fri, 26 Jul 2024 23:14:33 +1000 Subject: [PATCH 03/42] Regex Match - Timeout to limit the execution time --- GmailAPIHelper/GmailHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index ee6ce3c..18036d5 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -1433,7 +1433,7 @@ private static bool IsValidEmail(this string email) string pattern = @"^[^0-9](?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(? Date: Fri, 26 Jul 2024 23:33:40 +1000 Subject: [PATCH 04/42] Code quality check --- .github/workflows/code-quality.yml | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 .github/workflows/code-quality.yml diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml new file mode 100644 index 0000000..8d76ac1 --- /dev/null +++ b/.github/workflows/code-quality.yml @@ -0,0 +1,49 @@ +name: SonarCloud +on: + push: + branches: + - main + - dev + pull_request: + types: [opened, synchronize, reopened] +jobs: + build: + name: Build and analyze + runs-on: windows-latest + steps: + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'zulu' + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Cache SonarCloud packages + uses: actions/cache@v3 + with: + path: ~\sonar\cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar + - name: Cache SonarCloud scanner + id: cache-sonar-scanner + uses: actions/cache@v3 + with: + path: .\.sonar\scanner + key: ${{ runner.os }}-sonar-scanner + restore-keys: ${{ runner.os }}-sonar-scanner + - name: Install SonarCloud scanner + if: steps.cache-sonar-scanner.outputs.cache-hit != 'true' + shell: powershell + run: | + New-Item -Path .\.sonar\scanner -ItemType Directory + dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner + - name: Build and analyze + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + shell: powershell + run: | + .\.sonar\scanner\dotnet-sonarscanner begin /k:"abhinavminhas_Gmail-API-Helper" /o:"abhinavminhas" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" + dotnet build + .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}" \ No newline at end of file From 192c7dee8eb4b15332d4e83855821ecbef9b1a8c Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Fri, 26 Jul 2024 23:39:58 +1000 Subject: [PATCH 05/42] Test cleanup yml renamed --- .github/workflows/{testcleanup.yml => test-cleanup.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{testcleanup.yml => test-cleanup.yml} (100%) diff --git a/.github/workflows/testcleanup.yml b/.github/workflows/test-cleanup.yml similarity index 100% rename from .github/workflows/testcleanup.yml rename to .github/workflows/test-cleanup.yml From 6525f56f96fb33c156db15852a16bdf1d665dd43 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Fri, 26 Jul 2024 23:57:06 +1000 Subject: [PATCH 06/42] Code quality check update --- .github/workflows/code-quality.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index 8d76ac1..108e816 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -6,6 +6,8 @@ on: - dev pull_request: types: [opened, synchronize, reopened] + workflow_dispatch: + jobs: build: name: Build and analyze @@ -40,7 +42,7 @@ jobs: dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner - name: Build and analyze env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} shell: powershell run: | From bc299a55506122cb39ce2a091e0a3c40ae241735 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 00:05:07 +1000 Subject: [PATCH 07/42] Code quality check update --- .github/workflows/code-quality.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index 108e816..4f5d597 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -1,4 +1,4 @@ -name: SonarCloud +name: Code Quality (SonarCloud) on: push: branches: From a7a1469c560aa223ec103f3c5d4e4112c3d8ca21 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 00:25:13 +1000 Subject: [PATCH 08/42] GetMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 18036d5..b6cebc4 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -190,13 +190,16 @@ public static Message GetMessage(this GmailService gmailService, string query, b if (messages.Count > 0) { var requiredLatestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var messageRequest = service.Users.Messages.Get(userId, requiredLatestMessage.Id); - messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; - requiredLatestMessage = messageRequest.Execute(); - if (markRead) + if (requiredLatestMessage != null) { - var labelToRemove = new List { "UNREAD" }; - service.RemoveLabels(requiredLatestMessage.Id, labelToRemove, userId: userId); + var messageRequest = service.Users.Messages.Get(userId, requiredLatestMessage.Id); + messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; + requiredLatestMessage = messageRequest.Execute(); + if (markRead) + { + var labelToRemove = new List { "UNREAD" }; + service.RemoveLabels(requiredLatestMessage.Id, labelToRemove, userId: userId); + } } if (disposeGmailService) service.DisposeGmailService(); From 651eb217d3a293474291118216b83b3c0b0bb62e Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 10:03:11 +1000 Subject: [PATCH 09/42] GetLatestMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 49 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index b6cebc4..ebf9799 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -287,35 +287,38 @@ public static string GetLatestMessage(this GmailService gmailService, string que { string requiredMessage = null; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id); - messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; - var latestMessageDetails = messageRequest.Execute(); - MessagePart requiredMessagePart = null; - if (latestMessageDetails.Payload.MimeType == "text/plain") - requiredMessagePart = latestMessageDetails.Payload; - else if (latestMessageDetails.Payload.MimeType == "text/html") - requiredMessagePart = latestMessageDetails.Payload; - else + if (latestMessage != null) { - if (latestMessageDetails.Payload.Parts != null) + var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id); + messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; + var latestMessageDetails = messageRequest.Execute(); + MessagePart requiredMessagePart = null; + if (latestMessageDetails.Payload.MimeType == "text/plain") + requiredMessagePart = latestMessageDetails.Payload; + else if (latestMessageDetails.Payload.MimeType == "text/html") + requiredMessagePart = latestMessageDetails.Payload; + else { - requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/plain"); - if (requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) - requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/html"); + if (latestMessageDetails.Payload.Parts != null) + { + requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/plain"); + if (requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) + requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/html"); + } } - } - if (requiredMessagePart != null) - { - byte[] data = Convert.FromBase64String(requiredMessagePart.Body.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+")); - requiredMessage = Encoding.UTF8.GetString(data); - if (markRead) + if (requiredMessagePart != null) { - var labelToRemove = new List { "UNREAD" }; - service.RemoveLabels(latestMessage.Id, labelToRemove, userId: userId); + byte[] data = Convert.FromBase64String(requiredMessagePart.Body.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+")); + requiredMessage = Encoding.UTF8.GetString(data); + if (markRead) + { + var labelToRemove = new List { "UNREAD" }; + service.RemoveLabels(latestMessage.Id, labelToRemove, userId: userId); + } } + else + requiredMessagePart = null; } - else - requiredMessagePart = null; if (disposeGmailService) service.DisposeGmailService(); return requiredMessage; From a1bf6b05b3a0921a39535e34caeb1da24af54f07 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 10:20:40 +1000 Subject: [PATCH 10/42] GetLatestMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index ebf9799..787a93a 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -302,7 +302,7 @@ public static string GetLatestMessage(this GmailService gmailService, string que if (latestMessageDetails.Payload.Parts != null) { requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/plain"); - if (requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) + if (requiredMessagePart != null || requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/html"); } } From 167716bd2e497a77e76fc330206f8a7c0978537c Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 10:36:00 +1000 Subject: [PATCH 11/42] GetLatestMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 787a93a..887af41 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -302,7 +302,7 @@ public static string GetLatestMessage(this GmailService gmailService, string que if (latestMessageDetails.Payload.Parts != null) { requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/plain"); - if (requiredMessagePart != null || requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) + if (requiredMessagePart == null || requiredMessagePart.Body.Data == "" || requiredMessagePart.Body.Data == null) requiredMessagePart = latestMessageDetails.Payload.Parts.FirstOrDefault(x => x.MimeType == "text/html"); } } From 97e87e5d8444cb50906a0c5ee92ff6b824af144a Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 10:43:56 +1000 Subject: [PATCH 12/42] GetMessageAttachments() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 887af41..aca4a3d 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -368,22 +368,25 @@ public static int GetMessageAttachments(this GmailService gmailService, string q if (messages.Count > 0) { var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id); - messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; - var latestMessageDetails = messageRequest.Execute(); - if (latestMessageDetails.Payload != null) + if (latestMessage != null) { - if (latestMessageDetails.Payload.Parts.Count > 0) + var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id); + messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; + var latestMessageDetails = messageRequest.Execute(); + if (latestMessageDetails.Payload != null) { - foreach (var part in latestMessageDetails.Payload.Parts) + if (latestMessageDetails.Payload.Parts.Count > 0) { - if (part.Filename != "") + foreach (var part in latestMessageDetails.Payload.Parts) { - var messageAttachmentRequest = service.Users.Messages.Attachments.Get(userId, latestMessageDetails.Id, part.Body.AttachmentId); - var messageAttachmentResponse = messageAttachmentRequest.Execute(); - var messageAttachmentData = Convert.FromBase64String(messageAttachmentResponse.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+")); - File.WriteAllBytes(Path.Combine(directoryPath, part.Filename), messageAttachmentData); - count++; + if (part.Filename != "") + { + var messageAttachmentRequest = service.Users.Messages.Attachments.Get(userId, latestMessageDetails.Id, part.Body.AttachmentId); + var messageAttachmentResponse = messageAttachmentRequest.Execute(); + var messageAttachmentData = Convert.FromBase64String(messageAttachmentResponse.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+")); + File.WriteAllBytes(Path.Combine(directoryPath, part.Filename), messageAttachmentData); + count++; + } } } } From 277cd59ce3943ac5b78db24382e79fc484719520 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 14:20:12 +1000 Subject: [PATCH 13/42] MoveMessageToTrash() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index aca4a3d..b035109 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -646,12 +646,17 @@ public static bool MoveMessageToTrash(this GmailService gmailService, string que } if (messages.Count > 0) { + var isMoved = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var moveToTrashRequest = service.Users.Messages.Trash(userId, latestMessage.Id); - moveToTrashRequest.Execute(); + if (latestMessage != null) + { + var moveToTrashRequest = service.Users.Messages.Trash(userId, latestMessage.Id); + moveToTrashRequest.Execute(); + isMoved = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isMoved; } if (disposeGmailService) service.DisposeGmailService(); From 7afb4989c765ea5f14d05534760a30e91b24541d Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 14:55:27 +1000 Subject: [PATCH 14/42] UntrashMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index b035109..708ac0b 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -727,14 +727,19 @@ public static bool UntrashMessage(this GmailService gmailService, string query, } if (messages.Count > 0) { + var isMoved = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var untrashMessageRequest = service.Users.Messages.Untrash(userId, latestMessage.Id); - untrashMessageRequest.Execute(); - var labelToAdd = new List { "INBOX" }; - service.AddLabels(latestMessage.Id, labelToAdd, userId: userId); + if (latestMessage != null) + { + var untrashMessageRequest = service.Users.Messages.Untrash(userId, latestMessage.Id); + untrashMessageRequest.Execute(); + var labelToAdd = new List { "INBOX" }; + service.AddLabels(latestMessage.Id, labelToAdd, userId: userId); + isMoved = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isMoved; } if (disposeGmailService) service.DisposeGmailService(); From 5866e62287979be12f36db0ff4cf3dc1a043a815 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 15:09:17 +1000 Subject: [PATCH 15/42] ReportSpamMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 708ac0b..224d0d2 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -817,12 +817,17 @@ public static bool ReportSpamMessage(this GmailService gmailService, string quer } if (messages.Count > 0) { + var isModified = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); - modifyMessageRequest.Execute(); + if (latestMessage != null) + { + var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); + modifyMessageRequest.Execute(); + isModified = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isModified; } if (disposeGmailService) service.DisposeGmailService(); From 544d46070990f711b5e15ae038cdae2bbbb7f627 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 15:29:49 +1000 Subject: [PATCH 16/42] UnspamMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 224d0d2..60426e2 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -908,12 +908,17 @@ public static bool UnspamMessage(this GmailService gmailService, string query, s } if (messages.Count > 0) { + var isModified = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); - modifyMessageRequest.Execute(); + if (latestMessage != null) + { + var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); + modifyMessageRequest.Execute(); + isModified = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isModified; } if (disposeGmailService) service.DisposeGmailService(); From df6e060c30f164f3d4e9bbb95edd7897b14ebe55 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 17:06:31 +1000 Subject: [PATCH 17/42] MarkMessageAsRead() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 60426e2..1a9f661 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -998,12 +998,17 @@ public static bool MarkMessageAsRead(this GmailService gmailService, string quer } if (messages.Count > 0) { + var isModified = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); - modifyMessageRequest.Execute(); + if (latestMessage != null) + { + var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); + modifyMessageRequest.Execute(); + isModified = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isModified; } if (disposeGmailService) service.DisposeGmailService(); From d54d86999316ed18cbdcfeb570e1b311d902907a Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 18:51:02 +1000 Subject: [PATCH 18/42] MarkMessageAsUnread() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 1a9f661..c29ed9b 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -1087,12 +1087,17 @@ public static bool MarkMessageAsUnread(this GmailService gmailService, string qu } if (messages.Count > 0) { + var isModified = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); - modifyMessageRequest.Execute(); + if (latestMessage != null) + { + var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); + modifyMessageRequest.Execute(); + isModified = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isModified; } if (disposeGmailService) service.DisposeGmailService(); From 25e5b6cbb3cdd2798ef2c6c605d66517aea1d691 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 18:54:29 +1000 Subject: [PATCH 19/42] ModifyMessage() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index c29ed9b..32d9646 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -1183,12 +1183,17 @@ public static bool ModifyMessage(this GmailService gmailService, string query, L } if (messages.Count > 0) { + var isModified = false; var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault(); - var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); - modifyMessageRequest.Execute(); + if (latestMessage != null) + { + var modifyMessageRequest = service.Users.Messages.Modify(mods, userId, latestMessage.Id); + modifyMessageRequest.Execute(); + isModified = true; + } if (disposeGmailService) service.DisposeGmailService(); - return true; + return isModified; } if (disposeGmailService) service.DisposeGmailService(); From 38137f4370590c043874bf134e5dfbc680793747 Mon Sep 17 00:00:00 2001 From: abhinavminhas Date: Sat, 27 Jul 2024 21:11:07 +1000 Subject: [PATCH 20/42] GetMessageLabels() - Remove null execution path --- GmailAPIHelper/GmailHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GmailAPIHelper/GmailHelper.cs b/GmailAPIHelper/GmailHelper.cs index 32d9646..3e27b02 100644 --- a/GmailAPIHelper/GmailHelper.cs +++ b/GmailAPIHelper/GmailHelper.cs @@ -1277,7 +1277,7 @@ public static List