def test_lineNumberToDiffPositionAndHunkSmallDiff(self): mock = MagicMock(return_value=open('testdata/small-inline.diff.txt').read()) with patch('github.get_file_diff', mock): position, diff_hunk = github_comments.lineNumberToDiffPositionAndHunk( _, _, _, _, _, _, 33, False) self.assertEquals(4, position) self.assertEquals('@@ -30,6 +30,7 @@', diff_hunk.split('\n')[0])
def test_lineNumberToDiffPositionAndHunkCreation(self): mock = MagicMock(return_value=open('testdata/file-creation.diff.txt').read()) with patch('github.get_file_diff', mock): position, diff_hunk = github_comments.lineNumberToDiffPositionAndHunk( _, _, _, _, _, _, 58, False) self.assertEquals(58, position) self.assertEquals('@@ -0,0 +1,59 @@', diff_hunk.split('\n')[0])
def save_draft_comment(): owner = request.form['owner'] repo = request.form['repo'] path = request.form['path'] pull_number = request.form['pull_number'] commit_id = request.form['commit_id'] line_number = int(request.form['line_number']) in_reply_to = request.args.get('in_reply_to') comment = { 'owner': owner, 'repo': repo, 'pull_number': pull_number, 'path': path, 'original_commit_id': commit_id, 'body': request.form['body'] } if in_reply_to: comment['in_reply_to'] = in_reply_to comment_id = request.form.get('id') if comment_id: comment['id'] = comment_id token = session['token'] pr = github.get_pull_request(token, owner, repo, pull_number) base_sha = pr['base']['sha'] position, hunk = github_comments.lineNumberToDiffPositionAndHunk(token, owner, repo, base_sha, path, commit_id, line_number, False) if not position: return "Unable to get diff position for %s:%s @%s" % (path, line_number, commit_id) comment['original_position'] = position comment['diff_hunk'] = hunk result = db.add_draft_comment(session['login'], comment) result = db.githubify_comment(result) # This is a bit roundabout, but more reliable! github_comments.add_line_number_to_comment(token, owner, repo, base_sha, result) return jsonify(result)