-
Notifications
You must be signed in to change notification settings - Fork 10
Add context attribute to link TODOs to GitHub issues
#107
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7c8b486
Add `context` attribute to link `TODO`s with GitHub issues
couimet 39ccf2b
Add missing validation and documentation to ensure `context` isn't co…
couimet 8abc253
Fix broken tests
couimet c9e8887
Added missing newline
couimet e66af5a
Got rid of clanker-provided comments I should have removed
couimet 532ecee
PR feedback: re-organize the code and remove some comments
couimet 2090a29
PR feedback: organize in a closer approach to other blocks above
couimet 5d196bd
Remove comment that belongs somewhere else
couimet 7a0a3cf
Add missing newline
couimet d030ac6
PR feedback: use a string format for `context` instead of using a mor…
couimet 6bd985a
Make rubocop happy
couimet f6ad862
Merge pull request #110 from Shopify/add_context_as_single_string
couimet 57477e8
PR feedback: remove restriction on `context`; it can be added to any …
couimet 77ec138
PR feedback: use `to_return_json` instead of `to_return` + `JSON.dump()`
couimet ed3d9f8
PR feedback: rescuing for some errors and let the other ones go through
couimet c8f0dca
PR feedback: examples will be moved to the wiki
couimet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,25 @@ When the TODO's event is met (i.e. a certain date is reached), the TODO's assign | |
| end | ||
| ``` | ||
|
|
||
| You can also add context to your TODOs by linking them to GitHub issues. The `context` attribute | ||
| works with `date`, `gem_release`, `gem_bump`, and `ruby_version` events: | ||
|
|
||
| ```ruby | ||
| # TODO(on: date('2025-01-01'), to: '[email protected]', context: issue('shopify', 'smart_todo', '108')) | ||
| # Implement the caching strategy discussed in the issue | ||
| def process_order | ||
| end | ||
|
|
||
| # TODO(on: gem_release('rails', '> 7.2'), to: '[email protected]', context: issue('rails', 'rails', '456')) | ||
| # Upgrade to new Rails version as discussed in the issue | ||
| def legacy_method | ||
| end | ||
| ``` | ||
|
|
||
| When the TODO is triggered, the linked issue's title, state, and assignee will be included in the notification. | ||
|
|
||
| Note: The `context` attribute is not supported with `issue_close` or `pull_request_close` events as they already reference specific issues/PRs. | ||
|
|
||
| Documentation | ||
| ---------------- | ||
| Please check out the GitHub [wiki](https://github.com/Shopify/smart_todo/wiki) for documentation and example on how to setup SmartTodo in your project. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,5 +150,52 @@ def hello | |
| end | ||
| end | ||
| end | ||
|
|
||
| def test_should_apply_context_returns_false_without_context | ||
| cli = CLI.new | ||
| todo = Todo.new("# TODO(on: date('2015-03-01'), to: '[email protected]')") | ||
| event = Todo::CallNode.new(:date, ["2015-03-01"], nil) | ||
|
|
||
| refute(cli.send(:should_apply_context?, todo, event)) | ||
| end | ||
|
|
||
| def test_should_apply_context_returns_false_for_issue_close_event | ||
| cli = CLI.new | ||
| todo = Todo.new("# TODO(on: issue_close('org', 'repo', '123'), to: '[email protected]')") | ||
| todo.context = Todo::CallNode.new(:issue, ["org", "repo", "456"], nil) | ||
| event = Todo::CallNode.new(:issue_close, ["org", "repo", "123"], nil) | ||
|
|
||
| refute(cli.send(:should_apply_context?, todo, event)) | ||
| end | ||
|
|
||
| def test_should_apply_context_returns_false_for_pull_request_close_event | ||
| cli = CLI.new | ||
| todo = Todo.new("# TODO(on: pull_request_close('org', 'repo', '123'), to: '[email protected]')") | ||
| todo.context = Todo::CallNode.new(:issue, ["org", "repo", "456"], nil) | ||
| event = Todo::CallNode.new(:pull_request_close, ["org", "repo", "123"], nil) | ||
|
|
||
| refute(cli.send(:should_apply_context?, todo, event)) | ||
| end | ||
|
|
||
| def test_should_apply_context_returns_true_for_regular_event_with_context | ||
| cli = CLI.new | ||
| todo = Todo.new("# TODO(on: date('2015-03-01'), to: '[email protected]')") | ||
| todo.context = Todo::CallNode.new(:issue, ["org", "repo", "456"], nil) | ||
| event = Todo::CallNode.new(:date, ["2015-03-01"], nil) | ||
|
|
||
| assert(cli.send(:should_apply_context?, todo, event)) | ||
| end | ||
|
|
||
| def test_append_context_if_applicable_returns_original_message_when_context_not_applicable | ||
| cli = CLI.new | ||
| todo = Todo.new("# TODO(on: issue_close('org', 'repo', '123'), to: '[email protected]')") | ||
| todo.context = Todo::CallNode.new(:issue, ["org", "repo", "456"], nil) | ||
| event = Todo::CallNode.new(:issue_close, ["org", "repo", "123"], nil) | ||
| events = Events.new | ||
| original_message = "Original event message" | ||
|
|
||
| result = cli.send(:append_context_if_applicable, original_message, todo, event, events) | ||
| assert_equal(original_message, result) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| module SmartTodo | ||
| class Events | ||
| class IssueContextTest < Minitest::Test | ||
| def test_when_issue_exists_and_is_open | ||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
couimet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| state: "open", | ||
| title: "Add support for caching", | ||
| number: 123, | ||
| assignee: { login: "johndoe" }, | ||
| )) | ||
|
|
||
| expected = "📌 Context: Issue #123 - \"Add support for caching\" [open] (@johndoe) - " \ | ||
| "https://github.com/rails/rails/issues/123" | ||
|
|
||
| assert_equal(expected, issue_context("rails", "rails", "123")) | ||
| end | ||
|
|
||
| def test_when_issue_exists_and_is_closed | ||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "closed", | ||
| title: "Fix memory leak", | ||
| number: 456, | ||
| assignee: { login: "janedoe" }, | ||
| )) | ||
|
|
||
| expected = "📌 Context: Issue #456 - \"Fix memory leak\" [closed] (@janedoe) - " \ | ||
| "https://github.com/shopify/smart_todo/issues/456" | ||
|
|
||
| assert_equal(expected, issue_context("shopify", "smart_todo", "456")) | ||
| end | ||
|
|
||
| def test_when_issue_has_no_assignee | ||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "open", | ||
| title: "Improve documentation", | ||
| number: 789, | ||
| assignee: nil, | ||
| )) | ||
|
|
||
| expected = "📌 Context: Issue #789 - \"Improve documentation\" [open] (unassigned) - " \ | ||
| "https://github.com/org/repo/issues/789" | ||
|
|
||
| assert_equal(expected, issue_context("org", "repo", "789")) | ||
| end | ||
|
|
||
| def test_when_issue_does_not_exist | ||
| stub_request(:get, /api.github.com/) | ||
| .to_return(status: 404) | ||
|
|
||
| assert_nil(issue_context("rails", "rails", "999")) | ||
| end | ||
|
|
||
| def test_when_token_env_is_not_present | ||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "open", | ||
| title: "Test issue", | ||
| number: 1, | ||
| assignee: nil, | ||
| )) | ||
|
|
||
| result = issue_context("rails", "rails", "1") | ||
| assert(result.include?("📌 Context: Issue #1")) | ||
|
|
||
| assert_requested(:get, /api.github.com/) do |request| | ||
| assert(!request.headers.key?("Authorization")) | ||
| end | ||
| end | ||
|
|
||
| def test_when_token_env_is_present | ||
| ENV[GITHUB_TOKEN] = "abc123" | ||
|
|
||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "open", | ||
| title: "Test issue", | ||
| number: 2, | ||
| assignee: nil, | ||
| )) | ||
|
|
||
| result = issue_context("rails", "rails", "2") | ||
| assert(result.include?("📌 Context: Issue #2")) | ||
|
|
||
| assert_requested(:get, /api.github.com/) do |request| | ||
| assert_equal("token abc123", request.headers["Authorization"]) | ||
| end | ||
| ensure | ||
| ENV.delete(GITHUB_TOKEN) | ||
| end | ||
|
|
||
| def test_when_organization_specific_token_is_present | ||
| ENV["#{GITHUB_TOKEN}__RAILS"] = "rails_token" | ||
|
|
||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "open", | ||
| title: "Test issue", | ||
| number: 3, | ||
| assignee: nil, | ||
| )) | ||
|
|
||
| result = issue_context("rails", "rails", "3") | ||
| assert(result.include?("📌 Context: Issue #3")) | ||
|
|
||
| assert_requested(:get, /api.github.com/) do |request| | ||
| assert_equal("token rails_token", request.headers["Authorization"]) | ||
| end | ||
| ensure | ||
| ENV.delete("#{GITHUB_TOKEN}__RAILS") | ||
| end | ||
|
|
||
| def test_when_repo_specific_token_is_present | ||
| ENV["#{GITHUB_TOKEN}__RAILS__RAILS"] = "rails_rails_token" | ||
|
|
||
| stub_request(:get, /api.github.com/) | ||
| .to_return(body: JSON.dump( | ||
| state: "open", | ||
| title: "Test issue", | ||
| number: 4, | ||
| assignee: nil, | ||
| )) | ||
|
|
||
| result = issue_context("rails", "rails", "4") | ||
| assert(result.include?("📌 Context: Issue #4")) | ||
|
|
||
| assert_requested(:get, /api.github.com/) do |request| | ||
| assert_equal("token rails_rails_token", request.headers["Authorization"]) | ||
| end | ||
| ensure | ||
| ENV.delete("#{GITHUB_TOKEN}__RAILS__RAILS") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def issue_context(*args) | ||
| Events.new.issue_context(*args) | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.