Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using regular expressions when defining license header templates #3486

Merged
merged 1 commit into from
Feb 22, 2021

Conversation

serso
Copy link
Contributor

@serso serso commented Feb 20, 2021

Problem: different projects might impose different rules on license headers.
It's not uncommon, for example, that a year in the license header is the year when the source
file was created. This leads to a situation when different source files have different license
headers (at least some parts of them differ) making it impossible to use a single static
license header template to match against.

This change makes it possible to define license header templates as regexps.
An example of such template is:
// Copyright 20[0-9]{2} Artur Bosch & Contributors
which would match both
\ Copyright 2020 Artur Bosch & Contributors
and
\ Copyright 2021 Artur Bosch & Contributors

The way AbsentOrWrongFileLicense works now is controlled
by the new configuration option licenseTemplateIsRegex:
when licenseTemplateIsRegex is false the template is considered to be static
and the old matching rules apply.
If licenseTemplateIsRegex is set to true the new regexp based logic kicks in.

To avoid breakage of existing projects, the added configuration option
licenseTemplateIsRegex is disabled by default.

This commit implements a feature requested here #3100.

A similar PR may already be submitted!
Please search among the Pull requests before creating one.

Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. Link to relevant issues if possible.

For more information, see the CONTRIBUTING guide.

@codecov
Copy link

codecov bot commented Feb 20, 2021

Codecov Report

Merging #3486 (6a6a755) into master (c64b1c7) will increase coverage by 0.02%.
The diff coverage is 78.94%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #3486      +/-   ##
============================================
+ Coverage     80.06%   80.09%   +0.02%     
- Complexity     2864     2868       +4     
============================================
  Files           458      458              
  Lines          8584     8596      +12     
  Branches       1663     1666       +3     
============================================
+ Hits           6873     6885      +12     
  Misses          794      794              
  Partials        917      917              
Impacted Files Coverage Δ Complexity Δ
...kt/rules/documentation/AbsentOrWrongFileLicense.kt 87.50% <50.00%> (-5.36%) 12.00 <7.00> (+4.00) ⬇️
...ules/documentation/LicenceHeaderLoaderExtension.kt 80.95% <86.66%> (+9.07%) 6.00 <0.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c64b1c7...6a6a755. Read the comment docs.

@serso serso force-pushed the dev/regexp-license-headers branch from 771515f to 7aaf36c Compare February 20, 2021 16:41
Problem: different projects might impose different rules on license headers.
It's not uncommon, for example, that a year in the license header is the year when the source
file was created. This leads to a situation when different source files have different license
headers (at least some parts of them differ) making it impossible to use a single static
license header template to match against.

This change makes it possible to define license header templates as regexps.
An example of such template is:
  \/\/ Copyright 20[0-9]{2} Artur Bosch & Contributors
which would match both
  // Copyright 2020 Artur Bosch & Contributors
and
  // Copyright 2021 Artur Bosch & Contributors

The way `AbsentOrWrongFileLicense` works now is controlled
by the new configuration option `licenseTemplateIsRegex`:
when `licenseTemplateIsRegex` is false the template is considered to be static
and the old matching rules apply.
If `licenseTemplateIsRegex` is set to true the new regexp based logic kicks in.

To avoid breakage of existing projects, the added configuration option
`licenseTemplateIsRegex` is disabled by default.

This commit implements a feature requested here detekt#3100.
@serso serso force-pushed the dev/regexp-license-headers branch from 7aaf36c to 6a6a755 Compare February 20, 2021 17:50
@serso
Copy link
Contributor Author

serso commented Feb 20, 2021

@BraisGabin @arturbosch can you please advice me what I should do with the test coverage? Codecov marks red the lines that I haven't touched. Or maybe I'm reading its report wrong?

@chao2zhang
Copy link
Member

I would say let's ignore the codecov/path failure. Since the codecov/project is increased, I am doubtful if the codecov/patch not hitting 80% is mathematically correct.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing this rule! Great job! 😀
I have 1 remaining question. I think this ready to be merged.

@schalkms schalkms merged commit d2dc38c into detekt:master Feb 22, 2021
@cortinico cortinico added this to the 1.16.0 milestone Mar 3, 2021
This was referenced Mar 11, 2021
This was referenced Mar 11, 2021
mnonnenmacher added a commit to oss-review-toolkit/ort that referenced this pull request Mar 29, 2021
It could be replaced by `AbsentOrWrongFileLicense` in the previous
commit, because the rule supports regular expressions since detekt
1.16.0 [1].

[1] detekt/detekt#3486

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@here.com>
mnonnenmacher added a commit to oss-review-toolkit/ort that referenced this pull request Mar 30, 2021
It could be replaced by `AbsentOrWrongFileLicense` in the previous
commit, because the rule supports regular expressions since detekt
1.16.0 [1].

[1] detekt/detekt#3486

Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@here.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants