This commit is contained in:
Andras Bacsai 2023-05-16 12:17:39 +02:00
parent 0072343e0f
commit ce265e1f91
4 changed files with 97 additions and 12 deletions

View File

@ -2,11 +2,15 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
class GithubApp extends BaseModel
{
protected $fillable = ['name', 'uuid', 'organization', 'api_url', 'html_url', 'custom_user', 'custom_port', 'team_id'];
protected $appends = ['type'];
protected $casts = [
'is_public' => 'boolean',
'type' => 'string'
];
protected static function booted(): void
{
@ -25,6 +29,16 @@ public function privateKey()
{
return $this->belongsTo(PrivateKey::class);
}
public function type(): Attribute
{
return Attribute::make(
get: function () {
if ($this->getMorphClass() === 'App\Models\GithubApp') {
return 'github';
}
},
);
}
static public function public()
{
return GithubApp::where('team_id', session('currentTeam')->id)->where('is_public', true)->get();

View File

@ -190,6 +190,30 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
</div>
</div>
</template>
{{-- Sources --}}
<template x-cloak x-if="sourcesMenu">
<div x-on:click.outside="closeMenus">
<input x-ref="search" x-model="search" class="magic-input" placeholder="Select a source..."
x-on:keyup.escape="closeMenus" x-on:keydown.down="focusNext(sources.length)"
x-on:keydown.up="focusPrev(sources.length)"
x-on:keyup.enter="focusedIndex !== '' && await set('jumpToSource',filteredSources()[focusedIndex])" />
<div class="magic-items">
<template x-if="sources.length === 0">
<div class="magic-item" x-on:click="set('newSource')">
<span>No Source found. Click here to add a new one!</span>
</div>
</template>
<template x-for="(source,index) in filteredSources" :key="source.name ?? source">
<div x-on:click="await set('jumpToSource',source)"
:class="focusedIndex === index && 'magic-item-focused'"
class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
<span class="px-2 mr-1 text-xs bg-purple-700 rounded">Jump</span>
<span x-text="source.name"></span>
</div>
</template>
</div>
</div>
</template>
</div>
<script>
@ -202,7 +226,8 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
!this.environmentMenu &&
!this.projectsMenu &&
!this.destinationsMenu &&
!this.privateKeysMenu
!this.privateKeysMenu &&
!this.sourcesMenu
},
focus() {
if (this.$refs.search) this.$refs.search.focus()
@ -229,6 +254,9 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
this.$watch('privateKeysMenu', () => {
this.focus()
})
this.$watch('sourcesMenu', () => {
this.focus()
})
},
mainMenu: false,
serverMenu: false,
@ -238,6 +266,7 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
projectsMenu: false,
environmentMenu: false,
privateKeysMenu: false,
sourcesMenu: false,
search: '',
selectedAction: '',
@ -251,6 +280,7 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
projects: ['Loading...'],
environments: ['Loading...'],
privateKeys: ['Loading...'],
sources: ['Loading...'],
focusedIndex: "",
items: [{
@ -322,6 +352,12 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
type: 'Jump',
tags: 'private keys,ssh, keys, key',
next: 'privateKeys'
},
{
name: 'Sources',
type: 'Jump',
tags: 'github,apps,source',
next: 'sources'
}
],
focusPrev(maxLength) {
@ -362,6 +398,10 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
this.destinationMenu = false
this.projectMenu = false
this.environmentMenu = false
this.projectsMenu = false
this.destinationsMenu = false
this.privateKeysMenu = false
this.sourcesMenu = false
},
checkMainMenu() {
if (this.serverMenu) return
@ -410,6 +450,13 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
.toLowerCase())
})
},
filteredSources() {
if (this.search === '') return this.sources
return this.sources.filter(source => {
return source.name.toLowerCase().includes(this.search
.toLowerCase())
})
},
async newProject() {
const response = await fetch('/magic?server=' + this.selectedServer +
'&destination=' + this.selectedDestination +
@ -519,6 +566,17 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
this.closeMenus()
this.privateKeysMenu = true
break
case 'sources':
response = await fetch('/magic?sources=true');
if (response.ok) {
const {
sources
} = await response.json();
this.sources = sources;
}
this.closeMenus()
this.sourcesMenu = true
break
case 'environment':
if (this.focusedIndex === 0) {
this.focusedIndex = ''
@ -573,6 +631,10 @@ class="py-2 pl-4 cursor-pointer hover:bg-neutral-700">
window.location = `/private-key/${id}`
this.closeMenus()
break
case 'jumpToSource':
window.location = `/source/${id.type}/${id.uuid}`
this.closeMenus()
break
case 'newServer':
window.location = `/server/new`
this.closeMenus()

View File

@ -23,7 +23,7 @@ class="box">{{ data_get($project, 'name') }}</a>
<p>No servers found.</p>
@endforelse
</div>
<h1>Destinations </h1>
{{-- <h1>Destinations </h1>
<div class="flex gap-2">
@forelse ($destinations as $destination)
<a href="{{ route('destination.show', [$destination->uuid]) }}"
@ -31,8 +31,8 @@ class="box">{{ data_get($destination, 'name') }}</a>
@empty
<p>No destinations found.</p>
@endforelse
</div>
<h1>Private Keys </h1>
</div> --}}
{{-- <h1>Private Keys </h1>
<div class="flex gap-2">
@forelse ($private_keys as $private_key)
<a href="{{ route('private-key.show', [$private_key->uuid]) }}"
@ -40,8 +40,8 @@ class="box">{{ data_get($private_key, 'name') }}</a>
@empty
<p>No servers found.</p>
@endforelse
</div>
<h1>GitHub Apps </h1>
</div> --}}
{{-- <h1>GitHub Apps </h1>
<div class="flex">
@forelse ($github_apps as $github_app)
<a href="{{ route('source.github.show', [$github_app->uuid]) }}"
@ -49,7 +49,7 @@ class="box">{{ data_get($github_app, 'name') }}</a>
@empty
<p>No servers found.</p>
@endforelse
</div>
</div> --}}
@endif
</x-layout>

View File

@ -30,11 +30,12 @@
Route::middleware(['auth'])->group(function () {
Route::get('/magic', function () {
try {
$id = session('currentTeam')->id;
$is_new_project = request()->query('project') === 'new';
$is_new_environment = request()->query('environment') === 'new';
// Get servers
if (request()->query('servers') === 'true') {
$servers = Server::where('team_id', session('currentTeam')->id)->get();
$servers = Server::where('team_id', $id)->get();
return response()->json([
'servers' => $servers,
]);
@ -49,14 +50,22 @@
}
// Get private Keys
if (request()->query('privateKeys') === 'true') {
$privateKeys = PrivateKey::where('team_id', session('currentTeam')->id)->get();
$privateKeys = PrivateKey::where('team_id', $id)->get();
return response()->json([
'privateKeys' => $privateKeys->toArray(),
]);
}
// Get sources
if (request()->query('sources') === 'true') {
$github_apps = GithubApp::private();
$sources = $github_apps;
return response()->json([
'sources' => $sources->toArray(),
]);
}
// Get projects
if ((request()->query('server') && request()->query('destination') && request()->query('projects') === 'true') || request()->query('projects') === 'true') {
$projects = Project::where('team_id', session('currentTeam')->id)->get();
$projects = Project::where('team_id', $id)->get()->sortBy('name');
return response()->json([
'projects' => $projects->toArray(),
]);
@ -64,7 +73,7 @@
// Get environments
if (request()->query('server') && request()->query('destination') && request()->query('project') && request()->query('environments') === 'true') {
$environments = Project::where('team_id', session('currentTeam')->id)->where('uuid', request()->query('project'))->first()->environments;
$environments = Project::where('team_id', $id)->where('uuid', request()->query('project'))->first()->environments;
return response()->json([
'environments' => $environments->toArray(),
]);
@ -73,7 +82,7 @@
if ($is_new_project) {
$project = Project::create([
'name' => request()->query('name') ?? generateRandomName(),
'team_id' => session('currentTeam')->id,
'team_id' => $id,
]);
return response()->json([
'project_uuid' => $project->uuid