@@ -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 */
0 commit comments