Advertisement
Guest User

huge_waterfall

a guest
Aug 31st, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         function gatherTransitions(issues, cb) {
  2.             //for each issue, it goes through the transitions and sends them to an array.
  3.             //Could probably be coupled with the next function..
  4.             var transitionOptions = [];
  5.  
  6.             async.each(issues, function (issue, callback) {
  7.  
  8.                     jira.listTransitions(issue.key, function (error, transitions) {
  9.                         if (error) {
  10.                             console.log("Error");
  11.                             callback(error);
  12.                             cb(error, null);
  13.                             return;
  14.                         } else {
  15.                             var object = {
  16.                                 issue: issue.key,
  17.                                 summary: issue.fields.summary,
  18.                                 description: issue.fields.description,
  19.                                 transitions: transitions.transitions
  20.                             };
  21.                             transitionOptions.push(object);
  22.                             callback(null);
  23.                         }
  24.                     });
  25.                 },
  26.                 function (err) {
  27.                     if (err) {
  28.                         console.log("An error in gatherTransitions".red);
  29.                     } else {
  30.                         cb(null, transitionOptions)
  31.                     }
  32.                 })
  33.         }
  34.  
  35.         function removeUnnecessaryTransitions(issues, cb) {
  36.             //Removes all transitions, so the release note is clean.
  37.             //Sends the appropriate releases to 'toClose' array
  38.             var toClose = [];
  39.  
  40.             _.each(issues, function (issue, iterator) {
  41.  
  42.                 _.each(issue.transitions, function (transition) {
  43.  
  44.                     var goodIssue = {
  45.                         key: null,
  46.                         summary: null,
  47.                         description: null,
  48.                         transitionDetails: null
  49.                     };
  50.  
  51.                     if (transition.name === req.body.transition) {
  52.                         goodIssue.key = issue.issue;
  53.                         goodIssue.summary = issue.summary;
  54.                         goodIssue.description = issue.description;
  55.                         goodIssue.transitionDetails = transition;
  56.                         toClose.push(goodIssue);
  57.                     }
  58.                 });
  59.             });
  60.             return cb(null, toClose);
  61.         }
  62.  
  63.         function transitionJiras(issues, cb) {
  64.             //Transitions all Jiras in the issues array (toClose from above function).
  65.             //Callback sent with the same issues passed to this function.
  66.             async.each(issues, function (issue, callback) {
  67.  
  68.                 var issueData = {
  69.                     update: {
  70.                         comment: [{add: {body: req.body.note}}]
  71.                     },
  72.                     fields: {
  73.                         resolution: {
  74.                             name: req.body.resolution
  75.                         }
  76.                     },
  77.                     transition: {
  78.                         id: issue.transitionDetails.id
  79.                     }
  80.                 };
  81.  
  82.                 jira.transitionIssue(issue.key, issueData, function (error, result) {
  83.                     if (error) {
  84.                         console.log("Error from transitionIssue".red);
  85.                         console.log(error);
  86.                         callback(error);
  87.                     } else {
  88.                         callback(null);
  89.                     }
  90.                 })
  91.             },
  92.             function (err) {
  93.                 if (err) {
  94.                     console.log(err);
  95.                     cb(err);
  96.                 } else {
  97.                     cb(null, issues);
  98.                 }
  99.             });
  100.         }
  101.  
  102.         function closeVersions(issues, cb) {
  103.             //Closes each version in req.body.versions array.
  104.             //TODO: Add close date.
  105.             //Callback sent with same issues.
  106.             async.each(req.body.versions, function(version, callback) {
  107.                 var versionInfo = {
  108.                     id: version,
  109.                     released: true
  110.                 };
  111.  
  112.                 jira.updateVersion(versionInfo, function(error, done) {
  113.                     if(error) {
  114.                         console.log(error);
  115.                     } else {
  116.                         callback(null);
  117.                     }
  118.                 })
  119.             }, function(err) {
  120.                 if(err) {
  121.                     console.log(err)
  122.                 } else {
  123.                     cb(null, issues);
  124.                 }
  125.             })
  126.         }
  127.  
  128.         function sendReleaseNote(issues, cb) {
  129.             //TODO: Send release note, cb with email note of time and issues.
  130.             //TODO: in res.send, send email stamp AND issues to display on site.\
  131.             mailer.sendRelease(issues, req.body.mail.gmail, req.body.mail.recipients, function(note) {
  132.                 cb(null, issues, note);
  133.             })
  134.         }
  135.  
  136.         function returnNotes(err, issues, notes) {
  137.             if(err) {
  138.                 console.log(err);
  139.             } else {
  140.                 console.log(notes);
  141.                 res.send(issues);
  142.             }
  143.         }
  144.  
  145.         var tasks = [
  146.             searchJiras,
  147.             gatherTransitions,
  148.             removeUnnecessaryTransitions,
  149.             transitionJiras,
  150.             closeVersions,
  151.             sendReleaseNote
  152.         ];
  153.  
  154.         async.waterfall(tasks, returnNotes);
  155.  
  156.     },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement