feat: add private gh repos
This commit is contained in:
@@ -23,7 +23,7 @@ class Deploy extends Component
|
||||
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->application = Application::where('id', $this->applicationId)->first();
|
||||
$this->destination = $this->application->destination->getMorphClass()::where('id', $this->application->destination->id)->first();
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ class Add extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
||||
@@ -20,7 +20,7 @@ class Show extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
||||
@@ -23,7 +23,7 @@ class Add extends Component
|
||||
];
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
|
||||
142
app/Http/Livewire/Project/New/GithubPrivateRepository.php
Normal file
142
app/Http/Livewire/Project/New/GithubPrivateRepository.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Livewire\Project\New;
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\GithubApp;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Livewire\Component;
|
||||
|
||||
class GithubPrivateRepository extends Component
|
||||
{
|
||||
public $github_apps;
|
||||
public GithubApp $github_app;
|
||||
public $parameters;
|
||||
|
||||
public int $selected_repository_id;
|
||||
public string $selected_repository_owner;
|
||||
public string $selected_repository_repo;
|
||||
|
||||
public string $selected_branch_name = 'main';
|
||||
|
||||
public int $selected_server_id;
|
||||
public int $selected_destination_id;
|
||||
public string $selected_destination_class;
|
||||
|
||||
public string $token;
|
||||
|
||||
protected int $page = 1;
|
||||
|
||||
public $servers;
|
||||
public $destinations;
|
||||
public $repositories;
|
||||
public int $total_repositories_count = 0;
|
||||
|
||||
public $branches;
|
||||
public int $total_branches_count = 0;
|
||||
|
||||
protected function loadRepositoryByPage()
|
||||
{
|
||||
Log::info('Loading page ' . $this->page);
|
||||
$response = Http::withToken($this->token)->get("{$this->github_app->api_url}/installation/repositories?per_page=100&page={$this->page}");
|
||||
$json = $response->json();
|
||||
if ($response->status() !== 200) {
|
||||
return $this->emit('error', $json['message']);
|
||||
}
|
||||
|
||||
if ($json['total_count'] === 0) {
|
||||
return;
|
||||
}
|
||||
$this->total_repositories_count = $json['total_count'];
|
||||
$this->repositories = $this->repositories->concat(collect($json['repositories']));
|
||||
}
|
||||
protected function loadBranchByPage()
|
||||
{
|
||||
Log::info('Loading page ' . $this->page);
|
||||
$response = Http::withToken($this->token)->get("{$this->github_app->api_url}/repos/{$this->selected_repository_owner}/{$this->selected_repository_repo}/branches?per_page=100&page={$this->page}");
|
||||
$json = $response->json();
|
||||
if ($response->status() !== 200) {
|
||||
return $this->emit('error', $json['message']);
|
||||
}
|
||||
|
||||
$this->total_branches_count = count($json);
|
||||
$this->branches = $this->branches->concat(collect($json));
|
||||
}
|
||||
public function loadRepositories($github_app_id)
|
||||
{
|
||||
$this->repositories = collect();
|
||||
$this->page = 1;
|
||||
$this->github_app = GithubApp::where('id', $github_app_id)->first();
|
||||
$this->token = generate_github_token($this->github_app);
|
||||
$this->loadRepositoryByPage();
|
||||
if ($this->repositories->count() < $this->total_repositories_count) {
|
||||
while ($this->repositories->count() < $this->total_repositories_count) {
|
||||
$this->page++;
|
||||
$this->loadRepositoryByPage();
|
||||
}
|
||||
}
|
||||
$this->selected_repository_id = $this->repositories[0]['id'];
|
||||
}
|
||||
public function loadBranches()
|
||||
{
|
||||
$this->selected_repository_owner = $this->repositories->where('id', $this->selected_repository_id)->first()['owner']['login'];
|
||||
$this->selected_repository_repo = $this->repositories->where('id', $this->selected_repository_id)->first()['name'];
|
||||
$this->branches = collect();
|
||||
$this->page = 1;
|
||||
$this->loadBranchByPage();
|
||||
if ($this->total_branches_count === 100) {
|
||||
while ($this->total_branches_count === 100) {
|
||||
$this->page++;
|
||||
$this->loadBranchByPage();
|
||||
}
|
||||
}
|
||||
}
|
||||
public function loadServers()
|
||||
{
|
||||
$this->servers = Server::validated();
|
||||
$this->selected_server_id = $this->servers[0]['id'];
|
||||
}
|
||||
public function loadDestinations()
|
||||
{
|
||||
$server = $this->servers->where('id', $this->selected_server_id)->first();
|
||||
$this->destinations = $server->standaloneDockers->merge($server->swarmDockers);
|
||||
$this->selected_destination_id = $this->destinations[0]['id'];
|
||||
$this->selected_destination_class = $this->destinations[0]->getMorphClass();
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
try {
|
||||
$project = Project::where('uuid', $this->parameters['project_uuid'])->first();
|
||||
$environment = $project->load(['environments'])->environments->where('name', $this->parameters['environment_name'])->first();
|
||||
$application = Application::create([
|
||||
'name' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}:{$this->selected_branch_name}",
|
||||
'git_repository' => "{$this->selected_repository_owner}/{$this->selected_repository_repo}",
|
||||
'git_branch' => $this->selected_branch_name,
|
||||
'build_pack' => 'nixpacks',
|
||||
'ports_exposes' => '3000',
|
||||
'environment_id' => $environment->id,
|
||||
'destination_id' => $this->selected_destination_id,
|
||||
'destination_type' => $this->selected_destination_class,
|
||||
'source_id' => $this->github_app->id,
|
||||
'source_type' => GithubApp::class,
|
||||
]);
|
||||
redirect()->route('project.application.configuration', [
|
||||
'application_uuid' => $application->uuid,
|
||||
'project_uuid' => $project->uuid,
|
||||
'environment_name' => $environment->name
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->emit('error', $e->getMessage());
|
||||
}
|
||||
}
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = saveParameters();
|
||||
$this->repositories = $this->branches = $this->servers = $this->destinations = collect();
|
||||
$this->github_apps = GithubApp::private();
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ class PublicGitRepository extends Component
|
||||
$this->public_repository_url = 'https://github.com/coollabsio/coolify-examples/tree/nodejs-fastify';
|
||||
$this->port = 3000;
|
||||
}
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->servers = session('currentTeam')->load(['servers'])->servers;
|
||||
}
|
||||
public function chooseServer($server)
|
||||
|
||||
@@ -20,7 +20,7 @@ class PrivateKey extends Component
|
||||
}
|
||||
public function mount()
|
||||
{
|
||||
$this->parameters = Route::current()->parameters();
|
||||
$this->parameters = saveParameters();
|
||||
$this->private_keys = ModelsPrivateKey::where('team_id', session('currentTeam')->id)->get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,29 +372,6 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap
|
||||
return implode(' ', $generated_healthchecks_commands);
|
||||
}
|
||||
|
||||
private function generate_jwt_token_for_github()
|
||||
{
|
||||
$signingKey = InMemory::plainText($this->source->privateKey->private_key);
|
||||
$algorithm = new Sha256();
|
||||
$tokenBuilder = (new Builder(new JoseEncoder(), ChainedFormatter::default()));
|
||||
$now = new DateTimeImmutable();
|
||||
$now = $now->setTime($now->format('H'), $now->format('i'));
|
||||
$issuedToken = $tokenBuilder
|
||||
->issuedBy($this->source->app_id)
|
||||
->issuedAt($now)
|
||||
->expiresAt($now->modify('+10 minutes'))
|
||||
->getToken($algorithm, $signingKey)
|
||||
->toString();
|
||||
$token = Http::withHeaders([
|
||||
'Authorization' => "Bearer $issuedToken",
|
||||
'Accept' => 'application/vnd.github.machine-man-preview+json'
|
||||
])->post("{$this->source->api_url}/app/installations/{$this->source->installation_id}/access_tokens");
|
||||
if ($token->failed()) {
|
||||
throw new \Exception("Failed to get access token for $this->application->name from " . $this->source->name . " with error: " . $token->json()['message']);
|
||||
}
|
||||
return $token->json()['token'];
|
||||
}
|
||||
|
||||
private function set_labels_for_applications()
|
||||
{
|
||||
$labels = [];
|
||||
@@ -472,8 +449,7 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap
|
||||
$source_html_url_scheme = $url['scheme'];
|
||||
|
||||
$git_clone_command = "git clone -q -b {$this->application->git_branch}";
|
||||
|
||||
if ($this->application->source->getMorphClass() == 'App\Models\GithubApp') {
|
||||
if ($this->source->getMorphClass() == 'App\Models\GithubApp') {
|
||||
if ($this->source->is_public) {
|
||||
$git_clone_command = "{$git_clone_command} {$this->source->html_url}/{$this->application->git_repository} {$this->workdir}";
|
||||
$git_clone_command = $this->setGitImportSettings($git_clone_command);
|
||||
@@ -481,12 +457,11 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap
|
||||
$this->execute_in_builder($git_clone_command)
|
||||
];
|
||||
} else {
|
||||
if (!$this->application->source->app_id) {
|
||||
$private_key = base64_encode($this->application->source->privateKey->private_key);
|
||||
if (!$this->source->app_id) {
|
||||
$private_key = base64_encode($this->source->privateKey->private_key);
|
||||
|
||||
$git_clone_command = "GIT_SSH_COMMAND=\"ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} git@$source_html_url_host:{$this->application->git_repository}.git {$this->workdir}";
|
||||
$git_clone_command = $this->setGitImportSettings($git_clone_command);
|
||||
|
||||
return [
|
||||
$this->execute_in_builder("mkdir -p /root/.ssh"),
|
||||
$this->execute_in_builder("echo '{$private_key}' | base64 -d > /root/.ssh/id_rsa"),
|
||||
@@ -494,7 +469,7 @@ COPY --from={$this->application->uuid}:{$this->git_commit}-build /app/{$this->ap
|
||||
$this->execute_in_builder($git_clone_command)
|
||||
];
|
||||
} else {
|
||||
$github_access_token = $this->generate_jwt_token_for_github();
|
||||
$github_access_token = generate_github_token($this->source);
|
||||
return [
|
||||
$this->execute_in_builder("git clone -q -b {$this->application->git_branch} $source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$this->application->git_repository}.git {$this->workdir}")
|
||||
];
|
||||
|
||||
@@ -15,4 +15,12 @@ class GithubApp extends BaseModel
|
||||
{
|
||||
return $this->belongsTo(PrivateKey::class);
|
||||
}
|
||||
static public function public()
|
||||
{
|
||||
return GithubApp::where('team_id', session('currentTeam')->id)->where('is_public', true)->get();
|
||||
}
|
||||
static public function private()
|
||||
{
|
||||
return GithubApp::where('team_id', session('currentTeam')->id)->where('is_public', false)->get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user