'use strict'

const fs = require('fs')
const path = require('path')
const Config = require('../config.json')
const Fetcher = require('./fetcher')

class GitHub extends Fetcher {
  // https://developer.github.com/v3/activity/notifications/#list-your-notifications
  getNotifications() {
    return this.get('notifications')
  }

  // https://developer.github.com/v3/search/#search-issues
  getTasks(query=Config.searchQuery) {
    const urlPath = `search/issues?q=${query}`
    return this.get(urlPath).then(({ items }) => {
      return items.map(item => {
        return {
          id: item.id,
          title: item.title,
          body: item.body,
          state: item.state,
          createdAt: new Date(item.created_at),
          updatedAt: new Date(item.updated_at),
          isPullRequest: !!item.pull_request,
        }
      })
    })
  }

  static getToken() {
    const tokenPath = path.join(__dirname, '..', '..', '.env')
    return fs.readFileSync(tokenPath).toString().trim()
  }

  get(relativeUrl) {
    const url = `${Config.githubApiUrl}/${relativeUrl}`
    const token = GitHub.getToken()
    const options = {
      headers: {
        Accept: 'application/vnd.github.v3+json',
        Authorization: `token ${token}`,
      },
    }
    return super.get(url, options)
  }
}

module.exports = GitHub

@@ -785,42 +785,219 @@
     
-isPullRequest: !!item.pull_request
+closedAt: item.closed_at ? new Date(item.closed_at) : null,%0A          isPullRequest: !!item.pull_request,%0A          repositoryUrl: item.repository_url,%0A          url: item.html_url,%0A          number: item.number
 ,%0A  

Include more issue properties