~edwargix/git.sr.ht

91eb5822e84959ec1989c08c1230adaefb169832 — Adnan Maolood 2 years ago 115487d
gitsrht: Indicate if a clone is in progress
M api/clones/middleware.go => api/clones/middleware.go +18 -1
@@ 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,

M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +8 -5
@@ 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)

A gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py => gitsrht/alembic/versions/5f59f2639ca3_add_clone_in_progress_to_repository.py +30 -0
@@ 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;
    """)

A gitsrht/templates/empty-repo.html => gitsrht/templates/empty-repo.html +37 -0
@@ 0,0 1,37 @@
{% extends "repo.html" %}
{% block repohead %}
<meta http-equiv="refresh" content="5">
{% endblock %}

{% block content %}
{% if repo.description %}
<div class="header-extension">
  <div class="container">{{ repo.description }}</div>
</div>
{% endif %}
<div class="container">
  <div class="row" style="margin-bottom: 1rem">
    <div class="col-md-4">
      <h3>clone</h3>
      <dl>
        {% for url in clone_urls %}
        <dt>{{url.desc}}</dt>
        {% if url.link %}<dd><a href="{{url.url}}">{{url.url}}</a></dd>
        {% else %}<dd>{{url.url}}</dd>
        {% endif -%}
        {% endfor -%}
      </dl>
    </div>
    <div class="col-md-8">
      {% if repo.clone_in_progress %}
      <div class="alert alert-primary">
        A clone operation is currently in progress.
      </div>
      {% endif %}
      <p>
        Nothing here yet!
      </p>
    </div>
  </div>
</div>
{% endblock %}

M gitsrht/types/__init__.py => gitsrht/types/__init__.py +2 -0
@@ 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):