summaryrefslogtreecommitdiffstats
path: root/scripts/gerrit/cherry-pick_automation/requestProcessor.js
blob: e827d505e84886537569edabf4a3ed576dd834c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
/* eslint-disable no-unused-vars */
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

const EventEmitter = require("events");
const safeJsonStringify = require("safe-json-stringify");

const toolbox = require("./toolbox");
const gerritTools = require("./gerritRESTTools");
const emailClient = require("./emailClient");
const config = require("./config");

function envOrConfig(ID) {
  return process.env[ID] || config[ID];
}
class requestProcessor extends EventEmitter {
  constructor(logger, retryProcessor) {
    super();
    this.logger = logger;
    this.retryProcessor = retryProcessor;
    this.toolbox = toolbox;
    // Set default values with the config file, but prefer environment variable.

    this.adminEmail = envOrConfig("ADMIN_EMAIL");
    this.gerritURL = envOrConfig("GERRIT_URL");
    this.gerritPort = envOrConfig("GERRIT_PORT");
    this.logger.log(
      `Initialized RequestProcessor with gerritURL=${
        this.gerritURL}, gerritPort=${this.gerritPort}`,
      "debug"
    );
    this.eventCache = {};
  }

  // Cache an event with an expiry duration in ms
  // This allows listeners to check for missed events
  cacheEvent(event, ttl) {
    let _this = this;
    // Try to clear any existing timeout to prevent it from firing.
    // This does not delete the event from the cache, so it can still be
    // read while we set up the new timeout below.
    clearTimeout(_this.eventCache[event]);
    // NOTE: There is an extremely small race condition here where the
    // event may be deleted from the cache and then requested by a listener
    // before it can be re-added.
    _this.eventCache[event] = setTimeout(function() { delete _this.eventCache[event]; }, ttl);
  }

  // Pull the request from the database and start processing it.
  processMerge(uuid) {
    let _this = this;

    let incoming = {};
    toolbox.retrieveRequestJSONFromDB(uuid, function (success, data) {
      if (!success) {
        _this.logger.log(
          `ERROR: Database access error on uuid key: ${uuid}. Data: ${data}`,
          "error", uuid
        );
        return;
      }

      incoming = data;

      // Set the state to processing.
      toolbox.setDBState(incoming.uuid, "processing", function (success, data) {
        if (!success)
          _this.logger.log(safeJsonStringify(data), "error", uuid);
      });

      // Parse the commit message and look for branches to pick to
      let allBranches = toolbox.findPickToBranches(incoming.uuid, incoming.change.commitMessage);
      toolbox.findMissingTargets(incoming.uuid, incoming.fullChangeID, incoming.project.name || incoming.project,
        allBranches, (error, _change, missing) => {
          if (missing.length > 0) {
            missing.forEach(allBranches.add, allBranches);
          }
          const suggestedPicks = Array.from(allBranches);
          const morePicks = suggestedPicks.length > 0;
          if (error) {
            allBranches.delete(_change.branch);
            const message = ` ${_change.branch} was identified as a missing target based on this change's commit message.\n`
              + `WARN: Cherry-pick bot cannot pick this change to ${_change.branch} because`
              + ` a change already exists on ${_change.branch} which is in status: ${_change.status}.\n`
              + `Cherry-pick bot will only proceed automatically for any release branch targets of this branch. (${incoming.change.branch})\n`
              + (morePicks ? `It is recommended to update the existing change with further pick-to targets.\n\n` : "\n\n")
              + `    Change ID: ${_change.change_id}\n`
              + `    Subject: ${_change.subject}\n`
              + (morePicks
                ? `    Suggested Pick-to: ${suggestedPicks.join(" ")}\n\n`
                : "\n\n")
              + `Link: ${gerritTools.gerritResolvedURL}/c/${_change.project}/+/${_change._number}`;
            gerritTools.locateDefaultAttentionUser(incoming.uuid, incoming,
              incoming.change.owner.email, (user) => {
                gerritTools.addToAttentionSet(incoming.uuid, incoming, user, undefined, undefined, () => {
                  const notifyScope = "ALL";
                  _this.gerritCommentHandler(
                    incoming.uuid, incoming.fullChangeID, undefined,
                    message, notifyScope
                  );
                });
              }
            );
            _this.logger.log(`Aborting non-release cherry picking due to unpickable primary target`
              + ` on ${_change.branch}`, "error", uuid);
            let thisStableBranch = incoming.change.branch.split(".")
            if (thisStableBranch.length >= 2) {
              thisStableBranch.pop();
              thisStableBranch = thisStableBranch.join("\\.");
              const restring = new RegExp(`^${thisStableBranch}\\.\\d+$`);
              // Filter out all branches except releases of the current branch.
              //Does not apply to dev.
              allBranches = new Set(Array.from(allBranches).filter(
                (branch) => branch.match(restring)));
            } else {
              // Non numeric branches cannot have release branches. Delete all targets.
              allBranches.clear();
            }
          }
          const picks = toolbox.waterfallCherryPicks(incoming.uuid, allBranches);
          const pickCount = Object.keys(picks).length;
          if (pickCount == 0) {
            _this.logger.log(`Nothing to cherry-pick. Discarding`, "verbose", incoming.uuid);
            // The change did not have a "Pick To: " keyword or "Pick To:"
            // did not include any branches.
            toolbox.setDBState(incoming.uuid, "discarded");
          } else {
            _this.logger.log(
              `Found ${pickCount} branches to pick to for ${incoming.uuid}`,
              "info", uuid
            );
            toolbox.setPickCountRemaining(incoming.uuid, pickCount, function (success, data) {
              // The change has a Pick-to label with at least one branch.
              // Next, determine if it's part of a relation chain and handle
              // it as a member of that chain.
              _this.emit("determineProcessingPath", incoming, picks);
            });
          }
      });
    });
  }

  // Determine if the change is part of a relation chain.
  // Hand it off to the relationChainManager if it is.
  // Otherwise, pass it off to singleRequestManager
  determineProcessingPath(incoming, branches) {
    let _this = this;
    _this.logger.log(`Determining processing path...`, "debug", incoming.uuid);
    gerritTools.queryRelated(
      incoming.uuid, incoming.fullChangeID, incoming.patchSet.number, incoming.customGerritAuth,
      function (success, data) {
        if (success && data.length > 0) {
        // Update the request in the database with the relation chain.
        // Pass it to the relation chain manager once the database finishes.
          incoming["relatedChanges"] = data;
          _this.logger.log(`Found related changes`, "debug", incoming.uuid);
          toolbox.updateBaseChangeJSON(incoming.uuid, incoming, function () {
            _this.emit("processAsRelatedChange", incoming, branches);
          });
        } else if (success) {
        // Pass this down the normal pipeline and just pick the branches
          _this.logger.log(`This is a standalone change`, "debug", incoming.uuid);
          _this.emit("processAsSingleChange", incoming, branches);
        } else if (data == "retry") {
        // Failed to query for related changes, schedule a retry
          _this.retryProcessor.addRetryJob(
            incoming.uuid, "determineProcessingPath",
            [incoming, branches]
          );
        } else {
        // A non-timeout failure occurred when querying gerrit. This should not happen.
          _this.logger.log(
            `Permanently failed to query the relation chain for ${incoming.fullChangeID}.`,
            "error", incoming.uuid
          );
          const message = `An unknown error occurred processing cherry picks for this change. Please create cherry picks manually.`;
          const notifyScope = "OWNER";
          _this.gerritCommentHandler(
            incoming.uuid, incoming.fullChangeID, undefined,
            message, notifyScope
          );
          emailClient.genericSendEmail(
            _this.adminEmail,
            `Cherry-pick bot: Error in querying for related changes [${incoming.fullChangeID}]`,
            undefined, safeJsonStringify(data, undefined, 4)
          );
        }
      }
    );
  }

