summaryrefslogtreecommitdiffstats
path: root/tests/integration/repo_activity_test.go
blob: c1177fac11de8a76db3da4f3d91003363627d0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"testing"

	auth_model "code.gitea.io/gitea/models/auth"
	"code.gitea.io/gitea/models/db"
	repo_model "code.gitea.io/gitea/models/repo"
	unit_model "code.gitea.io/gitea/models/unit"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/test"
	repo_service "code.gitea.io/gitea/services/repository"
	"code.gitea.io/gitea/tests"

	"github.com/PuerkitoBio/goquery"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestRepoActivity(t *testing.T) {
	onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
		session := loginUser(t, "user1")

		// Create PRs (1 merged & 2 proposed)
		testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
		testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
		resp := testPullCreate(t, session, "user1", "repo1", false, "master", "master", "This is a pull title")
		elem := strings.Split(test.RedirectURL(resp), "/")
		assert.EqualValues(t, "pulls", elem[3])
		testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleMerge, false)

		testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feat/better_readme", "README.md", "Hello, World (Edited Again)\n")
		testPullCreate(t, session, "user1", "repo1", false, "master", "feat/better_readme", "This is a pull title")

		testEditFileToNewBranch(t, session, "user1", "repo1", "master", "feat/much_better_readme", "README.md", "Hello, World (Edited More)\n")
		testPullCreate(t, session, "user1", "repo1", false, "master", "feat/much_better_readme", "This is a pull title")

		// Create issues (3 new issues)
		testNewIssue(t, session, "user2", "repo1", "Issue 1", "Description 1")
		testNewIssue(t, session, "user2", "repo1", "Issue 2", "Description 2")
		testNewIssue(t, session, "user2", "repo1", "Issue 3", "Description 3")

		// Create releases (1 release, 1 pre-release, 1 release-draft, 1 tag)
		createNewRelease(t, session, "/user2/repo1", "v1.0.0", "v1 Release", false, false)
		createNewRelease(t, session, "/user2/repo1", "v0.1.0", "v0.1 Pre-release", true, false)
		createNewRelease(t, session, "/user2/repo1", "v2.0.0", "v2 Release-Draft", false, true)
		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
		createNewTagUsingAPI(t, token, "user2", "repo1", "v3.0.0", "master", "Tag message")

		// Open Activity page and check stats
		req := NewRequest(t, "GET", "/user2/repo1/activity")
		resp = session.MakeRequest(t, req, http.StatusOK)
		htmlDoc := NewHTMLParser(t, resp.Body)

		// Should be 3 published releases
		list := htmlDoc.doc.Find("#published-releases").Next().Find("p.desc")
		assert.Len(t, list.Nodes, 3)
		var labels []string
		var titles []string
		list.Each(func(i int, s *goquery.Selection) {
			labels = append(labels, s.Find(".label").Text())
			titles = append(titles, s.Find(".title").Text())
		})
		sort.Strings(labels)
		sort.Strings(titles)
		assert.Equal(t, []string{"Pre-release", "Release", "Tag"}, labels)
		assert.Equal(t, []string{"", "v0.1 Pre-release", "v1 Release"}, titles)

		// Should be 1 merged pull request
		list = htmlDoc.doc.Find("#merged-pull-requests").Next().Find("p.desc")
		assert.Len(t, list.Nodes, 1)
		assert.Equal(t, "Merged", list.Find(".label").Text())

		// Should be 2 proposed pull requests
		list = htmlDoc.doc.Find("#proposed-pull-requests").Next().Find("p.desc")
		assert.Len(t, list.Nodes, 2)
		assert.Equal(t, "Proposed", list.Find(".label").First().Text())

		// Should be 0 closed issues
		list = htmlDoc.doc.Find("#closed-issues").Next().Find("p.desc")
		assert.Empty(t, list.Nodes)

		// Should be 3 new issues
		list = htmlDoc.doc.Find("#new-issues").Next().Find("p.desc")
		assert.Len(t, list.Nodes, 3)
		assert.Equal(t, "Opened", list.Find(".label").First().Text())
	})
}

func TestRepoActivityAllUnitsDisabled(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
	session := loginUser(t, user.Name)

	unit_model.LoadUnitConfig()

	// Create a repo, with no unit enabled.
	repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
		Name:     "empty-repo",
		AutoInit: false,
	})
	require.NoError(t, err)
	assert.NotEmpty(t, repo)

	enabledUnits := make([]repo_model.RepoUnit, 0)
	disabledUnits := []unit_model.Type{unit_model.TypeCode, unit_model.TypeIssues, unit_model.TypePullRequests, unit_model.TypeReleases}
	err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, disabledUnits)
	require.NoError(t, err)

	req := NewRequest(t, "GET", fmt.Sprintf("%s/activity", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/contributors", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/code-frequency", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/recent-commits", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
}

func TestRepoActivityOnlyCodeUnitWithEmptyRepo(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
	session := loginUser(t, user.Name)

	unit_model.LoadUnitConfig()

	// Create a empty repo, with only code unit enabled.
	repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
		Name:     "empty-repo",
		AutoInit: false,
	})
	require.NoError(t, err)
	assert.NotEmpty(t, repo)

	enabledUnits := make([]repo_model.RepoUnit, 1)
	enabledUnits[0] = repo_model.RepoUnit{RepoID: repo.ID, Type: unit_model.TypeCode}
	disabledUnits := []unit_model.Type{unit_model.TypeIssues, unit_model.TypePullRequests, unit_model.TypeReleases}
	err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, disabledUnits)
	require.NoError(t, err)

	req := NewRequest(t, "GET", fmt.Sprintf("%s/activity", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)

	// Git repo empty so no activity for contributors etc
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/contributors", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/code-frequency", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/recent-commits", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
}

func TestRepoActivityOnlyCodeUnitWithNonEmptyRepo(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
	session := loginUser(t, user.Name)

	unit_model.LoadUnitConfig()

	// Create a repo, with only code unit enabled.
	repo, _, f := tests.CreateDeclarativeRepo(t, user, "", []unit_model.Type{unit_model.TypeCode}, nil, nil)
	defer f()

	req := NewRequest(t, "GET", fmt.Sprintf("%s/activity", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)

	// Git repo not empty so activity for contributors etc
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/contributors", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/code-frequency", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/recent-commits", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)
}

func TestRepoActivityOnlyIssuesUnit(t *testing.T) {
	defer tests.PrepareTestEnv(t)()
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
	session := loginUser(t, user.Name)

	unit_model.LoadUnitConfig()

	// Create a empty repo, with only code unit enabled.
	repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
		Name:     "empty-repo",
		AutoInit: false,
	})
	require.NoError(t, err)
	assert.NotEmpty(t, repo)

	enabledUnits := make([]repo_model.RepoUnit, 1)
	enabledUnits[0] = repo_model.RepoUnit{RepoID: repo.ID, Type: unit_model.TypeIssues}
	disabledUnits := []unit_model.Type{unit_model.TypeCode, unit_model.TypePullRequests, unit_model.TypeReleases}
	err = repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, disabledUnits)
	require.NoError(t, err)

	req := NewRequest(t, "GET", fmt.Sprintf("%s/activity", repo.Link()))
	session.MakeRequest(t, req, http.StatusOK)

	// Git repo empty so no activity for contributors etc
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/contributors", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/code-frequency", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
	req = NewRequest(t, "GET", fmt.Sprintf("%s/activity/recent-commits", repo.Link()))
	session.MakeRequest(t, req, http.StatusNotFound)
}