'use strict'

const Joi = require('joi')
const { InvalidResponse } = require('..')
const { errorMessagesFor } = require('./github-helpers')

const issueSchema = Joi.object({
  head: Joi.object({
    sha: Joi.string().required(),
  }).required(),
}).required()

async function fetchIssue(serviceInstance, { user, repo, number }) {
  return serviceInstance._requestJson({
    schema: issueSchema,
    url: `/repos/${user}/${repo}/pulls/${number}`,
    errorMessages: errorMessagesFor('pull request or repo not found'),
  })
}

const contentSchema = Joi.object({
  // https://github.com/hapijs/joi/issues/1430
  content: Joi.string().required(),
  encoding: Joi.equal('base64').required(),
}).required()

async function fetchJsonFromRepo(
  serviceInstance,
  { schema, user, repo, branch = 'master', filename }
) {
  let url, options
  if (serviceInstance.staticAuthConfigured) {
    url = `/repos/${user}/${repo}/contents/${filename}`
    options = { qs: { ref: branch } }
  } else {
    url = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`
  }

  const { content } = await serviceInstance._requestJson({
    schema: contentSchema,
    url,
    options,
    errorMessages: errorMessagesFor(
      `repo not found, branch not found, or ${filename} missing`
    ),
  })

  let decoded
  try {
    decoded = Buffer.from(content, 'base64').toString('utf-8')
  } catch (e) {
    throw InvalidResponse({ prettyMessage: 'undecodable content' })
  }
  const json = serviceInstance._parseJson(decoded)
  return serviceInstance.constructor._validate(json, schema)
}

module.exports = {
  fetchIssue,
  fetchJsonFromRepo,
}

@@ -812,24 +812,114 @@
 %7B%0A  
-let url, options
+const errorMessages = errorMessagesFor(%0A    %60repo not found, branch not found, or $%7Bfilename%7D missing%60%0A  )
 %0A  i
@@ -961,24 +961,30 @@
 ured) %7B%0A    
+const 
 url = %60/repo
@@ -1023,24 +1023,30 @@
 ename%7D%60%0A    
+const 
 options = %7B 
@@ -1073,106 +1073,8 @@
 %7D%0A  
-%7D else %7B%0A    url = %60https://raw.githubusercontent.com/$%7Buser%7D/$%7Brepo%7D/$%7Bbranch%7D/$%7Bfilename%7D%60%0A  %7D%0A%0A
   co
@@ -1120,32 +1120,34 @@
 ._requestJson(%7B%0A
+  
     schema: cont
@@ -1165,13 +1165,17 @@
     
+  
 url,%0A
+  
     
@@ -1191,119 +1191,35 @@
     
-errorMessages: errorMessagesFor(%0A      %60repo not found, branch not found, or $%7Bfilename%7D missing%60%0A    ),%0A  %7D)%0A%0A
+  errorMessages,%0A    %7D)%0A%0A  
   le
@@ -1228,16 +1228,18 @@
 decoded%0A
+  
   try %7B%0A
@@ -1242,16 +1242,18 @@
 y %7B%0A    
+ 
+ 
 decoded 
@@ -1303,16 +1303,18 @@
 utf-8')%0A
+  
   %7D catc
@@ -1325,16 +1325,18 @@
 ) %7B%0A    
+ 
+ 
 throw In
@@ -1395,14 +1395,18 @@
  %7D)%0A
+  
   %7D%0A  
+ 
+ 
 cons
@@ -1450,16 +1450,18 @@
 ecoded)%0A
+  
   return
@@ -1513,16 +1513,216 @@
 schema)%0A
+  %7D else %7B%0A    const url = %60https://raw.githubusercontent.com/$%7Buser%7D/$%7Brepo%7D/$%7Bbranch%7D/$%7Bfilename%7D%60%0A    return serviceInstance._requestJson(%7B%0A      schema,%0A      url,%0A      errorMessages,%0A    %7D)%0A  %7D%0A
 %7D%0A%0Amodul

Fix [GithubManifest] and [GithubPackageJson] when static auth is not configured (#3035)