From 1776030ee6ecc0abc4840bbbccbea2cf06c4ccb0 Mon Sep 17 00:00:00 2001 From: Nikkel Mollenhauer <57323886+NikkelM@users.noreply.github.com> Date: Thu, 22 Feb 2024 22:45:38 +0100 Subject: [PATCH] Added `additional_lines` option (#49) --- .github/config.yml | 5 ++++- CHANGELOG.md | 8 ++++++-- README.md | 3 ++- app.rb | 16 +++++++++------- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/.github/config.yml b/.github/config.yml index c1ec374..bad2172 100644 --- a/.github/config.yml +++ b/.github/config.yml @@ -16,4 +16,7 @@ todo-pr-checker: case_sensitive: false # If multiline comments should also be searched for action items # This may result in some false positives or negatives if the opening or closing lines of a comment are not in the diff - multiline_comments: false \ No newline at end of file + multiline_comments: false + # How many lines below found action items should be shown in the embedded code snippet + # If there are no more lines below the action item before the file ends, the code snippet will not be able to render + additional_lines: 0 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a765a72..b006c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,15 @@ # Changelog -## v1.3.2 +## v1.4.0 (Unreleased) -- Renamed the `multiline_comments` configuration option. +- Added a new `additional_lines` configuration option to control how many lines below action items are rendered in code snippets. +## v1.3.2 + +- Renamed the `multiline_comments` configuration option. + ## v1.3.1 - Both `.github/config.yml` and `.github/config.yaml` are now supported for the app's configuration. diff --git a/README.md b/README.md index ac5758f..0845321 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Do you keep forgetting to resolve that one `// TODO:...` or fix the last ` # Bug...` before merging your Pull Requests? The Todo PR Checker will make sure that doesn't happen anymore. -The app checks all code changes in your open Pull Requests for remaining `Todo`, `Fixme` etc. action items in code comments and leaves a comment on the PR with embedded code links to any items that were found. +The app checks all code changes in your open Pull Requests for remaining `Todo`, `Fixme` etc. action items in code comments and leaves a comment on the PR with embedded code snippets to any items that were found. This list will update whenever new changes are pushed, so you always know exactly how much work is left. @@ -57,6 +57,7 @@ To get started, you can copy the `.github/config.yml` file from this repository | `add_languages` | `[string[file_type, line_start, block_start, block_end]]`
Example: `[['js', '//', '/*', '*,'], ['css', null, '/*', '*/'], ['.py', '#']]`, maximum 10 entries | A list of a list of programming languages to add support for. This list will be added to the already supported languages. If you define a language that is already supported, the default values will be overwritten. `file_type` must be the extension of the file (e.g. `js`) and may start with a `.`. You may omit the block comment definitions if the file type does not support block comments. If you want to omit the definition of a line comment, you must set `line_start` to `null`. If defining `block_start`, `block_end` must also be defined. You may specify up to 10 new file types. *The file types shown in the example are already natively supported by the app.* | `null` | | `case_sensitive` | `true`, `false` | Controls whether the app should look for action items in a case-sensitive manner. | `false` | | `multiline_comments` | `true`, `false` | Whether or not looking for action items in multiline block comments is enabled or not. When enabled, the app *may* incorrectly mark action items in your Pull Request if at least one of the opening or closing line of the block comment (e.g. `*/` and `/*` in JavaScript) are not included in the Pull Request diff, which causes them to not be found by the app. For multiline comments to always work, you must ensure that both the opening and closing characters are included in the diff. Action items located on the first line of a block comment will always be detected, even if this option is disabled. | `false` | +| `additional_lines` | `integer` between `0` and `10` | The number of additional lines to include below found action items in embedded code snippets. The default of `0` shows only the line with the action item. This setting does not influence the behaviour of showing multiple action items in one snippet if they are located close to each other. *Note that if there are not enough lines left in the file to display, the embedded code snippet will not be able to render at all and will display as a link only.* | `0` |
Expand me to see the currently supported file types: diff --git a/app.rb b/app.rb index 883bc87..2088415 100644 --- a/app.rb +++ b/app.rb @@ -117,7 +117,7 @@ def initiate_check_run if todo_changes.any? # If the user has enabled post_comment in the options if app_options['post_comment'] != 'never' - check_run_title, comment_summary, comment_body = create_pr_comment_from_changes(todo_changes, full_repo_name).values_at(:title, :summary, :body) + check_run_title, comment_summary, comment_body = create_pr_comment_from_changes(todo_changes, full_repo_name, app_options['additional_lines']).values_at(:title, :summary, :body) # Post or update the comment with the found action items if app_comment @@ -171,7 +171,8 @@ def get_app_options(full_repo_name, head_sha) 'action_items' => %w[todo fixme bug], 'case_sensitive' => false, 'add_languages' => [], - 'ignore_files' => [] + 'ignore_files' => [], + 'additional_lines' => 0 } accepted_option_values = { @@ -181,7 +182,8 @@ def get_app_options(full_repo_name, head_sha) 'case_sensitive' => ->(value) { [true, false].include?(value) }, 'add_languages' => ->(value) { value.is_a?(Array) && (1..10).include?(value.size) && value.all? { |v| v.is_a?(Array) && (2..4).include?(v.size) && v.all? { |i| i.is_a?(String) || i.nil? } } }, # The regex checks if the given input is a valid .gitignore pattern - 'ignore_files' => ->(value) { value.is_a?(Array) && (1..7).include?(value.size) && value.all? { |v| v.is_a?(String) && %r{\A(/?(\*\*/)?[\w*\[\]{}?\.\/-]+(/\*\*)?/?)\Z}.match?(v) } } + 'ignore_files' => ->(value) { value.is_a?(Array) && (1..7).include?(value.size) && value.all? { |v| v.is_a?(String) && %r{\A(/?(\*\*/)?[\w*\[\]{}?\.\/-]+(/\*\*)?/?)\Z}.match?(v) } }, + 'additional_lines' => ->(value) { value.is_a?(Integer) && (0..10).include?(value) } } file = @installation_client.contents(full_repo_name, path: '.github/config.yml', ref: head_sha) rescue nil @@ -343,7 +345,7 @@ def get_comment_chars(added_languages) end # (7) Creates a comment text from the found action items, with embedded links to the relevant lines - def create_pr_comment_from_changes(todo_changes, full_repo_name) + def create_pr_comment_from_changes(todo_changes, full_repo_name, additional_lines) number_of_todos = todo_changes.values.flatten.count check_run_title = if number_of_todos == 1 '✘ 1 unresolved action item found!' @@ -368,14 +370,14 @@ def create_pr_comment_from_changes(todo_changes, full_repo_name) # Sort the changes by their line number, and group those that are close together into one embedded link changes.sort_by! { |change| change[:line] } - grouped_changes = changes.slice_when { |prev, curr| curr[:line] - prev[:line] > 3 }.to_a + grouped_changes = changes.slice_when { |prev, curr| (curr[:line] - prev[:line] > 3) && (curr[:line] - (prev[:line] + additional_lines) > 1) }.to_a grouped_changes.each do |group| first_line = group.first[:line] last_line = group.last[:line] - comment_body += if first_line == last_line + comment_body += if first_line == last_line && additional_lines.zero? "https://github.com/#{full_repo_name}/blob/#{@payload['check_run']['head_sha']}/#{file}#L#{first_line} " else - "https://github.com/#{full_repo_name}/blob/#{@payload['check_run']['head_sha']}/#{file}#L#{first_line}-L#{last_line} " + "https://github.com/#{full_repo_name}/blob/#{@payload['check_run']['head_sha']}/#{file}#L#{first_line}-L#{last_line + additional_lines} " end end end