Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/VCS/Adapter/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getType(): string
* @param string $message Commit message
* @return array<mixed> Response from API
*/
abstract public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array;
abstract public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array;

/**
* Create a branch in a repository
Expand All @@ -57,4 +57,17 @@ abstract public function createFile(string $owner, string $repositoryName, strin
* @return array<mixed> Response from API
*/
abstract public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array;

/**
* Create a pull request
*
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $title PR title
* @param string $head Source branch
* @param string $base Target branch
* @param string $body PR description (optional)
* @return array<mixed> Created PR details
*/
abstract public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array;
}
55 changes: 48 additions & 7 deletions src/VCS/Adapter/Git/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,61 @@ public function createRepository(string $owner, string $repositoryName, bool $pr

return $response['body'] ?? [];
}
/**
* Create a pull request
*
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $title PR title
* @param string $head Source branch
* @param string $base Target branch
* @param string $body PR description (optional)
* @return array<mixed> Created PR details
*/
public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array
{
throw new Exception('Not implemented');
}

/**
* Create a file in a repository
*
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $filepath Path where file should be created
* @param string $content Content of the file
* @param string $message Commit message
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $filepath Path where file should be created
* @param string $content Content of the file
* @param string $message Commit message
* @param string $branch Branch to create file on (optional)
* @return array<mixed> Response from API
*/
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array
{
throw new Exception("Not implemented");
$url = "/repos/{$owner}/{$repositoryName}/contents/{$filepath}";

$payload = [
'message' => $message,
'content' => base64_encode($content),
];

// GitHub supports branch parameter
if (! empty($branch)) {
$payload['branch'] = $branch;
}

$response = $this->call(
self::METHOD_PUT,
$url,
['Authorization' => "Bearer $this->accessToken"],
$payload
);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to create file {$filepath}: HTTP {$responseHeadersStatusCode}");
}

return $response['body'] ?? [];
}

/**
Expand Down
131 changes: 121 additions & 10 deletions src/VCS/Adapter/Git/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,25 @@ public function getRepositoryTree(string $owner, string $repositoryName, string
* @param string $message Commit message
* @return array<mixed> Response from API
*/
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array
{
$url = "/repos/{$owner}/{$repositoryName}/contents/{$filepath}";
Comment thread
jaysomani marked this conversation as resolved.

$payload = [
'content' => base64_encode($content),
'message' => $message
];

// Add branch if specified
if (!empty($branch)) {
$payload['branch'] = $branch;
}
Comment thread
Meldiron marked this conversation as resolved.

$response = $this->call(
self::METHOD_POST,
$url,
['Authorization' => "token $this->accessToken"],
[
'content' => base64_encode($content),
'message' => $message
]
$payload
);

$responseHeaders = $response['headers'] ?? [];
Expand Down Expand Up @@ -347,19 +354,100 @@ public function deleteRepository(string $owner, string $repositoryName): bool
return true;
}

/**
* Create a pull request
*
* @param string $owner Owner of the repository
* @param string $repositoryName Name of the repository
* @param string $title PR title
* @param string $head Source branch
* @param string $base Target branch
* @param string $body PR description (optional)
* @return array<mixed> Created PR details
*/
public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array
Comment thread
Meldiron marked this conversation as resolved.
{
$url = "/repos/{$owner}/{$repositoryName}/pulls";

$payload = [
'title' => $title,
'head' => $head,
'base' => $base,
];

if (!empty($body)) {
$payload['body'] = $body;
}

$response = $this->call(
self::METHOD_POST,
$url,
['Authorization' => "token $this->accessToken"],
$payload
);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to create pull request: HTTP {$responseHeadersStatusCode}");
}

$responseBody = $response['body'] ?? [];

return $responseBody;
Comment thread
jaysomani marked this conversation as resolved.
}

public function createComment(string $owner, string $repositoryName, int $pullRequestNumber, string $comment): string
{
throw new Exception("Not implemented yet");
$url = "/repos/{$owner}/{$repositoryName}/issues/{$pullRequestNumber}/comments";

$response = $this->call(self::METHOD_POST, $url, ['Authorization' => "token $this->accessToken"], ['body' => $comment]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to create comment: HTTP {$responseHeadersStatusCode}");
}

$responseBody = $response['body'] ?? [];

if (!array_key_exists('id', $responseBody)) {
throw new Exception("Comment creation response is missing comment ID.");
}

return (string) ($responseBody['id'] ?? '');
}
Comment thread
jaysomani marked this conversation as resolved.

