Skip to content

Commit 5070e75

Browse files
committed
enhance github author/vendor username guessing
1 parent b4f5d4a commit 5070e75

File tree

1 file changed

+106
-7
lines changed

1 file changed

+106
-7
lines changed

configure.php

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,118 @@ function replaceForAllOtherOSes(): array
141141
return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|migration_table_name|vendor_name|vendor_slug|[email protected]" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__)));
142142
}
143143

144+
function getGithubApiEndpoint(string $endpoint): ?stdClass
145+
{
146+
try {
147+
$curl = curl_init("https://api.github.com/{$endpoint}");
148+
curl_setopt_array($curl, [
149+
CURLOPT_RETURNTRANSFER => true,
150+
CURLOPT_FOLLOWLOCATION => true,
151+
CURLOPT_HTTPGET => true,
152+
CURLOPT_HTTPHEADER => [
153+
'User-Agent: spatie-configure-script/1.0'
154+
],
155+
]);
156+
157+
$response = curl_exec($curl);
158+
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
159+
160+
curl_close($curl);
161+
162+
if ($statusCode === 200) {
163+
return json_decode($response);
164+
}
165+
} catch (Exception $e) {
166+
// ignore
167+
}
168+
169+
return null;
170+
}
171+
172+
function searchCommitsForGithubUsername(): string
173+
{
174+
$authorName = strtolower(trim(shell_exec("git config user.name")));
175+
176+
$committersRaw = shell_exec("git log --author='@users.noreply.github.com' --pretty='%an:%ae' --reverse");
177+
$committersLines = explode("\n", $committersRaw);
178+
$committers = array_filter(array_map(function($line) use ($authorName) {
179+
$line = trim($line);
180+
[$name, $email] = explode(':', $line) + [null, null];
181+
182+
return [
183+
'name' => $name,
184+
'email' => $email,
185+
'isMatch' => strtolower($name) === $authorName && !str_contains($name, '[bot]')
186+
];
187+
}, $committersLines), fn($item) => $item['isMatch']);
188+
189+
if (empty($committers)) {
190+
return '';
191+
}
192+
193+
$firstCommitter = reset($committers);
194+
return explode('@', $firstCommitter['email'])[0] ?? '';
195+
}
196+
197+
function guessGithubUsernameUsingCli()
198+
{
199+
try {
200+
if (preg_match('/ogged in to github\.com as ([a-zA-Z-_]+).+/', shell_exec('gh auth status -h github.com'), $matches) === 1) {
201+
return $matches[1];
202+
}
203+
} catch (Exception $e) {
204+
// ignore
205+
}
206+
207+
return '';
208+
}
209+
210+
function guessGithubUsername(): string
211+
{
212+
$username = searchCommitsForGithubUsername();
213+
if (!empty($username)) {
214+
return $username;
215+
}
216+
217+
$username = guessGithubUsernameUsingCli();
218+
if (!empty($username)) {
219+
return $username;
220+
}
221+
222+
// fall back to using the username from the git remote
223+
$remoteUrl = shell_exec("git config remote.origin.url");
224+
$remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl)));
225+
226+
return $remoteUrlParts[1] ?? '';
227+
}
228+
229+
function guessGithubVendorInfo($authorName, $username): array
230+
{
231+
$remoteUrl = shell_exec("git config remote.origin.url");
232+
$remoteUrlParts = explode('/', str_replace(':', '/', trim($remoteUrl)));
233+
234+
$response = getGithubApiEndpoint("orgs/{$remoteUrlParts[1]}");
235+
236+
if ($response === null) {
237+
return $username;
238+
}
239+
240+
return [$response->name ?? $authorName, $response->login ?? $username];
241+
}
242+
144243
$gitName = run('git config user.name');
145244
$authorName = ask('Author name', $gitName);
146245

147246
$gitEmail = run('git config user.email');
148247
$authorEmail = ask('Author email', $gitEmail);
248+
$authorUsername = ask('Author username', guessGithubUsername());
249+
250+
$guessGithubVendorInfo = guessGithubVendorInfo($authorName, $authorUsername);
149251

150-
$usernameGuess = explode(':', run('git config remote.origin.url'))[1];
151-
$usernameGuess = dirname($usernameGuess);
152-
$usernameGuess = basename($usernameGuess);
153-
$authorUsername = ask('Author username', $usernameGuess);
252+
$vendorName = ask('Vendor name', $guessGithubVendorInfo[0]);
253+
$vendorUsername = ask('Vendor username', $guessGithubVendorInfo[1] ?? slugify($vendorName));
254+
$vendorSlug = slugify($vendorUsername);
154255

155-
$vendorName = ask('Vendor name', $authorUsername);
156-
$vendorSlug = slugify($vendorName);
157256
$vendorNamespace = str_replace('-', '', ucwords($vendorName));
158257
$vendorNamespace = ask('Vendor namespace', $vendorNamespace);
159258

@@ -183,7 +282,7 @@ function replaceForAllOtherOSes(): array
183282
writeln("Class name : {$className}");
184283
writeln('---');
185284
writeln('Packages & Utilities');
186-
writeln('Use Laravel/Pint : '.($useLaravelPint ? 'yes' : 'no'));
285+
writeln('Use Laravel/Pint : '.($useLaravelPint ? 'yes' : 'no'));
187286
writeln('Use Larastan/PhpStan : '.($usePhpStan ? 'yes' : 'no'));
188287
writeln('Use Dependabot : '.($useDependabot ? 'yes' : 'no'));
189288
writeln('Use Ray App : '.($useLaravelRay ? 'yes' : 'no'));

0 commit comments

Comments
 (0)