Skip to content

Commit d56894b

Browse files
authored
merge(#19 #21 #6): implemented cli and suppor to co-authors
WIP: Script to cli
2 parents 2f79e28 + 64cf043 commit d56894b

14 files changed

+180
-79
lines changed

.editorconfig

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
charset = utf-8
33
indent_style = space
44
end_of_line = lf
5-
trim_trailing_whitespace = false
5+
trim_trailing_whitespace = true
6+
indent_size = 2
67

78
[*.py]
89
indent_size = 4

.github/PULL_REQUEST_TEMPLATE.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Proposed changes
2+
(description of our changes and why)
3+
4+
## Screen-shot of changes
5+
(if it applies)
6+
7+
## Link to issue
8+
(link issues here, specially if there is more than one issue being solved)
9+
10+
## Other comments
11+
(is there anything else you want to tell us?)

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.vscode/
22
commiter.yml
33
__pycache__/
4+
package.json
5+
package-lock.json

README.md

+38-19
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Just follow the commands below:
2020
# have git ready to use
2121
$ sudo apt install python3-pip git
2222

23-
$ pip3 install -r requirements.txt
24-
2523
# clone the repo into your home
2624
$ git clone https://github.com/andre-filho/commit-helper.git ~/.commit-helper
2725

26+
$ pip3 install -r ~/.commit-helper/requirements.txt
27+
2828
# create a function in your .bashrc
2929
$ echo "commit(){ python3 ~/.commit-helper/generator.py; }" >> ~/.bashrc
3030

@@ -34,7 +34,41 @@ Just follow the commands below:
3434

3535
## Usage and configuration
3636

37-
For this project to work smoothly, you must have in your working directory a file named **commiter.yml**. In this file you must pass the commit convention that you want to use, following the example:
37+
This program has a cli that you can take advantage of. Running `commit --help`
38+
will show you the usage and options for your commit. All of them are optional
39+
for the sake of not losing your precious time.
40+
41+
```bash
42+
$ commit -h
43+
usage: generator.py [-h] [--co-author CO_AUTHOR] [--no-generate NO_FILE]
44+
[--convention {angular,changelog,symphony,message}]
45+
46+
A commit formatter tool to help you follow commit conventions.
47+
48+
optional arguments:
49+
-h, --help show this help message and exit
50+
--co-author CO_AUTHOR
51+
make your friend an co-author to the commit
52+
--no-generate NO_FILE
53+
disables the creation of a commiter.yml file
54+
--convention {angular,changelog,symphony,message}
55+
Selects a convention to be used for the commit.
56+
Required if there is no commiter.yml file.
57+
```
58+
59+
So, if you want to write a co-authored commit, you should use:
60+
61+
```bash
62+
$ commit --co-author "foo bar doritous <foobar@douritos.com>"
63+
```
64+
65+
Or if you are using this for the first time in your project:
66+
67+
```bash
68+
$ commit --convention changelog
69+
```
70+
71+
To work even more smoothly, have in your working directory a file named **commiter.yml**. In this file you must pass the commit convention that you want to use, following the example:
3872
3973
```yaml
4074
convention: angular # tag(context): commit message
@@ -53,27 +87,11 @@ convention: none # Commit message
5387
```
5488
5589
Supported conventions available:
56-
<!-- list here all tags that are used in configuration file -->
5790
5891
- angular/karma
5992
- changelog
6093
- symphony
6194
62-
In the event of no commiter.yml file presence, you will be prompted with the following option menu:
63-
64-
```bash
65-
No config files found!
66-
Running default script...
67-
what type of commit convention are you using?
68-
69-
(default): No convention
70-
1: Karma/Angular
71-
2: Conventional changelog
72-
3: Symfony CMF
73-
74-
```
75-
76-
7795
7896
## Project's maintainers
7997
| **Name** | **Username** |
@@ -84,3 +102,4 @@ Running default script...
84102
| **Name** | **Username** |
85103
| :------: | :----------: |
86104
| Arthur José Benedito de Oliveira Assis | @arthur120496 |
105+

conventions/changelog.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from os import system
22
from utils import get_text
3+
from utils import change_if_none
34

45

5-
def changelog_convention():
6+
def changelog_convention(co_author=''):
67
tag, msg = get_text()
78
tag = tag.upper()
8-
system("git commit -m '%s: %s'" % (tag, msg))
9+
co_author = change_if_none(co_author)
10+
composed_message = """%s: %s\n\nCo-authored-by: """ % (tag, msg)
11+
system("git commit -m '%s%s'" % (composed_message, co_author))
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# FUTURE: implement
2-
def custom_convention():
3-
pass
2+
# def custom_convention():
3+
# pass

conventions/karma_angular.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from os import system
22
from utils import get_text
3+
from utils import change_if_none
34

45

5-
def angular_convention():
6+
def angular_convention(co_author=''):
67
tag, msg, context = get_text(context=True)
78
tag = tag.lower()
8-
system("git commit -m '%s(%s): %s'" % (tag, context, msg))
9+
co_author = change_if_none(co_author)
10+
composed_message = """%s(%s): %s\n\nCo-authored-by:
11+
""" % (tag, context, msg)
12+
system('git commit -m "%s%s"' % (composed_message, co_author))

conventions/no_convention.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from os import system
2+
from utils import change_if_none
23

34

4-
def just_message():
5+
def just_message(co_author=''):
56
msg = str(input("commit message: "))
6-
system("git commit -m '%s'" % msg.capitalize())
7+
co_author = change_if_none(co_author)
8+
composed = """%s\n\nCo-authored-by: """ % msg.capitalize()
9+
system("git commit -m '%s%s'" % (composed, co_author))

conventions/symphony_cmf.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from os import system
22
from utils import get_text
3+
from utils import change_if_none
34

45

5-
def symphony_convention():
6+
def symphony_convention(co_author=''):
67
tag, msg = get_text()
78
tag = tag.capitalize()
8-
system("git commit -m '[%s] %s'" % (tag, msg))
9+
co_author = change_if_none(co_author)
10+
composed = """[%s] %s\n\nCo-authored-by: """ % (tag, msg)
11+
system("git commit -m '%s%s'" % (composed, co_author))

generator.py

100644100755
+29-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#! /usr/bin/env python3
2+
3+
# dependencies imports
14
from pathlib import Path
25
from yaml import safe_load
36
from yaml import YAMLError
@@ -8,60 +11,51 @@
811
from conventions.symphony_cmf import symphony_convention
912
from conventions.no_convention import just_message
1013

14+
# utils imports
15+
from utils import parser_cli
1116
from utils import create_file
1217

18+
19+
parser = parser_cli()
20+
args = parser.parse_args()
21+
1322
file_path = Path('commiter.yml')
1423
if file_path.is_file():
1524
with open(str(file_path), 'r') as stream:
1625
try:
1726
config = safe_load(stream)
18-
1927
if config['convention'] is not None:
2028
convention = str(config['convention']).lower()
2129
else:
2230
convention = 'none'
23-
2431
if convention == 'angular' or convention == 'karma':
2532
print('You are using the %s convention' % convention)
26-
angular_convention()
33+
angular_convention(args.co_author)
2734
elif convention == 'changelog':
2835
print('You are using the %s convention' % convention)
29-
changelog_convention()
36+
changelog_convention(args.co_author)
3037
elif convention == 'symphony':
3138
print('You are using the %s convention' % convention)
32-
symphony_convention()
39+
symphony_convention(args.co_author)
3340
elif convention == 'none':
34-
just_message()
35-
elif convention == 'custom':
36-
custom_convention()
37-
41+
just_message(args.co_author)
3842
except YAMLError as exc:
3943
print(exc)
40-
else:
41-
print("No config files found!\nRunning default script...")
42-
opt = int(input("""
43-
what type of commit convention are you using?
4444

45-
default: Just the message
46-
1: Karma/Angular
47-
2: Conventional changelog
48-
3: Symfony CMF
45+
elif args.convention is not '':
46+
convention = str(args.convention)
47+
if convention == 'angular' or convention == 'karma':
48+
angular_convention(args.co_author)
49+
create_file(convention, args.no_file)
50+
elif convention == 'changelog':
51+
changelog_convention(args.co_author)
52+
create_file(convention, args.no_file)
53+
elif convention == 'symphony':
54+
symphony_convention(args.co_author)
55+
create_file(convention, args.no_file)
56+
elif convention == 'message':
57+
just_message(convention)
58+
create_file('none', args.no_file)
4959

50-
""") or 4)
51-
52-
if opt == 1:
53-
print("You're using the angular convention")
54-
angular_convention()
55-
create_file('angular')
56-
elif opt == 2:
57-
print("You're using the changelog convention")
58-
changelog_convention()
59-
create_file('changelog')
60-
elif opt == 3:
61-
print("You're using the symphony convention")
62-
symphony_convention()
63-
create_file('symphony')
64-
elif opt == 4:
65-
print("You're not using a convention")
66-
just_message()
67-
create_file('none')
60+
else:
61+
parser.print_help()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pyyaml==3.13
22
pytest==3.8.1
33
pycodestyle==2.4.0
4+
argparse==1.4.0

test/test_conventions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import pytest
1+
# import pytest
22
# import conventions.karma_angular as angular
33
# import conventions.changelog as changelog
44
# import convention.symphony_cmf as symphony
55
# import conventions.no_convention as no_convention
66

7+
78
def test_angular_convention():
89
pass
910

test/test_utils.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import pytest
21
import utils
32
import yaml
43

@@ -13,8 +12,10 @@ def mock_input(s):
1312
return inputs.pop(0)
1413
utils.input = mock_input
1514
a, b = utils.get_text()
16-
assert a == 'tag'
17-
assert b == 'message'
15+
if not a == 'tag':
16+
raise AssertionError()
17+
if not b == 'message':
18+
raise AssertionError()
1819

1920

2021
def test_get_text_context():
@@ -28,14 +29,27 @@ def mock_input(s):
2829
return inputs.pop(0)
2930
utils.input = mock_input
3031
a, b, c = utils.get_text(context=True)
31-
assert a == 'tag'
32-
assert b == 'message'
33-
assert c == 'context'
32+
if not a == 'tag':
33+
raise AssertionError()
34+
if not b == 'message':
35+
raise AssertionError()
36+
if not c == 'context':
37+
raise AssertionError()
38+
39+
40+
def test_change_if_none():
41+
string = 'asopdfha'
42+
string2 = None
43+
string = utils.change_if_none(string)
44+
string2 = utils.change_if_none(string2)
45+
if not (string == 'asopdfha' and string2 == ''):
46+
raise AssertionError()
3447

3548
# FIXME
3649
# def test_create_file(tmpdir):
3750
# test_file = tmpdir.mkdir('test').join('commiter.yml')
3851
# utils.create_file("changelog")
3952
# stream = open('commiter.yml', 'r')
4053
# convention = yaml.load(stream)
41-
# assert convention['convention'] == 'changelog'
54+
# if not convention['convention'] == 'changelog':
55+
# raise AssertionError()

0 commit comments

Comments
 (0)