Skip to content

Commit

Permalink
Added a new option to always split code snippets (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Feb 22, 2024
1 parent 1776030 commit cb6a62a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .github/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ todo-pr-checker:
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
additional_lines: 0
# Whether or not each action item should always be rendered in a separate code snippet
# If disabled, action items that are located close to each other will be rendered in the same code snippet
always_split_snippets: false
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<!--Releasenotes start-->
- Added a new `additional_lines` configuration option to control how many lines below action items are rendered in code snippets.
- Added a new `always_split_snippets` configuration option to control whether or not action items should always be rendered in seperate code snippets, even when they are located close to each other in code.
<!--Releasenotes end-->

## v1.3.2
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ To get started, you can copy the `.github/config.yml` file from this repository
| `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` |
| `always_split_snippets` | `true`, `false` | Whether or not action items should always be rendered in separate code snippets, even when they are located close to each other in code. | `false` |

<details>
<summary>Expand me to see the currently supported file types:</summary>
Expand Down
20 changes: 15 additions & 5 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, app_options['additional_lines']).values_at(:title, :summary, :body)
check_run_title, comment_summary, comment_body = create_pr_comment_from_changes(todo_changes, full_repo_name, app_options).values_at(:title, :summary, :body)

# Post or update the comment with the found action items
if app_comment
Expand Down Expand Up @@ -172,7 +172,8 @@ def get_app_options(full_repo_name, head_sha)
'case_sensitive' => false,
'add_languages' => [],
'ignore_files' => [],
'additional_lines' => 0
'additional_lines' => 0,
'always_split_snippets' => false
}

accepted_option_values = {
Expand All @@ -183,7 +184,8 @@ def get_app_options(full_repo_name, head_sha)
'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) } },
'additional_lines' => ->(value) { value.is_a?(Integer) && (0..10).include?(value) }
'additional_lines' => ->(value) { value.is_a?(Integer) && (0..10).include?(value) },
'always_split_snippets' => ->(value) { [true, false].include?(value) }
}

file = @installation_client.contents(full_repo_name, path: '.github/config.yml', ref: head_sha) rescue nil
Expand Down Expand Up @@ -345,7 +347,10 @@ 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, additional_lines)
def create_pr_comment_from_changes(todo_changes, full_repo_name, app_options)
additional_lines = app_options['additional_lines']
always_split_snippets = app_options['always_split_snippets']

number_of_todos = todo_changes.values.flatten.count
check_run_title = if number_of_todos == 1
'✘ 1 unresolved action item found!'
Expand All @@ -370,7 +375,12 @@ def create_pr_comment_from_changes(todo_changes, full_repo_name, additional_line

# 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) && (curr[:line] - (prev[:line] + additional_lines) > 1) }.to_a
if always_split_snippets
grouped_changes = changes.map { |change| [change] }
else
grouped_changes = changes.slice_when { |prev, curr| (curr[:line] - prev[:line] > 3) && (curr[:line] - (prev[:line] + additional_lines) > 1) }.to_a
end

grouped_changes.each do |group|
first_line = group.first[:line]
last_line = group.last[:line]
Expand Down

0 comments on commit cb6a62a

Please sign in to comment.