var request = require('request')
var Promise = require('es6-promise').Promise
var parseLinkHeader = require('parse-link-header')

function GithubClient (config) {
  this.owner = config.owner
  this.repo = config.repo
  this.token = config.token
  this.head = config.head || 'master'
  this.base = config.base || 'production'
}

GithubClient.prototype.pullRequestEndpoint = function () {
  return 'https://api.github.com/repos/' + this.owner + '/' + this.repo + '/pulls'
}

GithubClient.prototype.headers = function () {
  return {
    'Authorization': 'token ' + this.token,
    'User-Agent': 'uiureo/github-pr-release'
  }
}

GithubClient.prototype.createReleasePR = function () {
  var self = this

  return new Promise(function (resolve, reject) {
    request.post({
      url: self.pullRequestEndpoint(),
      body: {
        title: 'Preparing release pull request...',
        head: self.head,
        base: self.base
      },
      json: true,
      headers: self.headers()
    }, function (err, res, body) {
      if (err) return reject(err)

      if (res.statusCode === 200) {
        resolve(body)
      } else if (res.statusCode === 422) {
        request.get({
          url: self.pullRequestEndpoint(),
          qs: {
            base: self.base,
            head: self.head,
            state: 'open'
          },
          headers: self.headers()
        }, function (err, res) {
          if (err) return reject(err)
          resolve(JSON.parse(res.body)[0])
        })
      } else {
        reject(res)
      }
    })
  })
}

GithubClient.prototype.getPRCommits = function (pr) {
  var self = this
  var result = []

  function getCommits (page) {
    page = page || 1

    return new Promise(function (resolve, reject) {
      request.get({
        url: self.pullRequestEndpoint() + '/' + pr.number + '/commits',
        qs: {
          per_page: 100,
          page: page
        },
        headers: self.headers()
      }, function (err, res) {
        if (err) return reject(err)
        resolve(res)
      })
    }).then(function (res) {
      var commits = JSON.parse(res.body)
      result = result.concat(commits)

      var link = parseLinkHeader(res.headers.link)

      if (link && link.next) {
        return getCommits(page + 1)
      } else {
        return result
      }
    })
  }

  return getCommits()
}

module.exports = GithubClient

@@ -647,23 +647,11 @@
 ype.
-createReleasePR
+get
  = f
@@ -655,24 +655,34 @@
 = function (
+url, query
 ) %7B%0A  var se
@@ -690,16 +690,38 @@
 f = this
+%0A  query = query %7C%7C %7B%7D
 %0A%0A  retu
@@ -776,19 +776,18 @@
 request.
-pos
+ge
 t(%7B%0A    
@@ -797,280 +797,761 @@
 rl: 
-self.pullRequestEndpoint(),%0A      body: %7B%0A        title: 'Preparing release pull request...',%0A        head: self.head,%0A        base: self.base%0A      %7D,%0A      json: true,%0A      headers: self.headers()%0A    %7D, function (err, res, body) %7B%0A      if (err) return reject(err)%0A%0A  
+url,%0A      qs: query,%0A      headers: self.headers(),%0A      json: true%0A    %7D, function (err, res) %7B%0A      if (err) return reject(err)%0A      resolve(res)%0A    %7D)%0A  %7D)%0A%7D%0A%0AGithubClient.prototype.post = function (url, body) %7B%0A  var self = this%0A  body = body %7C%7C %7B%7D%0A%0A  return new Promise(function (resolve, reject) %7B%0A    request.post(%7B%0A      url: url,%0A      body: body,%0A      json: true,%0A      headers: self.headers()%0A    %7D, function (err, res, body) %7B%0A      if (err) return reject(err)%0A%0A      resolve(res)%0A    %7D)%0A  %7D)%0A%7D%0A%0AGithubClient.prototype.createReleasePR = function () %7B%0A  var self = this%0A%0A  return self.post(self.pullRequestEndpoint(), %7B%0A    title: 'Preparing release pull request...',%0A    head: self.head,%0A    base: self.base%0A  %7D).then(function (res) %7B%0A
     
@@ -1590,26 +1590,24 @@
     
-  resolve(body)%0A  
+return res.body%0A
     
@@ -1653,39 +1653,24 @@
     
-  request.get(%7B%0A          url: 
+return self.get(
 self
@@ -1696,22 +1696,8 @@
 t(),
-%0A          qs:
  %7B%0A 
@@ -1703,20 +1703,16 @@
         
-  
-  
 base: se
@@ -1720,20 +1720,16 @@
 f.base,%0A
-    
         
@@ -1753,20 +1753,16 @@
         
- 
-   
 state: '
@@ -1771,66 +1771,22 @@
 en'%0A
-    
       %7D
-,%0A          headers: self.headers()%0A        %7D, 
+).then(
 func
@@ -1783,37 +1783,32 @@
 .then(function (
-err, 
 res) %7B%0A         
@@ -1810,75 +1810,23 @@
     
-  if (err) return reject(err)%0A          resolve(JSON.parse(
+return 
 res.body
 )%5B0%5D
@@ -1825,16 +1825,12 @@
 body
-)
 %5B0%5D
-)%0A  
+%0A
     
@@ -1842,53 +1842,9 @@
     
-  %7D else %7B%0A        reject(res)%0A      %7D%0A    %7D)
+%7D
 %0A  %7D

Add client.post(), .get()