Skip to content
quarry Artifact [844fc151dd]
Login

Artifact [844fc151dd]

Artifact 844fc151dd3d8fcf9c48596813e0db26a078abbf12c03c939d369e304019c57a:


package api

import (
	"context"
	"errors"
	"fmt"
	"reflect"
	"strings"

	"craftdesign.group/quarry/internal/ops"
)

// canonRepo is the repository key a name stands for. URLs name a
// repository OWNER/NAME (RFC 0025) while the forge keys it by the bare
// key, and a token or grant that kept the URL's spelling reached
// nothing. A key is itself; OWNER/NAME is the key at that address; any
// other name is left as given, for the caller's own checks to refuse.
// It maps spellings only: whether the caller may read the repository is
// asked after, by whoever needs the answer.
func (d Deps) canonRepo(name string) string {
	name = strings.TrimSpace(name)
	i := strings.Index(name, "/")
	if i <= 0 || i == len(name)-1 || d.Repos == nil {
		return name
	}
	if _, ok := d.Repos.Get(name); ok {
		return name
	}
	ns, handle := name[:i], name[i+1:]
	if d.AddressBook != nil {
		for key, a := range d.AddressBook() {
			if a[0] == ns && a[1] == handle {
				return key
			}
		}
		return name
	}
	if d.Address != nil {
		for _, key := range d.Repos.Names() {
			if n, h, ok := d.Address(key); ok && n == ns && h == handle {
				return key
			}
		}
	}
	return name
}

// repoKeys resolves the repositories a token or grant is to name, each
// as OWNER/NAME or NAME, to their keys, in order and without repeats.
// A name that is no repository, or one the caller cannot read, is
// refused alike with the words a missing repository gets, so asking
// cannot probe for private ones.
func (d Deps) repoKeys(ctx context.Context, names []string) ([]string, error) {
	out := make([]string, 0, len(names))
	seen := map[string]bool{}
	for _, n := range names {
		key := d.canonRepo(n)
		if _, err := d.repoFor(ctx, key); err != nil && !errors.Is(err, ops.ErrStarting) {
			return nil, fmt.Errorf("no repository %q: %w", strings.TrimSpace(n), ops.ErrInvalid)
		}
		if !seen[key] {
			seen[key] = true
			out = append(out, key)
		}
	}
	return out, nil
}

// canonAsked is an asked call's input with the repositories it names
// spelled as keys, for the ops that name repositories for a token or a
// grant. Unknown names stay as sent: the checks that follow refuse them
// as they refuse them on a plain call.
func (d Deps) canonAsked(op string, in any) any {
	switch op {
	case "agent.token.new", "agent.token.repos", "agent.grant", setupOp:
	default:
		return in
	}
	v := reflect.ValueOf(in)
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	if v.Kind() != reflect.Struct {
		return in
	}
	c := reflect.New(v.Type()).Elem()
	c.Set(v)
	if f := c.FieldByName("NewRepo"); f.IsValid() && f.Kind() == reflect.Bool && f.Bool() {
		return in
	}
	if f := c.FieldByName("Repos"); f.IsValid() && f.Kind() == reflect.String {
		parts := splitList(f.String())
		changed := false
		for i := range parts {
			if key := d.canonRepo(parts[i]); key != parts[i] {
				parts[i], changed = key, true
			}
		}
		if changed {
			f.SetString(strings.Join(parts, ","))
		}
	}
	if f := c.FieldByName("Repositories"); f.IsValid() && f.Kind() == reflect.Slice {
		for i := 0; i < f.Len(); i++ {
			f.Index(i).SetString(d.canonRepo(f.Index(i).String()))
		}
	}
	if f := c.FieldByName("Repo"); f.IsValid() && f.Kind() == reflect.String {
		f.SetString(d.canonRepo(f.String()))
	}
	if v.Type() == reflect.TypeOf(in) {
		return c.Interface()
	}
	p := reflect.New(v.Type())
	p.Elem().Set(c)
	return p.Interface()
}

// canonAll is canonRepo over a list.
func (d Deps) canonAll(names []string) []string {
	out := make([]string, len(names))
	for i, n := range names {
		out[i] = d.canonRepo(n)
	}
	return out
}