<commit_before>Constants   = require './constants'
helpers     = require './utils/helpers'
async       = require 'async'
request     = require 'request'
URL         = require 'url'
_           = require 'lodash'
GitlabAPI   = require 'gitlab'
KodingError = require '../../error'
getPrivateToken = (options, callback) ->

  { urlData: { baseUrl }, token } = options

  params    =
    url     : "#{baseUrl}/api/v3/user"
    headers : { Authorization : "Bearer #{token}" }

  request.get params, (err, res, body) ->
    return callback err  if err

    try
      data = JSON.parse body
    catch e
      return callback new KodingError 'Auth failed'

    callback null, data.private_token



module.exports = GitLabProvider =

  importStackTemplateData: (importParams, user, callback) ->

    { url, privateToken } = importParams
    return  unless urlData = @parseImportUrl url

    if privateToken
      @importStackTemplateWithPrivateToken privateToken, urlData, callback
    else
      @importStackTemplateWithRawUrl urlData, callback

    return yes


  parseImportUrl: (url) ->

    { GITLAB_HOST } = Constants
    { protocol, host, pathname } = URL.parse url

    [ empty, user, repo, tree, branch, rest... ] = pathname.split '/'
    return  if rest.length > 0

    branch ?= 'master'
    baseUrl = "#{protocol}//#{host}"

    return { originalUrl : url, baseUrl, user, repo, branch }


  importStackTemplateWithPrivateToken: (privateToken, urlData, callback) ->

    { baseUrl, user, repo, branch } = urlData
    { TEMPLATE_PATH, README_PATH }  = Constants

    gitlab  = GitlabAPI {
      url   : baseUrl
      token : privateToken
    }

    queue = [
      (next) ->

        gitlab.projects.all (projects) ->
          project = projects.filter((item) -> item.path_with_namespace is "#{user}/#{repo}")[0]
          if project
          then next null, project.id
          else next new KodingError 'No repository found'

      (projectId, next) ->
        params = { id : projectId, ref : branch, file_path : TEMPLATE_PATH }
        gitlab.repositoryFiles.get params, (err, file) ->
          return next err  if err
          rawContent = helpers.decodeContent file
          next null, projectId, rawContent

      (projectId, rawContent, next) ->
        params = { id : projectId, ref : branch, file_path : README_PATH }
        gitlab.repositoryFiles.get params, (err, file) ->
          description = helpers.decodeContent file  if file
          next null, { rawContent, description }<commit_msg>GitLabProvider: if oauth token exists then try to fetch from that one<commit_after>Constants   = require './constants'
helpers     = require './utils/helpers'
async       = require 'async'
request     = require 'request'
URL         = require 'url'
_           = require 'lodash'
GitlabAPI   = require 'gitlab'
KodingError = require '../../error'
KONFIG      = require 'koding-config-manager'


getPrivateToken = (options, callback) ->

  { urlData: { baseUrl }, token } = options

  params    =
    url     : "#{baseUrl}/api/v3/user"
    headers : { Authorization : "Bearer #{token}" }

  request.get params, (err, res, body) ->
    return callback err  if err

    try
      data = JSON.parse body
    catch e
      return callback new KodingError 'Auth failed'

    callback null, data.private_token



module.exports = GitLabProvider =


  importStackTemplateData: (importParams, user, callback) ->

    { url, privateToken } = importParams
    return  unless urlData = @parseImportUrl url

    if privateToken
      @importStackTemplateWithPrivateToken privateToken, urlData, callback
    else if oauth = user.getAt 'foreignAuth.gitlab'
      @importStackTemplateWithOauth oauth, urlData, callback
    else
      @importStackTemplateWithRawUrl urlData, callback

    return yes


  parseImportUrl: (url) ->

    { GITLAB_HOST } = Constants
    { protocol, host, pathname } = URL.parse url

    [ empty, user, repo, tree, branch, rest... ] = pathname.split '/'
    return  if rest.length > 0

    branch ?= 'master'
    baseUrl = "#{protocol}//#{host}"

    return { originalUrl : url, baseUrl, user, repo, branch }


  importStackTemplateWithOauth: (oauth, urlData, callback) ->

    { baseUrl, user, repo, branch } = urlData
    { token } = oauth

    getPrivateToken { urlData, token }, (err, privateToken) =>

      return callback err  if err

      gitlab  = GitlabAPI {
        url   : baseUrl
        token : privateToken
      }

      @importStackTemplateWithPrivateToken privateToken, urlData, callback


  importStackTemplateWithPrivateToken: (privateToken, urlData, callback) ->

    { baseUrl, user, repo, branch } = urlData
    { TEMPLATE_PATH, README_PATH }  = Constants

    gitlab  = GitlabAPI {
      url   : baseUrl
      token : privateToken
    }

    queue = [

      (next) ->

        gitlab.projects.all (projects) ->
          project = projects.filter((item) -> item.path_with_namespace is "#{user}/#{repo}")[0]
          if project
          then next null, project.id
          else next new KodingError 'No repository found'

      (projectId, next) ->
        params = { id : projectId, ref : branch, file_path : TEMPLATE_PATH }
        gitlab.repositoryFiles.get params, (err, file) ->
          return next err  if err
          rawContent = helpers.decodeContent file
          next null, projectId, rawContent

      (projectId, rawContent, next) ->
        params = { id : projectId, ref : branch, file_path : README_PATH }
        gitlab.repositoryFiles.get params, (err, file) ->
          description = helpers.decodeContent file  if file
          next null, { rawContent, description }
{'fraction_non_alphanumeric': 0.10363153232949512, 'fraction_numerical': 0.0010628875110717448, 'mean_word_length': 2.5756808106396454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5396316', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1611, 'n_words': 468}