-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.php
159 lines (127 loc) · 4.62 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
require 'vendor/autoload.php';
function main(): void
{
$commitSha = getenv('GITHUB_SHA') ?: '';
exec('git config --global --add safe.directory /github/workspace');
$commitTitle = exec('git log -1 --pretty=%s');
$committerName = exec("git log -1 --pretty=%cn $commitSha");
$committerEmail = exec("git log -1 --pretty=%ce $commitSha");
if ($commitTitle === '[ai]') {
$model = getenv('OPENAI_MODEL') ?: 'gpt-3.5-turbo'; // Default to gpt-3.5-turbo if no environment variable is set
if (!in_array($model, ['gpt-4', 'gpt-4-32k', 'gpt-3.5-turbo'])) {
echo "::error::Invalid model specified. Please use either gpt-3.5-turbo', 'gpt-4' or 'gpt-4-32k'." .
PHP_EOL;
exit(1);
}
list($newTitle, $newDescription) = fetchAiGeneratedTitleAndDescription(
getCommitChanges($commitSha),
getenv('OPENAI_API_KEY'),
$model,
);
updateLastCommitMessage($newTitle, $newDescription, $committerEmail, $committerName);
}
}
main();
function fetchAiGeneratedTitleAndDescription(string $commitChanges, string $openAiApiKey, string $model): array
{
$prompt = generatePrompt($commitChanges);
$input_data = [
"temperature" => 0.7,
"max_tokens" => 300,
"frequency_penalty" => 0,
'model' => $model,
"messages" => [
[
'role' => 'user',
'content' => $prompt
],
]
];
try {
$client = new Client([
'base_uri' => 'https://api.openai.com',
'headers' => [
'Authorization' => 'Bearer ' . $openAiApiKey,
'Content-Type' => 'application/json'
]
]);
$response = $client->post('/v1/chat/completions', [
'json' => $input_data
]);
$complete = json_decode($response->getBody()->getContents(), true);
$output = $complete['choices'][0]['message']['content'];
return extractTitleAndDescription($output);
} catch (GuzzleException $e) {
echo "::error::Error fetching AI-generated title and description: " . $e->getMessage() . PHP_EOL;
exit(1);
}
}
function generatePrompt(string $commitChanges): string
{
return "Based on the following line-by-line changes in a commit, please generate an informative commit title and description
\n(max two or three lines of description to not exceed the model max token limitation):
\nCommit changes:
\n{$commitChanges}
\nFormat your response as follows:
\nCommit title: [Generated commit title]
\nCommit description: [Generated commit description]";
}
function extractTitleAndDescription(string $output): array
{
$title = '';
$description = '';
$responseLines = explode("\n", $output);
foreach ($responseLines as $line) {
if (str_starts_with($line, 'Commit title: ')) {
$title = str_replace('Commit title: ', '', $line);
} elseif (str_starts_with($line, 'Commit description: ')) {
$description = str_replace('Commit description: ', '', $line);
}
}
return [$title, $description];
}
function updateLastCommitMessage(
string $newTitle,
string $newDescription,
string $committerEmail,
string $committerName
): void {
configureGitCommitter($committerEmail, $committerName);
$newTitle = escapeshellarg($newTitle);
$newDescription = escapeshellarg($newDescription);
exec("git reset --soft HEAD~1");
exec("git commit -m {$newTitle} -m {$newDescription}");
exec("git push origin --force");
unsetGitCommitterConfiguration();
}
function configureGitCommitter(string $committerEmail, string $committerName): void
{
exec("git config user.email '{$committerEmail}'");
exec("git config user.name '{$committerName}'");
}
function unsetGitCommitterConfiguration(): void
{
exec("git config --unset user.email");
exec("git config --unset user.name");
}
function getCommitChanges(string $commitSha): string
{
$command = "git diff {$commitSha}~ {$commitSha} | grep -v 'warning'";
exec($command, $output, $return_var);
if ($return_var == 0) {
$length = getenv('OPENAI_MODEL') ? match (getenv('OPENAI_MODEL')) {
'gpt-3.5-turbo' => 400,
'gpt-4' => 800,
'gpt-4-32k' => 3200,
} : 400;
$output = array_slice($output, 0, $length);
return implode("\n", $output);
} else {
echo "Error: Could not run git diff. Return code: " . $return_var;
exit(1);
}
}