const _ = require('lodash');

const execution = require('./execution');
const http = require('./http');

const REVIEW_TOOLS = {
  github: {
    getPullRequestUrl (owner, repository) {
      return `https://api.github.com/repos/${owner}/${repository}/pulls`;
    },
    handleResponseError (response) {
      const message = _.get(response.body, 'message');
      const error = _.get(response.body, 'errors[0]') || {};

      if (message && _.isEmpty(error)) {
        execution.exit(1, `GitHub API call failed with message: ${message}.`);
      }

      if (error.field === 'head') {
        execution.exit(1, 'It looks like your branch was not pushed to upstream.');

      } else if (error.message) {
        execution.exit(1, error.message);

      } else {
        execution.exit(1, `GitHub API call failed with status ${response.statusCode}.\nThe full error:\n${JSON.stringify(response.body, null, 2)}`);

      }
    },
    async createPullRequest (options) {
      const callOptions = {
        method: http.HTTP_METHODS.POST,
        headers: {
          Accept: 'application/json',
          Authorization: `Bearer ${options.pullRequestToken}`,
          'Content-Type': 'application/json',
        },
        json: true,
        body: {
          title: options.prTitle,
          head: options.currentBranch,
          base: options.baseBranch,
          body: options.description,
        },
      };

      try {
        const response = await http.send(this.getPullRequestUrl(options.owner, options.repository), callOptions);
        if (response.statusCode < 299) {
          const prUrl = response.body.html_url;
          execution.print(`Your PR is available at ${prUrl}`);
          return response.body.html_url;

        }
        return this.handleResponseError(response);

      } catch (error) {
        return execution.exit(1, `Impossible to create the PR:\n${error.stack}`);
      }
    },
  },
};

module.exports = { REVIEW_TOOLS };

@@ -71,20 +71,38 @@
 ;%0Aconst 
-http
+%7B HTTP_METHODS, send %7D
  = requi
@@ -272,24 +272,810 @@
 ls%60;%0A    %7D,%0A
+    getAssigneesUrl (owner, repository, issueNumber) %7B%0A      return %60https://api.github.com/repos/$%7Bowner%7D/$%7Brepository%7D/issues/$%7BissueNumber%7D%60;%0A    %7D,%0A    async setCreatorAsAssignee (options, pullRequest) %7B%0A      const callOptions = %7B%0A        method: HTTP_METHODS.POST,%0A        headers: %7B%0A          Accept: 'application/json',%0A          Authorization: %60Bearer $%7Boptions.pullRequestToken%7D%60,%0A          'Content-Type': 'application/json',%0A        %7D,%0A        json: true,%0A        body: %7B%0A          assignees: %5B pullRequest.user.login %5D,%0A        %7D,%0A      %7D;%0A      try %7B%0A        send(this.getAssigneesUrl(options.owner, options.repository, pullRequest.number), callOptions);%0A%0A      %7D catch (error) %7B%0A        execution.print(%60Could not set the assignee due to $%7Berror.stack%7D%60);%0A      %7D%0A    %7D,%0A
     handleRe
@@ -1807,21 +1807,16 @@
 method: 
-http.
 HTTP_MET
@@ -2253,13 +2253,8 @@
 ait 
-http.
 send
@@ -2385,20 +2385,26 @@
  const p
-rUrl
+ullRequest
  = respo
@@ -2403,32 +2403,69 @@
  = response.body
+;%0A          const prUrl = pullRequest
 .html_url;%0A     
@@ -2536,28 +2536,85 @@
     
-return response.body
+this.setCreatorAsAssignee(options, pullRequest);%0A          return pullRequest
 .htm

Set PR creator as assignee