def publish_draft_comments(): owner = request.form['owner'] repo = request.form['repo'] pull_number = request.form['pull_number'] token = session['token'] login = session['login'] new_top_level = request.form['top_level_comment'] if not owner: return "Incomplete post_comment request, missing owner" if not repo: return "Incomplete post_comment request, missing repo" if not pull_number: return "Incomplete post_comment request, missing pull_number" draft_comments = db.get_draft_comments(login, owner, repo, pull_number) if not draft_comments and not new_top_level: return "No comments to publish!" # TODO(danvk): publish comments in parallel errors = [] for comment in draft_comments: result = github.post_comment(token, owner, repo, pull_number, comment) if not result: errors.append(comment) sys.stderr.write("Unable to publish comment: %s" % json.dumps(comment)) else: db.delete_draft_comments([comment['id']]) logging.info('Successfully published %d comments', len(draft_comments)) if new_top_level: result = github.post_issue_comment(token, owner, repo, pull_number, new_top_level) if not result: return "Unable to publish comment: %s" % new_top_level github.expire_cache_for_pull_request(owner, repo, pull_number) github.expire_cache_for_pull_request_children(owner, repo, pull_number) if errors: flash("Some comments could not be published. They've been left as drafts.") return redirect(url_for('pull', owner=owner, repo=repo, number=pull_number))
def post_comment(): owner = request.form['owner'] repo = request.form['repo'] pull_number = request.form['pull_number'] path = request.form['path'] commit_id = request.form['commit_id'] line_number = int(request.form['line_number']) body = request.form['body'] if not owner: return "Incomplete post_comment request, missing owner" if not repo: return "Incomplete post_comment request, missing repo" if not pull_number: return "Incomplete post_comment request, missing pull_number" if not path: return "Incomplete post_comment request, missing path" if not commit_id: return "Incomplete post_comment request, missing commit_id" if not line_number: return "Incomplete post_comment request, missing line_number" if not body: return "Incomplete post_comment request, missing body" token = session['token'] if not token: return "You must be oauthed to post a comment." pr = github.get_pull_request(token, owner, repo, pull_number) base_sha = pr['base']['sha'] diff_position = github_comments.lineNumberToDiffPosition(token, owner, repo, base_sha, path, commit_id, line_number, False) # False = on_left (for now!) if not diff_position: return "Unable to get diff position for %s:%s @%s" % (path, line_number, commit_id) sys.stderr.write('diff_position=%s\n' % diff_position) response = github.post_comment(token, owner, repo, pull_number, commit_id, path, diff_position, body) if response: github_comments.add_line_number_to_comment(token, owner, repo, base_sha, response) return jsonify(response)