Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
Go
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273package api
import ( "errors" "fmt" "net/http" "net/url" "strings"
"tangled.org/core/api/org_tangled" "tangled.org/core/migrator/git" xrpcerr "tangled.org/core/xrpc/errors")
func invalidSource(message string) xrpcerr.XrpcError { return xrpcerr.NewXrpcError( xrpcerr.WithTag("InvalidSource"), xrpcerr.WithMessage(message), )}
func unreachableSource(message string) xrpcerr.XrpcError { return xrpcerr.NewXrpcError( xrpcerr.WithTag("UnreachableSource"), xrpcerr.WithMessage(message), )}
func RepoNameFromURL(urlStr string) (string, error) { u, err := url.Parse(urlStr) if err != nil { return "", fmt.Errorf("malformed URL: %w", err) } segments := strings.Split(strings.Trim(u.Path, "/"), "/") name := strings.TrimSuffix(segments[len(segments)-1], ".git") if name == "" { return "", errors.New("source URL must name a repository path") } return name, nil}
func (s *Server) handleDescribeSource(w http.ResponseWriter, r *http.Request) { if _, ok := actorDid(r); !ok { s.writeError(w, missingActorError, http.StatusUnauthorized) return }
sourceURL := r.URL.Query().Get("sourceUrl") if sourceURL == "" { s.writeError(w, invalidRequest("sourceUrl is required"), http.StatusBadRequest) return } if err := ValidateSourceURL(sourceURL); err != nil { s.writeError(w, invalidSource(err.Error()), http.StatusBadRequest) return }
name, err := RepoNameFromURL(sourceURL) if err != nil { s.writeError(w, invalidSource(err.Error()), http.StatusBadRequest) return }
branch, err := git.RemoteHead(r.Context(), s.gitRunnerOrDefault(), s.cfg.WorkDir, s.proxyAddr, sourceURL) if err != nil { s.logger.Info("describeSource could not read the repository", "sourceUrl", sourceURL, "err", err) s.writeError(w, unreachableSource("could not read the repository: "+err.Error()), http.StatusBadRequest) return }
s.writeJSON(w, org_tangled.TempMigratorDescribeSource_Output{Name: name, DefaultBranch: branch}, http.StatusOK)}