Skip to content

Commit a982a6f

Browse files
committed
Merge branch 'dev'
2 parents 18a7db6 + fdcbd29 commit a982a6f

10 files changed

Lines changed: 155 additions & 23 deletions

File tree

MANIFEST.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include LICENSE
2+
include README.rst
3+
include rules.cfg.sample
4+
include token.cfg.sample

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Sample webhook is running at https://labelbot-api.herokuapp.com/ and
77
it's functionality can be tested in
88
https://github.com/Fanarim/github\_labelbot\_testrepo repository.
99

10+
Package can be installed from testpypi using:
11+
12+
::
13+
14+
$ python -m pip install --extra-index-url https://testpypi.python.org/pypi github-labelbot
15+
1016
Configuration
1117
-------------
1218

labelbot/__init__.py

Whitespace-only changes.

labelbot/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .run import main
2+
3+
main()

labelbot/labelbot.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
from posixpath import join as urljoin
44

5+
import appdirs
56
import click
67
import json
8+
import os
79
import re
810
import requests
911
import sched
12+
import shutil
1013
import sys
1114
import time
1215
import validators
1316

17+
module_path = os.path.dirname(__file__)
18+
1419

1520
class LabelBot(object):
1621
"""Class taking care of whole GitHub labeling bot's functionality. """
@@ -24,24 +29,56 @@ class LabelBot(object):
2429

2530
def __init__(self, token_file, github_token, rules_file, default_label,
2631
check_comments, skip_labeled):
32+
config_dir = appdirs.user_config_dir('labelbot')
33+
# create config dir
34+
if not os.path.exists(config_dir):
35+
os.mkdir(config_dir)
36+
37+
# create/copy config files
38+
source_file = os.path.join(module_path, 'token.cfg.sample')
39+
sample_token_file = os.path.join(config_dir,
40+
'token.cfg.sample')
41+
if not os.path.exists(sample_token_file):
42+
shutil.copyfile(source_file, sample_token_file)
43+
44+
source_file = os.path.join(module_path, 'rules.cfg.sample')
45+
sample_rules_file = os.path.join(config_dir,
46+
'rules.cfg.sample')
47+
if not os.path.exists(sample_rules_file):
48+
shutil.copyfile(source_file, sample_rules_file)
49+
50+
# set default config files path if not set, get github token
51+
if token_file:
52+
self.token_file = token_file
53+
elif github_token:
54+
self.token = github_token
55+
else:
56+
print("Warning: You didn't set a GitHub token using -t or -u . "
57+
"Using default config file at " + sample_token_file + ". ",
58+
file=sys.stderr)
59+
self.token_file = sample_token_file
60+
self.token = self._get_token(self.token_file)
61+
62+
if rules_file:
63+
self.rules_file = rules_file
64+
else:
65+
print("Warning: You didn't set a rules config file. Using default "
66+
"file at " + sample_rules_file + ". ",
67+
file=sys.stderr)
68+
self.rules_file = sample_rules_file
69+
2770
self.last_issue_checked = 0
2871
self.iterval = 0
2972
self.scheduler = sched.scheduler(time.time, time.sleep)
3073
self.default_label = default_label
3174
self.check_comments = check_comments
3275
self.skip_labeled = skip_labeled
3376

34-
# get GitHub token
35-
if github_token:
36-
self.token = github_token
37-
else:
38-
self.token = self._get_token(token_file)
39-
4077
# get request session and validate token
4178
self.session = self._get_requests_session(self.token)
4279

4380
# load and validate rules
44-
self.rules = self._get_rules(rules_file)
81+
self.rules = self._get_rules(self.rules_file)
4582

4683
self._update_accessible_repos()
4784

@@ -95,7 +132,7 @@ def run_scheduled(self):
95132
self.scheduler.run()
96133

97134
def _label_repo(self, repo, reschedule=True):
98-
"""Iterates through all issues in given repo and runs _label_issue() on
135+
"""Iterates through all issues in given repo and runs label_issue() on
99136
each of them.
100137
101138
Args:
@@ -120,7 +157,7 @@ def _label_repo(self, repo, reschedule=True):
120157

121158
# iterate through all isues
122159
for issue in issues:
123-
self._label_issue(repo, issue)
160+
self.label_issue(repo, issue)
124161

125162
# run this again after given interval
126163
if reschedule:
@@ -235,7 +272,8 @@ def _get_requests_session(self, token):
235272
file=sys.stderr)
236273