  // Verify the target branch exists, and target private LTS branches if necessary,
  // then call the response.
  validateBranch(incoming, branch, responseSignal) {
    let _this = this;

    let gerrit_user = envOrConfig("GERRIT_USER")
    _this.logger.log(`Validating branch ${branch}`, "debug", incoming.uuid);
    toolbox.addToCherryPickStateUpdateQueue(
      incoming.uuid, { branch: branch, args: [incoming, branch, responseSignal], statusDetail: "" },
      "validateBranch"
    );

    function done(responseSignal, incoming, branch, success, data, message) {
      if (success) {
        // Check to see if a change already exists on the target branch.
        // If it does, abort the cherry-pick and notify the owner.
        gerritTools.queryChange(incoming.uuid,
          encodeURIComponent(`${incoming.change.project}~${branch}~${incoming.change.id}`),
          undefined, undefined, function (exists, changeData) {
          if (exists && changeData.status != "MERGED") {
            _this.logger.log(
              `A change already exists on ${branch} for ${incoming.change.id}`
              + ` and is ${changeData.status}`, "verbose", incoming.uuid);
            let targets = toolbox.findPickToBranches(incoming.uuid, incoming.change.commitMessage);
            targets.delete(branch);
            const suggestedPicks = Array.from(targets);
            const morePicks = suggestedPicks.length > 0;
            let message = `WARN: Cherry-pick bot cannot pick this change to ${branch} because`
              + ` a change already exists on ${branch} which is in status: ${changeData.status}.\n`
              + `Cherry-pick bot will not proceed automatically.\n`
              + (morePicks ? `It is recommended to update the existing change with further pick-to targets.\n\n` : "\n\n")
              + `    Change ID: ${incoming.change.id}\n`
              + `    Subject: ${incoming.change.subject}\n`
              + (morePicks
                  ? `    Suggested Pick-to: ${suggestedPicks.join(" ")}\n\n`
                  : "\n\n")
              + `Link: ${gerritTools.gerritResolvedURL}/c/${changeData.project}/+/${changeData._number}`;
            gerritTools.locateDefaultAttentionUser(incoming.uuid, incoming,
              incoming.change.owner.email, (user) => {
                gerritTools.addToAttentionSet(incoming.uuid, incoming, user, undefined, undefined, () => {
                  _this.gerritCommentHandler(
                    incoming.uuid, incoming.fullChangeID, undefined,
                    message, "OWNER"
                  );
                });
              });
            toolbox.addToCherryPickStateUpdateQueue(
              incoming.uuid,
              { branch: branch, statusDetail: `OpenOrAbandonedExistsOnTarget`, args: [] },
              "done_targetExistsIsOpen",
              function () {
                toolbox.decrementPickCountRemaining(incoming.uuid);
              }
            );
          } else if (data == "retry") {
            _this.retryProcessor.addRetryJob(
              incoming.uuid, "validateBranch",
              [incoming, branch, responseSignal]
            );
          } else {
            // Success (Change on target branch may not exist or is already merged)
            _this.emit(responseSignal, incoming, branch, data);
          }
        });
        // _this.emit(responseSignal, incoming, branch, data);
      } else if (data == "retry") {
        _this.retryProcessor.addRetryJob(
          incoming.uuid, "validateBranch",
          [incoming, branch, responseSignal]
        );
      } else {
        // While the sanity bot should be warning about non-existent branches,
        // it may occur that Pick-to: footers specify a closed branch by the time
        // the change is merged. In this case, or if an lts branch fails to be created
        // for an intended target branch, this error will occur and the appropriate
        // error message will be posted to the original change, notifying the Owner.
        _this.logger.log(
          `Branch validation failed for ${branch}. Reason: ${safeJsonStringify(data)}`,
          "error", incoming.uuid
        );
        if (message) {
          const notifyScope = "OWNER";
          _this.gerritCommentHandler(
            incoming.uuid, incoming.fullChangeID, undefined,
            message, notifyScope
          );
        }
        toolbox.addToCherryPickStateUpdateQueue(
          incoming.uuid, { branch: branch, args: [], statusDetail: "" },
          "done_invalidBranch",
          function () {
            toolbox.decrementPickCountRemaining(incoming.uuid);
          }
        );
      }
    }

    function tryLTS() {
      // The target branch existed in the project, but was closed for new changes.
      // So if either the branch or the project is not currently tqtc private, search for
      // a public lts branch first in the passed repo, then search the possible
      // tqtc- repo for a matching tqtc/ branch.
      _this.logger.log("Checking if a public LTS branch exists", "info", incoming.uuid);
      let publicLtsBranch = `lts-${branch}`;
      gerritTools.checkBranchAndAccess(incoming.uuid, incoming.change.project,
        publicLtsBranch, gerrit_user, "push", incoming.customGerritAuth,
        function(validPublicLts, canPushPublicLts, data) {
          if (validPublicLts && canPushPublicLts) {
            // A non-tqtc marked lts- branch was found.
            _this.logger.log(`Using public branch ${publicLtsBranch}.`, "verbose",
                              incoming.uuid);
            done(responseSignal, incoming, publicLtsBranch, true, data);
          } else {
            // It doesn't matter if the bot user is blocked on the branch
            // or it simply doesn't exist, fall back to searching for a
            // tqtc repo and search for the LTS branch there.
            let privateProject = incoming.change.project;
            let privateBranch = branch;
            if (!privateProject.includes("tqtc-")) {
              let projectSplit = privateProject.split('/');
              privateProject = projectSplit.slice(0, -1);
              privateProject.push(`tqtc-${projectSplit.slice(-1)}`);
              privateProject = privateProject.join('/');
            }
            if (!branch.includes("tqtc/lts-"))
              privateBranch = `tqtc/${publicLtsBranch}`;
            gerritTools.checkBranchAndAccess(incoming.uuid, privateProject, privateBranch,
              gerrit_user, "push", incoming.customGerritAuth,
              function(validPrivateLTS, canPushPrivateLTS, data) {
                let message;
                if (validPrivateLTS && canPushPrivateLTS) {
                  // Modify the original object so the bot treats it like it's always been
                  // on tqtc/* with an LTS branch target.
                  incoming.change.project = privateProject;
                  incoming.change.branch = privateBranch;
                } else if (validPrivateLTS) {
                  // Valid private branch, but closed for changes.
                  let errMsg = `Unable to cherry-pick this change to ${branch} or`
                  + ` ${privateBranch} because the branch is closed for new changes.`
                  _this.logger.log(errMsg, "error", incoming.uuid);
                  message =  errMsg + "\n" + closedBranchMsg;
                } else {
                    // No private lts branch exists.
                    let errMsg = `Unable to cherry-pick this change to ${branch}`
                    + ` because the branch is closed for new changes and`
                    + validPublicLts ? ` ${publicLtsBranch} is also closed for new changes.`
                                      : " no private LTS branch exists."
                  _this.logger.log(errMsg, "error", incoming.uuid);
                  message = errMsg + "\n" + closedBranchMsg;
                }
                done(responseSignal, incoming, incoming.change.branch,
                      validPrivateLTS && canPushPrivateLTS, data, message);
              }
            )
          }
        }
      )
    }

    incoming.originalProject = incoming.change.project;
    incoming.originalBranch = incoming.change.branch;
    let closedBranchMsg = "If you need this change in a closed branch, please contact the"
      + " Releasing group to argue for inclusion: releasing@qt-project.org";

    gerritTools.checkBranchAndAccess(
      incoming.uuid, incoming.change.project, branch, gerrit_user,
      "push", incoming.customGerritAuth,
      function(validBranch, canPush, data) {
        let tqtcBranch = /tqtc\//.test(branch);
        if (!validBranch && !tqtcBranch) {
          if (incoming.change.project.includes("/tqtc-")) {
            tryLTS();
          } else {
            // Invalid branch specified in Pick-to: footer or some other critical failure
            let message = `Failed to cherry pick to ${incoming.change.project}:${branch} due`
            + ` to an unknown problem with the target branch.`
            + `\nPlease contact the gerrit admins.`;
            done(responseSignal, incoming, branch, false, data, message);
            return;
          }
        }
        if (canPush) {
          // The incoming branch and project targets are available for pushing new changes.
          _this.logger.log(`Using ${tqtcBranch ? "private" : "public"} branch ${branch}.`,
                           "verbose", incoming.uuid);
          done(responseSignal, incoming, branch, true, data);
        } else if (tqtcBranch && /tqtc-/.test(incoming.change.project)) {
          // The target tqtc branch in the tqtc repo was valid, but required push
          // permissions are denied. Cannot fall back any further.
          _this.logger.log(
            `Unable to push to branch ${branch} in ${incoming.change.project} because either the`
            + " branch is closed or the bot user account does not have permissions"
            + " to create changes there.",
            "error", incoming.uuid
          );
          let message = `Unable to cherry-pick this change to ${branch}`
            + ` because the branch is closed for new changes.\n${closedBranchMsg}`;
          done(responseSignal, incoming, branch, false, data, message);
        } else {
          tryLTS();
        }
      }
    );
  }

