Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions modules/structs/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ type Issue struct {
PullRequest *PullRequestMeta `json:"pull_request"`
Repo *RepositoryMeta `json:"repository"`

PinOrder int `json:"pin_order"`
PinOrder int `json:"pin_order"`
// The version of the issue content for optimistic locking
ContentVersion int `json:"content_version"`
}

Expand Down Expand Up @@ -115,7 +116,8 @@ type EditIssueOption struct {
// swagger:strfmt date-time
Deadline *time.Time `json:"due_date"`
RemoveDeadline *bool `json:"unset_due_date"`
ContentVersion *int `json:"content_version"`
// The current version of the issue content to detect conflicts during editing
ContentVersion *int `json:"content_version"`
}

// EditDeadlineOption options for creating a deadline
Expand Down
6 changes: 4 additions & 2 deletions modules/structs/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ type PullRequest struct {
Closed *time.Time `json:"closed_at"`

// The pin order for the pull request
PinOrder int `json:"pin_order"`
PinOrder int `json:"pin_order"`
// The version of the pull request content for optimistic locking
ContentVersion int `json:"content_version"`
}

Expand Down Expand Up @@ -169,7 +170,8 @@ type EditPullRequestOption struct {
RemoveDeadline *bool `json:"unset_due_date"`
// Whether to allow maintainer edits
AllowMaintainerEdit *bool `json:"allow_maintainer_edit"`
ContentVersion *int `json:"content_version"`
// The current version of the pull request content to detect conflicts during editing
ContentVersion *int `json:"content_version"`
}

// ChangedFile store information about files affected by the pull request
Expand Down
4 changes: 4 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions tests/integration/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,7 @@ func testAPIIssueContentVersion(t *testing.T) {

req := NewRequest(t, "GET", urlStr).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var before api.Issue
DecodeJSON(t, resp, &before)
before := DecodeJSON(t, resp, &api.Issue{})
req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditIssueOption{
Body: new("updated body with correct version"),
ContentVersion: new(before.ContentVersion),
Expand Down
62 changes: 59 additions & 3 deletions tests/integration/api_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,8 @@ func TestAPIEditPull(t *testing.T) {
Base: "master",
Title: title,
}).AddTokenAuth(token)
apiPull := new(api.PullRequest)
resp := MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, apiPull)
apiPull := DecodeJSON(t, resp, &api.PullRequest{})
assert.Equal(t, "master", apiPull.Base.Name)

newTitle := "edit a this pr"
Expand All @@ -470,8 +469,9 @@ func TestAPIEditPull(t *testing.T) {
Body: &newBody,
}).AddTokenAuth(token)
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, apiPull)
apiPull = DecodeJSON(t, resp, &api.PullRequest{})
assert.Equal(t, "feature/1", apiPull.Base.Name)

// check comment history
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: apiPull.ID})
err := pull.LoadIssue(t.Context())
Expand All @@ -483,6 +483,62 @@ func TestAPIEditPull(t *testing.T) {
Base: "not-exist",
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusNotFound)

t.Run("PullContentVersion", func(t *testing.T) {
testAPIPullContentVersion(t, pull.ID)
})
}

func testAPIPullContentVersion(t *testing.T, pullID int64) {
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pullID})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pull.BaseRepoID})
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})

session := loginUser(t, owner.Name)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner.Name, repo.Name, pull.Index)

t.Run("ResponseIncludesContentVersion", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

req := NewRequest(t, "GET", urlStr).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
apiPull := DecodeJSON(t, resp, &api.PullRequest{})
assert.GreaterOrEqual(t, apiPull.ContentVersion, 0)
})

t.Run("EditWithCorrectVersion", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

req := NewRequest(t, "GET", urlStr).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
before := DecodeJSON(t, resp, &api.PullRequest{})
req = NewRequestWithJSON(t, "PATCH", urlStr, api.EditPullRequestOption{
Body: new("updated body with correct version"),
ContentVersion: new(before.ContentVersion),
}).AddTokenAuth(token)
resp = MakeRequest(t, req, http.StatusCreated)
after := DecodeJSON(t, resp, &api.PullRequest{})
assert.Equal(t, "updated body with correct version", after.Body)
assert.Greater(t, after.ContentVersion, before.ContentVersion)
})

t.Run("EditWithWrongVersion", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditPullRequestOption{
Body: new("should fail"),
ContentVersion: new(99999),
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusConflict)
})

t.Run("EditWithoutVersion", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
req := NewRequestWithJSON(t, "PATCH", urlStr, api.EditPullRequestOption{
Body: new("edit without version succeeds"),
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusCreated)
})
}

func doAPIGetPullFiles(ctx APITestContext, pr *api.PullRequest, callback func(*testing.T, []*api.ChangedFile)) func(*testing.T) {
Expand Down