forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview: pulls: add check for knot caps

Changed files
+48 -2
appview
types
+26
appview/state/pull.go
···
return
}
+
us, err := NewUnsignedClient(f.Knot, s.config.Dev)
+
if err != nil {
+
log.Println("failed to create unsigned client to %s: %v", f.Knot, err)
+
s.pages.Notice(w, "pull", "Failed to create a pull request. Try again later.")
+
return
+
}
+
+
caps, err := us.Capabilities()
+
if err != nil {
+
log.Println("error fetching knot caps", f.Knot, err)
+
s.pages.Notice(w, "pull", "Failed to create a pull request. Try again later.")
+
return
+
}
+
// Determine PR type based on input parameters
isPushAllowed := f.RepoInfo(s, user).Roles.IsPushAllowed()
isBranchBased := isPushAllowed && sourceBranch != "" && fromFork == ""
···
// Handle the PR creation based on the type
if isBranchBased {
+
if !caps.PullRequests.BranchSubmissions {
+
s.pages.Notice(w, "pull", "This knot doesn't support branch-based pull requests. Try another way?")
+
return
+
}
s.handleBranchBasedPull(w, r, f, user, title, body, targetBranch, sourceBranch)
} else if isForkBased {
+
if !caps.PullRequests.ForkSubmissions {
+
s.pages.Notice(w, "pull", "This knot doesn't support fork-based pull requests. Try another way?")
+
return
+
}
s.handleForkBasedPull(w, r, f, user, fromFork, title, body, targetBranch, sourceBranch)
} else if isPatchBased {
+
if !caps.PullRequests.PatchSubmissions {
+
s.pages.Notice(w, "pull", "This knot doesn't support patch-based pull requests. Send your patch over email.")
+
return
+
}
s.handlePatchBasedPull(w, r, f, user, title, body, targetBranch, patch)
}
return
+13 -2
appview/state/signer.go
···
return us.client.Do(req)
}
-
func (us *UnsignedClient) Capabilities(ownerDid, repoName string) (*http.Response, error) {
+
func (us *UnsignedClient) Capabilities() (*types.Capabilities, error) {
const (
Method = "GET"
Endpoint = "/capabilities"
···
return nil, err
}
-
return us.client.Do(req)
+
resp, err := us.client.Do(req)
+
if err != nil {
+
return nil, err
+
}
+
defer resp.Body.Close()
+
+
var capabilities types.Capabilities
+
if err := json.NewDecoder(resp.Body).Decode(&capabilities); err != nil {
+
return nil, err
+
}
+
+
return &capabilities, nil
}
func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*http.Response, error) {
+9
types/capabilities.go
···
+
package types
+
+
type Capabilities struct {
+
PullRequests struct {
+
PatchSubmissions bool `json:"patch_submissions"`
+
BranchSubmissions bool `json:"branch_submissions"`
+
ForkSubmissions bool `json:"fork_submissions"`
+
} `json:"pull_requests"`
+
}