  // Check the prospective cherry-pick branch for LTS. If it is to be picked to
  // an lts branch, check to make sure the original change exists in the shadow repo.
  // If it doesn't, set up an action to wait for replication.
  checkLtsTarget(currentJSON, branch, branchHeadSha, responseSignal) {
    let _this = this;
    toolbox.addToCherryPickStateUpdateQueue(
      currentJSON.uuid,
      {
        branch: branch,
        args: [currentJSON, branch, branchHeadSha, responseSignal], statusDetail: ""
      }, "checkLtsTarget"
    );
    if (! /^tqtc(?:%2F|\/)lts-/.test(currentJSON.change.branch)) {
      _this.emit(responseSignal, currentJSON, branch, branchHeadSha);
    } else {
      _this.logger.log(
        `Checking to see if ${currentJSON.patchSet.revision} has been replicated to ${
          currentJSON.change.project} shadow repo yet`,
        "info", currentJSON.uuid
      );
      gerritTools.queryProjectCommit(
        currentJSON.uuid, currentJSON.change.project, currentJSON.patchSet.revision, currentJSON.customGerritAuth,
        function (success, data) {
          if (success) {
            _this.logger.log(
              `${currentJSON.patchSet.revision} is a valid ${currentJSON.change.project} target. Continuing.`,
              "info", currentJSON.uuid
            );
            _this.emit(responseSignal, currentJSON, branch, branchHeadSha);
          } else {
            _this.logger.log(
              `${currentJSON.patchSet.revision} hasn't been replicated yet. Waiting for it for 15 minutes.`,
              "info", currentJSON.uuid
            );
            // Set a 15 minute timeout to check again. This process will be iterated so long as
            // the target has not been replicated.
            setTimeout(function () {
              _this.emit("checkLtsTarget", currentJSON, branch, branchHeadSha, responseSignal)
            }, 15 * 60 * 1000);
          }
        }
      )
    }
  }

