From 50c877f6903e88c618486228755108f2a8b6e0fd Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Nov 16 2016 15:44:35 +0000 Subject: [PATCH 1/2] support adding custom fields in issues via git --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index a753b25..126e2e2 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -3470,3 +3470,17 @@ def update_log_email_user(session, email, user): {model.PagureLog.user_id: user.id}, synchronize_session=False ) + + +def get_custom_key(session, project, keyname): + ''' Returns custom key object given it's name and the project ''' + + query = session.query( + model.IssueKeys + ).filter( + model.IssueKeys.project_id == project.id + ).filter( + model.IssueKeys.name == keyname + ) + + return query.first() diff --git a/pagure/lib/git.py b/pagure/lib/git.py index 15a028e..bf5c87f 100644 --- a/pagure/lib/git.py +++ b/pagure/lib/git.py @@ -419,6 +419,59 @@ def get_project_from_json( return project +def update_custom_field_from_json(session, repo, issue, json_data): + ''' Update the custom fields according to the custom fields of + the issue. If the custom field is not present for the repo in + it's settings, this will create them. + + :arg session: the session to connect to the database with. + :arg repo: the sqlalchemy object of the project + :arg issue: the sqlalchemy object of the issue + :arg json_data: the json representation of the issue taken from the git + and used to update the data in the database. + ''' + + # Update custom key value, if present + custom_fields = json_data.get('custom_fields') + if not custom_fields: + return + + current_keys = [] + for key in repo.issue_keys: + current_keys.append(key.name) + + for new_key in custom_fields: + if new_key['name'] not in current_keys: + issuekey = model.IssueKeys( + project_id=repo.id, + name=new_key['name'], + key_type=new_key['key_type'], + ) + try: + session.add(issuekey) + session.commit() + except SQLAlchemyError: + session.rollback() + continue + + # The key should be present in the database now + key_obj = pagure.lib.get_custom_key(session, repo, new_key['name']) + + value = new_key.get('value') + if value: + value = value.strip() + pagure.lib.set_custom_key_value( + session, + issue=issue, + key=key_obj, + value=value, + ) + try: + session.commit() + except SQLAlchemyError: + session.rollback() + + def update_ticket_from_git( session, reponame, namespace, username, issue_uid, json_data): """ Update the specified issue (identified by its unique identifier) @@ -478,6 +531,13 @@ def update_ticket_from_git( issue = pagure.lib.get_issue_by_uid(session, issue_uid=issue_uid) + update_custom_field_from_json( + session, + repo=repo, + issue=issue, + json_data=json_data, + ) + # Update milestone milestone = json_data.get('milestone') From 763ae5d16196a065fa645f45c99be06ffe481925 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Nov 16 2016 15:44:38 +0000 Subject: [PATCH 2/2] Add unit test for update_custom_field_from_json --- diff --git a/tests/test_pagure_lib_git.py b/tests/test_pagure_lib_git.py index f963777..af0e5c8 100644 --- a/tests/test_pagure_lib_git.py +++ b/tests/test_pagure_lib_git.py @@ -1588,6 +1588,94 @@ index 0000000..60f7480 self.path, 'repos', 'forks', 'user', '/bar/foo.test.git')) self.assertEqual(repo_name, 'bar') + def test_update_custom_fields_from_json(self): + """ Test the update_custom_fields_from_json method of lib.git """ + + tests.create_projects(self.session) + repo = pagure.lib.get_project(self.session, 'test') + + # Create issues to play with + pagure.lib.new_issue( + session=self.session, + repo=repo, + title='Test issue', + content='We should work on this', + user='pingou', + ticketfolder=None, + issue_uid='someuid' + ) + self.session.commit() + + issue = pagure.lib.get_issue_by_uid(self.session, 'someuid') + + # Fake json data, currently without custom_fields + # This should bring no new custom_fields to the issue + json_data = { + "status": "Open", + "title": "Test issue", + "private": False, + "content": "We should work on this", + "user": { + "fullname": "PY C", + "name": "pingou", + "default_email": "bar@pingou.com", + "emails": ["bar@pingou.com"] + }, + "id": 1, + "blocks": [], + "depends": [], + "date_created": "1234567", + "comments": [], + } + + pagure.lib.git.update_custom_field_from_json( + self.session, repo, issue, json_data) + + updated_issue = pagure.lib.get_issue_by_uid(self.session, 'someuid') + + self.assertEqual(updated_issue.to_json().get('custom_fields'), []) + custom_fields = [ + { + "name": "custom1", + "key_type": "text", + "value": "value1", + }, + { + "name": "custom2", + "key_type": "text", + "value": "value2", + } + ] + + # Again, Fake the json data but, with custom_fields in it + # The updated issue should have the custom_fields as + # was in the json_data + json_data = { + "status": "Open", + "title": "Test issue", + "private": False, + "content": "We should work on this", + "user": { + "fullname": "PY C", + "name": "pingou", + "default_email": "bar@pingou.com", + "emails": ["bar@pingou.com"] + }, + "id": 1, + "blocks": [], + "depends": [], + "date_created": "1234567", + "comments": [], + "custom_fields": custom_fields, + } + + pagure.lib.git.update_custom_field_from_json( + self.session, repo, issue, json_data) + + updated_issue = pagure.lib.get_issue_by_uid(self.session, 'someuid') + + custom_fields_of_issue = updated_issue.to_json().get('custom_fields') + self.assertEqual(custom_fields_of_issue, custom_fields) if __name__ == '__main__':