forked from tangled.org/core
Monorepo for Tangled

appview: pulls: add check for knot caps

Changed files
+48 -2
appview
types
+26
appview/state/pull.go
··· 563 563 return 564 564 } 565 565 566 + us, err := NewUnsignedClient(f.Knot, s.config.Dev) 567 + if err != nil { 568 + log.Println("failed to create unsigned client to %s: %v", f.Knot, err) 569 + s.pages.Notice(w, "pull", "Failed to create a pull request. Try again later.") 570 + return 571 + } 572 + 573 + caps, err := us.Capabilities() 574 + if err != nil { 575 + log.Println("error fetching knot caps", f.Knot, err) 576 + s.pages.Notice(w, "pull", "Failed to create a pull request. Try again later.") 577 + return 578 + } 579 + 566 580 // Determine PR type based on input parameters 567 581 isPushAllowed := f.RepoInfo(s, user).Roles.IsPushAllowed() 568 582 isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == "" ··· 583 597 584 598 // Handle the PR creation based on the type 585 599 if isBranchBased { 600 + if !caps.PullRequests.BranchSubmissions { 601 + s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?") 602 + return 603 + } 586 604 s.handleBranchBasedPull(w, r, f, user, title, body, targetBranch, sourceBranch) 587 605 } else if isForkBased { 606 + if !caps.PullRequests.ForkSubmissions { 607 + s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?") 608 + return 609 + } 588 610 s.handleForkBasedPull(w, r, f, user, fromFork, title, body, targetBranch, sourceBranch) 589 611 } else if isPatchBased { 612 + if !caps.PullRequests.PatchSubmissions { 613 + s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.") 614 + return 615 + } 590 616 s.handlePatchBasedPull(w, r, f, user, title, body, targetBranch, patch) 591 617 } 592 618 return
+13 -2
appview/state/signer.go
··· 351 351 return us.client.Do(req) 352 352 } 353 353 354 - func (us *UnsignedClient) Capabilities(ownerDid, repoName string) (*http.Response, error) { 354 + func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) { 355 355 const ( 356 356 Method = "GET" 357 357 Endpoint = "/capabilities" ··· 362 362 return nil, err 363 363 } 364 364 365 - return us.client.Do(req) 365 + resp, err := us.client.Do(req) 366 + if err != nil { 367 + return nil, err 368 + } 369 + defer resp.Body.Close() 370 + 371 + var capabilities types.Capabilities 372 + if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil { 373 + return nil, err 374 + } 375 + 376 + return &capabilities, nil 366 377 } 367 378 368 379 func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*http.Response, error) {
+9
types/capabilities.go
··· 1 + package types 2 + 3 + type Capabilities struct { 4 + PullRequests struct { 5 + PatchSubmissions bool `json:"patch_submissions"` 6 + BranchSubmissions bool `json:"branch_submissions"` 7 + ForkSubmissions bool `json:"fork_submissions"` 8 + } `json:"pull_requests"` 9 + }