  // From the current change, determine if the direct parent has a cherry-pick
  // on the target branch. If it does, call the response signal with its revision
  verifyParentPickExists(currentJSON, branch, responseSignal, errorSignal, isRetry) {
    let _this = this;
    _this.logger.log(`Verifying parent pick exists on ${branch}...`, "debug", currentJSON.uuid);
    toolbox.addToCherryPickStateUpdateQueue(
      currentJSON.uuid,
      {
        branch: branch, args: [currentJSON, branch, responseSignal, errorSignal, isRetry],
        statusDetail: ""
      },
      "verifyParentPickExists"
    );

    function fatalError(data) {
      _this.logger.log(`Failed to locate a parent pick on ${branch}`, "error", currentJSON.uuid);
      toolbox.addToCherryPickStateUpdateQueue(
        currentJSON.uuid,
        { branch: branch, statusCode: data.statusCode, statusDetail: data.statusDetail, args: [] },
        "done_parentValidationFailed",
        function () {
          toolbox.decrementPickCountRemaining(currentJSON.uuid);
        }
      );
      _this.gerritCommentHandler(
        currentJSON.uuid, currentJSON.fullChangeID, undefined,
        `Failed to find this change's parent revision for cherry-picking!\nPlease verify that this change's parent is a valid commit in gerrit and process required cherry-picks manually.`
      );
    }

    function retryThis() {
      _this.retryProcessor.addRetryJob(
        currentJSON.uuid, "verifyParentPickExists",
        [currentJSON, branch, responseSignal, errorSignal, isRetry]
      );
    }

    function queryParent(immediateParent) {
      // Query for the immediate parent to get its change ID.
      gerritTools.queryChange(
        currentJSON.uuid, immediateParent, undefined, currentJSON.customGerritAuth,
        function (exists, data) {
          if (exists) {
            let targetPickParent = `${encodeURIComponent(currentJSON.change.project)}~${
              encodeURIComponent(branch)}~${data.change_id}`;
            _this.logger.log(
              `Set target pick parent for ${branch} to ${targetPickParent}`,
              "debug", currentJSON.uuid
            );
            // Success - Found the parent (change ID) of the current change.
            if (data.status == "ABANDONED") {
              // The parent is an abandoned state. Send the error signal.
              _this.logger.log(
                `Immediate parent (${immediateParent}) for ${
                  currentJSON.fullChangeID} is in state: ${data.status}`,
                "warn", currentJSON.uuid
              );
              _this.emit(
                errorSignal, currentJSON, branch,
                { error: data.status, parentJSON: data, isRetry: isRetry }
              );
            } else if (["NEW", "STAGED", "INTEGRATING"].some((element) => data.status == element)) {
              // The parent has not yet been merged.
              // Fire the error signal with the parent's state.
              _this.logger.log(
                `Immediate parent (${immediateParent}) for ${
                  currentJSON.fullChangeID} is in state: ${data.status}`,
                "verbose", currentJSON.uuid
              );
              _this.emit(errorSignal, currentJSON, branch, {
                error: data.status,
                unmergedChangeID: `${
                  encodeURIComponent(currentJSON.change.project)}~${
                  encodeURIComponent(data.branch)}~${data.change_id}`,
                targetPickParent: targetPickParent, parentJSON: data, isRetry: isRetry
              });
            } else {
              // The status of the parent should be MERGED at this point.
              // Try to see if it was picked to the target branch.
              _this.logger.log(
                `Immediate parent (${immediateParent}) for ${
                  currentJSON.fullChangeID} is in state: ${data.status}`,
                "debug", currentJSON.uuid
              );
              gerritTools.queryChange(
                currentJSON.uuid, targetPickParent, undefined, currentJSON.customGerritAuth,
                function (exists, targetData) {
                  if (exists) {
                    _this.logger.log(
                      `Target pick parent ${
                        targetPickParent} exists and will be used as the the parent for ${branch}`,
                      "debug", currentJSON.uuid
                    );
                    // Success - The target exists and can be set as the parent.
                    _this.emit(
                      responseSignal, currentJSON, branch,
                      { target: targetData.current_revision, isRetry: isRetry }
                    );
                  } else if (targetData == "retry") {
                  // Do nothing. This callback function will be called again on retry.
                    retryThis();
                  } else {
                  // The target change ID doesn't exist on the branch specified.
                    _this.logger.log(
                      `Target pick parent ${targetPickParent} does not exist on ${branch}`,
                      "debug", currentJSON.uuid
                    );
                    toolbox.addToCherryPickStateUpdateQueue(
                      currentJSON.uuid,
                      { branch: branch, statusDetail: "parentMergedNoPick" },
                      "verifyParentPickExists",
                      function () {
                        _this.emit(
                          errorSignal, currentJSON, branch,
                          {
                            error: "notPicked",
                            parentChangeID: data.id,
                            parentJSON: data, targetPickParent: targetPickParent, isRetry: isRetry
                          }
                        );
                      }
                    );
                  }
                }
              );
            } // End of target pick parent queryChange call
          } else if (data == "retry") {
            // Do nothing. This callback function will be called again on retry.
            retryThis();
          } else {
            fatalError(data);
          }
        }
      ); // End of parent change queryChange call
    }

    // Query for the current change to get a list of its parents.
    gerritTools.queryChange(
      currentJSON.uuid, currentJSON.fullChangeID, undefined, currentJSON.customGerritAuth,
      function (exists, data) {
        if (exists) {
          let immediateParent;
          // Success - Locate the parent revision (SHA) to the current change.
          // If the current change is part of a relation chain and is not the last change
          // in the list, use the previous change in the chain as the parent.
          // The retrieve the current revision of that parent.
          if (currentJSON.relatedChanges.length > 0) {
            let next = [...currentJSON.relatedChanges].reverse().findIndex((i) =>
              i.change_id === currentJSON.change.id) - 1;
            if (next >= 0) {
              _this.logger.log(
                `This change is part of a relation chain. Using the previous change in the chain as the parent.`,
                "verbose", currentJSON.uuid
              );
              const repo_branch = currentJSON.fullChangeID.split('~').slice(0, 2).join('~');
              let parentChangeId = `${repo_branch}~${currentJSON.relatedChanges[next].change_id}`;
              // Query for the parent change ID to get the current revision.
              gerritTools.queryChange(
                currentJSON.uuid, parentChangeId, undefined, currentJSON.customGerritAuth,
                function (exists, data) {
                  if (exists) {
                    _this.logger.log(
                      `Found the parent change ID for ${currentJSON.fullChangeID}: ${
                        data.current_revision}`,
                      "debug", currentJSON.uuid
                    );
                    immediateParent = data.current_revision;
                    queryParent(immediateParent);
                  } else if (data == "retry") {
                  // Do nothing. This callback function will be called again on retry.
                    retryThis();
                  } else {
                    fatalError(data);
                  }
                }); // End of parent change queryChange call
            } else {
              _this.logger.log(
                `This change is part of a relation chain, but is the first change in the chain.`
                + ` Using the latest patchset's parent as the parent.`,
                "verbose", currentJSON.uuid
              );
              immediateParent = data.revisions[data.current_revision].commit.parents[0].commit;
              queryParent(immediateParent);
            }
          } else {
            _this.logger.log(
              `This change is not part of a relation chain.`
              + ` Using the latest patchset's parent as the parent.`,
              "verbose", currentJSON.uuid
            );
            immediateParent = data.revisions[data.current_revision].commit.parents[0].commit;
            queryParent(immediateParent);
          }
      } else if (data == "retry") {
        // Do nothing. This callback function will be called again on retry.
          retryThis();
        } else {
          fatalError(data);
        }
      }
    ); // End of current change queryChange call
  }

