@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class HarbormasterAbortOlderBuildsBuildStepImplementation
4 extends HarbormasterBuildStepImplementation {
5
6 public function getName() {
7 return pht('Abort Older Builds');
8 }
9
10 public function getGenericDescription() {
11 return pht(
12 'When building a revision, abort copies of this build plan which are '.
13 'currently running against older diffs.');
14 }
15
16 public function getBuildStepGroupKey() {
17 return HarbormasterControlBuildStepGroup::GROUPKEY;
18 }
19
20 public function getEditInstructions() {
21 return pht(<<<EOTEXT
22When run against a revision, this build step will abort any older copies of
23the same build plan which are currently running against older diffs.
24
25There are some nuances to the behavior:
26
27 - if this build step is triggered manually, it won't abort anything;
28 - this build step won't abort manual builds;
29 - this build step won't abort anything if the diff it is building isn't
30 the active diff when it runs.
31
32Build results on outdated diffs often aren't very important, so this may
33reduce build queue load without any substantial cost.
34EOTEXT
35 );
36 }
37
38 public function willStartBuild(
39 PhabricatorUser $viewer,
40 HarbormasterBuildable $buildable,
41 HarbormasterBuild $build,
42 HarbormasterBuildPlan $plan,
43 HarbormasterBuildStep $step) {
44
45 if ($buildable->getIsManualBuildable()) {
46 // Don't abort anything if this is a manual buildable.
47 return;
48 }
49
50 $object_phid = $buildable->getBuildablePHID();
51 if (phid_get_type($object_phid) !== DifferentialDiffPHIDType::TYPECONST) {
52 // If this buildable isn't building a diff, bail out. For example, we
53 // might be building a commit. In this case, this step has no effect.
54 return;
55 }
56
57 $diff = id(new DifferentialDiffQuery())
58 ->setViewer($viewer)
59 ->withPHIDs(array($object_phid))
60 ->executeOne();
61 if (!$diff) {
62 return;
63 }
64
65 $revision_id = $diff->getRevisionID();
66
67 $revision = id(new DifferentialRevisionQuery())
68 ->setViewer($viewer)
69 ->withIDs(array($revision_id))
70 ->executeOne();
71 if (!$revision) {
72 return;
73 }
74
75 $active_phid = $revision->getActiveDiffPHID();
76 if ($active_phid !== $object_phid) {
77 // If we aren't building the active diff, bail out.
78 return;
79 }
80
81 $diffs = id(new DifferentialDiffQuery())
82 ->setViewer($viewer)
83 ->withRevisionIDs(array($revision_id))
84 ->execute();
85 $abort_diff_phids = array();
86 foreach ($diffs as $diff) {
87 if ($diff->getPHID() !== $active_phid) {
88 $abort_diff_phids[] = $diff->getPHID();
89 }
90 }
91
92 if (!$abort_diff_phids) {
93 return;
94 }
95
96 // We're fetching buildables even if they have "passed" or "failed"
97 // because they may still have ongoing builds. At the time of writing
98 // only "failed" buildables may still be ongoing, but it seems likely that
99 // "passed" buildables may be ongoing in the future.
100
101 $abort_buildables = id(new HarbormasterBuildableQuery())
102 ->setViewer($viewer)
103 ->withBuildablePHIDs($abort_diff_phids)
104 ->withManualBuildables(false)
105 ->execute();
106 if (!$abort_buildables) {
107 return;
108 }
109
110 $statuses = HarbormasterBuildStatus::getIncompleteStatusConstants();
111
112 $abort_builds = id(new HarbormasterBuildQuery())
113 ->setViewer($viewer)
114 ->withBuildablePHIDs(mpull($abort_buildables, 'getPHID'))
115 ->withBuildPlanPHIDs(array($plan->getPHID()))
116 ->withBuildStatuses($statuses)
117 ->execute();
118 if (!$abort_builds) {
119 return;
120 }
121
122 foreach ($abort_builds as $abort_build) {
123 $abort_build->sendMessage(
124 $viewer,
125 HarbormasterBuildMessageAbortTransaction::MESSAGETYPE);
126 }
127 }
128
129 public function execute(
130 HarbormasterBuild $build,
131 HarbormasterBuildTarget $build_target) {
132 return;
133 }
134
135}