Skip to content

Commit 3bb14cb

Browse files
author
Your Name
committed
updated and fixed linting
1 parent 724008f commit 3bb14cb

File tree

5 files changed

+162
-10
lines changed

5 files changed

+162
-10
lines changed

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
/**

src/VCS/Adapter/Git/Gitea.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,21 @@ public function getPullRequestFromBranch(string $owner, string $repositoryName,
468468
$url = "/repos/{$owner}/{$repositoryName}/pulls?state=open&sort=recentupdate";
469469

470470
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
471+
$responseHeaders = $response['headers'] ?? [];
472+
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
473+
if ($responseHeadersStatusCode >= 400) {
474+
throw new Exception("Failed to list pull requests: HTTP {$responseHeadersStatusCode}");
475+
}
471476

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

474479
// Filter by head branch (source branch of the PR)
475480
foreach ($responseBody as $pr) {
476-
$prHead = $pr['head'] ?? [];
477-
$prHeadRef = $prHead['ref'] ?? '';
481+
if (! is_array($pr) || ! isset($pr['head']['ref'])) {
482+
continue;
483+
}
484+
485+
$prHeadRef = $pr['head']['ref'];
478486
if ($prHeadRef === $branch) {
479487
return $pr;
480488
}

tests/VCS/Adapter/GitHubTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,39 @@ public function testGetLatestCommit(): void
443443
$this->assertSame('https://avatars.githubusercontent.com/u/43381712?v=4', $commitDetails['commitAuthorAvatar']);
444444
$this->assertSame('https://github.com/vermakhushboo', $commitDetails['commitAuthorUrl']);
445445
}
446+
447+
public function test_create_file(): void
448+
{
449+
$owner = 'test-kh';
450+
$repositoryName = 'test1';
451+
452+
$result = $this->vcsAdapter->createFile(
453+
$owner,
454+
$repositoryName,
455+
'test-create-'.\uniqid().'.md',
456+
'# Test File',
457+
'Test file creation'
458+
);
459+
460+
$this->assertIsArray($result);
461+
$this->assertNotEmpty($result);
462+
}
463+
464+
public function test_create_file_on_branch(): void
465+
{
466+
$owner = 'test-kh';
467+
$repositoryName = 'test1';
468+
469+
$result = $this->vcsAdapter->createFile(
470+
$owner,
471+
$repositoryName,
472+
'test-branch-'.\uniqid().'.md',
473+
'# Test Branch File',
474+
'Test file on branch',
475+
'test'
476+
);
477+
478+
$this->assertIsArray($result);
479+
$this->assertNotEmpty($result);
480+
}
446481
}

tests/VCS/Adapter/GiteaTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,61 @@ public function testCreateComment(): void
704704
$this->markTestSkipped('Will be implemented in follow-up PR');
705705
}
706706

707+
public function test_create_file(): void
708+
{
709+
$repositoryName = 'test-create-file-'.\uniqid();
710+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
711+
712+
try {
713+
$result = $this->vcsAdapter->createFile(
714+
self::$owner,
715+
$repositoryName,
716+
'test.md',
717+
'# Test',
718+
'Add test file'
719+
);
720+
721+
$this->assertIsArray($result);
722+
$this->assertNotEmpty($result);
723+
} finally {
724+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
725+
}
726+
}
727+
728+
public function test_create_file_on_branch(): void
729+
{
730+
$repositoryName = 'test-create-file-branch-'.\uniqid();
731+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
732+
733+
try {
734+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Main');
735+
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature', 'main');
736+
737+
// Create file on specific branch
738+
$result = $this->vcsAdapter->createFile(
739+
self::$owner,
740+
$repositoryName,
741+
'feature.md',
742+
'# Feature',
743+
'Add feature file',
744+
'feature' // ← Branch parameter
745+
);
746+
747+
$this->assertIsArray($result);
748+
749+
// Verify it's on the right branch
750+
$content = $this->vcsAdapter->getRepositoryContent(
751+
self::$owner,
752+
$repositoryName,
753+
'feature.md',
754+
'feature'
755+
);
756+
$this->assertSame('# Feature', $content['content']);
757+
} finally {
758+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
759+
}
760+
}
761+
707762
public function testListBranches(): void
708763
{
709764
$this->markTestSkipped('Will be implemented in follow-up PR');

0 commit comments

Comments
 (0)