  // From the current change, try to locate the nearest parent in the
  // chain that is picked to the same branch as this pick is intended for.
  // Use the nearmost change on the target branch as the parent for this pick
  // Use branch HEAD if no parents to this change are picked to the same
  // branch.
  locateNearestParent(currentJSON, next, branch, responseSignal) {
    let _this = this;

    function retryThis() {
      toolbox.addToCherryPickStateUpdateQueue(
        currentJSON.uuid, { branch: branch, statusDetail: "locateNearestParentRetryWait" },
        "locateNearestParent"
      );
      _this.retryProcessor.addRetryJob(
        currentJSON.uuid, "locateNearestParent",
        [currentJSON, next, branch, responseSignal]
      );
    }

    toolbox.addToCherryPickStateUpdateQueue(
      currentJSON.uuid,
      {
        branch: branch, statusDetail: "locateNearestParent",
        args: [currentJSON, next, branch, responseSignal]
      },
      "locateNearestParent"
    );

    let positionInChain = currentJSON.relatedChanges.findIndex((i) =>
      i.change_id === (next || currentJSON.change.id));

    if (next === -1) {
      // We've reached the top of the chain and found no suitable parent.
      // Send the response signal with the target branch head.
      _this.logger.log(
        `Using ${branch} head as the target pick parent in ${currentJSON.change.project}`,
        "verbose", currentJSON.uuid
      );
      gerritTools.validateBranch(
        currentJSON.uuid, currentJSON.change.project, branch, currentJSON.customGerritAuth,
        (success, branchHead) => {
          // This should never hard-fail since the branch is already
          // validated!
          _this.emit(responseSignal, currentJSON, branch, { target: branchHead });
        }
      );
    } else {
      let targetPickParent = `${encodeURIComponent(currentJSON.change.project)}~${
        encodeURIComponent(branch)}~${currentJSON.relatedChanges[positionInChain].change_id
      }`;
      _this.logger.log(
        `Locating nearest parent in relation chain to ${currentJSON.fullChangeID}. Now trying: ${
          targetPickParent}\nCurrent position in parent chain=${positionInChain}`,
        "debug", currentJSON.uuid
      );

      // See if a pick exists on the target branch for this candidate.
      gerritTools.queryChange(
        currentJSON.uuid, targetPickParent, undefined, currentJSON.customGerritAuth,
        (success, data) => {
          if (success) {
            _this.logger.log(
              `Found a parent to use: ${data.current_revision} for ${branch}`,
              "verbose", currentJSON.uuid
            );
            // The target parent exists on the target branch. Use it.
            _this.emit(responseSignal, currentJSON, branch, { target: data.current_revision });
          } else if (data == "retry") {
          // Do nothing. This callback function will be called again on retry.
            retryThis();
          } else if (positionInChain < currentJSON.relatedChanges.length - 1) {
          // Still more items to check. Check the next parent.
            _this.emit(
              "locateNearestParent", currentJSON,
              currentJSON.relatedChanges[positionInChain + 1].change_id, branch,
              "relationChain_validBranchReadyForPick"
            );
          } else {
          // No more items to check. Pass -1 in "next" param to send the
          // sha of the target branch head.
            _this.logger.log(
              `Reached the end of the relation chain for finding a parent`,
              "debug", currentJSON.uuid
            );
            _this.emit(
              "locateNearestParent", currentJSON, -1, branch,
              "relationChain_validBranchReadyForPick"
            );
          }
        }
      );
    }
  }

  // Sanity check to make sure the cherry-pick we have can actually be staged.
  // Check to make sure its parent is merged or currently staging.
  // Send the error signal if the parent is abandoned, not yet staged,
  // or presently integrating.
  stagingReadyCheck(originalRequestJSON, cherryPickJSON, responseSignal, errorSignal) {
    let _this = this;

    function fatalError(data) {
      _this.logger.log(
        `Failed to validate staging readiness for ${cherryPickJSON.id}`,
        "debug", originalRequestJSON.uuid
      );
      toolbox.addToCherryPickStateUpdateQueue(
        originalRequestJSON.uuid,
        {
          branch: cherryPickJSON.branch,
          statusCode: data.statusCode, statusDetail: data.statusDetail
        },
        "done_parentValidationFailed",
        function () {
          toolbox.decrementPickCountRemaining(originalRequestJSON.uuid);
        }
      );
      _this.gerritCommentHandler(
        originalRequestJSON.uuid, cherryPickJSON.id, undefined, data.message,
        "OWNER"
      );
    }

    function retryThis() {
      toolbox.addToCherryPickStateUpdateQueue(
        originalRequestJSON.uuid,
        { branch: cherryPickJSON.branch, statusDetail: "verifyParentRetryWait" },
        "stageEligibilityCheck"
      );
      _this.retryProcessor.addRetryJob(
        originalRequestJSON.uuid,
        "relationChain_cherrypickReadyForStage",
        [originalRequestJSON, cherryPickJSON, responseSignal, errorSignal]
      );
    }

    _this.logger.log(
      `Checking for staging readiness on ${cherryPickJSON.id}`,
      "verbose", originalRequestJSON.uuid
    );

    toolbox.addToCherryPickStateUpdateQueue(
      originalRequestJSON.uuid,
      {
        branch: cherryPickJSON.branch,
        args: [originalRequestJSON, cherryPickJSON, responseSignal, errorSignal]
      },
      "stageEligibilityCheck"
    );

    gerritTools.queryChange(
      originalRequestJSON.uuid, cherryPickJSON.id, undefined,  originalRequestJSON.customGerritAuth,
      function (success, data) {
        if (success) {
          gerritTools.queryChange(
            originalRequestJSON.uuid,
            data.revisions[data.current_revision].commit.parents[0].commit, undefined,
            originalRequestJSON.customGerritAuth,
            function (success, data) {
              if (success) {
                _this.logger.log(
                  `Parent ${data.id} for ${cherryPickJSON.id} is in state ${data.status}`,
                  "debug", originalRequestJSON.uuid
                );
                if (data.status == "MERGED" || data.status == "STAGED") {
                  toolbox.addToCherryPickStateUpdateQueue(
                    originalRequestJSON.uuid,
                    { branch: cherryPickJSON.branch, statusDetail: "stageEligibilityCheckPassed" },
                    "stageEligibilityCheck"
                  );
                  _this.emit(
                    responseSignal, originalRequestJSON, cherryPickJSON,
                    data.id, data.status
                  );
                } else if (data.status == "INTEGRATING" || data.status == "NEW") {
                  toolbox.addToCherryPickStateUpdateQueue(
                    originalRequestJSON.uuid,
                    {
                      branch: cherryPickJSON.branch,
                      statusDetail: "stageEligibilityCheckWaitParent"
                    }, "stageEligibilityCheck"
                  );
                  // Stop processing this request and consider it done.
                  // If further processing is needed, the caller should
                  // handle the error signal as needed.
                  _this.emit(errorSignal, originalRequestJSON, cherryPickJSON, data.id, data.status);
                  toolbox.addToCherryPickStateUpdateQueue(
                    originalRequestJSON.uuid,
                    {
                      branch: cherryPickJSON.branch,
                      statusDetail: data.status
                    },
                    "done_waitParent",
                    function () {
                      toolbox.decrementPickCountRemaining(originalRequestJSON.uuid);
                    }
                  );
                } else {
                // Uh-oh! The parent is in some other status like ABANDONED! This
                // is bad, and shouldn't happen, since it was a cherry-pick. It's
                // possible that the owner abandoned it and created a new patch
                // to take its place. Call this a fatal error and post a comment.
                  fatalError({
                    statusCode: data.statusCode, statusDetail: data.statusDetail,
                    message: `The parent to this cherry pick is in a state unsuitable for using as a parent for this cherry-pick. Please reparent it and stage it manually.`
                  });
                }
              } else if (data == "retry") {
                retryThis();
              } else {
              // We somehow managed to fail querying for the cherry pick we're trying to check...
              // This should not happen, but could theoretically occur in a race condition.
                fatalError({
                  statusCode: data.statusCode, statusDetail: data.statusDetail,
                  message: `Cherry-pick bot permanently failed to query the status of this pick's parent. Please stage it manually.`
                });
              }
            } // End of callback
          );  // End of nested queryChange()
        } else if (data == "retry") {
          retryThis();
        } else {
          fatalError({
            statusCode: data.statusCode, statusDetail: data.statusDetail,
            message: `Cherry-pick bot permanently failed to query the status of this pick's parent. Please stage it manually.`
          });
        }
      }
    );  // End of top-level queryChange() and its callback
  }

