-
Notifications
You must be signed in to change notification settings - Fork 580
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
New actions for GitCommitBear #2927
Open
akshatkarani
wants to merge
2
commits into
coala:master
Choose a base branch
from
akshatkarani:add-newline-action
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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,35 @@ | ||
from coalib.misc.Shell import run_shell_command | ||
from coalib.results.result_actions.ResultAction import ResultAction | ||
|
||
|
||
class AddNewlineAction(ResultAction): | ||
""" | ||
Adds a newline between shortlog and body of the commit message | ||
of the HEAD commit. | ||
""" | ||
|
||
SUCCESS_MESSAGE = 'New Line added successfully.' | ||
|
||
def is_applicable(self, | ||
result, | ||
original_file_dict, | ||
file_diff_dict, | ||
applied_actions=()): | ||
new_message, _ = run_shell_command('git log -1 --pretty=%B') | ||
new_message = new_message.rstrip('\n') | ||
pos = new_message.find('\n') | ||
self.shortlog = new_message[:pos] if pos != -1 else new_message | ||
self.body = new_message[pos+1:] if pos != -1 else '' | ||
if self.body[0] != '\n': | ||
return True | ||
else: | ||
return False | ||
|
||
def apply(self, result, original_file_dict, file_diff_dict): | ||
""" | ||
Add New(L)ine [Note: This may rewrite your commit history] | ||
""" | ||
new_commit_message = '{}\n\n{}'.format(self.shortlog, self.body) | ||
command = 'git commit -o --amend -m "{}"'.format(new_commit_message) | ||
stdout, err = run_shell_command(command) | ||
return file_diff_dict |
This file contains 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,17 @@ | ||
import subprocess | ||
from coalib.results.result_actions.ResultAction import ResultAction | ||
|
||
|
||
class EditCommitMessageAction(ResultAction): | ||
""" | ||
Opens an editor to edit the commit message of the HEAD commit. | ||
""" | ||
|
||
SUCCESS_MESSAGE = 'Commit message edited successfully.' | ||
|
||
def apply(self, result, original_file_dict, file_diff_dict): | ||
""" | ||
Edit (C)ommit Message [Note: This may rewrite your commit history] | ||
""" | ||
subprocess.check_call(['git', 'commit', '-o', '--amend']) | ||
return file_diff_dict |
Empty file.
This file contains 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,96 @@ | ||
import unittest | ||
import os | ||
import platform | ||
import shutil | ||
from tempfile import mkdtemp, mkstemp | ||
from unittest.mock import Mock | ||
|
||
from coalib.results.Result import Result | ||
from bears.vcs.actions.AddNewlineAction import AddNewlineAction | ||
from coala_utils.ContextManagers import retrieve_stdout | ||
from coalib.misc.Shell import run_shell_command | ||
|
||
|
||
class AddNewlineActionTest(unittest.TestCase): | ||
|
||
@staticmethod | ||
def run_git_command(*args, stdin=None): | ||
return run_shell_command(' '.join(('git',) + args), stdin) | ||
|
||
def setUp(self): | ||
self.shortlog = 'file.py: Add something' | ||
self.body = ('Added something, wrote some things\n' | ||
'Wrote tests\n' | ||
'\n' | ||
'Fixes #issue') | ||
self.uut = AddNewlineAction() | ||
self.result = Result('origin', 'message') | ||
|
||
# Creating a temporary git repository and | ||
# adding a commit to test | ||
self._old_cwd = os.getcwd() | ||
self.gitdir = mkdtemp() | ||
os.chdir(self.gitdir) | ||
self.gitfile = mkstemp(dir=self.gitdir) | ||
self.run_git_command('init') | ||
self.run_git_command('config', 'user.email coala@coala.io') | ||
self.run_git_command('config', 'user.name coala') | ||
self.msg = self.shortlog + '\n' + self.body | ||
self.run_git_command('add .') | ||
self.run_git_command('commit', | ||
'--file=-', | ||
stdin=self.msg) | ||
|
||
def tearDown(self): | ||
# Deleting the temporary repository | ||
os.chdir(self._old_cwd) | ||
if platform.system() == 'Windows': | ||
onerror = self._windows_rmtree_remove_readonly | ||
else: | ||
onerror = None | ||
shutil.rmtree(self.gitdir, onerror=onerror) | ||
|
||
def test_is_applicable_apply(self): | ||
# Applicable because there is no newline between shortlog and body | ||
self.assertTrue(self.uut.is_applicable(self.result, {}, {})) | ||
|
||
with retrieve_stdout() as stdout: | ||
self.uut.apply(self.result, {}, {}) | ||
new_message, _ = run_shell_command('git log -1 --pretty=%B') | ||
new_message = new_message.rstrip('\n') | ||
self.assertEqual(new_message, | ||
self.shortlog + '\n\n' + self.body) | ||
self.assertEqual(stdout.getvalue(), '') | ||
|
||
# Not applicable after action is applied | ||
self.assertFalse(self.uut.is_applicable(self.result, {}, {})) | ||
|
||
# Undoing the amend done by applying the action | ||
self.run_git_command('commit', | ||
'--amend', | ||
'--file=-', | ||
stdin=self.msg) | ||
|
||
def test_is_applicable_edited_message(self): | ||
# Applicable because there is no newline between shortlog and body | ||
self.assertTrue(self.uut.is_applicable(self.result, {}, {})) | ||
|
||
# Mocking EditCommitMessageAction to test cases where user first | ||
# changes commit message by appying EditCommitMessageAction, then | ||
# checking the applicability of AddNewlineAction | ||
EditCommitMessageAction = Mock() | ||
edited_msg1 = ('This is new commit message\n' | ||
'Still no new line') | ||
edited_msg2 = ('This is lastest commit message\n' | ||
'\n' | ||
'Finally a new line!!') | ||
|
||
EditCommitMessageAction.apply.side_effect = self.run_git_command( | ||
'commit', '--amend', '--file=-', stdin=edited_msg1) | ||
EditCommitMessageAction.apply() | ||
self.assertTrue(self.uut.is_applicable(self.result, {}, {})) | ||
|
||
EditCommitMessageAction.apply.side_effect = self.run_git_command( | ||
'commit', '--amend', '--file=-', stdin=edited_msg2) | ||
EditCommitMessageAction.apply() | ||
self.assertFalse(self.uut.is_applicable(self.result, {}, {})) |
This file contains 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,24 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from coala_utils.ContextManagers import retrieve_stdout | ||
from coalib.results.Result import Result | ||
from bears.vcs.actions.EditCommitMessageAction import EditCommitMessageAction | ||
|
||
|
||
class EditCommitMessageActionTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.uut = EditCommitMessageAction() | ||
self.result = Result('origin', 'message') | ||
|
||
def test_is_applicable(self): | ||
self.assertTrue(self.uut.is_applicable(self.result, {}, {})) | ||
|
||
def test_apply(self): | ||
with retrieve_stdout() as stdout: | ||
patcher = ('bears.vcs.actions.EditCommitMessageAction.' | ||
'subprocess.check_call') | ||
with patch(patcher): | ||
ret = self.uut.apply(self.result, {}, {'file': 'diff'}) | ||
self.assertEqual(ret, {'file': 'diff'}) | ||
self.assertEqual(stdout.getvalue(), '') |
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User can first apply
EditCommitMessageAction
and change the commit message, so to check ifAddNewlineAction
is applicable or not, commit message is extracted first.