-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__/ |
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 |
---|---|---|
@@ -1,2 +1,65 @@ | ||
# ptl | ||
Automatically log time in issue tracker for COMP23311 at UoM. | ||
# ptl | ||
|
||
<a target="_blank" href="https://www.python.org/downloads/" title="Python version"><img src="https://img.shields.io/badge/python-%3E=_3.6-green.svg"></a> | ||
[](https://www.gnu.org/licenses/gpl-3.0) | ||
**ptl**: Automatically log time in GitLab issue tracker for COMP23311 at UoM. | ||
|
||
## Installation | ||
|
||
```bash | ||
# Clone the repo | ||
joss@moff:~$: git clone /~https://github.com/JossMoff/ptl.git | ||
|
||
# Change the working directory to PyRequisite | ||
joss@moff:~$: cd ptl | ||
|
||
# Install PyRequisite onto your system | ||
joss@moff:~$: python3 setup.py install | ||
``` | ||
|
||
> Sometimes the last step will require sudo as you might not has write access to /usr/local/lib/python3.6/dist-packages/ | ||
## Usage | ||
|
||
```bash | ||
joss@moff:~$ ptl --help | ||
usage: ptl [-h] [-c] [-t TOKEN] [-p PROJECT_ID] [-i ISSUE_ID] [-s] | ||
|
||
ptl: Automatically Log Time in issue tracking at UoM. | ||
|
||
optional arguments: | ||
-h, --help show this help message and exit | ||
-c, --config Shows config of current ptl settings | ||
-t TOKEN, --token TOKEN | ||
Sets private token for GitLab user | ||
-p PROJECT_ID, --projectid PROJECT_ID | ||
Sets project id for a GitLab repository | ||
-i ISSUE_ID, --issueid ISSUE_ID | ||
Sets issue id for an issue in a GitLab repository | ||
-s, --start Start timing IDE open time. | ||
``` | ||
In order to correctly use the tool you will need to set the TOKEN, PROJECT_ID and ISSUE_ID using: | ||
```bash | ||
joss@moff:~$ ptl -t <token> -p <project_id> -i <issue_id> | ||
``` | ||
Then open eclipse and run: | ||
```bash | ||
joss@moff:~$ ptl -s | ||
``` | ||
Once eclipse has closed it will then write the time you've spent rounded up to the nearest hour in the selected issue tracker. | ||
## Getting Access Token | ||
In order to gain a personal access token for to set with the -t/--token flag | ||
 | ||
## Further Improvements | ||
How I plan to extend the quick project: | ||
- 🍎Provide switch ability to maintain multiple issues. | ||
- 🔒Add safety precautions to make sure you can connect to gitlab. | ||
- 💻Add extra terminal features |
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 @@ | ||
from .__main__ import * |
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,90 @@ | ||
import psutil | ||
import time | ||
import math | ||
import gitlab | ||
import os | ||
from argparse import ArgumentParser | ||
import configparser | ||
|
||
MODULE_NAME = "ptl: Automatically log time in GitLab issue tracker for COMP23311 at UoM." | ||
__version__ = "0.1.0" | ||
|
||
def print_config(token, project_id, issue_id): | ||
print("--:CONFIG:--\n" + "🎫 TOKEN:" + token + "\n🆔 PROJECT-ID:" + project_id + "\n🆔 ISSUE-ID:" + issue_id) | ||
|
||
def record_time(token, project_id, issue_id, ide="eclipse"): | ||
eclipse_id = -1 | ||
# Iterate over all running process | ||
for proc in psutil.process_iter(): | ||
try: | ||
# Get process name & pid from process object. | ||
proc_name = proc.name() | ||
proc_id = proc.pid | ||
if ide in proc_name: | ||
eclipse_id = proc_id | ||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | ||
pass | ||
if eclipse_id != -1: | ||
# Get start_time | ||
print("⏱️ Recording elapsed worktime for " + ide) | ||
start_time = time.time() | ||
while psutil.pid_exists(eclipse_id): | ||
time.sleep(1) | ||
end_time = time.time() | ||
elapsed_time = (end_time - start_time) / 3600 | ||
elapsed_time = (math.ceil(elapsed_time)) | ||
# private token or personal token authentication | ||
gl = gitlab.Gitlab('https://gitlab.cs.man.ac.uk', private_token=token) | ||
# Make an API request and authenticate in order to add issue. | ||
gl.auth() | ||
project = gl.projects.get(project_id) | ||
issue = project.issues.get(issue_id) | ||
issue.add_spent_time(str(elapsed_time)+'h') | ||
print("⏱️ " + str(elapsed_time) + "h of time recorded.") | ||
else: | ||
print("❌ IDE not running yet!") | ||
|
||
def set_config(token, project_id, issue_id, config): | ||
config['SETTINGS']['token'] = token | ||
config['SETTINGS']['project_id'] = project_id | ||
config['SETTINGS']['issue_id'] = issue_id | ||
with open('config.ini', 'w') as configfile: | ||
config.write(configfile) | ||
return config | ||
|
||
def main(): | ||
config = configparser.ConfigParser() | ||
config.read('config.ini') | ||
if len(config) == 1 and 'DEFAULT' in config: | ||
config['SETTINGS'] = {} | ||
config = set_config("0", "0", "0", config) | ||
parser = ArgumentParser(description=MODULE_NAME) | ||
parser.add_argument('-c','--config', | ||
action="store_true", dest="config", default=False, | ||
help="Shows config of current ptl settings") | ||
parser.add_argument('-t','--token', | ||
type=str, dest="token", default=config['SETTINGS']['token'], | ||
help="Sets private token for GitLab user") | ||
parser.add_argument('-p','--projectid', | ||
type=str, dest="project_id", default=config['SETTINGS']['project_id'], | ||
help="Sets project id for a GitLab repository") | ||
parser.add_argument('-i','--issueid', | ||
type=str, dest="issue_id", default=config['SETTINGS']['issue_id'], | ||
help="Sets issue id for an issue in a GitLab repository") | ||
parser.add_argument('-s','--start', | ||
action="store_true", dest="time", default=False, | ||
help="Start timing IDE open time.") | ||
|
||
|
||
args = parser.parse_args() | ||
if args.config: | ||
print_config(args.token,args. project_id, args.issue_id) | ||
else: | ||
if (args.token != config['SETTINGS']['token'] or args.project_id != config['SETTINGS']['project_id'] | ||
or args.issue_id != config['SETTINGS']['issue_id']): | ||
config = set_config(args.token, args.project_id, args.issue_id, config) | ||
elif args.time: | ||
record_time(args.token, int(args.project_id), int(args.issue_id)) | ||
|
||
if __name__ == "__main__": | ||
main() |
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,5 @@ | ||
[SETTINGS] | ||
token = 0 | ||
project_id = 0 | ||
issue_id = 0 | ||
|
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,19 @@ | ||
#!/usr/local/bin python3 | ||
from setuptools import setup, find_packages | ||
from ptl.__main__ import __version__ as version | ||
import shutil | ||
import os | ||
setup(name='ptl', | ||
python_requires='>=3.6', | ||
version=version, | ||
description='Automatically log time in GitLab issue tracker for COMP23311 at UoM.', | ||
url='/~https://github.com/JossMoff/ptl', | ||
author='Joss Moffatt', | ||
author_email='joss.moffatt@student.manchester.ac.uk', | ||
license='MIT', | ||
packages=find_packages(), | ||
data_files=[('ptl', ['ptl/config.ini'])], | ||
entry_points={ | ||
'console_scripts': [ | ||
'ptl = ptl.__main__:main']}, | ||
zip_safe=False) |