  // Generate a cherry pick and call the response signal.
  doCherryPick(incoming, branch, newParentRev, responseSignal) {
    let _this = this;

    function _doPickAlreadyExists(data, message) {
      toolbox.addToCherryPickStateUpdateQueue(
        incoming.uuid,
        {
          branch: branch, statusCode: data.statusCode, statusDetail: data.statusDetail, args: []
        },
        "done_pickAlreadyExists",
        function () {
          toolbox.decrementPickCountRemaining(incoming.uuid);
          gerritTools.locateDefaultAttentionUser(incoming.uuid, incoming,
            incoming.change.owner.email, (user) => {
              gerritTools.addToAttentionSet(incoming.uuid, incoming, user, undefined, undefined, () => {
                const notifyScope = "ALL";
                _this.gerritCommentHandler(
                  incoming.uuid, incoming.fullChangeID, undefined,
                  message, notifyScope
                );
              });
            }
          );
        });
    }

    _this.logger.log(
      `Performing cherry-pick to ${branch} from ${incoming.fullChangeID}`,
      "info", incoming.uuid
    );
    toolbox.addToCherryPickStateUpdateQueue(
      incoming.uuid,
      {
        branch: branch, revision: newParentRev, statusDetail: "pickStarted",
        args: [incoming, branch, newParentRev, responseSignal]
      }, "validBranchReadyForPick"
    );
    gerritTools.generateCherryPick(
      incoming, newParentRev, branch, incoming.customGerritAuth,
      function (success, data) {
        _this.logger.log(
          `Cherry-pick result on ${incoming.change.branch}: ${success}:\n${safeJsonStringify(data)}`,
          "info", incoming.uuid
        );
        if (success) {
          let message = `Successfully created cherry-pick to ${branch}`;
          // Some nasty assembly of the gerrit URL of the change.
          // Formatted as https://codereview.qt-project.org/c/qt%2Fqtqa/+/294338
          let gerritResolvedURL = /^(http)s?:\/\//g.test(_this.gerritURL)
            ? _this.gerritURL
            : `${_this.gerritPort == 80 ? "http" : "https"}://${_this.gerritURL}`;
          gerritResolvedURL +=
          _this.gerritPort != 80 && _this.gerritPort != 443 ? ":" + _this.gerritPort : "";
          message += `\nView it here: ${gerritResolvedURL}/c/${encodeURIComponent(data.project)}/+/${
            data._number
          }`;
          _this.gerritCommentHandler(incoming.uuid, incoming.fullChangeID, undefined, message);
          // Result looks okay, let's see what to do next.
          _this.emit(responseSignal, incoming, data);
        } else if (data.statusCode) {
          // Failed to cherry pick to target branch. Post a comment on the original change
          // and stop paying attention to this pick.
          if (data.statusCode == 400 && data.statusDetail.includes("could not update the existing change")) {
            // The cherry-pick failed because the change already exists. This can happen if
            // the pick-targets are user-specified and the user has already cherry-picked
            // to the target branch.
            // Pretend that the target branch has just merged and emit a change-merged signal.


            let remainingPicks = toolbox.findPickToBranches(incoming.uuid, incoming.change.commitMessage);
            remainingPicks.delete(branch);  // Delete this branch from the list of remaining picks.
            // Parse the status from the statusDetail. It exists as the last word in the string,
            // wrapped in ().
            let changeStatus = data.statusDetail.match(/\(([^)]+)\)(?:\\n)?/)[1];
            // Parse the change number for the existing change. It is surrounded by "change" and "in destination"
            // in the statusDetail.
            let existingChangeNumber = data.statusDetail.match(/change (\d+) in destination/)[1];
            let existingChangeURL = `${gerritTools.gerritResolvedURL}/c/${incoming.project.name || incoming.project}/+/${existingChangeNumber}`;
            _this.logger.log(
              `Cherry-pick to ${branch} already exists in state ${changeStatus}.`,
              "info", incoming.uuid
            );
            if (changeStatus == "MERGED") {
              if (remainingPicks.size == 0) {
                // No more picks to do. Just post a comment.
                _doPickAlreadyExists(data,
                  `A closed change already exists on ${branch} with the same change ID.\n`
                  + `No further picks are necessary. Please verify that the existing change`
                  + ` is correct.\n\n`
                  + `Link: ${existingChangeURL}`
                  );
              } else {
              // Mock up a change-merged signal and re-emit it as though the target
              // branch just merged.
              _this.logger.log(`Mocking Merge on ${branch}.`, "info", incoming.uuid);
                toolbox.mockChangeMergedFromOther(incoming.uuid, incoming, branch, remainingPicks, (mockObject) => {
                  if (mockObject) {
                    _this.emit("mock-change-merged", mockObject);
                  }
                  _doPickAlreadyExists(data,
                    `A closed change already exists on ${branch} with this change ID.\n`
                    + `Picks to ${Array.from(remainingPicks).join(", ")} will be performed using`
                    + ` that change as a base.\n`
                    + `Please verify that the existing change and resulting picks are correct.\n\n`
                    + `    Change ID: ${mockObject.change.change_id}\n`
                    + `    Subject: ${mockObject.change.subject}\n\n`
                    + `Link: ${mockObject.change.url}`
                  );
                });
              }
            } else if (changeStatus == "ABANDONED" || changeStatus == "DEFERRED") {
              _doPickAlreadyExists(data,
                `An abandoned change already exists on ${branch} with this change ID .\n`
                + `WARN: Cherry-pick bot cannot continue.\n`
                + `Picks to ${Array.from(remainingPicks).join(", ")} will not be performed automatically.\n\n`
                + `Link: ${existingChangeURL}`
              );
            } else if (changeStatus == "INTEGRATING" || changeStatus == "STAGED") {
              _doPickAlreadyExists(data,
                `A change in in state ${changeStatus} already exists on ${branch} with this change ID .\n`
                + `WARN: Cherry-pick bot cannot continue.\n`
                + `Picks to ${Array.from(remainingPicks).join(", ")} will not be performed automatically from this change.\n`
                + `Picks from the ${changeStatus} change on ${branch} will execute normally upon merge.`
                + ` Please review that change's Pick-to: for correctness.`
                + `Link: ${existingChangeURL}`
              );
            } else {
              _doPickAlreadyExists(data,
                `A change in in state ${changeStatus} already exists on ${branch} with this change ID .\n`
                + `WARN: Cherry-pick bot cannot continue. Please report this issue to gerrit admins.\n`
                + `Cherry-pick bot does not know how to handle changes in ${changeStatus} state.`
              );
            }
          } else {
            toolbox.addToCherryPickStateUpdateQueue(
              incoming.uuid,
              {
                branch: branch, statusCode: data.statusCode, statusDetail: data.statusDetail, args: []
              },
              "done_pickFailed",
              function () {
                toolbox.decrementPickCountRemaining(incoming.uuid);
              }
            );
            _this.gerritCommentHandler(
              incoming.uuid, incoming.fullChangeID, undefined,
              `Failed to cherry pick to ${branch}.\nReason: ${data.statusCode}: ${data.statusDetail}`
            );
          }
        } else if (data == "retry") {
        // Do nothing. This callback function will be called again on retry.
          toolbox.addToCherryPickStateUpdateQueue(
            incoming.uuid, { branch: branch, statusDetail: "pickCreateRetryWait" },
            "validBranchReadyForPick"
          );
          _this.retryProcessor.addRetryJob(
            incoming.uuid, "validBranchReadyForPick",
            [incoming, branch, newParentRev, responseSignal]
          );
        } else {
          toolbox.addToCherryPickStateUpdateQueue(
            incoming.uuid,
            {
              branch: branch, statusCode: "",
              statusDetail: "Unknown HTTP error. Contact the gerrit admins at gerrit-admin@qt-project.org", args: []
            },
            "done_pickFailed",
            function () {
              toolbox.decrementPickCountRemaining(incoming.uuid);
            }
          );
          emailClient.genericSendEmail(
            _this.adminEmail, "Cherry-pick bot: Error in Cherry Pick request",
            undefined, safeJsonStringify(data, undefined, 4)
          );
        }
      }
    );
  }

  // For a newly created cherry pick, check to see if there are merge
  // conflicts and update the attention set to add reviewers if so.
  processNewCherryPick(parentJSON, cherryPickJSON, responseSignal) {
    let _this = this;

    _this.logger.log(
      `Checking cherry-pick ${cherryPickJSON.id} for conflicts`,
      "verbose", parentJSON.uuid
    );
    toolbox.addToCherryPickStateUpdateQueue(
      parentJSON.uuid,
      {
        branch: cherryPickJSON.branch, args: [parentJSON, cherryPickJSON, responseSignal],
        statusDetail: "processNewCherryPick"
      },
      "newCherryPick"
    );

    if (cherryPickJSON.contains_git_conflicts) {
      _this.logger.log(
        `Conflicts found for ${cherryPickJSON.id}`,
        "verbose", parentJSON.uuid
      );
      // Internal emitter in case anything needs to know about conflicts on this change.
      _this.emit(`mergeConflict_${cherryPickJSON.id}`);
      let owner = parentJSON.change.owner.email || parentJSON.change.owner.username;
      gerritTools.checkAccessRights(parentJSON.uuid, parentJSON.change.project,
        cherryPickJSON.branch, owner, "read", undefined,
        (canRead) => {
          if (canRead) {
            gerritTools.setChangeReviewers(parentJSON.uuid, cherryPickJSON.id, [owner], undefined,
              () =>{
                gerritTools.addToAttentionSet(
                  parentJSON.uuid, cherryPickJSON, owner, "Original change owner",
                  parentJSON.customGerritAuth,
                  function (success, data) {
                    if (!success) {
                      _this.logger.log(
                        `Failed to add "${safeJsonStringify(parentJSON.change.owner)}" to the attention`
                        + ` set on ${cherryPickJSON.id}.\nReason: ${safeJsonStringify(data)}`,
                        "warn", parentJSON.uuid
                      );
                      _this.gerritCommentHandler(
                        parentJSON.uuid, cherryPickJSON.id, undefined,
                        `Unable to add ${owner} to the attention set of this issue.\n`
                        + `Reason: ${safeJsonStringify(data)}`,
                        "NONE"
                      );
                    }
                  }
              );
            });
          } else {
            gerritTools.locateDefaultAttentionUser(parentJSON.uuid, cherryPickJSON,
              parentJSON.patchSet.uploader.email,
              (user) => {
                if (user == "copyReviewers")
                  return;  // Copying users is done later regardless of attention set users.
                else {
                  gerritTools.setChangeReviewers(parentJSON.uuid, cherryPickJSON.id,
                    [user], undefined, function(){
                      gerritTools.addToAttentionSet(
                        parentJSON.uuid, cherryPickJSON, owner, "Original Reviewer",
                        undefined, function(){});
                    });
                }
            } )
          }
        });
      gerritTools.copyChangeReviewers(
        parentJSON.uuid, parentJSON.fullChangeID, cherryPickJSON.id, parentJSON.customGerritAuth,
        function (success, failedItems) {
          _this.gerritCommentHandler(
            parentJSON.uuid, cherryPickJSON.id, undefined,
            `INFO: This cherry-pick from your recently merged change on ${
              parentJSON.originalBranch} has conflicts.\nPlease review.`
          );
          if (success && failedItems.length > 0) {
            _this.gerritCommentHandler(
              parentJSON.uuid, cherryPickJSON.id, undefined,
              `INFO: Some reviewers were not successfully added to this change. You may wish to add them manually.\n ${
                safeJsonStringify(failedItems, undefined, "\n")}`,
              "OWNER"
            );
          } else if (!success) {
            _this.gerritCommentHandler(
              parentJSON.uuid, cherryPickJSON.id, undefined,
              `INFO: Reviewers were unable to be automatically added to this change. Please add reviewers manually.`,
              "OWNER"
            );
          }
          // We're done with this one since it now needs human review.
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid, { branch: cherryPickJSON.branch, args: [] },
            "done_mergeConflicts",
            function () {
              toolbox.decrementPickCountRemaining(parentJSON.uuid);
            }
          );
        }
      );
    } else {
      _this.emit(responseSignal, parentJSON, cherryPickJSON);
    }
  }

  autoApproveCherryPick(parentJSON, cherryPickJSON, responseSignal) {
    // The resulting cherry pick passed all requirements for automatic merging.
    // Set the approval since a +2 on code-review
    // and a +1 is required on sanity-review for staging.
    let _this = this;

    _this.logger.log(`Auto-approving ${cherryPickJSON.id} for staging`, "verbose", parentJSON.uuid);
    toolbox.addToCherryPickStateUpdateQueue(
      parentJSON.uuid,
      {
        branch: cherryPickJSON.branch, statusDetail: "startedApproval",
        args: [parentJSON, cherryPickJSON, responseSignal]
      },
      "cherryPickDone"
    );

    const approvalmsg = `This change is being approved because it was automatically cherry-picked from dev and contains no conflicts.`;
    gerritTools.setApproval(
      parentJSON.uuid, cherryPickJSON, 2, approvalmsg, "NONE", parentJSON.customGerritAuth,
      function (success, data) {
        if (success) {
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid,
            { branch: cherryPickJSON.branch, statusDetail: "approvalSet" },
            "cherryPickDone"
          );
          _this.emit(responseSignal, parentJSON, cherryPickJSON);
        } else if (data == "retry") {
          _this.logger.log(
            `Failed to approve pick ${
              cherryPickJSON.id} due to a network issue. Retrying in a bit.`,
            "warn", parentJSON.uuid
          );
          _this.retryProcessor.addRetryJob(
            parentJSON.uuid, "cherryPickDone",
            [parentJSON, cherryPickJSON, responseSignal]
          );
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid,
            { branch: cherryPickJSON.branch, statusDetail: "setApprovalRetryWait" },
            "cherryPickDone"
          );
        // Do nothing. This callback function will be called again on retry.
        } else {
        // This shouldn't happen. The bot should never be denied a +2.
          _this.logger.log(
            `Failed to set approvals on ${cherryPickJSON.id}.\nReason: ${safeJsonStringify(data)}`,
            "error", parentJSON.uuid
          );
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid,
            {
              branch: cherryPickJSON.branch, statusDetail: data || undefined, args: []
            },
            "done_setApprovalFailed",
            function () {
              toolbox.decrementPickCountRemaining(parentJSON.uuid);
            }
          );
          gerritTools.addToAttentionSet(
            parentJSON.uuid, cherryPickJSON,
            parentJSON.change.owner.email || parentJSON.change.owner.username, "Original Owner",
            parentJSON.customGerritAuth,
            function (success, data) {
              if (!success) {
                _this.logger.log(
                  `Failed to add "${safeJsonStringify(parentJSON.change.owner)}" to the`
                  + ` attention set of ${cherryPickJSON.id}\nReason: ${safeJsonStringify(data)}`,
                  "error", parentJSON.uuid
                );
              }
            }
          );
          _this.gerritCommentHandler(
            parentJSON.uuid, cherryPickJSON.id, undefined,
            `INFO: The Cherry-Pick bot was unable to automatically approve this change. Please review.\nReason:${
              data
                ? safeJsonStringify(data, undefined, 4)
                : "Unknown error. Please contact the gerrit admins at gerrit-admin@qt-project.org"
            }`,
            "OWNER"
          );
        }
      }
    );
  }

  // Attempt to stage the cherry-pick to CI.
  stageCherryPick(parentJSON, cherryPickJSON, responseSignal) {
    let _this = this;
    _this.logger.log(`Starting staging for ${cherryPickJSON.id}`, "verbose", parentJSON.uuid);
    toolbox.addToCherryPickStateUpdateQueue(
      parentJSON.uuid,
      {
        branch: cherryPickJSON.branch, statusDetail: "stagingStarted",
        args: [parentJSON, cherryPickJSON, responseSignal]
      },
      "cherrypickReadyForStage"
    );
    gerritTools.stageCherryPick(
      parentJSON.uuid, cherryPickJSON, parentJSON.customGerritAuth,
      function (success, data) {
        if (success) {
          _this.logger.log(`Staged ${cherryPickJSON.id}`, "info", parentJSON.uuid);
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid, { branch: cherryPickJSON.branch, statusDetail: "staged", args: [] },
            "done_staged",
            function () {
              toolbox.decrementPickCountRemaining(parentJSON.uuid);
            }
          );
          _this.emit(responseSignal, true, parentJSON, cherryPickJSON);
        } else if (data == "retry") {
        // Do nothing. This callback function will be called again on retry.
          _this.logger.log(`Failed to stage cherry pick ${
            cherryPickJSON.id} due to a network issue. Retrying in a bit.`);
          _this.retryProcessor.addRetryJob(
            parentJSON.uuid, "cherrypickReadyForStage",
            [parentJSON, cherryPickJSON, responseSignal]
          );

          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid,
            { branch: cherryPickJSON.branch, statusDetail: "stageFailedRetryWait" },
            "cherrypickReadyForStage"
          );
        } else {
          _this.logger.log(
            `Failed to stage ${cherryPickJSON.id}. Reason: ${safeJsonStringify(data)}`,
            "error", parentJSON.uuid
          );
          gerritTools.locateDefaultAttentionUser(parentJSON.uuid, cherryPickJSON,
            parentJSON.patchSet.uploader.email, function(user) {
              if (user && user == "copyReviewers") {
                gerritTools.copyChangeReviewers(parentJSON.uuid, parentJSON.fullChangeID,
                  cherryPickJSON.id);
              } else {
                gerritTools.setChangeReviewers(parentJSON.uuid, cherryPickJSON.id,
                  [user], undefined, function() {
                    gerritTools.addToAttentionSet(
                      parentJSON.uuid, cherryPickJSON, user, "Relevant user",
                      parentJSON.customGerritAuth,
                      function (success, data) {
                        if (!success) {
                          _this.logger.log(
                            `Failed to add "${user}" to the`
                            + ` attention set of ${cherryPickJSON.id}\n`
                            + `Reason: ${safeJsonStringify(data)}`,
                            "error", parentJSON.uuid
                          );
                        }
                      }
                    );
                  });
              }
            }
          );
          _this.gerritCommentHandler(
            parentJSON.uuid, cherryPickJSON.id,
            undefined,
            `INFO: The cherry-pick bot failed to automatically stage this change to CI. Please try to stage it manually.\n\nContact gerrit administration if you continue to experience issues.\n\nReason: ${
              data
                ? safeJsonStringify(data, undefined, 4)
                : "Unknown error. Please contact the gerrit admins at gerrit-admin@qt-project.org"
            }`,
            "OWNER"
          );
          toolbox.addToCherryPickStateUpdateQueue(
            parentJSON.uuid,
            {
              branch: cherryPickJSON.branch, statusDetail: data.data || data.message,
              statusCode: data.status || "", args: []
            },
            "stageFailed",
            function () {
              toolbox.decrementPickCountRemaining(parentJSON.uuid);
            }
          );
          _this.emit(responseSignal, false, parentJSON, cherryPickJSON);
        }
      }
    );
  }

  // Set up a a post-comment action and retry it until it goes through.
  // this function should never be relied upon to succeed, as posting
  // comments will be handled in an async "it's done when it's done"
  // manner.
  gerritCommentHandler(parentUuid, fullChangeID, revision, message, notifyScope, customGerritAuth) {
    let _this = this;
    gerritTools.postGerritComment(
      parentUuid, fullChangeID, revision, message, notifyScope, customGerritAuth,
      function (success, data) {
        if (!success && data == "retry") {
          _this.emit(
            "addRetryJob", "postGerritComment",
            [parentUuid, fullChangeID, undefined, message, notifyScope, customGerritAuth]
          );
        }
      }
    );
  }
}

module.exports = requestProcessor;