|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Legacy GitHub credential settings migration. |
| 4 | + * |
| 5 | + * @package DataMachineCode\Support |
| 6 | + */ |
| 7 | + |
| 8 | +namespace DataMachineCode\Support; |
| 9 | + |
| 10 | +use DataMachine\Core\PluginSettings; |
| 11 | + |
| 12 | +defined('ABSPATH') || exit; |
| 13 | + |
| 14 | +final class GitHubCredentialSettingsMigration { |
| 15 | + |
| 16 | + public const MIGRATED_OPTION = 'datamachine_code_github_legacy_credentials_migrated_v1'; |
| 17 | + |
| 18 | + private const LEGACY_KEYS = array( |
| 19 | + 'github_pat', |
| 20 | + 'github_auth_mode', |
| 21 | + 'github_app_id', |
| 22 | + 'github_app_installation_id', |
| 23 | + 'github_app_private_key', |
| 24 | + 'github_default_repo', |
| 25 | + ); |
| 26 | + |
| 27 | + /** |
| 28 | + * Report non-secret migration state for operators. |
| 29 | + * |
| 30 | + * @return array<string,mixed> |
| 31 | + */ |
| 32 | + public static function status(): array { |
| 33 | + $legacy_present = self::legacy_keys_present(); |
| 34 | + $profiles = PluginSettings::get('github_credential_profiles', array()); |
| 35 | + |
| 36 | + return array( |
| 37 | + 'legacy_keys_present' => ! empty($legacy_present), |
| 38 | + 'legacy_keys' => $legacy_present, |
| 39 | + 'profiles_present' => is_array($profiles) && ! empty($profiles), |
| 40 | + 'migrated' => (bool) get_option(self::MIGRATED_OPTION, false), |
| 41 | + 'removal_target' => 'Remove legacy writes and implicit synthesis after live installs report migrated=true.', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Migrate the legacy single-credential shape into credential profiles. |
| 47 | + * |
| 48 | + * @return array<string,mixed> |
| 49 | + */ |
| 50 | + public static function migrate( bool $apply = false, bool $force = false ): array { |
| 51 | + $legacy_present = self::legacy_keys_present(); |
| 52 | + $existing = PluginSettings::get('github_credential_profiles', array()); |
| 53 | + $has_profiles = is_array($existing) && ! empty($existing); |
| 54 | + |
| 55 | + if ( empty($legacy_present) ) { |
| 56 | + return array( |
| 57 | + 'success' => true, |
| 58 | + 'applied' => false, |
| 59 | + 'legacy_keys_present' => false, |
| 60 | + 'profiles_present' => $has_profiles, |
| 61 | + 'message' => 'No legacy GitHub credential settings found.', |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if ( $has_profiles && ! $force ) { |
| 66 | + return array( |
| 67 | + 'success' => true, |
| 68 | + 'applied' => false, |
| 69 | + 'legacy_keys_present' => true, |
| 70 | + 'legacy_keys' => $legacy_present, |
| 71 | + 'profiles_present' => true, |
| 72 | + 'message' => 'Credential profiles already exist; pass force to overwrite from legacy settings.', |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + $profile = self::legacy_profile(); |
| 77 | + $preview = self::redact_profile($profile); |
| 78 | + |
| 79 | + if ( ! $apply ) { |
| 80 | + return array( |
| 81 | + 'success' => true, |
| 82 | + 'applied' => false, |
| 83 | + 'legacy_keys_present' => true, |
| 84 | + 'legacy_keys' => $legacy_present, |
| 85 | + 'profiles_present' => $has_profiles, |
| 86 | + 'profile' => $preview, |
| 87 | + 'message' => 'Dry run only; pass apply to write github_credential_profiles.', |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + self::write_setting('github_credential_profiles', array( $profile )); |
| 92 | + self::write_setting('github_default_profile_id', GitHubCredentialResolver::DEFAULT_PROFILE_ID); |
| 93 | + update_option(self::MIGRATED_OPTION, true, false); |
| 94 | + |
| 95 | + return array( |
| 96 | + 'success' => true, |
| 97 | + 'applied' => true, |
| 98 | + 'legacy_keys_present' => true, |
| 99 | + 'legacy_keys' => $legacy_present, |
| 100 | + 'profiles_present' => true, |
| 101 | + 'profile' => $preview, |
| 102 | + 'message' => 'Migrated legacy GitHub credential settings to github_credential_profiles.', |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return array<int,string> |
| 108 | + */ |
| 109 | + private static function legacy_keys_present(): array { |
| 110 | + $present = array(); |
| 111 | + foreach ( self::LEGACY_KEYS as $key ) { |
| 112 | + $value = PluginSettings::get($key, ''); |
| 113 | + if ( is_string($value) && '' !== trim($value) ) { |
| 114 | + $present[] = $key; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $present; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array<string,mixed> |
| 123 | + */ |
| 124 | + private static function legacy_profile(): array { |
| 125 | + $mode = strtolower(trim( (string) PluginSettings::get('github_auth_mode', '') )); |
| 126 | + if ( ! in_array($mode, array( 'pat', 'app' ), true) ) { |
| 127 | + $mode = 'pat'; |
| 128 | + } |
| 129 | + |
| 130 | + return GitHubProfileSanitizer::sanitize( |
| 131 | + array( |
| 132 | + array( |
| 133 | + 'id' => GitHubCredentialResolver::DEFAULT_PROFILE_ID, |
| 134 | + 'label' => 'Default', |
| 135 | + 'mode' => $mode, |
| 136 | + 'pat' => (string) PluginSettings::get('github_pat', ''), |
| 137 | + 'app_id' => (string) PluginSettings::get('github_app_id', ''), |
| 138 | + 'app_installation_id' => (string) PluginSettings::get('github_app_installation_id', ''), |
| 139 | + 'app_private_key' => (string) PluginSettings::get('github_app_private_key', ''), |
| 140 | + 'default_repo' => (string) PluginSettings::get('github_default_repo', ''), |
| 141 | + ), |
| 142 | + ) |
| 143 | + )[0]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @param array<string,mixed> $profile |
| 148 | + * @return array<string,mixed> |
| 149 | + */ |
| 150 | + private static function redact_profile( array $profile ): array { |
| 151 | + $profile['pat_configured'] = '' !== trim( (string) ( $profile['pat'] ?? '' ) ); |
| 152 | + $profile['app_private_key_configured'] = '' !== trim( (string) ( $profile['app_private_key'] ?? '' ) ); |
| 153 | + unset($profile['pat'], $profile['app_private_key']); |
| 154 | + return $profile; |
| 155 | + } |
| 156 | + |
| 157 | + private static function write_setting( string $key, mixed $value ): void { |
| 158 | + $settings = get_option('datamachine_settings', array()); |
| 159 | + if ( ! is_array($settings) ) { |
| 160 | + $settings = array(); |
| 161 | + } |
| 162 | + $settings[ $key ] = $value; |
| 163 | + update_option('datamachine_settings', $settings, false); |
| 164 | + } |
| 165 | +} |
0 commit comments