Skip to content

WIP: Script to cli #21

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 12 commits into from
Oct 8, 2018
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
charset = utf-8
indent_style = space
end_of_line = lf
trim_trailing_whitespace = false
trim_trailing_whitespace = true
indent_size = 2

[*.py]
indent_size = 4
11 changes: 11 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Proposed changes
(description of our changes and why)

## Screen-shot of changes
(if it applies)

## Link to issue
(link issues here, specially if there is more than one issue being solved)

## Other comments
(is there anything else you want to tell us?)
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/
commiter.yml
__pycache__/
package.json
package-lock.json
57 changes: 38 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Just follow the commands below:
# have git ready to use
$ sudo apt install python3-pip git

$ pip3 install -r requirements.txt

# clone the repo into your home
$ git clone https://github.com/andre-filho/commit-helper.git ~/.commit-helper

$ pip3 install -r ~/.commit-helper/requirements.txt

# create a function in your .bashrc
$ echo "commit(){ python3 ~/.commit-helper/generator.py; }" >> ~/.bashrc

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

## Usage and configuration

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:
This program has a cli that you can take advantage of. Running `commit --help`
will show you the usage and options for your commit. All of them are optional
for the sake of not losing your precious time.

```bash
$ commit -h
usage: generator.py [-h] [--co-author CO_AUTHOR] [--no-generate NO_FILE]
[--convention {angular,changelog,symphony,message}]

A commit formatter tool to help you follow commit conventions.

optional arguments:
-h, --help show this help message and exit
--co-author CO_AUTHOR
make your friend an co-author to the commit
--no-generate NO_FILE
disables the creation of a commiter.yml file
--convention {angular,changelog,symphony,message}
Selects a convention to be used for the commit.
Required if there is no commiter.yml file.
```

So, if you want to write a co-authored commit, you should use:

```bash
$ commit --co-author "foo bar doritous <foobar@douritos.com>"
```

Or if you are using this for the first time in your project:

```bash
$ commit --convention changelog
```

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:

```yaml
convention: angular # tag(context): commit message
Expand All @@ -53,27 +87,11 @@ convention: none # Commit message
```

Supported conventions available:
<!-- list here all tags that are used in configuration file -->

- angular/karma
- changelog
- symphony

In the event of no commiter.yml file presence, you will be prompted with the following option menu:

```bash
No config files found!
Running default script...
what type of commit convention are you using?

(default): No convention
1: Karma/Angular
2: Conventional changelog
3: Symfony CMF

```



## Project's maintainers
| **Name** | **Username** |
Expand All @@ -84,3 +102,4 @@ Running default script...
| **Name** | **Username** |
| :------: | :----------: |
| Arthur José Benedito de Oliveira Assis | @arthur120496 |

7 changes: 5 additions & 2 deletions conventions/changelog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from os import system
from utils import get_text
from utils import change_if_none


def changelog_convention():
def changelog_convention(co_author=''):
tag, msg = get_text()
tag = tag.upper()
system("git commit -m '%s: %s'" % (tag, msg))
co_author = change_if_none(co_author)
composed_message = """%s: %s\n\nCo-authored-by: """ % (tag, msg)
system("git commit -m '%s%s'" % (composed_message, co_author))
4 changes: 2 additions & 2 deletions conventions/custom_convention_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# FUTURE: implement
def custom_convention():
pass
# def custom_convention():
# pass
8 changes: 6 additions & 2 deletions conventions/karma_angular.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from os import system
from utils import get_text
from utils import change_if_none


def angular_convention():
def angular_convention(co_author=''):
tag, msg, context = get_text(context=True)
tag = tag.lower()
system("git commit -m '%s(%s): %s'" % (tag, context, msg))
co_author = change_if_none(co_author)
composed_message = """%s(%s): %s\n\nCo-authored-by:
""" % (tag, context, msg)
system('git commit -m "%s%s"' % (composed_message, co_author))
7 changes: 5 additions & 2 deletions conventions/no_convention.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from os import system
from utils import change_if_none


def just_message():
def just_message(co_author=''):
msg = str(input("commit message: "))
system("git commit -m '%s'" % msg.capitalize())
co_author = change_if_none(co_author)
composed = """%s\n\nCo-authored-by: """ % msg.capitalize()
system("git commit -m '%s%s'" % (composed, co_author))
7 changes: 5 additions & 2 deletions conventions/symphony_cmf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from os import system
from utils import get_text
from utils import change_if_none


