var semver = require('semver')
var async = require("async");
var request = require("request");

function UpgradeChecker (config) {
  var checker = {
    boshRunner: config.boshRunner,
    boshioClient: config.boshioClient,
    stemcells: config.stemcells || [],
    releases: config.releases || []
  }

  checker.upgradeableStemcells = function (cb) {
    if (checker.stemcells.length === 0) {
      cb(null, [])
      return
    }

    var stemcellIDs = checker.stemcells.map(function (stemcell) {
      return stemcell.boshio_id
    })

    checker.boshioClient.getLatestStemcellVersions(stemcellIDs, function (err, boshioVersions) {
      if (err != null) {
        cb(err, [])
        return
      }

      checker.boshRunner.getLatestStemcellVersions(function (err, directorVersions) {
        if (err) {
          cb(err, [])
          return
        }

        var stemcellsToUpload = filterNewerVersions(checker.stemcells, directorVersions, boshioVersions)

        stemcellsToUpload.forEach(function(stemcell) {
          stemcell.displayName = `${stemcell.name} ${stemcell.version}`
        })

        cb(null, stemcellsToUpload)
      })
    })
  }

  checker.upgradeableReleases = function (cb) {
    if (checker.releases.length === 0) {
      cb(null, [])
      return
    }

    var releaseIDs = checker.releases.map(function (release) {
      return release.boshio_id
    })

    checker.boshioClient.getLatestReleaseVersions(releaseIDs, function (err, boshioVersions) {
      if (err != null) {
        cb(err, [])
        return
      }

      checker.boshRunner.getLatestReleaseVersions(function (err, directorVersions) {
        if (err != null) {
          cb(err, [])
          return
        }

        var releasesToUpload = filterNewerVersions(checker.releases, directorVersions, boshioVersions)

        async.map(releasesToUpload, function(release, callback) {
          if (release.boshioID.includes('github.com') === false) {
            callback(null, null)
            return
          }

          var releaseNotesURL = `https://${release.boshioID}/releases/tag/v${release.version}`
          request(releaseNotesURL, function (error, response, _) {
            if (!error && response.statusCode == 200) {
              callback(null, releaseNotesURL)
            } else {
              callback(null, null)
            }
          });
        }, function(_, results) {
          for (var i = 0; i < results.length; i++) {
            releasesToUpload[i].displayName = `${releasesToUpload[i].name} ${releasesToUpload[i].version}`

            var releaseNotesURL = results[i]
            if (releaseNotesURL) {
              releasesToUpload[i].displayName = `<${releaseNotesURL}|${releasesToUpload[i].displayName}>`
            }
          }

          cb(null, releasesToUpload)
        });
      })
    })
  }

  function filterNewerVersions (itemsToCheck, directorVersions, boshioVersions) {
    return itemsToCheck.map(function (item) {
      var boshioID = item.boshio_id
      var directorID = item.name || item.boshio_id
      var boshioResult = boshioVersions[boshioID]
      var directorResult = directorVersions[directorID] || { version: '0.0.0' }

      var boshioVersion = boshioResult.version
      while (boshioVersion.match(/\./g).length < 2) {
        boshioVersion += '.0'
      }
      var directorVersion = directorResult.version
      while (directorVersion.match(/\./g).length < 2) {
        directorVersion += '.0'
      }
      if (semver.gt(boshioVersion, directorVersion)) {
        return {
          name: directorID,
          boshioID: boshioID,
          url: boshioResult.url,
          version: boshioResult.version
        }
      }
      return null
    }).filter(function (element) { return element != null })
  }

  return checker
};

module.exports = UpgradeChecker

@@ -48,17 +48,16 @@
 ire(
-%22
+'
 async
-%22);
+')
 %0Avar
@@ -79,19 +79,18 @@
 ire(
-%22
+'
 request
-%22);
+')
 %0A%0Afu
@@ -999,16 +999,17 @@
 function
+ 
 (stemcel
@@ -1858,16 +1858,17 @@
 function
+ 
 (release
@@ -2223,16 +2223,17 @@
 sCode ==
+=
  200) %7B%0A
@@ -2356,25 +2356,24 @@
           %7D)
-;
 %0A        %7D, 
@@ -2380,16 +2380,17 @@
 function
+ 
 (_, resu
@@ -2803,24 +2803,24 @@
 esToUpload)%0A
+
         %7D);%0A
@@ -2817,17 +2817,16 @@
       %7D)
-;
 %0A      %7D

Fix formatting errors