Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
129 changes: 119 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
Comment thread
jaysomani marked this conversation as resolved.
* @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,88 @@ 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]);

$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]);

$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 +450,45 @@ 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&sort=recentupdate";

$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}");
}

Comment thread
jaysomani marked this conversation as resolved.
$responseBody = $response['body'] ?? [];

// Filter by head branch (source branch of the PR)
foreach ($responseBody as $pr) {
if (! is_array($pr) || ! isset($pr['head']['ref'])) {
continue;
}

$prHeadRef = $pr['head']['ref'];
if ($prHeadRef === $branch) {
return $pr;
}
}

return [];
}
Comment thread
jaysomani marked this conversation as resolved.

public function listBranches(string $owner, string $repositoryName): array
Expand Down
35 changes: 35 additions & 0 deletions tests/VCS/Adapter/GitHubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,39 @@ public function testGetLatestCommit(): void
$this->assertSame('https://avatars.githubusercontent.com/u/43381712?v=4', $commitDetails['commitAuthorAvatar']);
$this->assertSame('https://github.com/vermakhushboo', $commitDetails['commitAuthorUrl']);
}

public function test_create_file(): void
{
$owner = 'test-kh';
$repositoryName = 'test1';

$result = $this->vcsAdapter->createFile(
$owner,
$repositoryName,
'test-create-'.\uniqid().'.md',
'# Test File',
'Test file creation'
);

$this->assertIsArray($result);
$this->assertNotEmpty($result);
}

public function test_create_file_on_branch(): void
Comment thread
Meldiron marked this conversation as resolved.
Outdated
{
$owner = 'test-kh';
$repositoryName = 'test1';

$result = $this->vcsAdapter->createFile(
$owner,
$repositoryName,
'test-branch-'.\uniqid().'.md',
'# Test Branch File',
'Test file on branch',
'test'
);

$this->assertIsArray($result);
$this->assertNotEmpty($result);
}
}
Loading
Loading