def test_clean_git(self): """ Test the clean_git method of pagure.lib.git. """ pagure.lib.git.clean_git(None, None, None) self.test_update_git() gitpath = os.path.join(tests.HERE, 'test_ticket_repo.git') gitrepo = pygit2.init_repository(gitpath, bare=True) # Get the uid of the ticket created commit = gitrepo.revparse_single('HEAD') patch = pagure.lib.git.commit_to_patch(gitrepo, commit) hash_file = None for row in patch.split('\n'): if row.startswith('+++ b/'): hash_file = row.split('+++ b/')[-1] break # The only file in git is the one of that ticket files = [entry.name for entry in commit.tree] self.assertEqual(files, [hash_file]) repo = pagure.lib.get_project(self.session, 'test_ticket_repo') issue = pagure.lib.search_issues(self.session, repo, issueid=1) pagure.lib.git.clean_git(issue, repo, tests.HERE) # No more files in the git repo commit = gitrepo.revparse_single('HEAD') files = [entry.name for entry in commit.tree] self.assertEqual(files, [])
def test_dumping_ticket(self, send_email): """ Test dumping a ticket into a JSON blob. """ send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(tests.HERE, 'tickets', 'test.git') repopath = os.path.join(tests.HERE, 'tickets') os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.get_project(self.session, 'test') # Create an issue to play with msg = pagure.lib.new_issue(session=self.session, repo=repo, title='Test issue', content='We should work on this', user='******', ticketfolder=repopath) self.assertEqual(msg.title, 'Test issue') # Need another two issue to test the dependencie chain msg = pagure.lib.new_issue(session=self.session, repo=repo, title='Test issue #2', content='Another bug', user='******', ticketfolder=repopath) self.assertEqual(msg.title, 'Test issue #2') msg = pagure.lib.new_issue(session=self.session, repo=repo, title='Test issue #3', content='That would be nice feature no?', user='******', ticketfolder=repopath) self.assertEqual(msg.title, 'Test issue #3') issue = pagure.lib.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment='Hey look a comment!', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Comment added') msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment='crazy right?', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Comment added') # Assign the ticket to someone msg = pagure.lib.add_issue_assignee( session=self.session, issue=issue, assignee='pingou', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Issue assigned') # Add a couple of tags on the ticket msg = pagure.lib.add_tag_obj( session=self.session, obj=issue, tags=[' feature ', 'future '], user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Tag added: feature, future') # Add dependencies msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Dependency added') msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue3, issue_blocked=issue, user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Dependency added') # Dump the JSON pagure.lib.git.update_git(issue, repo, repopath) repo = pygit2.Repository(self.gitrepo) cnt = len([ commit for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL) ]) self.assertEqual(cnt, 10) last_commit = repo.revparse_single('HEAD') patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split('\n'): if line.startswith('--- a/'): fileid = line.split('--- a/')[1] break newpath = tempfile.mkdtemp(prefix='pagure-dump-load') clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(tests.HERE, 'test_ticket.json') self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) shutil.rmtree(newpath)
def test_update_git_requests(self, email_f): """ Test the update_git of pagure.lib.git for pull-requests. """ email_f.return_value = True # Create project item = pagure.lib.model.Project( user_id=1, # pingou name='test_ticket_repo', description='test project for ticket', hook_token='aaabbbxxx', ) self.session.add(item) self.session.commit() # Create repo self.gitrepo = os.path.join(tests.HERE, 'test_ticket_repo.git') os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.get_project(self.session, 'test_ticket_repo') # Create an issue to play with req = pagure.lib.new_pull_request( session=self.session, repo_from=repo, branch_from='feature', repo_to=repo, branch_to='master', title='test PR', user='******', requestfolder=tests.HERE, requestuid='foobar', requestid=None, status='Open', notify=True ) self.assertEqual(req.id, 1) self.assertEqual(req.title, 'test PR') request = repo.requests[0] self.assertEqual(request.title, 'test PR') pagure.lib.git.update_git(request, request.project, tests.HERE) repo = pygit2.Repository(self.gitrepo) commit = repo.revparse_single('HEAD') # Use patch to validate the repo patch = pagure.lib.git.commit_to_patch(repo, commit) exp = """Mon Sep 17 00:00:00 2001 From: pagure <pagure> Subject: Updated pull-request <hash>: test PR --- diff --git a/123 b/456 new file mode 100644 index 0000000..60f7480 --- /dev/null +++ b/456 @@ -0,0 +1,83 @@ +{ + "assignee": null, + "branch": "master", + "branch_from": "feature", + "closed_at": null, + "closed_by": null, + "comments": [], + "commit_start": null, + "commit_stop": null, + "date_created": null, + "id": 1, + "initial_comment": null, + "project": { + "date_created": null, + "description": "test project for ticket", + "id": 1, + "name": "test_ticket_repo", + "parent": null, + "priorities": {}, + "settings": { + "Enforce_signed-off_commits_in_pull-request": false, + "Minimum_score_to_merge_pull-request": -1, + "Only_assignee_can_merge_pull-request": false, + "Web-hooks": null, + "always_merge": false, + "issue_tracker": true, + "project_documentation": false, + "pull_requests": true + }, + "tags": [], + "user": { + "default_email": "*****@*****.**", + "emails": [ + "*****@*****.**", + "*****@*****.**" + ], + "fullname": "PY C", + "name": "pingou" + } + }, + "remote_git": null, + "repo_from": { + "date_created": null, + "description": "test project for ticket", + "id": 1, + "name": "test_ticket_repo", + "parent": null, + "priorities": {}, + "settings": { + "Enforce_signed-off_commits_in_pull-request": false, + "Minimum_score_to_merge_pull-request": -1, + "Only_assignee_can_merge_pull-request": false, + "Web-hooks": null, + "always_merge": false, + "issue_tracker": true, + "project_documentation": false, + "pull_requests": true + }, + "tags": [], + "user": { + "default_email": "*****@*****.**", + "emails": [ + "*****@*****.**", + "*****@*****.**" + ], + "fullname": "PY C", + "name": "pingou" + } + }, + "status": "Open", + "title": "test PR", + "uid": "foobar", + "updated_on": null, + "user": { + "default_email": "*****@*****.**", + "emails": [ + "*****@*****.**", + "*****@*****.**" + ], + "fullname": "PY C", + "name": "pingou" + } +} \ No newline at end of file """ npatch = [] for row in patch.split('\n'): if row.startswith('Date:'): continue elif row.startswith('From '): row = row.split(' ', 2)[2] elif row.startswith('diff --git '): row = row.split(' ') row[2] = 'a/123' row[3] = 'b/456' row = ' '.join(row) elif 'Updated pull-request' in row: row = row.split() row[3] = '<hash>:' row = ' '.join(row) elif 'date_created' in row: t = row.split(': ')[0] row = '%s: null,' % t elif 'updated_on' in row: t = row.split(': ')[0] row = '%s: null,' % t elif row.startswith('index 00'): row = 'index 0000000..60f7480' elif row.startswith('+++ b/'): row = '+++ b/456' npatch.append(row) patch = '\n'.join(npatch) #print patch self.assertEqual(patch, exp)
def test_commit_to_patch(self): """ Test the commit_to_patch function of pagure.lib.git. """ # Create a git repo to play with self.gitrepo = os.path.join(tests.HERE, 'test_repo.git') os.makedirs(self.gitrepo) repo = pygit2.init_repository(self.gitrepo) # Create a file in that git repo with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream: stream.write('foo\n bar') repo.index.add('sources') repo.index.write() # Commits the files added tree = repo.index.write_tree() author = pygit2.Signature( 'Alice Author', '*****@*****.**') committer = pygit2.Signature( 'Cecil Committer', '*****@*****.**') repo.create_commit( 'refs/heads/master', # the name of the reference to update author, committer, 'Add sources file for testing', # binary string representing the tree object ID tree, # list of binary strings representing parents of the new commit [] ) first_commit = repo.revparse_single('HEAD') # Edit the sources file again with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream: stream.write('foo\n bar\nbaz\n boose') repo.index.add('sources') repo.index.write() # Commits the files added tree = repo.index.write_tree() author = pygit2.Signature( 'Alice Author', '*****@*****.**') committer = pygit2.Signature( 'Cecil Committer', '*****@*****.**') repo.create_commit( 'refs/heads/master', # the name of the reference to update author, committer, 'Add baz and boose to the sources\n\n There are more objects to ' 'consider', # binary string representing the tree object ID tree, # list of binary strings representing parents of the new commit [first_commit.oid.hex] ) second_commit = repo.revparse_single('HEAD') # Generate a patch for 2 commits patch = pagure.lib.git.commit_to_patch( repo, [first_commit, second_commit]) exp = """Mon Sep 17 00:00:00 2001 From: Alice Author <*****@*****.**> Subject: [PATCH 1/2] Add sources file for testing --- diff --git a/sources b/sources new file mode 100644 index 0000000..9f44358 --- /dev/null +++ b/sources @@ -0,0 +1,2 @@ +foo + bar \ No newline at end of file Mon Sep 17 00:00:00 2001 From: Alice Author <*****@*****.**> Subject: [PATCH 2/2] Add baz and boose to the sources There are more objects to consider --- diff --git a/sources b/sources index 9f44358..2a552bb 100644 --- a/sources +++ b/sources @@ -1,2 +1,4 @@ foo - bar \ No newline at end of file + bar +baz + boose \ No newline at end of file """ npatch = [] for row in patch.split('\n'): if row.startswith('Date:'): continue if row.startswith('From '): row = row.split(' ', 2)[2] npatch.append(row) patch = '\n'.join(npatch) self.assertEqual(patch, exp) # Generate a patch for a single commit patch = pagure.lib.git.commit_to_patch(repo, second_commit) exp = """Mon Sep 17 00:00:00 2001 From: Alice Author <*****@*****.**> Subject: Add baz and boose to the sources There are more objects to consider --- diff --git a/sources b/sources index 9f44358..2a552bb 100644 --- a/sources +++ b/sources @@ -1,2 +1,4 @@ foo - bar \ No newline at end of file + bar +baz + boose \ No newline at end of file """ npatch = [] for row in patch.split('\n'): if row.startswith('Date:'): continue if row.startswith('From '): row = row.split(' ', 2)[2] npatch.append(row) patch = '\n'.join(npatch) self.assertEqual(patch, exp)
def test_update_git(self, email_f): """ Test the update_git of pagure.lib.git. """ email_f.return_value = True # Create project item = pagure.lib.model.Project( user_id=1, # pingou name='test_ticket_repo', description='test project for ticket', hook_token='aaabbbwww', ) self.session.add(item) self.session.commit() # Create repo self.gitrepo = os.path.join(tests.HERE, 'test_ticket_repo.git') os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.get_project(self.session, 'test_ticket_repo') # Create an issue to play with msg = pagure.lib.new_issue( session=self.session, repo=repo, title='Test issue', content='We should work on this', user='******', ticketfolder=tests.HERE ) self.assertEqual(msg.title, 'Test issue') issue = pagure.lib.search_issues(self.session, repo, issueid=1) pagure.lib.git.update_git(issue, repo, tests.HERE) repo = pygit2.Repository(self.gitrepo) commit = repo.revparse_single('HEAD') # Use patch to validate the repo patch = pagure.lib.git.commit_to_patch(repo, commit) exp = """Mon Sep 17 00:00:00 2001 From: pagure <pagure> Subject: Updated issue <hash>: Test issue --- diff --git a/123 b/456 new file mode 100644 index 0000000..60f7480 --- /dev/null +++ b/456 @@ -0,0 +1,24 @@ +{ + "assignee": null, + "blocks": [], + "closed_at": null, + "comments": [], + "content": "We should work on this", + "date_created": null, + "depends": [], + "id": 1, + "priority": null, + "private": false, + "status": "Open", + "tags": [], + "title": "Test issue", + "user": { + "default_email": "*****@*****.**", + "emails": [ + "*****@*****.**", + "*****@*****.**" + ], + "fullname": "PY C", + "name": "pingou" + } +} \ No newline at end of file """ npatch = [] for row in patch.split('\n'): if row.startswith('Date:'): continue elif row.startswith('From '): row = row.split(' ', 2)[2] elif row.startswith('diff --git '): row = row.split(' ') row[2] = 'a/123' row[3] = 'b/456' row = ' '.join(row) elif 'Updated issue' in row: row = row.split() row[3] = '<hash>:' row = ' '.join(row) elif 'date_created' in row: t = row.split(': ')[0] row = '%s: null,' % t elif 'closed_at' in row: t = row.split(': ')[0] row = '%s: null,' % t elif row.startswith('index 00'): row = 'index 0000000..60f7480' elif row.startswith('+++ b/'): row = '+++ b/456' npatch.append(row) patch = '\n'.join(npatch) #print patch self.assertEqual(patch, exp) # Test again after adding a comment msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment='Hey look a comment!', user='******', ticketfolder=tests.HERE ) self.session.commit() self.assertEqual(msg, 'Comment added') # Use patch to validate the repo repo = pygit2.Repository(self.gitrepo) commit = repo.revparse_single('HEAD') patch = pagure.lib.git.commit_to_patch(repo, commit) exp = """Mon Sep 17 00:00:00 2001 From: pagure <pagure> Subject: Updated issue <hash>: Test issue --- diff --git a/123 b/456 index 458821a..77674a8 --- a/123 +++ b/456 @@ -2,7 +2,25 @@ "assignee": null, "blocks": [], "closed_at": null, - "comments": [], + "comments": [ + { + "comment": "Hey look a comment!", + "date_created": null, + "edited_on": null, + "editor": null, + "id": 1, + "notification": false, + "parent": null, + "user": { + "default_email": "*****@*****.**", + "emails": [ + "*****@*****.**" + ], + "fullname": "foo bar", + "name": "foo" + } + } + ], "content": "We should work on this", "date_created": null, "depends": [], """ npatch = [] for row in patch.split('\n'): if row.startswith('Date:'): continue elif row.startswith('From '): row = row.split(' ', 2)[2] elif row.startswith('diff --git '): row = row.split(' ') row[2] = 'a/123' row[3] = 'b/456' row = ' '.join(row) elif 'Updated issue' in row: row = row.split() row[3] = '<hash>:' row = ' '.join(row) elif 'date_created' in row: t = row.split(': ')[0] row = '%s: null,' % t elif 'closed_at' in row: t = row.split(': ')[0] row = '%s: null,' % t elif row.startswith('index'): row = 'index 458821a..77674a8' elif row.startswith('--- a/'): row = '--- a/123' elif row.startswith('+++ b/'): row = '+++ b/456' npatch.append(row) patch = '\n'.join(npatch) #print patch self.assertEqual(patch, exp)
def test_dumping_reloading_ticket(self, mw, send_email): """ Test dumping a ticket into a JSON blob. """ mw.side_effect = lambda result: result.get() send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test.git') repopath = os.path.join(self.path, 'repos', 'tickets') os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.query.get_authorized_project(self.session, 'test') # Create an issue to play with msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title='Test issue', content='We should work on this', user='******', ) self.assertEqual(msg.title, 'Test issue') # Need another two issue to test the dependencie chain msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title='Test issue #2', content='Another bug', user='******', ) self.assertEqual(msg.title, 'Test issue #2') msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title='Test issue #3', content='That would be nice feature no?', user='******', ) self.assertEqual(msg.title, 'Test issue #3') issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment='Hey look a comment!', user='******', ) self.session.commit() self.assertEqual(msg, 'Comment added') msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment='crazy right?', user='******', ) self.session.commit() self.assertEqual(msg, 'Comment added') # Assign the ticket to someone msg = pagure.lib.query.add_issue_assignee( session=self.session, issue=issue, assignee='pingou', user='******', ) self.session.commit() self.assertEqual(msg, 'Issue assigned to pingou') # Add a couple of tags on the ticket msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags=[' feature ', 'future '], user='******', ) self.session.commit() self.assertEqual(msg, 'Issue tagged with: feature, future') # Add dependencies msg = pagure.lib.query.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user='******', ) self.session.commit() self.assertEqual(msg, 'Issue marked as depending on: #2') msg = pagure.lib.query.add_issue_dependency( session=self.session, issue=issue3, issue_blocked=issue, user='******', ) self.session.commit() self.assertEqual(msg, 'Issue marked as depending on: #1') # Dump the JSON pagure.lib.git.update_git(issue, repo).wait() repo = pygit2.Repository(self.gitrepo) cnt = len([commit for commit in repo.walk( repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)]) self.assertIn(cnt, (9, 10)) last_commit = repo.revparse_single('HEAD') patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split('\n'): if line.startswith('--- a/'): fileid = line.split('--- a/')[1] break newpath = tempfile.mkdtemp(prefix='pagure-dump-load') clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(self.path, 'test_ticket.json') self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) jsondata = None with open(ticket_json) as stream: jsondata = json.load(stream) self.assertNotEqual(jsondata, None) shutil.rmtree(newpath) # Test reloading the JSON self.tearDown() self.setUp() # Give the worker time to spawn time.sleep(2) tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, 'tickets', 'test.git') repopath = os.path.join(self.path, 'tickets') os.makedirs(self.gitrepo) pygit2.init_repository(self.gitrepo, bare=True) pagure.lib.git.update_ticket_from_git( self.session, reponame='test', namespace=None, username=None, issue_uid='foobar', json_data=jsondata, agent='pingou', ) # Post loading repo = pagure.lib.query.get_authorized_project(self.session, 'test') self.assertEqual(len(repo.issues), 1) issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) # Check after re-loading self.assertEqual(len(issue.comments), 3) self.assertEqual(len(issue.tags), 2) self.assertEqual(sorted(issue.tags_text), sorted(['future', 'feature'])) self.assertEqual(issue.assignee.username, 'pingou') self.assertEqual(issue.children, []) self.assertEqual(issue.parents, []) self.assertEqual(issue.status, 'Open')
def test_dumping_reloading_ticket(self, send_email): """ Test dumping a ticket into a JSON blob. """ send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, 'tickets', 'test.git') repopath = os.path.join(self.path, 'tickets') os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.get_project(self.session, 'test') # Create an issue to play with msg = pagure.lib.new_issue( session=self.session, repo=repo, title='Test issue', content='We should work on this', user='******', ticketfolder=repopath ) self.assertEqual(msg.title, 'Test issue') # Need another two issue to test the dependencie chain msg = pagure.lib.new_issue( session=self.session, repo=repo, title='Test issue #2', content='Another bug', user='******', ticketfolder=repopath ) self.assertEqual(msg.title, 'Test issue #2') msg = pagure.lib.new_issue( session=self.session, repo=repo, title='Test issue #3', content='That would be nice feature no?', user='******', ticketfolder=repopath ) self.assertEqual(msg.title, 'Test issue #3') issue = pagure.lib.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment='Hey look a comment!', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Comment added') msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment='crazy right?', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Comment added') # Assign the ticket to someone msg = pagure.lib.add_issue_assignee( session=self.session, issue=issue, assignee='pingou', user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Issue assigned') # Add a couple of tags on the ticket msg = pagure.lib.add_tag_obj( session=self.session, obj=issue, tags=[' feature ', 'future '], user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Tag added: feature, future') # Add dependencies msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Dependency added') msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue3, issue_blocked=issue, user='******', ticketfolder=repopath, ) self.session.commit() self.assertEqual(msg, 'Dependency added') # Dump the JSON pagure.lib.git.update_git(issue, repo, repopath) repo = pygit2.Repository(self.gitrepo) cnt = len([commit for commit in repo.walk( repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)]) self.assertEqual(cnt, 10) last_commit = repo.revparse_single('HEAD') patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split('\n'): if line.startswith('--- a/'): fileid = line.split('--- a/')[1] break newpath = tempfile.mkdtemp(prefix='pagure-dump-load') clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(self.path, 'test_ticket.json') self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) jsondata = None with open(ticket_json) as stream: jsondata = json.load(stream) self.assertNotEqual(jsondata, None) shutil.rmtree(newpath) # Test reloading the JSON self.tearDown() self.setUp() tests.create_projects(self.session) pagure.lib.git.update_ticket_from_git( self.session, reponame='test', namespace=None, username=None, issue_uid='foobar', json_data=jsondata, ) # Post loading repo = pagure.lib.get_project(self.session, 'test') self.assertEqual(len(repo.issues), 1) issue = pagure.lib.search_issues(self.session, repo, issueid=1) # Check after re-loading self.assertEqual(len(issue.comments), 2) self.assertEqual(len(issue.tags), 2) self.assertEqual(issue.tags_text, ['future', 'feature']) self.assertEqual(issue.assignee.username, 'pingou') self.assertEqual(issue.children, []) self.assertEqual(issue.parents, [])
def test_dumping_ticket(self, send_email): """ Test dumping a ticket into a JSON blob. """ send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(tests.HERE, "tickets", "test.git") repopath = os.path.join(tests.HERE, "tickets") os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.get_project(self.session, "test") # Create an issue to play with msg = pagure.lib.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="******", ticketfolder=repopath, ) self.assertEqual(msg.title, "Test issue") # Need another two issue to test the dependencie chain msg = pagure.lib.new_issue( session=self.session, repo=repo, title="Test issue #2", content="Another bug", user="******", ticketfolder=repopath, ) self.assertEqual(msg.title, "Test issue #2") msg = pagure.lib.new_issue( session=self.session, repo=repo, title="Test issue #3", content="That would be nice feature no?", user="******", ticketfolder=repopath, ) self.assertEqual(msg.title, "Test issue #3") issue = pagure.lib.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment="Hey look a comment!", user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Comment added") msg = pagure.lib.add_issue_comment( session=self.session, issue=issue, comment="crazy right?", user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Comment added") # Assign the ticket to someone msg = pagure.lib.add_issue_assignee( session=self.session, issue=issue, assignee="pingou", user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Issue assigned") # Add a couple of tags on the ticket msg = pagure.lib.add_tag_obj( session=self.session, obj=issue, tags=[" feature ", "future "], user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Tag added: feature, future") # Add dependencies msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Dependency added") msg = pagure.lib.add_issue_dependency( session=self.session, issue=issue3, issue_blocked=issue, user="******", ticketfolder=repopath ) self.session.commit() self.assertEqual(msg, "Dependency added") # Dump the JSON pagure.lib.git.update_git(issue, repo, repopath) repo = pygit2.Repository(self.gitrepo) cnt = len([commit for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)]) self.assertEqual(cnt, 10) last_commit = repo.revparse_single("HEAD") patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split("\n"): if line.startswith("--- a/"): fileid = line.split("--- a/")[1] break newpath = tempfile.mkdtemp(prefix="pagure-dump-load") clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(tests.HERE, "test_ticket.json") self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) shutil.rmtree(newpath)
def test_dumping_reloading_ticket(self, mw, send_email): """ Test dumping a ticket into a JSON blob. """ mw.side_effect = lambda result: result.get() send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git") repopath = os.path.join(self.path, "repos", "tickets") os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.query.get_authorized_project(self.session, "test") # Create an issue to play with msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="******", ) self.assertEqual(msg.title, "Test issue") # Need another two issue to test the dependencie chain msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue #2", content="Another bug", user="******", ) self.assertEqual(msg.title, "Test issue #2") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue #3", content="That would be nice feature no?", user="******", ) self.assertEqual(msg.title, "Test issue #3") issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment="Hey look a comment!", user="******", ) self.session.commit() self.assertEqual(msg, "Comment added") msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment="crazy right?", user="******", ) self.session.commit() self.assertEqual(msg, "Comment added") # Assign the ticket to someone msg = pagure.lib.query.add_issue_assignee(session=self.session, issue=issue, assignee="pingou", user="******") self.session.commit() self.assertEqual(msg, "Issue assigned to pingou") # Add a couple of tags on the ticket msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags=[" feature ", "future "], user="******", ) self.session.commit() self.assertEqual(msg, "Issue tagged with: feature, future") # Add dependencies msg = pagure.lib.query.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user="******", ) self.session.commit() self.assertEqual(msg, "Issue marked as depending on: #2") msg = pagure.lib.query.add_issue_dependency(session=self.session, issue=issue3, issue_blocked=issue, user="******") self.session.commit() self.assertEqual(msg, "Issue marked as depending on: #1") # Dump the JSON pagure.lib.git.update_git(issue, repo).wait() repo = pygit2.Repository(self.gitrepo) cnt = len([ commit for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL) ]) self.assertIn(cnt, (9, 10)) last_commit = repo.revparse_single("HEAD") patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split("\n"): if line.startswith("--- a/"): fileid = line.split("--- a/")[1] break newpath = tempfile.mkdtemp(prefix="pagure-dump-load") clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(self.path, "test_ticket.json") self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) jsondata = None with open(ticket_json) as stream: jsondata = json.load(stream) self.assertNotEqual(jsondata, None) shutil.rmtree(newpath) # Test reloading the JSON # Re-create the DB from scratch self.session.rollback() self._clear_database() self.db_session.close() del self.session del self.db_session os.unlink(os.path.join(self.dbfolder, "db.sqlite")) self.db_session = pagure.lib.model.create_tables( self.dbpath, acls=pagure_config.get("ACLS", {}), ) self._prepare_db() tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, "tickets", "test.git") repopath = os.path.join(self.path, "tickets") os.makedirs(self.gitrepo) pygit2.init_repository(self.gitrepo, bare=True) pagure.lib.git.update_ticket_from_git( self.session, reponame="test", namespace=None, username=None, issue_uid="foobar", json_data=jsondata, agent="pingou", ) # Post loading repo = pagure.lib.query.get_authorized_project(self.session, "test") self.assertEqual(len(repo.issues), 1) issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) # Check after re-loading self.assertEqual(len(issue.comments), 3) self.assertEqual(len(issue.tags), 2) self.assertEqual(sorted(issue.tags_text), sorted(["future", "feature"])) self.assertEqual(issue.assignee.username, "pingou") self.assertEqual(issue.children, []) self.assertEqual(issue.parents, []) self.assertEqual(issue.status, "Open")
def test_dumping_reloading_ticket(self, mw, send_email): """ Test dumping a ticket into a JSON blob. """ mw.side_effect = lambda result: result.get() send_email.return_value = True tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git") repopath = os.path.join(self.path, "repos", "tickets") os.makedirs(self.gitrepo) repo_obj = pygit2.init_repository(self.gitrepo, bare=True) repo = pagure.lib.query.get_authorized_project(self.session, "test") # Create an issue to play with msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue", content="We should work on this", user="******", ) self.assertEqual(msg.title, "Test issue") # Need another two issue to test the dependencie chain msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue #2", content="Another bug", user="******", ) self.assertEqual(msg.title, "Test issue #2") msg = pagure.lib.query.new_issue( session=self.session, repo=repo, title="Test issue #3", content="That would be nice feature no?", user="******", ) self.assertEqual(msg.title, "Test issue #3") issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2) issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3) # Add a couple of comment on the ticket msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment="Hey look a comment!", user="******", ) self.session.commit() self.assertEqual(msg, "Comment added") msg = pagure.lib.query.add_issue_comment( session=self.session, issue=issue, comment="crazy right?", user="******", ) self.session.commit() self.assertEqual(msg, "Comment added") # Assign the ticket to someone msg = pagure.lib.query.add_issue_assignee( session=self.session, issue=issue, assignee="pingou", user="******" ) self.session.commit() self.assertEqual(msg, "Issue assigned to pingou") # Add a couple of tags on the ticket msg = pagure.lib.query.add_tag_obj( session=self.session, obj=issue, tags=[" feature ", "future "], user="******", ) self.session.commit() self.assertEqual(msg, "Issue tagged with: feature, future") # Add dependencies msg = pagure.lib.query.add_issue_dependency( session=self.session, issue=issue, issue_blocked=issue2, user="******", ) self.session.commit() self.assertEqual(msg, "Issue marked as depending on: #2") msg = pagure.lib.query.add_issue_dependency( session=self.session, issue=issue3, issue_blocked=issue, user="******" ) self.session.commit() self.assertEqual(msg, "Issue marked as depending on: #1") # Dump the JSON pagure.lib.git.update_git(issue, repo).wait() repo = pygit2.Repository(self.gitrepo) cnt = len( [ commit for commit in repo.walk( repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL ) ] ) self.assertIn(cnt, (9, 10)) last_commit = repo.revparse_single("HEAD") patch = pagure.lib.git.commit_to_patch(repo, last_commit) for line in patch.split("\n"): if line.startswith("--- a/"): fileid = line.split("--- a/")[1] break newpath = tempfile.mkdtemp(prefix="pagure-dump-load") clone_repo = pygit2.clone_repository(self.gitrepo, newpath) self.assertEqual(len(os.listdir(newpath)), 4) ticket_json = os.path.join(self.path, "test_ticket.json") self.assertFalse(os.path.exists(ticket_json)) shutil.copyfile(os.path.join(newpath, fileid), ticket_json) self.assertTrue(os.path.exists(ticket_json)) jsondata = None with open(ticket_json) as stream: jsondata = json.load(stream) self.assertNotEqual(jsondata, None) shutil.rmtree(newpath) # Test reloading the JSON self.tearDown() self.setUp() tests.create_projects(self.session) # Create repo self.gitrepo = os.path.join(self.path, "tickets", "test.git") repopath = os.path.join(self.path, "tickets") os.makedirs(self.gitrepo) pygit2.init_repository(self.gitrepo, bare=True) pagure.lib.git.update_ticket_from_git( self.session, reponame="test", namespace=None, username=None, issue_uid="foobar", json_data=jsondata, agent="pingou", ) # Post loading repo = pagure.lib.query.get_authorized_project(self.session, "test") self.assertEqual(len(repo.issues), 1) issue = pagure.lib.query.search_issues(self.session, repo, issueid=1) # Check after re-loading self.assertEqual(len(issue.comments), 3) self.assertEqual(len(issue.tags), 2) self.assertEqual( sorted(issue.tags_text), sorted(["future", "feature"]) ) self.assertEqual(issue.assignee.username, "pingou") self.assertEqual(issue.children, []) self.assertEqual(issue.parents, []) self.assertEqual(issue.status, "Open")