@@ -37,6 +38,10 @@
{{ end }}
{{ if and $isPullAuthor $isOpen $isLastRound }}
+ {{ $disabled := "" }}
+ {{ if $isUpToDate }}
+ {{ $disabled = "disabled" }}
+ {{ end }}
diff --git a/appview/pages/templates/repo/pulls/pull.html b/appview/pages/templates/repo/pulls/pull.html
index b8a2608..3415bc3 100644
--- a/appview/pages/templates/repo/pulls/pull.html
+++ b/appview/pages/templates/repo/pulls/pull.html
@@ -135,10 +135,11 @@
{{ if eq $lastIdx .RoundNumber }}
{{ block "mergeStatus" $ }} {{ end }}
+ {{ block "resubmitStatus" $ }} {{ end }}
{{ end }}
{{ if $.LoggedInUser }}
- {{ template "fragments/pullActions" (dict "LoggedInUser" $.LoggedInUser "Pull" $.Pull "RepoInfo" $.RepoInfo "RoundNumber" .RoundNumber "MergeCheck" $.MergeCheck) }}
+ {{ template "fragments/pullActions" (dict "LoggedInUser" $.LoggedInUser "Pull" $.Pull "RepoInfo" $.RepoInfo "RoundNumber" .RoundNumber "MergeCheck" $.MergeCheck "ResubmitCheck" $.ResubmitCheck) }}
{{ else }}
{{ end }}
{{ end }}
+
+{{ define "resubmitStatus" }}
+ {{ if .ResubmitCheck.Yes }}
+
+
+
+ {{ i "triangle-alert" "w-4 h-4" }}
+ this branch has been updated, consider resubmitting
+
+
+ {{ end }}
+{{ end }}
diff --git a/appview/state/pull.go b/appview/state/pull.go
index 605dbb5..7df9257 100644
--- a/appview/state/pull.go
+++ b/appview/state/pull.go
@@ -50,13 +50,18 @@ func (s *State) PullActions(w http.ResponseWriter, r *http.Request) {
}
mergeCheckResponse := s.mergeCheck(f, pull)
+ var resubmitResult pages.ResubmitResult
+ if user.Did == pull.OwnerDid {
+ resubmitResult = s.resubmitCheck(f, pull)
+ }
s.pages.PullActionsFragment(w, pages.PullActionsParams{
- LoggedInUser: user,
- RepoInfo: f.RepoInfo(s, user),
- Pull: pull,
- RoundNumber: roundNumber,
- MergeCheck: mergeCheckResponse,
+ LoggedInUser: user,
+ RepoInfo: f.RepoInfo(s, user),
+ Pull: pull,
+ RoundNumber: roundNumber,
+ MergeCheck: mergeCheckResponse,
+ ResubmitCheck: resubmitResult,
})
return
}
@@ -105,13 +110,18 @@ func (s *State) RepoSinglePull(w http.ResponseWriter, r *http.Request) {
}
mergeCheckResponse := s.mergeCheck(f, pull)
+ var resubmitResult pages.ResubmitResult
+ if user.Did == pull.OwnerDid {
+ resubmitResult = s.resubmitCheck(f, pull)
+ }
s.pages.RepoSinglePull(w, pages.RepoSinglePullParams{
- LoggedInUser: user,
- RepoInfo: f.RepoInfo(s, user),
- DidHandleMap: didHandleMap,
- Pull: pull,
- MergeCheck: mergeCheckResponse,
+ LoggedInUser: user,
+ RepoInfo: f.RepoInfo(s, user),
+ DidHandleMap: didHandleMap,
+ Pull: pull,
+ MergeCheck: mergeCheckResponse,
+ ResubmitCheck: resubmitResult,
})
}
@@ -175,6 +185,48 @@ func (s *State) mergeCheck(f *FullyResolvedRepo, pull *db.Pull) types.MergeCheck
return mergeCheckResponse
}
+func (s *State) resubmitCheck(f *FullyResolvedRepo, pull *db.Pull) pages.ResubmitResult {
+ if pull.State == db.PullMerged {
+ return pages.Unknown
+ }
+
+ if pull.PullSource == nil {
+ return pages.Unknown
+ }
+
+ us, err := NewUnsignedClient(f.Knot, s.config.Dev)
+ if err != nil {
+ log.Printf("failed to setup signed client for %s; ignoring: %v", f.Knot, err)
+ return pages.Unknown
+ }
+
+ resp, err := us.Branch(f.OwnerDid(), f.RepoName, pull.PullSource.Branch)
+ if err != nil {
+ log.Println("failed to reach knotserver", err)
+ return pages.Unknown
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ log.Printf("Error reading response body: %v", err)
+ return pages.Unknown
+ }
+
+ var result types.RepoBranchResponse
+ err = json.Unmarshal(body, &result)
+ if err != nil {
+ log.Println("failed to parse response:", err)
+ return pages.Unknown
+ }
+
+ if pull.Submissions[pull.LastRoundNumber()].SourceRev != result.Branch.Hash {
+ log.Println(pull.Submissions[pull.LastRoundNumber()].SourceRev, result.Branch.Hash)
+ return pages.ShouldResubmit
+ } else {
+ return pages.ShouldNotResubmit
+ }
+}
+
func (s *State) RepoPullPatch(w http.ResponseWriter, r *http.Request) {
user := s.auth.GetUser(r)
f, err := fullyResolvedRepo(r)
@@ -457,16 +509,15 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
sourceBranch := r.FormValue("sourceBranch")
patch := r.FormValue("patch")
- if patch == "" {
- if isPushAllowed && sourceBranch == "" {
- s.pages.Notice(w, "pull", "Neither source branch nor patch supplied.")
- return
- }
- s.pages.Notice(w, "pull", "Patch is empty.")
+ isBranchBased := isPushAllowed && (sourceBranch != "")
+ isPatchBased := patch != ""
+
+ if !isBranchBased && !isPatchBased {
+ s.pages.Notice(w, "pull", "Neither source branch nor patch supplied.")
return
}
- if patch != "" && sourceBranch != "" {
+ if isBranchBased && isPatchBased {
s.pages.Notice(w, "pull", "Cannot select both patch and source branch.")
return
}
@@ -477,11 +528,16 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
}
// TODO: check if knot has this capability
+ var sourceRev string
var pullSource *db.PullSource
- if sourceBranch != "" && isPushAllowed {
+ var recordPullSource *tangled.RepoPull_Source
+ if isBranchBased {
pullSource = &db.PullSource{
Branch: sourceBranch,
}
+ recordPullSource = &tangled.RepoPull_Source{
+ Branch: sourceBranch,
+ }
// generate a patch using /compare
ksClient, err := NewUnsignedClient(f.Knot, s.config.Dev)
if err != nil {
@@ -490,8 +546,6 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
return
}
- log.Println(targetBranch, sourceBranch)
-
resp, err := ksClient.Compare(f.OwnerDid(), f.RepoName, targetBranch, sourceBranch)
switch resp.StatusCode {
case 404:
@@ -510,15 +564,13 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(respBody, &diffTreeResponse)
if err != nil {
log.Println("failed to unmarshal diff tree response", err)
- log.Println(string(respBody))
s.pages.Notice(w, "pull", "Failed to create pull request. Try again later.")
}
+ sourceRev = diffTreeResponse.DiffTree.Rev2
patch = diffTreeResponse.DiffTree.Patch
}
- log.Println(patch)
-
// Validate patch format
if !isPatchValid(patch) {
s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.")
@@ -535,7 +587,8 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
rkey := s.TID()
initialSubmission := db.PullSubmission{
- Patch: patch,
+ Patch: patch,
+ SourceRev: sourceRev,
}
err = db.NewPull(tx, &db.Pull{
Title: title,
@@ -573,6 +626,7 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
TargetRepo: string(f.RepoAt),
TargetBranch: targetBranch,
Patch: patch,
+ Source: recordPullSource,
},
},
})
@@ -613,12 +667,17 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
return
case http.MethodPost:
patch := r.FormValue("patch")
+ var sourceRev string
+ var recordPullSource *tangled.RepoPull_Source
// this pull is a branch based pull
isPushAllowed := f.RepoInfo(s, user).Roles.IsPushAllowed()
if pull.IsSameRepoBranch() && isPushAllowed {
sourceBranch := pull.PullSource.Branch
targetBranch := pull.TargetBranch
+ recordPullSource = &tangled.RepoPull_Source{
+ Branch: sourceBranch,
+ }
// extract patch by performing compare
ksClient, err := NewUnsignedClient(f.Knot, s.config.Dev)
if err != nil {
@@ -627,8 +686,6 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
return
}
- log.Println(targetBranch, sourceBranch)
-
resp, err := ksClient.Compare(f.OwnerDid(), f.RepoName, targetBranch, sourceBranch)
switch resp.StatusCode {
case 404:
@@ -647,10 +704,10 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(respBody, &diffTreeResponse)
if err != nil {
log.Println("failed to unmarshal diff tree response", err)
- log.Println(string(respBody))
s.pages.Notice(w, "pull", "Failed to create pull request. Try again later.")
}
+ sourceRev = diffTreeResponse.DiffTree.Rev2
patch = diffTreeResponse.DiffTree.Patch
}
@@ -664,6 +721,11 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
return
}
+ if sourceRev == pull.Submissions[pull.LastRoundNumber()].SourceRev {
+ s.pages.Notice(w, "resubmit-error", "This branch has not changed since the last submission.")
+ return
+ }
+
// Validate patch format
if !isPatchValid(patch) {
s.pages.Notice(w, "resubmit-error", "Invalid patch format. Please provide a valid diff.")
@@ -678,7 +740,7 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
}
defer tx.Rollback()
- err = db.ResubmitPull(tx, pull, patch)
+ err = db.ResubmitPull(tx, pull, patch, sourceRev)
if err != nil {
log.Println("failed to create pull request", err)
s.pages.Notice(w, "resubmit-error", "Failed to create pull request. Try again later.")
@@ -705,6 +767,7 @@ func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
TargetRepo: string(f.RepoAt),
TargetBranch: pull.TargetBranch,
Patch: patch, // new patch
+ Source: recordPullSource,
},
},
})
diff --git a/appview/state/signer.go b/appview/state/signer.go
index ca9984b..4aa7493 100644
--- a/appview/state/signer.go
+++ b/appview/state/signer.go
@@ -287,6 +287,21 @@ func (us *UnsignedClient) Branches(ownerDid, repoName string) (*http.Response, e
return us.client.Do(req)
}
+func (us *UnsignedClient) Branch(ownerDid, repoName, branch string) (*http.Response, error) {
+ const (
+ Method = "GET"
+ )
+
+ endpoint := fmt.Sprintf("/%s/%s/branches/%s", ownerDid, repoName, branch)
+
+ req, err := us.newRequest(Method, endpoint, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ return us.client.Do(req)
+}
+
func (us *UnsignedClient) DefaultBranch(ownerDid, repoName string) (*http.Response, error) {
const (
Method = "GET"
diff --git a/knotserver/git/diff.go b/knotserver/git/diff.go
index 519f346..59db755 100644
--- a/knotserver/git/diff.go
+++ b/knotserver/git/diff.go
@@ -97,8 +97,6 @@ func (g *GitRepo) DiffTree(rev1, rev2 string) (*types.DiffTree, error) {
return nil, fmt.Errorf("Invalid revision: %s", rev2)
}
- log.Println(commit1, commit2)
-
tree1, err := commit1.Tree()
if err != nil {
return nil, err
diff --git a/knotserver/git/git.go b/knotserver/git/git.go
index d223bae..8bc3e47 100644
--- a/knotserver/git/git.go
+++ b/knotserver/git/git.go
@@ -238,6 +238,19 @@ func (g *GitRepo) Branches() ([]*plumbing.Reference, error) {
return branches, nil
}
+func (g *GitRepo) Branch(name string) (*plumbing.Reference, error) {
+ ref, err := g.r.Reference(plumbing.NewBranchReferenceName(name), false)
+ if err != nil {
+ return nil, fmt.Errorf("branch: %w", err)
+ }
+
+ if !ref.Name().IsBranch() {
+ return nil, fmt.Errorf("branch: %s is not a branch", ref.Name())
+ }
+
+ return ref, nil
+}
+
func (g *GitRepo) SetDefaultBranch(branch string) error {
ref := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(branch))
return g.r.Storer.SetReference(ref)
diff --git a/knotserver/handler.go b/knotserver/handler.go
index e49a52d..84a8062 100644
--- a/knotserver/handler.go
+++ b/knotserver/handler.go
@@ -106,6 +106,7 @@ func Setup(ctx context.Context, c *config.Config, db *db.DB, e *rbac.Enforcer, j
r.Get("/tags", h.Tags)
r.Route("/branches", func(r chi.Router) {
r.Get("/", h.Branches)
+ r.Get("/{branch}", h.Branch)
r.Route("/default", func(r chi.Router) {
r.Get("/", h.DefaultBranch)
r.With(h.VerifySignature).Put("/", h.SetDefaultBranch)
diff --git a/knotserver/routes.go b/knotserver/routes.go
index f77cc0e..5fa7a52 100644
--- a/knotserver/routes.go
+++ b/knotserver/routes.go
@@ -456,6 +456,37 @@ func (h *Handle) Branches(w http.ResponseWriter, r *http.Request) {
return
}
+func (h *Handle) Branch(w http.ResponseWriter, r *http.Request) {
+ path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
+ branchName := chi.URLParam(r, "branch")
+ l := h.l.With("handler", "Branch")
+
+ gr, err := git.PlainOpen(path)
+ if err != nil {
+ notFound(w)
+ return
+ }
+
+ ref, err := gr.Branch(branchName)
+ if err != nil {
+ l.Error("getting branches", "error", err.Error())
+ writeError(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ resp := types.RepoBranchResponse{
+ Branch: types.Branch{
+ Reference: types.Reference{
+ Name: ref.Name().Short(),
+ Hash: ref.Hash().String(),
+ },
+ },
+ }
+
+ writeJSON(w, resp)
+ return
+}
+
func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {
l := h.l.With("handler", "Keys")
diff --git a/types/repo.go b/types/repo.go
index 211f5cb..0e0ed9e 100644
--- a/types/repo.go
+++ b/types/repo.go
@@ -67,6 +67,10 @@ type RepoBranchesResponse struct {
Branches []Branch `json:"branches,omitempty"`
}
+type RepoBranchResponse struct {
+ Branch Branch `json:"branch,omitempty"`
+}
+
type RepoDefaultBranchResponse struct {
Branch string `json:"branch,omitempty"`
}