def symphony_convention():
def symphony_convention(co_author=''):
tag, msg = get_text()
tag = tag.capitalize()
system("git commit -m '[%s] %s'" % (tag, msg))
co_author = change_if_none(co_author)
composed = """[%s] %s\n\nCo-authored-by: """ % (tag, msg)
system("git commit -m '%s%s'" % (composed, co_author))
64 changes: 29 additions & 35 deletions generator.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#! /usr/bin/env python3

# dependencies imports
from pathlib import Path
from yaml import safe_load
from yaml import YAMLError
Expand All @@ -8,60 +11,51 @@
from conventions.symphony_cmf import symphony_convention
from conventions.no_convention import just_message

# utils imports
from utils import parser_cli
from utils import create_file


parser = parser_cli()
args = parser.parse_args()

file_path = Path('commiter.yml')
if file_path.is_file():
with open(str(file_path), 'r') as stream:
try:
config = safe_load(stream)

if config['convention'] is not None:
convention = str(config['convention']).lower()
else:
convention = 'none'

if convention == 'angular' or convention == 'karma':
print('You are using the %s convention' % convention)
angular_convention()
angular_convention(args.co_author)
elif convention == 'changelog':
print('You are using the %s convention' % convention)
changelog_convention()
changelog_convention(args.co_author)
elif convention == 'symphony':
print('You are using the %s convention' % convention)
symphony_convention()
symphony_convention(args.co_author)
elif convention == 'none':
just_message()
elif convention == 'custom':
custom_convention()

just_message(args.co_author)
except YAMLError as exc:
print(exc)
else:
print("No config files found!\nRunning default script...")
opt = int(input("""
what type of commit convention are you using?

default: Just the message
1: Karma/Angular
2: Conventional changelog
3: Symfony CMF
elif args.convention is not '':
convention = str(args.convention)
if convention == 'angular' or convention == 'karma':
angular_convention(args.co_author)
create_file(convention, args.no_file)
elif convention == 'changelog':
changelog_convention(args.co_author)
create_file(convention, args.no_file)
elif convention == 'symphony':
symphony_convention(args.co_author)
create_file(convention, args.no_file)
elif convention == 'message':
just_message(convention)
create_file('none', args.no_file)

""") or 4)

if opt == 1:
print("You're using the angular convention")
angular_convention()
create_file('angular')
elif opt == 2:
print("You're using the changelog convention")
changelog_convention()
create_file('changelog')
elif opt == 3:
print("You're using the symphony convention")
symphony_convention()
create_file('symphony')
elif opt == 4:
print("You're not using a convention")
just_message()
create_file('none')
else:
parser.print_help()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pyyaml==3.13
pytest==3.8.1
pycodestyle==2.4.0
argparse==1.4.0
3 changes: 2 additions & 1 deletion test/test_conventions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
# import pytest
# import conventions.karma_angular as angular
# import conventions.changelog as changelog
# import convention.symphony_cmf as symphony
# import conventions.no_convention as no_convention


def test_angular_convention():
pass

Expand Down
28 changes: 21 additions & 7 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
import utils
import yaml

Expand All @@ -13,8 +12,10 @@ def mock_input(s):
return inputs.pop(0)
utils.input = mock_input
a, b = utils.get_text()
assert a == 'tag'
assert b == 'message'
if not a == 'tag':
raise AssertionError()
if not b == 'message':
raise AssertionError()


def test_get_text_context():
Expand All @@ -28,14 +29,27 @@ def mock_input(s):
return inputs.pop(0)
utils.input = mock_input
a, b, c = utils.get_text(context=True)
assert a == 'tag'
assert b == 'message'
assert c == 'context'
if not a == 'tag':
raise AssertionError()
if not b == 'message':
raise AssertionError()
if not c == 'context':
raise AssertionError()


def test_change_if_none():
string = 'asopdfha'
string2 = None
string = utils.change_if_none(string)
string2 = utils.change_if_none(string2)
if not (string == 'asopdfha' and string2 == ''):
raise AssertionError()

# FIXME
# def test_create_file(tmpdir):
# test_file = tmpdir.mkdir('test').join('commiter.yml')
# utils.create_file("changelog")
# stream = open('commiter.yml', 'r')
# convention = yaml.load(stream)
# assert convention['convention'] == 'changelog'
# if not convention['convention'] == 'changelog':
# raise AssertionError()
Loading