Skip to content

Commit

Permalink
Added additional_lines option (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Feb 22, 2024
1 parent 2874325 commit 1776030
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 4 additions & 1 deletion .github/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Changelog

## v1.3.2
## v1.4.0 (Unreleased)

<!--Releasenotes start-->
- 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.
<!--Releasenotes end-->

## 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.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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]]`</br>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` |

<details>
<summary>Expand me to see the currently supported file types:</summary>
Expand Down
16 changes: 9 additions & 7 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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!'
Expand All @@ -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
Expand Down

0 comments on commit 1776030

Please sign in to comment.