function findFirstBuildID()

in buildid_changeset.js [154:231]


function findFirstBuildID(date, year = null, month = null) {
  // Who knows where Firefox is built, so give the build ID some slack.
  let possibleDate = new Date(date.getTime());
  possibleDate.setUTCHours(possibleDate.getUTCHours() - 9);
  let possibleBuildID =
    "" +
    possibleDate.getUTCFullYear() +
    toTwoDigits(possibleDate.getUTCMonth() + 1) +
    toTwoDigits(possibleDate.getUTCDate()) +
    toTwoDigits(possibleDate.getUTCHours()) +
    toTwoDigits(possibleDate.getUTCMinutes()) +
    toTwoDigits(possibleDate.getUTCSeconds());

  if (year == null) {
    year = possibleDate.getUTCFullYear();
    month = possibleDate.getUTCMonth() + 1;
  }

  return fetch(
    "https://ftp.mozilla.org/pub/firefox/nightly/" +
      year +
      "/" +
      toTwoDigits(month) +
      "/"
  )
    .then((response) => response.text())
    .then((data) => {
      let re = />(\d+)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-mozilla-central\/</g;
      let results;

      let buildIDs = [];

      while ((results = re.exec(data)) !== null) {
        let buildID =
          results[1] +
          results[2] +
          results[3] +
          results[4] +
          results[5] +
          results[6];

        // Check if this build ID is 'newer' than date.
        if (compareBuildIDs(possibleBuildID, buildID) != 1) {
          buildIDs.push(buildID);
        }
      }

      return buildIDs;
    })
    .then((buildIDs) => {
      if (buildIDs.length === 0) {
        return null;
      }

      return findFirstBuildIDInSet(buildIDs, date);
    })
    .then((rev) => {
      if (!rev) {
        let nextYear = month + 1 == 13 ? year + 1 : year;
        let nextMonth = month + 1;
        if (nextMonth == 13) {
          nextMonth = 1;
        }

        if (
          nextYear > new Date().getUTCFullYear() ||
          (nextYear == new Date().getUTCFullYear() &&
            nextMonth > new Date().getUTCMonth() + 1)
        ) {
          return null;
        }

        return findFirstBuildID(date, nextYear, nextMonth);
      }

      return rev;
    });
}