From 91eb5822e84959ec1989c08c1230adaefb169832 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Mon, 14 Feb 2022 09:38:03 -0500 Subject: [PATCH] gitsrht: Indicate if a clone is in progress --- api/clones/middleware.go | 19 +++++++++- api/graph/schema.resolvers.go | 13 ++++--- ...ca3_add_clone_in_progress_to_repository.py | 30 +++++++++++++++ gitsrht/templates/empty-repo.html | 37 +++++++++++++++++++ gitsrht/types/__init__.py | 2 + 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py create mode 100644 gitsrht/templates/empty-repo.html diff --git a/api/clones/middleware.go b/api/clones/middleware.go index 911fc58..c4ea888 100644 --- a/api/clones/middleware.go +++ b/api/clones/middleware.go @@ -2,9 +2,11 @@ package clones import ( "context" + "database/sql" "log" "net/http" + "git.sr.ht/~sircmpwn/core-go/database" work "git.sr.ht/~sircmpwn/dowork" "github.com/go-git/go-git/v5" ) @@ -34,12 +36,27 @@ func Middleware(queue *ClonesQueue) func(next http.Handler) http.Handler { } // Schedules a clone. -func Schedule(ctx context.Context, repo *git.Repository, cloneURL string) { +func Schedule(ctx context.Context, repoID int, repo *git.Repository, cloneURL string) { queue, ok := ctx.Value(clonesCtxKey).(*ClonesQueue) if !ok { panic("No clones worker for this context") } task := work.NewTask(func(ctx context.Context) error { + defer func() { + err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { + _, err := tx.Exec( + `UPDATE repository SET clone_in_progress = false WHERE id = $1;`, + repoID, + ) + if err != nil { + return err + } + return nil + }) + if err != nil { + panic(err) + } + }() err := repo.Clone(ctx, &git.CloneOptions{ URL: cloneURL, RecurseSubmodules: git.NoRecurseSubmodules, diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index 13e4502..f5277f9 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -128,17 +128,20 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi panic(fmt.Errorf("Unknown visibility %s", visibility)) // Invariant } + cloneInProgress := cloneURL != nil + row := tx.QueryRowContext(ctx, ` INSERT INTO repository ( - created, updated, name, description, path, visibility, owner_id + created, updated, name, description, path, visibility, owner_id, + clone_in_progress ) VALUES ( NOW() at time zone 'utc', NOW() at time zone 'utc', - $1, $2, $3, $4, $5 + $1, $2, $3, $4, $5, $6 ) RETURNING id, created, updated, name, description, visibility, upstream_uri, path, owner_id; - `, name, description, repoPath, dvis, user.UserID) + `, name, description, repoPath, dvis, user.UserID, cloneInProgress) if err := row.Scan(&repo.ID, &repo.Created, &repo.Updated, &repo.Name, &repo.Description, &repo.RawVisibility, &repo.UpstreamURL, &repo.Path, &repo.OwnerID); err != nil { @@ -177,7 +180,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi } } - if cloneURL != nil { + if cloneInProgress { u, err := url.Parse(*cloneURL) if err != nil { return valid.Errorf(ctx, "cloneUrl", "Invalid clone URL: %s", err) @@ -217,7 +220,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi cloneURL = &repo.Path } - clones.Schedule(ctx, gitrepo, *cloneURL) + clones.Schedule(ctx, repo.ID, gitrepo, *cloneURL) } webhooks.DeliverRepoEvent(ctx, model.WebhookEventRepoCreated, &repo) diff --git a/gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py b/gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py new file mode 100644 index 0000000..124739e --- /dev/null +++ b/gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py @@ -0,0 +1,30 @@ +"""Add clone_in_progress to repository + +Revision ID: 5f59f2639ca3 +Revises: a4488cc1e42b +Create Date: 2022-02-10 13:24:27.859764 + +""" + +# revision identifiers, used by Alembic. +revision = '5f59f2639ca3' +down_revision = 'a4488cc1e42b' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.execute(""" + ALTER TABLE repository + ADD COLUMN clone_in_progress boolean NOT NULL DEFAULT false; + ALTER TABLE repository + ALTER COLUMN clone_in_progress DROP DEFAULT; + """) + + +def downgrade(): + op.execute(""" + ALTER TABLE repository + DROP COLUMN clone_in_progress; + """) diff --git a/gitsrht/templates/empty-repo.html b/gitsrht/templates/empty-repo.html new file mode 100644 index 0000000..154219a --- /dev/null +++ b/gitsrht/templates/empty-repo.html @@ -0,0 +1,37 @@ +{% extends "repo.html" %} +{% block repohead %} + +{% endblock %} + +{% block content %} +{% if repo.description %} +
+
{{ repo.description }}
+
+{% endif %} +
+
+
+

clone

+
+ {% for url in clone_urls %} +
{{url.desc}}
+ {% if url.link %}
{{url.url}}
+ {% else %}
{{url.url}}
+ {% endif -%} + {% endfor -%} +
+
+
+ {% if repo.clone_in_progress %} +
+ A clone operation is currently in progress. +
+ {% endif %} +

+ Nothing here yet! +

+
+
+
+{% endblock %} diff --git a/gitsrht/types/__init__.py b/gitsrht/types/__init__.py index c9f6967..4a75b55 100644 --- a/gitsrht/types/__init__.py +++ b/gitsrht/types/__init__.py @@ -22,6 +22,8 @@ class Redirect(Base, BaseRedirectMixin): class Repository(Base, BaseRepositoryMixin): _git_repo = None + clone_in_progress = sa.Column(sa.Boolean, nullable=False) + # Must match gitsrht-update-hook/post-update.go#updateRepoVisibility() def update_visibility(self): if not os.path.exists(self.path): -- 2.38.4