Skip to content

Commit b3673c3

Browse files
authored
Merge branch 'utopia-php:main' into feat/gitea-git-operations
2 parents b3e41f9 + f29fc06 commit b3673c3

5 files changed

Lines changed: 690 additions & 33 deletions

File tree

src/VCS/Adapter/Git.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getType(): string
4545
* @param string $message Commit message
4646
* @return array<mixed> Response from API
4747
*/
48-
abstract public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array;
48+
abstract public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array;
4949

5050
/**
5151
* Create a branch in a repository
@@ -57,4 +57,17 @@ abstract public function createFile(string $owner, string $repositoryName, strin
5757
* @return array<mixed> Response from API
5858
*/
5959
abstract public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array;
60+
61+
/**
62+
* Create a pull request
63+
*
64+
* @param string $owner Owner of the repository
65+
* @param string $repositoryName Name of the repository
66+
* @param string $title PR title
67+
* @param string $head Source branch
68+
* @param string $base Target branch
69+
* @param string $body PR description (optional)
70+
* @return array<mixed> Created PR details
71+
*/
72+
abstract public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array;
6073
}

src/VCS/Adapter/Git/GitHub.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,61 @@ public function createRepository(string $owner, string $repositoryName, bool $pr
9393

9494
return $response['body'] ?? [];
9595
}
96+
/**
97+
* Create a pull request
98+
*
99+
* @param string $owner Owner of the repository
100+
* @param string $repositoryName Name of the repository
101+
* @param string $title PR title
102+
* @param string $head Source branch
103+
* @param string $base Target branch
104+
* @param string $body PR description (optional)
105+
* @return array<mixed> Created PR details
106+
*/
107+
public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array
108+
{
109+
throw new Exception('Not implemented');
110+
}
96111

97112
/**
98113
* Create a file in a repository
99114
*
100-
* @param string $owner Owner of the repository
101-
* @param string $repositoryName Name of the repository
102-
* @param string $filepath Path where file should be created
103-
* @param string $content Content of the file
104-
* @param string $message Commit message
115+
* @param string $owner Owner of the repository
116+
* @param string $repositoryName Name of the repository
117+
* @param string $filepath Path where file should be created
118+
* @param string $content Content of the file
119+
* @param string $message Commit message
120+
* @param string $branch Branch to create file on (optional)
105121
* @return array<mixed> Response from API
106122
*/
107-
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array
123+
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array
108124
{
109-
throw new Exception("Not implemented");
125+
$url = "/repos/{$owner}/{$repositoryName}/contents/{$filepath}";
126+
127+
$payload = [
128+
'message' => $message,
129+
'content' => base64_encode($content),
130+
];
131+
132+
// GitHub supports branch parameter
133+
if (! empty($branch)) {
134+
$payload['branch'] = $branch;
135+
}
136+
137+
$response = $this->call(
138+
self::METHOD_PUT,
139+
$url,
140+
['Authorization' => "Bearer $this->accessToken"],
141+
$payload
142+
);
143+
144+
$responseHeaders = $response['headers'] ?? [];
145+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
146+
if ($responseHeadersStatusCode >= 400) {
147+
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}");
148+
}
149+
150+
return $response['body'] ?? [];
110151
}
111152

112153
/**

0 commit comments

Comments
 (0)