Skip to content

Commit c019fb4

Browse files
refactor: centralize PR label handling (#768)
Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent 23fd41b commit c019fb4

2 files changed

Lines changed: 46 additions & 63 deletions

File tree

inc/Abilities/GitHubAbilities.php

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,27 +1606,7 @@ public static function createPullRequest( array $input ): array|\WP_Error {
16061606

16071607
if ( null !== $existing_pull ) {
16081608
$pull = self::normalizePull($existing_pull);
1609-
$labels = self::mergeProvenanceLabels(isset($input['labels']) && is_array($input['labels']) ? $input['labels'] : array());
1610-
$labeling = null;
1611-
1612-
if ( ! empty($labels) && ! empty($pull['number']) ) {
1613-
$label_response = self::applyLabelsToNumber($repo, (int) $pull['number'], $labels, $pat);
1614-
if ( is_wp_error($label_response) ) {
1615-
$labeling = array(
1616-
'success' => false,
1617-
'labels' => $labels,
1618-
'error_code' => $label_response->get_error_code(),
1619-
'error' => $label_response->get_error_message(),
1620-
'status' => is_array($label_response->get_error_data()) ? ( $label_response->get_error_data()['status'] ?? null ) : null,
1621-
);
1622-
} else {
1623-
$labeling = array(
1624-
'success' => true,
1625-
'labels' => $labels,
1626-
'applied_labels' => $label_response['applied_labels'] ?? array(),
1627-
);
1628-
}
1629-
}
1609+
$labeling = self::labelPullRequest($repo, (int) ( $pull['number'] ?? 0 ), $input, $pat);
16301610

16311611
$result = array(
16321612
'success' => true,
@@ -1674,27 +1654,7 @@ public static function createPullRequest( array $input ): array|\WP_Error {
16741654
}
16751655

16761656
$pull = self::normalizePull($response['data']);
1677-
$labels = self::mergeProvenanceLabels(isset($input['labels']) && is_array($input['labels']) ? $input['labels'] : array());
1678-
$labeling = null;
1679-
1680-
if ( ! empty($labels) && ! empty($pull['number']) ) {
1681-
$label_response = self::applyLabelsToNumber($repo, (int) $pull['number'], $labels, $pat);
1682-
if ( is_wp_error($label_response) ) {
1683-
$labeling = array(
1684-
'success' => false,
1685-
'labels' => $labels,
1686-
'error_code' => $label_response->get_error_code(),
1687-
'error' => $label_response->get_error_message(),
1688-
'status' => is_array($label_response->get_error_data()) ? ( $label_response->get_error_data()['status'] ?? null ) : null,
1689-
);
1690-
} else {
1691-
$labeling = array(
1692-
'success' => true,
1693-
'labels' => $labels,
1694-
'applied_labels' => $label_response['applied_labels'] ?? array(),
1695-
);
1696-
}
1697-
}
1657+
$labeling = self::labelPullRequest($repo, (int) ( $pull['number'] ?? 0 ), $input, $pat);
16981658

16991659
$result = array(
17001660
'success' => true,
@@ -1931,6 +1891,41 @@ private static function mergeProvenanceLabels( array $labels ): array {
19311891
return $merged;
19321892
}
19331893

1894+
/**
1895+
* Apply PR labels and normalize the result for every PR creation path.
1896+
*
1897+
* @param string $repo Repository owner/name.
1898+
* @param int $number Pull request number.
1899+
* @param array $input Pull request input.
1900+
* @param string $pat GitHub token.
1901+
* @return array<string,mixed>|null
1902+
*/
1903+
private static function labelPullRequest( string $repo, int $number, array $input, string $pat ): ?array {
1904+
$labels = self::mergeProvenanceLabels(isset($input['labels']) && is_array($input['labels']) ? $input['labels'] : array());
1905+
if ( empty($labels) || $number <= 0 ) {
1906+
return null;
1907+
}
1908+
1909+
$label_response = self::applyLabelsToNumber($repo, $number, $labels, $pat);
1910+
if ( is_wp_error($label_response) ) {
1911+
$error_data = $label_response->get_error_data();
1912+
1913+
return array(
1914+
'success' => false,
1915+
'labels' => $labels,
1916+
'error_code' => $label_response->get_error_code(),
1917+
'error' => $label_response->get_error_message(),
1918+
'status' => is_array($error_data) ? ( $error_data['status'] ?? null ) : null,
1919+
);
1920+
}
1921+
1922+
return array(
1923+
'success' => true,
1924+
'labels' => $labels,
1925+
'applied_labels' => $label_response['applied_labels'] ?? array(),
1926+
);
1927+
}
1928+
19341929
/**
19351930
* Resolve the current Data Machine agent slug when running in agent context.
19361931
*/

inc/Handlers/GitHub/GitHubPullRequestPublish.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,33 +220,21 @@ protected function executePublish( array $parameters, array $handler_config ): a
220220
$pull_number = (int) ( $result['pull_number'] ?? 0 );
221221

222222
$labels = $this->resolveListParameter($parameters['labels'] ?? null, $handler_config['labels'] ?? '');
223-
$applied_labels = array();
224-
$label_error = null;
223+
$labeling = is_array($result['labeling'] ?? null) ? $result['labeling'] : null;
224+
$applied_labels = is_array($labeling['applied_labels'] ?? null) ? $labeling['applied_labels'] : array();
225+
$label_error = is_array($labeling) && false === ( $labeling['success'] ?? true ) ? (string) ( $labeling['error'] ?? '' ) : null;
225226

226-
if ( ! empty($labels) && $pull_number > 0 ) {
227-
$label_result = GitHubAbilities::addLabels(
227+
if ( null !== $label_error ) {
228+
$this->log(
229+
'warning',
230+
'GitHub Pull Request opened but failed to apply labels: ' . $label_error,
228231
array(
232+
'job_id' => $parameters['job_id'] ?? null,
229233
'repo' => $repo,
230234
'pull_number' => $pull_number,
231-
'labels' => $labels,
235+
'labels' => $labeling['labels'] ?? $labels,
232236
)
233237
);
234-
235-
if ( is_wp_error($label_result) ) {
236-
$label_error = $label_result->get_error_message();
237-
$this->log(
238-
'warning',
239-
'GitHub Pull Request opened but failed to apply labels: ' . $label_error,
240-
array(
241-
'job_id' => $parameters['job_id'] ?? null,
242-
'repo' => $repo,
243-
'pull_number' => $pull_number,
244-
'labels' => $labels,
245-
)
246-
);
247-
} else {
248-
$applied_labels = $label_result['applied_labels'] ?? array();
249-
}
250238
}
251239

252240
$response_data = array(

0 commit comments

Comments
 (0)