Skip to content

Commit

Permalink
Fix checks in PR for empty commits #19603
Browse files Browse the repository at this point in the history
* Fixes issue #19603
* fill HeadCommitID in PullRequest
* compare real commits ID as check for merging
* basd on zeripath patch
  • Loading branch information
jedi7 committed Jul 10, 2022
1 parent c273dea commit d686e09
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
28 changes: 27 additions & 1 deletion integrations/pull_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func doAPICreateCommitStatus(ctx APITestContext, commitID string, status api.Com
}
}

func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
// Merge must continue if commits SHA are different, even if content is same
// Reason: gitflow and merging master back into develop, where is high possiblity, there are no changes
// but just commit saying "Merge branch". And this meta commit can be also tagged,
// so we need to have this meta commit also in develop branch.
onGiteaRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
Expand All @@ -125,6 +129,28 @@ func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)

text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
assert.Contains(t, text, "This pull request can be merged automatically.")
})
}

func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
testCreateBranch(t, session, "user1", "repo1", "branch/master", "status1", http.StatusSeeOther)
url := path.Join("user1", "repo1", "compare", "master...status1")
req := NewRequestWithValues(t, "POST", url,
map[string]string{
"_csrf": GetCSRF(t, session, url),
"title": "pull request from status1",
},
)
session.MakeRequest(t, req, http.StatusSeeOther)
req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
resp := session.MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)

text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
assert.Contains(t, text, "This branch is equal with the target branch.")
})
Expand Down
5 changes: 5 additions & 0 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ func (pr *PullRequest) IsEmpty() bool {
return pr.Status == PullRequestStatusEmpty
}

// IsAncestor returns true if the Head Commit of this PR is an ancestor of the Base Commit
func (pr *PullRequest) IsAncestor() bool {
return pr.HeadCommitID == pr.MergeBase
}

// SetMerged sets a pull request to merged and closes the corresponding issue
func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) {
if pr.HasMerged {
Expand Down
3 changes: 2 additions & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,8 @@ pulls.remove_prefix = Remove <strong>%s</strong> prefix
pulls.data_broken = This pull request is broken due to missing fork information.
pulls.files_conflicted = This pull request has changes conflicting with the target branch.
pulls.is_checking = "Merge conflict checking is in progress. Try again in few moments."
pulls.is_empty = "This branch is equal with the target branch."
pulls.is_ancestor = "This branch is already included in the target branch. There is nothing to merge."
pulls.is_empty = "The changes on this branch are already on the target branch. This will be an empty commit."
pulls.required_status_check_failed = Some required checks were not successful.
pulls.required_status_check_missing = Some required checks are missing.
pulls.required_status_check_administrator = As an administrator, you may still merge this pull request.
Expand Down
2 changes: 1 addition & 1 deletion services/pull/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce
return ErrIsWorkInProgress
}

if !pr.CanAutoMerge() {
if !pr.CanAutoMerge() && !pr.IsEmpty() || pr.IsAncestor() {
return ErrNotMergableState
}

Expand Down
16 changes: 12 additions & 4 deletions templates/repo/issue/view_content/pull.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@
<i class="icon icon-octicon">{{svg "octicon-sync"}}</i>
{{$.locale.Tr "repo.pulls.is_checking"}}
</div>
{{else if .Issue.PullRequest.IsEmpty}}
{{else if .Issue.PullRequest.IsAncestor}}
<div class="item">
<i class="icon icon-octicon">{{svg "octicon-alert" 16}}</i>
{{$.locale.Tr "repo.pulls.is_empty"}}
{{$.locale.Tr "repo.pulls.is_ancestor"}}
</div>
{{else if .Issue.PullRequest.CanAutoMerge}}
{{else if or .Issue.PullRequest.CanAutoMerge .Issue.PullRequest.IsEmpty}}
{{if .IsBlockedByApprovals}}
<div class="item">
<i class="icon icon-octicon">{{svg "octicon-x"}}</i>
Expand Down Expand Up @@ -282,7 +282,6 @@
</div>
{{end}}
{{end}}

{{if and (gt .Issue.PullRequest.CommitsBehind 0) (not .Issue.IsClosed) (not .Issue.PullRequest.IsChecking) (not .IsPullFilesConflicted) (not .IsPullRequestBroken) (not $canAutoMerge)}}
<div class="ui divider"></div>
<div class="item item-section">
Expand Down Expand Up @@ -321,6 +320,14 @@
</div>
</div>
{{end}}
{{if .Issue.PullRequest.IsEmpty}}
<div class="ui divider"></div>

<div class="item">
<i class="icon icon-octicon">{{svg "octicon-alert" 16}}</i>
{{$.locale.Tr "repo.pulls.is_empty"}}
</div>
{{end}}

{{if .AllowMerge}} {{/* user is allowed to merge */}}
{{$prUnit := .Repository.MustGetUnit $.UnitTypePullRequests}}
Expand Down Expand Up @@ -348,6 +355,7 @@
'canMergeNow': {{$canMergeNow}},
'allOverridableChecksOk': {{not $notAllOverridableChecksOk}},
'emptyCommit': {{.Issue.PullRequest.IsEmpty}},
'pullHeadCommitID': {{.PullHeadCommitID}},
'isPullBranchDeletable': {{.IsPullBranchDeletable}},
'defaultDeleteBranchAfterMerge': {{$prUnit.PullRequestsConfig.DefaultDeleteBranchAfterMerge}},
Expand Down
2 changes: 1 addition & 1 deletion web_src/js/components/PullRequestMergeForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

<div v-if="!showActionForm" class="df">
<!-- the merge button -->
<div class="ui buttons merge-button" :class="mergeButtonStyleClass" @click="toggleActionForm(true)" >
<div class="ui buttons merge-button" :class="[mergeForm.emptyCommit?'grey':mergeForm.allOverridableChecksOk?'green':'red']" @click="toggleActionForm(true)" >
<button class="ui button">
<svg-icon name="octicon-git-merge"/>
<span class="button-text">
Expand Down

0 comments on commit d686e09

Please sign in to comment.