'use strict';

var List = require('immutable').List;
var Octokat = require('octokat');

/**
 * @exports lib/transition-checker
 */

var TransitionChecker = {};

/**
 * @returns {Promise.<List.<String>>}
 */

TransitionChecker.check = function (profile, latestVersion, previousVersion, isEditorial, isUpdate) {
  return new Promise(function (resolve, reject) {
    var errors = new List();

    if (profile === 'WD' || profile === 'WG-NOTE' || isUpdate) {
      resolve({ errors: errors, requiresCfE: false });
    }
    else if (profile === 'CR') {
      if (previousVersion.includes('/WD-') || (previousVersion.includes('/CR-') && !isEditorial)) {
        var octo = new Octokat({
          token: global.GH_TOKEN
        });
        var repo = octo.repos('w3c', 'transitions');

        var shortname = latestVersion.match(new RegExp(/.*\/([^/]+)\/$/))[1];
        var directorApproval = 'transition approved.';
        var commApproval = 'draft transition approved.';
        var directorApprovalFound = false;
        var commApprovalFound = false;
        var issueFound = false;

        repo.issues.fetch(
          {
            labels: 'Awaiting Publication',
            state: 'open',
            sort: 'updated',
            per_page: 100 // TODO: get all the issues, not just the first 100
          })
        .then((content) => {
          for (var issue of content.items) {
            if (issue.title.endsWith(' ' + shortname)) {
              issueFound = true;
              repo.issues(issue.number).comments.fetch()
              .then((comments) => {
                for (var comment of comments.items) {
                  if (comment.body.toLowerCase().startsWith(directorApproval)) {
                    // Director's approval
                    octo.teams(global.GH_DIRECTOR_TEAM_ID).members(comment.user.login).fetch((err) => {
                      if (!err) directorApprovalFound = true;
                    });
                  }
                  else if (comment.body.toLowerCase().startsWith(commApproval)) {
                    // Comm's approval
                    octo.teams(global.GH_COMM_TEAM_ID).members(comment.user.login).fetch((err) => {
                      if (!err) commApprovalFound = true;
                    });
                  }
                }
                if (!directorApprovalFound) errors = errors.push('Director\'s approval not found.');
                if (!commApprovalFound) errors = errors.push('Communication team\'s approval not found.');
                if (directorApprovalFound && commApprovalFound) {
                  // Close issue
                  repo.issues(issue.number).update({ state: 'closed' });
                }
                return resolve({ errors: errors, requiresCfE: true });
              });
            }
          }
          if (!issueFound) {
            // Issue not found
            return reject(new Error('Issue not found on the github repository w3c/transitions.'));
          }
        })
        .catch(function (error) {
          return reject(new Error('An error occured while looking for the transition issue: ' + error));
        });
      }
      else if (previousVersion.includes('/CR-') && isEditorial) {
        return resolve({ errors: errors, requiresCfE: false });
      }
    }
    else
      reject(new Error('Only WD, CR and Notes are allowed!'));
  });
};

module.exports = TransitionChecker;

@@ -1077,16 +1077,41 @@
 = false;
+%0A        var issueNumber;
 %0A%0A      
@@ -1490,32 +1490,74 @@
 ueFound = true;%0A
+              issueNumber = issue.number;%0A
               re
@@ -1629,24 +1629,60 @@
 ments) =%3E %7B%0A
+                var promises = %5B%5D;%0A%0A
             
@@ -1858,32 +1858,41 @@
                 
+ var p1 =
  octo.teams(glob
@@ -2037,32 +2037,72 @@
              %7D);
+%0A%0A                    promises.push(p1);
 %0A               
@@ -2246,16 +2246,25 @@
         
+ var p2 =
  octo.te
@@ -2409,32 +2409,72 @@
              %7D);
+%0A%0A                    promises.push(p2);
 %0A               
@@ -2488,32 +2488,91 @@
               %7D%0A
+                Promise.all(promises).then(function () %7B%0A  
                 
@@ -2625,28 +2625,30 @@
 sh('
-Director%5C's approval
+Approval from Director
  not
@@ -2650,32 +2650,34 @@
 r not found.');%0A
+  
                 
@@ -2722,16 +2722,30 @@
 s.push('
+Approval from 
 Communic
@@ -2754,24 +2754,12 @@
 ion 
-t
+T
 eam
-%5C's approval
  not
@@ -2761,32 +2761,34 @@
 m not found.');%0A
+  
                 
@@ -2851,24 +2851,26 @@
             
+  
 // Close iss
@@ -2872,16 +2872,18 @@
 e issue%0A
+  
         
@@ -2905,26 +2905,25 @@
 issues(issue
-.n
+N
 umber).updat
@@ -2954,34 +2954,38 @@
                 
-%7D%0A
+  %7D%0A  
                 
@@ -3035,24 +3035,44 @@
 E: true %7D);%0A
+                %7D);%0A
             

fix async issue