237274
if response.status_code == 401:
238-
print('Did you provide a valid token? ',
275+
print('Did you provide a valid token? You can specifiy it',
276+
'using --github-token option. ',
239277
file=sys.stderr)
240278

241279
sys.exit(1)

labelbot/run.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
#!/usr/bin/env python3
22

3+
import appdirs
34
import click
4-
import console as labelbot_console
55
import os
6+
import shutil
7+
import sys
68

7-
from labelbot import LabelBot, UrlParam
8-
from web import app
9+
from .console import run
10+
from .labelbot import LabelBot, UrlParam
11+
from .web import app
12+
13+
module_path = os.path.dirname(__file__)
14+
15+
16+
class DummyLabelBot(object):
17+
def __init__(self, token_file, github_token, rules_file,
18+
default_label, check_comments, skip_labeled):
19+
self.token_file = token_file
20+
self.rules_file = rules_file
21+
self.github_token = github_token
22+
self.default_label = default_label
23+
self.check_comments = check_comments
24+
self.skip_labeled = skip_labeled
925

1026

1127
@click.group()
@@ -14,14 +30,12 @@
1430
type=click.Path(exists=True,
1531
file_okay=True,
1632
readable=True),
17-
default='token.cfg.sample',
1833
help='file containing GitHub token information')
1934
@click.option('--rules-file',
2035
'-u',
2136
type=click.Path(exists=True,
2237
file_okay=True,
2338
readable=True),
24-
default='rules.cfg.sample',
2539
help='file containing issues labeling rules')
2640
@click.option('--github-token',
2741
'-g',
@@ -41,8 +55,9 @@
4155
@click.pass_context
4256
def cli(ctx, token_file, github_token, rules_file, default_label,
4357
check_comments, skip_labeled):
44-
ctx.obj = LabelBot(token_file, github_token, rules_file, default_label,
45-
check_comments, skip_labeled)
58+
# pass click context
59+
ctx.obj = DummyLabelBot(token_file, github_token, rules_file,
60+
default_label, check_comments, skip_labeled)
4661

4762

4863
@cli.command(short_help='Run console daemon periodically checking issues')
@@ -57,15 +72,29 @@ def cli(ctx, token_file, github_token, rules_file, default_label,
5772
type=UrlParam())
5873
@click.pass_obj
5974
def console(labelbot, interval, repo_urls):
60-
labelbot_console.run(labelbot, interval, repo_urls)
75+
labelbot = LabelBot(labelbot.token_file,
76+
labelbot.github_token,
77+
labelbot.rules_file,
78+
labelbot.default_label,
79+
labelbot.check_comments,
80+
labelbot.skip_labeled,)
81+
run(labelbot, interval, repo_urls)
6182

6283

6384
@cli.command(short_help='Run web API listening for issue updates')
6485
@click.pass_obj
6586
def web(labelbot):
66-
port = int(os.environ.get('PORT', 80))
67-
app.config['labelbot'] = labelbot
68-
app.run(host='0.0.0.0', port=port, debug=True)
87+
port = int(os.environ.get('PORT', 8080))
88+
app.config['labelbot'] = LabelBot(labelbot.token_file,
89+
labelbot.github_token,
90+
labelbot.rules_file,
91+
labelbot.default_label,
92+
labelbot.check_comments,
93+
labelbot.skip_labeled,)
94+
app.run(host='0.0.0.0', port=port)
95+
96+
cli(prog_name='labelbot')
97+
6998

70-
if __name__ == '__main__':
99+
def main():
71100
cli()

labelbot/web.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from flask import render_template
44
from flask import request
55
from hashlib import sha1
6-
from labelbot import LabelBot
6+
from .labelbot import LabelBot
77

88
import hmac
99
import os

setup.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3.5
2+
3+
from setuptools import setup, find_packages
4+
5+
with open('README.rst') as readme:
6+
long_description = ''.join(readme.readlines())
7+
8+
setup(
9+
name='github-labelbot',
10+
version='0.3.0.20',
11+
description='Simple bot labeling GitHub issues based on it\'s configuration. ',
12+
long_description=long_description,
13+
author='David Viktora',
14+
author_email='viktoda2@fit.cvut.cz',
15+
keywords='github, bot, issues',
16+
license='MIT License',
17+
url='https://github.com/fanarim/github_labelbot',
18+
packages=find_packages(),
19+
classifiers=[
20+
'Intended Audience :: Developers',
21+
'License :: OSI Approved :: MIT License',
22+
'Operating System :: POSIX :: Linux',
23+
'Programming Language :: Python',
24+
'Programming Language :: Python :: 3',
25+
'Programming Language :: Python :: 3.5',
26+
'Topic :: Software Development :: Libraries',
27+
],
28+
install_requires=['click==6.6',
29+
'decorator==4.0.10',
30+
'Flask==0.11.1',
31+
'itsdangerous==0.24',
32+
'Jinja2==2.8',
33+
'MarkupSafe==0.23',
34+
'requests==2.11.1',
35+
'six==1.10.0',
36+
'validators==0.11.0',
37+
'Werkzeug==0.11.11',
38+
'appdirs==1.4.0'],
39+
entry_points={
40+
'console_scripts': [
41+
'labelbot = labelbot.run:main',
42+
],
43+
},
44+
package_data={
45+
'labelbot': [
46+
'static/custom.css',
47+
'templates/index.html',
48+
'rules.cfg.sample',
49+
'token.cfg.sample',
50+
]
51+
},
52+
)

0 commit comments

Comments
 (0)