Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,12 @@ func ParseCodeOwnersLine(ctx context.Context, tokens []string) (*CodeOwnerRule,

warnings := make([]string, 0)

expr := fmt.Sprintf("^%s$", strings.TrimPrefix(tokens[0], "!"))
// Strip leading "!" for negative rules, then strip leading "/" since
// git returns relative paths (e.g. "docs/foo.md" not "/docs/foo.md")
// and the regex is already anchored with ^...$, so the "/" is redundant.
pattern := strings.TrimPrefix(tokens[0], "!")
pattern = strings.TrimPrefix(pattern, "/")
expr := fmt.Sprintf("^%s$", pattern)
rule.Rule, err = regexp2.Compile(expr, regexp2.None)
if err != nil {
warnings = append(warnings, fmt.Sprintf("incorrect codeowner regexp: %s", err))
Expand Down
134 changes: 90 additions & 44 deletions models/issues/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,43 @@
"github.com/stretchr/testify/require"
)

func TestPullRequest_LoadAttributes(t *testing.T) {
func TestPullRequest(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

t.Run("LoadAttributes", testPullRequest_LoadAttributes)
t.Run("LoadIssue", testPullRequest_LoadIssue)
t.Run("LoadBaseRepo", testPullRequest_LoadBaseRepo)
t.Run("LoadHeadRepo", testPullRequest_LoadHeadRepo)
t.Run("PullRequestsNewest", testPullRequestsNewest)
t.Run("PullRequestsOldest", testPullRequestsOldest)
t.Run("GetUnmergedPullRequest", testGetUnmergedPullRequest)
t.Run("HasUnmergedPullRequestsByHeadInfo", testHasUnmergedPullRequestsByHeadInfo)
t.Run("GetUnmergedPullRequestsByHeadInfo", testGetUnmergedPullRequestsByHeadInfo)
t.Run("GetUnmergedPullRequestsByBaseInfo", testGetUnmergedPullRequestsByBaseInfo)
t.Run("GetPullRequestByIndex", testGetPullRequestByIndex)
t.Run("GetPullRequestByID", testGetPullRequestByID)
t.Run("GetPullRequestByIssueID", testGetPullRequestByIssueID)
t.Run("PullRequest_UpdateCols", testPullRequest_UpdateCols)
t.Run("PullRequest_IsWorkInProgress", testPullRequest_IsWorkInProgress)
t.Run("PullRequest_GetWorkInProgressPrefixWorkInProgress", testPullRequest_GetWorkInProgressPrefixWorkInProgress)
t.Run("DeleteOrphanedObjects", testDeleteOrphanedObjects)
t.Run("ParseCodeOwnersLine", testParseCodeOwnersLine)
t.Run("CodeOwnerAbsolutePathPatterns", testCodeOwnerAbsolutePathPatterns)
t.Run("GetApprovers", testGetApprovers)
t.Run("GetPullRequestByMergedCommit", testGetPullRequestByMergedCommit)
t.Run("Migrate_InsertPullRequests", testMigrate_InsertPullRequests)
t.Run("PullRequestsClosedRecentSortType", testPullRequests_Closed_RecentSortType)
t.Run("LoadRequestedReviewers", testLoadRequestedReviewers)
}

func testPullRequest_LoadAttributes(t *testing.T) {

Check failure on line 49 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequest_LoadAttributes should be testPullRequestLoadAttributes (revive)

Check failure on line 49 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_LoadAttributes should be testPullRequestLoadAttributes (revive)

Check failure on line 49 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequest_LoadAttributes should be testPullRequestLoadAttributes (revive)
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
assert.NoError(t, pr.LoadAttributes(t.Context()))
assert.NotNil(t, pr.Merger)
assert.Equal(t, pr.MergerID, pr.Merger.ID)
}

func TestPullRequest_LoadIssue(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_LoadIssue(t *testing.T) {

Check failure on line 56 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequest_LoadIssue should be testPullRequestLoadIssue (revive)

Check failure on line 56 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_LoadIssue should be testPullRequestLoadIssue (revive)

Check failure on line 56 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequest_LoadIssue should be testPullRequestLoadIssue (revive)
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
assert.NoError(t, pr.LoadIssue(t.Context()))
assert.NotNil(t, pr.Issue)
Expand All @@ -36,8 +63,7 @@
assert.Equal(t, int64(2), pr.Issue.ID)
}

func TestPullRequest_LoadBaseRepo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_LoadBaseRepo(t *testing.T) {

Check failure on line 66 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequest_LoadBaseRepo should be testPullRequestLoadBaseRepo (revive)

Check failure on line 66 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_LoadBaseRepo should be testPullRequestLoadBaseRepo (revive)

Check failure on line 66 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequest_LoadBaseRepo should be testPullRequestLoadBaseRepo (revive)
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
assert.NoError(t, pr.LoadBaseRepo(t.Context()))
assert.NotNil(t, pr.BaseRepo)
Expand All @@ -47,8 +73,7 @@
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
}

func TestPullRequest_LoadHeadRepo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_LoadHeadRepo(t *testing.T) {

Check failure on line 76 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequest_LoadHeadRepo should be testPullRequestLoadHeadRepo (revive)

Check failure on line 76 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_LoadHeadRepo should be testPullRequestLoadHeadRepo (revive)

Check failure on line 76 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequest_LoadHeadRepo should be testPullRequestLoadHeadRepo (revive)
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
assert.NoError(t, pr.LoadHeadRepo(t.Context()))
assert.NotNil(t, pr.HeadRepo)
Expand All @@ -59,8 +84,7 @@

// TODO TestNewPullRequest

func TestPullRequestsNewest(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequestsNewest(t *testing.T) {
prs, count, err := issues_model.PullRequests(t.Context(), 1, &issues_model.PullRequestsOptions{
ListOptions: db.ListOptions{
Page: 1,
Expand All @@ -77,7 +101,7 @@
}
}

func TestPullRequests_Closed_RecentSortType(t *testing.T) {
func testPullRequests_Closed_RecentSortType(t *testing.T) {

Check failure on line 104 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequests_Closed_RecentSortType should be testPullRequestsClosedRecentSortType (revive)

Check failure on line 104 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequests_Closed_RecentSortType should be testPullRequestsClosedRecentSortType (revive)

Check failure on line 104 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequests_Closed_RecentSortType should be testPullRequestsClosedRecentSortType (revive)
// Issue ID | Closed At. | Updated At
// 2 | 1707270001 | 1707270001
// 3 | 1707271000 | 1707279999
Expand All @@ -90,7 +114,6 @@
{"recentclose", []int64{11, 3, 2}},
}

assert.NoError(t, unittest.PrepareTestDatabase())
_, err := db.Exec(t.Context(), "UPDATE issue SET closed_unix = 1707270001, updated_unix = 1707270001, is_closed = true WHERE id = 2")
require.NoError(t, err)
_, err = db.Exec(t.Context(), "UPDATE issue SET closed_unix = 1707271000, updated_unix = 1707279999, is_closed = true WHERE id = 3")
Expand Down Expand Up @@ -118,9 +141,8 @@
}
}

func TestLoadRequestedReviewers(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testLoadRequestedReviewers(t *testing.T) {

Check failure on line 144 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 144 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 144 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 145 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

File is not properly formatted (gofumpt)

Check failure on line 145 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

File is not properly formatted (gofumpt)

Check failure on line 145 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

File is not properly formatted (gofumpt)
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
assert.NoError(t, pull.LoadIssue(t.Context()))
issue := pull.Issue
Expand All @@ -146,8 +168,7 @@
assert.Empty(t, pull.RequestedReviewers)
}

func TestPullRequestsOldest(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequestsOldest(t *testing.T) {
prs, count, err := issues_model.PullRequests(t.Context(), 1, &issues_model.PullRequestsOptions{
ListOptions: db.ListOptions{
Page: 1,
Expand All @@ -164,8 +185,7 @@
}
}

func TestGetUnmergedPullRequest(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetUnmergedPullRequest(t *testing.T) {
pr, err := issues_model.GetUnmergedPullRequest(t.Context(), 1, 1, "branch2", "master", issues_model.PullRequestFlowGithub)
assert.NoError(t, err)
assert.Equal(t, int64(2), pr.ID)
Expand All @@ -175,8 +195,7 @@
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
}

func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testHasUnmergedPullRequestsByHeadInfo(t *testing.T) {

Check failure on line 198 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 198 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 198 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

empty-lines: extra empty line at the start of a block (revive)

exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(t.Context(), 1, "branch2")
assert.NoError(t, err)
Expand All @@ -187,8 +206,7 @@
assert.False(t, exist)
}

func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(t.Context(), 1, "branch2")
assert.NoError(t, err)
assert.Len(t, prs, 1)
Expand All @@ -198,8 +216,7 @@
}
}

func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(t.Context(), 1, "master")
assert.NoError(t, err)
assert.Len(t, prs, 1)
Expand All @@ -209,8 +226,7 @@
assert.Equal(t, "master", pr.BaseBranch)
}

func TestGetPullRequestByIndex(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetPullRequestByIndex(t *testing.T) {
pr, err := issues_model.GetPullRequestByIndex(t.Context(), 1, 2)
assert.NoError(t, err)
assert.Equal(t, int64(1), pr.BaseRepoID)
Expand All @@ -225,8 +241,7 @@
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
}

func TestGetPullRequestByID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetPullRequestByID(t *testing.T) {
pr, err := issues_model.GetPullRequestByID(t.Context(), 1)
assert.NoError(t, err)
assert.Equal(t, int64(1), pr.ID)
Expand All @@ -237,8 +252,7 @@
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
}

func TestGetPullRequestByIssueID(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetPullRequestByIssueID(t *testing.T) {
pr, err := issues_model.GetPullRequestByIssueID(t.Context(), 2)
assert.NoError(t, err)
assert.Equal(t, int64(2), pr.IssueID)
Expand All @@ -248,8 +262,7 @@
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
}

func TestPullRequest_UpdateCols(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_UpdateCols(t *testing.T) {

Check failure on line 265 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use underscores in Go names; func testPullRequest_UpdateCols should be testPullRequestUpdateCols (revive)

Check failure on line 265 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_UpdateCols should be testPullRequestUpdateCols (revive)

Check failure on line 265 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use underscores in Go names; func testPullRequest_UpdateCols should be testPullRequestUpdateCols (revive)
pr := &issues_model.PullRequest{
ID: 1,
BaseBranch: "baseBranch",
Expand All @@ -265,8 +278,7 @@

// TODO TestAddTestPullRequestTask

func TestPullRequest_IsWorkInProgress(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_IsWorkInProgress(t *testing.T) {

Check failure on line 281 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 281 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use underscores in Go names; func testPullRequest_IsWorkInProgress should be testPullRequestIsWorkInProgress (revive)

Check failure on line 281 in models/issues/pull_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

empty-lines: extra empty line at the start of a block (revive)

pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
pr.LoadIssue(t.Context())
Expand All @@ -280,8 +292,7 @@
assert.True(t, pr.IsWorkInProgress(t.Context()))
}

func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {

pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
pr.LoadIssue(t.Context())
Expand All @@ -296,8 +307,7 @@
assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix(t.Context()))
}

func TestDeleteOrphanedObjects(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testDeleteOrphanedObjects(t *testing.T) {

countBefore, err := db.GetEngine(t.Context()).Count(&issues_model.PullRequest{})
assert.NoError(t, err)
Expand All @@ -317,7 +327,7 @@
assert.Equal(t, countBefore, countAfter)
}

func TestParseCodeOwnersLine(t *testing.T) {
func testParseCodeOwnersLine(t *testing.T) {
type CodeOwnerTest struct {
Line string
Tokens []string
Expand All @@ -331,6 +341,8 @@
{Line: `docs/(aws|google|azure)/[^/]*\\.(md|txt) @org3 @org2/team2`, Tokens: []string{`docs/(aws|google|azure)/[^/]*\.(md|txt)`, "@org3", "@org2/team2"}},
{Line: `\#path @org3`, Tokens: []string{`#path`, "@org3"}},
{Line: `path\ with\ spaces/ @org3`, Tokens: []string{`path with spaces/`, "@org3"}},
{Line: `/docs/.*\\.md @user1`, Tokens: []string{`/docs/.*\.md`, "@user1"}},
{Line: `!/assets/.*\\.(bin|exe|msi) @user1`, Tokens: []string{`!/assets/.*\.(bin|exe|msi)`, "@user1"}},
}

for _, g := range given {
Expand All @@ -339,8 +351,44 @@
}
}

func TestGetApprovers(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testCodeOwnerAbsolutePathPatterns(t *testing.T) {

type testCase struct {
content string
file string
expected bool
}

cases := []testCase{
// Absolute path pattern should match (leading "/" stripped)
{content: "/README.md @user5\n", file: "README.md", expected: true},
// Absolute path pattern in subdirectory
{content: "/docs/.* @user5\n", file: "docs/foo.md", expected: true},
// Absolute path should not match nested paths it shouldn't
{content: "/docs/.* @user5\n", file: "other/docs/foo.md", expected: false},
// Relative path still works
{content: "README.md @user5\n", file: "README.md", expected: true},
// Negated absolute path pattern
{content: "!/.* @user5\n", file: "README.md", expected: false},
}

for _, c := range cases {
rules, _ := issues_model.GetCodeOwnersFromContent(t.Context(), c.content)
if len(rules) == 0 {
if c.expected {
t.Errorf("expected rules for content %q but got none", c.content)
}
continue
}
Comment thread
wxiaoguang marked this conversation as resolved.
Outdated
rule := rules[0]
shouldMatch := !rule.Negative
matched, _ := rule.Rule.MatchString(c.file)
got := matched == shouldMatch
assert.Equal(t, c.expected, got, "pattern %q against file %q", c.content, c.file)
}
}

func testGetApprovers(t *testing.T) {
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 5})
// Official reviews are already deduplicated. Allow unofficial reviews
// to assert that there are no duplicated approvers.
Expand All @@ -350,8 +398,7 @@
assert.Equal(t, expected, approvers)
}

func TestGetPullRequestByMergedCommit(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testGetPullRequestByMergedCommit(t *testing.T) {
pr, err := issues_model.GetPullRequestByMergedCommit(t.Context(), 1, "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3")
assert.NoError(t, err)
assert.EqualValues(t, 1, pr.ID)
Expand All @@ -362,8 +409,7 @@
assert.ErrorAs(t, err, &issues_model.ErrPullRequestNotExist{})
}

func TestMigrate_InsertPullRequests(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
func testMigrate_InsertPullRequests(t *testing.T) {
reponame := "repo1"
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
Expand Down
Loading