I am getting “The file header copyright text should match the copyright text from the settings” Code Smells (54 of them) but I beleive that I have configured it correctly.
I have a stylecop.json with a documentationRules node
"documentationRules": {
"companyName": "Magic Bullet Ltd",
"copyrightText": "Copyright (c) {companyName}. All rights reserved."
}
Yes, you should set it up in SonarCloud, to your organization rule configuration - https://sonarcloud.io/organizations/<YOUR_ORGANIZATION>/rules. Search for Track lack of copyright and license headers or the rule ID S1451 and filter by the language.
Scroll down below the rule description and click change:
I apologize for the delay. Our implementation CheckFileLicenseBase uses the .NET Regular Expression library System.Text.RegularExpressions.Regex - did you test your regular expression in a .NET Console playground app before configuring in SonarCloud? Did it work?
This website does not have the .NET regular expression engine available. How did you test your regex with .NET?
Please debug your regular expression in .NET. If that works in a .NET reproducer and does not work in SonarCloud, we will investigate (and we would appreciate providing a .NET reproducer test that will help us investigate).
I have updated the regEx and ran it thought a simple .NET Console application. I assume this is what you mean by a “.NET Reproducer test”
string strRegex = @"\/\/ -{71,}(\n|\r|\r\n)\/\/ <copyright file="".*\.cs"" company="".*"">(\n|\r|\r\n)\/\/\s{5}Copyright \(c\) Us Ltd. All rights reserved.(\n|\r|\r\n)\/\/ <\/copyright>(\n|\r|\r\n)\/\/ -{71,}";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString =
@"// -----------------------------------------------------------------------
// <copyright file=""BrowseObservationsController.cs"" company=""Us Ltd"">
// Copyright (c) Us Ltd. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
Console.WriteLine($"Index {myMatch.Index} Length {myMatch.Length}");
Console.WriteLine(myMatch.ToString());
}
}
Console.ReadLine();
This matches.
If I updated the rules file in VS2019 and run Code Analysis I still get Errors for the header in both VS2019 and https://sonarcloud.io/ report
I have dropped the Year from the copyright as despite us wanting to put the year in the copyright line I cannot be sure how to format the RegEx so that appears. I know how to test for it but I cannot see where RegEx rules for checking occurence meet the rules needed by VS2019|Sonarsource to insert the year into the Header when I choose “Add file header” in VS.
I tried with \/\/ -{71,}(\n|\r|\r\n)\/\/ <copyright file=".*\.cs" company=".*">(\n|\r|\r\n)\/\/\s{5}Copyright \(c\) Us Ltd. All rights reserved.(\n|\r|\r\n)\/\/ <\/copyright>(\n|\r|\r\n)\/\/ -{71,} and it worked with your test case.
Indeed, getting the right regex right can be difficult.
Also, in SonarCloud you need the “clean” regex, and when trying to use it in C# you need to escape characters so it’s difficult to test your regex.
But this is a regular expression problem rather than a SonarCloud problem. From our point of view, the rule works as expected, as long as the correct regular expression is provided.
Thanks Andrei. If I take that cleaned RegEx and configure the rule in sonarcloud.io, then “Update” the SonarQube rules in VS2019 I do now get a clean analysis in the IDE.
However when I push the updated changes to a branch AND check that branch it is still reporting the “file header copyright text should match the copyright text from the settings” as a CODE SMELL
No it doesn’t work well. I just ran our AzDo Pipeline on a new branch that just had the updates to the file headers and the same number of Code Smells as the previous branch. Same thing when that new branch is PR’ed.
Aplogies if this is not the correct protocol for chasing up this issue but the original “get helkp” request is now locked, I am happy to return to it if it can be unlocked and I will delete this one
My iussues are well documented in this post and you can jump to then end to see my current position
Essentially the Regex that Andrei helped me with works well in the IDE but the files containing the new file header still get flagged in sonarcloud as issues
Sorry for the delay in coming back. Reason didn’t see Notification, sorry
Heer are scome screenshots of the Resuklt of Code Analysis and the SonarQube screen in Team Explorer that will hopefully confirm I am in connected mode
I just pushed a minor textual change to the test branch and after the pipeline ran it was 100% clean with no “New Code Smells”. I will look to push it to the Master branch ASAP