From fa81ade668424ad53cd6010da91d4737616f9954 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 11:46:08 +0000 Subject: [PATCH 1/6] Modify existing close status to make more flexible Signed-off-by: Clement Verna --- diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py index d4c844c..383eec2 100644 --- a/pagure_importer/utils/importer_trac.py +++ b/pagure_importer/utils/importer_trac.py @@ -11,6 +11,9 @@ from pagure_importer.utils.git import ( clone_repo, get_secure_filename, push_delete_repo, update_git) from pagure_importer.utils.models import User, Issue, IssueComment +CLOSE_STATUS = {'Invalid': ['invalid', 'wontfix', 'worksforme'], + 'Insufficient data': ['insufficient_info'], 'Duplicate': ['duplicate']} + class TracImporter(): ''' Pagure importer for trac instance ''' @@ -226,12 +229,10 @@ class TracImporter(): if trac_ticket['status'] != 'closed': return ('Open', '') - elif trac_ticket['resolution'] in ['invalid', 'wontfix', - 'worksforme', 'duplicate']: - return ('Closed', 'Invalid') - elif trac_ticket['resolution'] == 'insufficient_info': - return ('Closed', 'Insufficient data') else: + for status in CLOSE_STATUS: + if trac_ticket['resolution'] in CLOSE_STATUS[status]: + return ('Closed', status) return ('Closed', 'Fixed') def get_comment_user(self, comment): From e77d70558eba643ce058368890a3a21c40fc6b67 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 11:46:08 +0000 Subject: [PATCH 2/6] Adding Close status to config file Signed-off-by: Clement Verna --- diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py index db77333..c243c3e 100644 --- a/pagure_importer/utils/__init__.py +++ b/pagure_importer/utils/__init__.py @@ -10,6 +10,10 @@ from github.GithubException import TwoFactorException from pagure_importer.utils.exceptions import FileNotFound, EmailNotFound from pagure_importer.app import REPO_PATH +CFG_PATH = os.path.join(os.environ.get('HOME'), '.pgimport') +CONFIG = ConfigParser() +CONFIG.optionxform = str + def create_auth_token(github): ''' Creates github authentication token. If Two Factor Authentication @@ -27,21 +31,36 @@ def create_auth_token(github): return otp_auth +def create_config(): + CONFIG['close_status'] = {'Invalid': ['invalid', 'wontfix', 'worksforme'], + 'Insufficient data': ['insufficient_info'], + 'Duplicate': ['duplicate']} + CONFIG['github'] = {'auth_token': ''} + with click.open_file(CFG_PATH, 'w+') as config_file: + CONFIG.write(config_file) + + +def get_close_status(): + close_status = None + if os.path.exists(CFG_PATH): + CONFIG.read(CFG_PATH) + close_status = CONFIG['close_status'] + return close_status + + def get_auth_token(github): ''' Checks the .pgimport file for github authentication key, if it is not present, creates it. ''' - cfg_path = os.path.join(os.environ.get('HOME'), '.pgimport') - if os.path.exists(cfg_path): - parser = ConfigParser() - parser.read(cfg_path) - otp_auth = parser['github']['auth_token'] - else: - otp_auth = create_auth_token(github) - otp_auth = otp_auth.token - with click.open_file(cfg_path, 'w+') as fp: - fp.write('[github] \nauth_token : %s' % otp_auth) - return otp_auth + if os.path.exists(CFG_PATH): + CONFIG.read(CFG_PATH) + otp_auth = CONFIG['github']['auth_token'] + if not otp_auth: + otp_auth = create_auth_token(github) + CONFIG['github']['auth_token'] = otp_auth.token + with click.open_file(CFG_PATH, 'w+') as config_file: + CONFIG.write(config_file) + return otp_auth def display_repo(): From d8abc9984ec81a7ec61776e5585e83eabd9329f3 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 11:46:08 +0000 Subject: [PATCH 3/6] Add custom close status to readme Signed-off-by: Clement Verna --- diff --git a/README.md b/README.md index dacb9a4..e4a6dd2 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,22 @@ CLI tool for importing issues etc. from different sources like github to pagure 4. Just answer what is asked. Check below instructions for particular source 5. The script will make commits in your cloned repo: push the changes back to pagure. Use : ```pgimport push foobar.git``` +## Custom Close Status +--- +pagure-importer creates a configuration under the home directory of the user $HOME/.pgimport. This configuration file contains the default close status. +If this file is not present run the following command. + ```$ pgimport mkconfig``` +To add some new close status just edit the config file as follow. Where ```Foo``` is the pagure custom status and ```bar``` is the trac resolution status + + [close_status] + Duplicate = ['duplicate'] + Insufficient data = ['insufficient_info'] + Invalid = ['invalid', 'wontfix', 'worksforme'] + Foo = ['bar'] + + [github] + auth_token = + ## Usage --- From 24028f8f527f5d978e740f2ea39d51380e1e22e1 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 11:46:08 +0000 Subject: [PATCH 4/6] Modified get_tickets_status to use custom status Signed-off-by: Clement Verna --- diff --git a/pagure_importer/utils/importer_trac.py b/pagure_importer/utils/importer_trac.py index 383eec2..8302083 100644 --- a/pagure_importer/utils/importer_trac.py +++ b/pagure_importer/utils/importer_trac.py @@ -6,14 +6,11 @@ import click import requests from datetime import datetime -from pagure_importer.utils import get_pagure_namespace +from pagure_importer.utils import get_pagure_namespace, get_close_status from pagure_importer.utils.git import ( clone_repo, get_secure_filename, push_delete_repo, update_git) from pagure_importer.utils.models import User, Issue, IssueComment -CLOSE_STATUS = {'Invalid': ['invalid', 'wontfix', 'worksforme'], - 'Insufficient data': ['insufficient_info'], 'Duplicate': ['duplicate']} - class TracImporter(): ''' Pagure importer for trac instance ''' @@ -226,14 +223,18 @@ class TracImporter(): def get_ticket_status(self, trac_ticket): ''' Returns the corresponding status of ticket on pagure ''' - - if trac_ticket['status'] != 'closed': - return ('Open', '') + close_status = get_close_status() + if close_status is not None: + if trac_ticket['status'] != 'closed': + return ('Open', '') + else: + for status in close_status: + if trac_ticket['resolution'] in close_status[status]: + return ('Closed', status) + return ('Closed', 'Fixed') else: - for status in CLOSE_STATUS: - if trac_ticket['resolution'] in CLOSE_STATUS[status]: - return ('Closed', status) - return ('Closed', 'Fixed') + click.echo('ERROR: Close Status not read from config file') + sys.exit(1) def get_comment_user(self, comment): ''' Returns the user who commented on the ticket ''' From f1633097dfc8b789c8fc678e409b72170ad60ef8 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 11:46:08 +0000 Subject: [PATCH 5/6] Create mkconfig command and managed error due to file missing Signed-off-by: Clement Verna --- diff --git a/README.md b/README.md index e4a6dd2..7c6fcbe 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ To add some new close status just edit the config file as follow. Where ```Foo`` clone fedorahosted github + mkconfig push @@ -99,6 +100,10 @@ To add some new close status just edit the config file as follow. Where ```Foo`` $ pgimport push foobar.git +4) The mkconfig command will create a default config `.pgimport` file under the user $HOME directory. + + $ pgimport mkconfig + ### Migrate github issues to pagure --- diff --git a/pagure_importer/app.py b/pagure_importer/app.py index 054c422..1e110e3 100644 --- a/pagure_importer/app.py +++ b/pagure_importer/app.py @@ -17,6 +17,7 @@ import pagure_importer.commands.fedorahosted import pagure_importer.commands.github import pagure_importer.commands.clone import pagure_importer.commands.push +import pagure_importer.commands.mkconfig if __name__ == '__main__': app() diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py index c243c3e..3baa118 100644 --- a/pagure_importer/utils/__init__.py +++ b/pagure_importer/utils/__init__.py @@ -1,5 +1,6 @@ import csv import os +import sys import json import click import urlparse @@ -36,8 +37,16 @@ def create_config(): 'Insufficient data': ['insufficient_info'], 'Duplicate': ['duplicate']} CONFIG['github'] = {'auth_token': ''} - with click.open_file(CFG_PATH, 'w+') as config_file: - CONFIG.write(config_file) + if os.path.exists(CFG_PATH): + if click.confirm('You already have a config file, if you continue ' + 'your custom settings will be lost'): + with click.open_file(CFG_PATH, 'w+') as config_file: + CONFIG.write(config_file) + else: + sys.exit(1) + else: + with click.open_file(CFG_PATH, 'w+') as config_file: + CONFIG.write(config_file) def get_close_status(): @@ -45,6 +54,10 @@ def get_close_status(): if os.path.exists(CFG_PATH): CONFIG.read(CFG_PATH) close_status = CONFIG['close_status'] + else: + create_config() + CONFIG.read(CFG_PATH) + close_status = CONFIG['close_status'] return close_status From ee64d3aefbcedbeca9995db81a9b5d2c52e2fa75 Mon Sep 17 00:00:00 2001 From: Clement Verna Date: Dec 04 2016 19:57:55 +0000 Subject: [PATCH 6/6] Added docstrings Signed-off-by: Clement Verna --- diff --git a/pagure_importer/utils/__init__.py b/pagure_importer/utils/__init__.py index 3baa118..22042ab 100644 --- a/pagure_importer/utils/__init__.py +++ b/pagure_importer/utils/__init__.py @@ -33,6 +33,9 @@ def create_auth_token(github): def create_config(): + '''Create the config file with default close statuses + and an empty github auth_token''' + CONFIG['close_status'] = {'Invalid': ['invalid', 'wontfix', 'worksforme'], 'Insufficient data': ['insufficient_info'], 'Duplicate': ['duplicate']} @@ -50,6 +53,8 @@ def create_config(): def get_close_status(): + ''' Read the config file and returns the close statuses''' + close_status = None if os.path.exists(CFG_PATH): CONFIG.read(CFG_PATH)