public function getComment(string $owner, string $repositoryName, string $commentId): string
{
throw new Exception("Not implemented yet");
$url = "/repos/{$owner}/{$repositoryName}/issues/comments/{$commentId}";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseBody = $response['body'] ?? [];

return $responseBody['body'] ?? '';
}

public function updateComment(string $owner, string $repositoryName, int $commentId, string $comment): string
{
throw new Exception("Not implemented yet");
$url = "/repos/{$owner}/{$repositoryName}/issues/comments/{$commentId}";

$response = $this->call(self::METHOD_PATCH, $url, ['Authorization' => "token $this->accessToken"], ['body' => $comment]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to update comment: HTTP {$responseHeadersStatusCode}");
}

$responseBody = $response['body'] ?? [];

if (!array_key_exists('id', $responseBody)) {
throw new Exception("Comment update response is missing comment ID.");
}

return (string) ($responseBody['id'] ?? '');
}
Comment thread
jaysomani marked this conversation as resolved.

public function getUser(string $username): array
Expand All @@ -374,12 +462,35 @@ public function getOwnerName(string $installationId): string

public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array
{
throw new Exception("Not implemented yet");
$url = "/repos/{$owner}/{$repositoryName}/pulls/{$pullRequestNumber}";

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to get pull request: HTTP {$responseHeadersStatusCode}");
}

return $response['body'] ?? [];
}

public function getPullRequestFromBranch(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented yet");

$url = "/repos/{$owner}/{$repositoryName}/pulls?state=open&head=" . urlencode($branch);

$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Failed to list pull requests: HTTP {$responseHeadersStatusCode}");
}

$responseBody = $response['body'] ?? [];

return $responseBody[0] ?? [];
}
Comment thread
jaysomani marked this conversation as resolved.

public function listBranches(string $owner, string $repositoryName): array
Expand Down
9 changes: 6 additions & 3 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public function testGetPullRequest(): void

public function testGenerateCloneCommand(): void
{
\exec('rm -rf /tmp/clone-branch');
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', 'test', GitHub::CLONE_TYPE_BRANCH, '/tmp/clone-branch', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
Expand All @@ -358,6 +359,7 @@ public function testGenerateCloneCommand(): void

public function testGenerateCloneCommandWithCommitHash(): void
{
\exec('rm -rf /tmp/clone-commit');
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', '4fb10447faea8a55c5cad7b5ebdfdbedca349fe4', GitHub::CLONE_TYPE_COMMIT, '/tmp/clone-commit', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
Expand All @@ -372,6 +374,7 @@ public function testGenerateCloneCommandWithCommitHash(): void

public function testGenerateCloneCommandWithTag(): void
{
\exec('rm -rf /tmp/clone-tag /tmp/clone-tag2 /tmp/clone-tag3');
$gitCloneCommand = $this->vcsAdapter->generateCloneCommand('test-kh', 'test2', '0.1.0', GitHub::CLONE_TYPE_TAG, '/tmp/clone-tag', '*');
$this->assertNotEmpty($gitCloneCommand);
$this->assertStringContainsString('sparse-checkout', $gitCloneCommand);
Expand Down Expand Up @@ -439,8 +442,8 @@ public function testGetCommit(): void
public function testGetLatestCommit(): void
{
$commitDetails = $this->vcsAdapter->getLatestCommit('test-kh', 'test1', 'test');
$this->assertSame('Khushboo Verma', $commitDetails['commitAuthor']);
$this->assertSame('https://avatars.githubusercontent.com/u/43381712?v=4', $commitDetails['commitAuthorAvatar']);
$this->assertSame('https://github.com/vermakhushboo', $commitDetails['commitAuthorUrl']);
$this->assertSame('appwritedemoapp[bot]', $commitDetails['commitAuthor']);
$this->assertSame('https://avatars.githubusercontent.com/in/287220?v=4', $commitDetails['commitAuthorAvatar']);
$this->assertSame('https://github.com/apps/appwritedemoapp', $commitDetails['commitAuthorUrl']);
}
}
Loading
Loading