Skip to content

Commit 49cab2b

Browse files
singuliereLoïc Dacharyzeripathlunny
authored
migrations: add test for importing pull requests in gitea uploader (#18752)
* logs: add the buffer logger to inspect logs during testing Signed-off-by: Loïc Dachary <[email protected]> * migrations: add test for importing pull requests in gitea uploader Signed-off-by: Loïc Dachary <[email protected]> * for each git.OpenRepositoryCtx, call Close * Content is expected to return the content of the log * test for errors before defer Co-authored-by: Loïc Dachary <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent e4ef61e commit 49cab2b

File tree

12 files changed

+512
-20
lines changed

12 files changed

+512
-20
lines changed

integrations/testlogger.go

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ func (log *TestLogger) Init(config string) error {
181181
return nil
182182
}
183183

184+
// Content returns the content accumulated in the content provider
185+
func (log *TestLogger) Content() (string, error) {
186+
return "", fmt.Errorf("not supported")
187+
}
188+
184189
// Flush when log should be flushed
185190
func (log *TestLogger) Flush() {
186191
}

models/migrations/testlogger_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func (log *TestLogger) Init(config string) error {
166166
return nil
167167
}
168168

169+
// Content returns the content accumulated in the content provider
170+
func (log *TestLogger) Content() (string, error) {
171+
return "", fmt.Errorf("not supported")
172+
}
173+
169174
// Flush when log should be flushed
170175
func (log *TestLogger) Flush() {
171176
}

modules/log/buffer.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"bytes"
9+
"sync"
10+
)
11+
12+
type bufferWriteCloser struct {
13+
mu sync.Mutex
14+
buffer bytes.Buffer
15+
}
16+
17+
func (b *bufferWriteCloser) Write(p []byte) (int, error) {
18+
b.mu.Lock()
19+
defer b.mu.Unlock()
20+
return b.buffer.Write(p)
21+
}
22+
23+
func (b *bufferWriteCloser) Close() error {
24+
return nil
25+
}
26+
27+
func (b *bufferWriteCloser) String() string {
28+
b.mu.Lock()
29+
defer b.mu.Unlock()
30+
return b.buffer.String()
31+
}
32+
33+
// BufferLogger implements LoggerProvider and writes messages in a buffer.
34+
type BufferLogger struct {
35+
WriterLogger
36+
}
37+
38+
// NewBufferLogger create BufferLogger returning as LoggerProvider.
39+
func NewBufferLogger() LoggerProvider {
40+
log := &BufferLogger{}
41+
log.NewWriterLogger(&bufferWriteCloser{})
42+
return log
43+
}
44+
45+
// Init inits connection writer
46+
func (log *BufferLogger) Init(string) error {
47+
log.NewWriterLogger(log.out)
48+
return nil
49+
}
50+
51+
// Content returns the content accumulated in the content provider
52+
func (log *BufferLogger) Content() (string, error) {
53+
return log.out.(*bufferWriteCloser).String(), nil
54+
}
55+
56+
// Flush when log should be flushed
57+
func (log *BufferLogger) Flush() {
58+
}
59+
60+
// ReleaseReopen does nothing
61+
func (log *BufferLogger) ReleaseReopen() error {
62+
return nil
63+
}
64+
65+
// GetName returns the default name for this implementation
66+
func (log *BufferLogger) GetName() string {
67+
return "buffer"
68+
}
69+
70+
func init() {
71+
Register("buffer", NewBufferLogger)
72+
}

modules/log/buffer_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
"testing"
11+
"time"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestBufferLogger(t *testing.T) {
17+
logger := NewBufferLogger()
18+
bufferLogger := logger.(*BufferLogger)
19+
assert.NotNil(t, bufferLogger)
20+
21+
err := logger.Init("")
22+
assert.NoError(t, err)
23+
24+
location, _ := time.LoadLocation("EST")
25+
date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
26+
27+
msg := "TEST MSG"
28+
event := Event{
29+
level: INFO,
30+
msg: msg,
31+
caller: "CALLER",
32+
filename: "FULL/FILENAME",
33+
line: 1,
34+
time: date,
35+
}
36+
logger.LogEvent(&event)
37+
content, err := bufferLogger.Content()
38+
assert.NoError(t, err)
39+
assert.Contains(t, content, msg)
40+
logger.Close()
41+
}
42+
43+
func TestBufferLoggerContent(t *testing.T) {
44+
level := INFO
45+
logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
46+
47+
logger.SetLogger("buffer", "buffer", "{}")
48+
defer logger.DelLogger("buffer")
49+
50+
msg := "A UNIQUE MESSAGE"
51+
Error(msg)
52+
53+
found := false
54+
for i := 0; i < 30000; i++ {
55+
content, err := logger.GetLoggerProviderContent("buffer")
56+
assert.NoError(t, err)
57+
if strings.Contains(content, msg) {
58+
found = true
59+
break
60+
}
61+
time.Sleep(1 * time.Millisecond)
62+
}
63+
assert.True(t, found)
64+
}

modules/log/conn.go

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ func (log *ConnLogger) Init(jsonconfig string) error {
119119
return nil
120120
}
121121

122+
// Content returns the content accumulated in the content provider
123+
func (log *ConnLogger) Content() (string, error) {
124+
return "", fmt.Errorf("not supported")
125+
}
126+
122127
// Flush does nothing for this implementation
123128
func (log *ConnLogger) Flush() {
124129
}

modules/log/console.go

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (log *ConsoleLogger) Init(config string) error {
6666
return nil
6767
}
6868

69+
// Content returns the content accumulated in the content provider
70+
func (log *ConsoleLogger) Content() (string, error) {
71+
return "", fmt.Errorf("not supported")
72+
}
73+
6974
// Flush when log should be flushed
7075
func (log *ConsoleLogger) Flush() {
7176
}

modules/log/event.go

+6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ func (m *MultiChannelledLog) GetEventLogger(name string) EventLogger {
216216
return m.loggers[name]
217217
}
218218

219+
// GetEventProvider returns a sub logger provider content from this MultiChannelledLog
220+
func (m *MultiChannelledLog) GetLoggerProviderContent(name string) (string, error) {
221+
channelledLogger := m.GetEventLogger(name).(*ChannelledLog)
222+
return channelledLogger.loggerProvider.Content()
223+
}
224+
219225
// GetEventLoggerNames returns a list of names
220226
func (m *MultiChannelledLog) GetEventLoggerNames() []string {
221227
m.rwmutex.RLock()

modules/log/file.go

+9
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ func (log *FileLogger) deleteOldLog() {
243243
})
244244
}
245245

246+
// Content returns the content accumulated in the content provider
247+
func (log *FileLogger) Content() (string, error) {
248+
b, err := os.ReadFile(log.Filename)
249+
if err != nil {
250+
return "", err
251+
}
252+
return string(b), nil
253+
}
254+
246255
// Flush flush file logger.
247256
// there are no buffering messages in file logger in memory.
248257
// flush file means sync file from disk.

modules/log/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package log
77
// LoggerProvider represents behaviors of a logger provider.
88
type LoggerProvider interface {
99
Init(config string) error
10+
Content() (string, error)
1011
EventLogger
1112
}
1213

modules/log/smtp.go

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (log *SMTPLogger) sendMail(p []byte) (int, error) {
9595
)
9696
}
9797

98+
// Content returns the content accumulated in the content provider
99+
func (log *SMTPLogger) Content() (string, error) {
100+
return "", fmt.Errorf("not supported")
101+
}
102+
98103
// Flush when log should be flushed
99104
func (log *SMTPLogger) Flush() {
100105
}

services/migrations/gitea_uploader.go

+29-20
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,9 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
484484
return nil
485485
}
486486

487-
func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullRequest, error) {
488-
var labels []*models.Label
489-
for _, label := range pr.Labels {
490-
lb, ok := g.labels[label.Name]
491-
if ok {
492-
labels = append(labels, lb)
493-
}
494-
}
495-
496-
milestoneID := g.milestones[pr.Milestone]
497-
487+
func (g *GiteaLocalUploader) updateGitForPullRequest(pr *base.PullRequest) (head string, err error) {
498488
// download patch file
499-
err := func() error {
489+
err = func() error {
500490
if pr.PatchURL == "" {
501491
return nil
502492
}
@@ -519,25 +509,25 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
519509
return err
520510
}()
521511
if err != nil {
522-
return nil, err
512+
return "", err
523513
}
524514

525515
// set head information
526516
pullHead := filepath.Join(g.repo.RepoPath(), "refs", "pull", fmt.Sprintf("%d", pr.Number))
527517
if err := os.MkdirAll(pullHead, os.ModePerm); err != nil {
528-
return nil, err
518+
return "", err
529519
}
530520
p, err := os.Create(filepath.Join(pullHead, "head"))
531521
if err != nil {
532-
return nil, err
522+
return "", err
533523
}
534524
_, err = p.WriteString(pr.Head.SHA)
535525
p.Close()
536526
if err != nil {
537-
return nil, err
527+
return "", err
538528
}
539529

540-
head := "unknown repository"
530+
head = "unknown repository"
541531
if pr.IsForkPullRequest() && pr.State != "closed" {
542532
if pr.Head.OwnerName != "" {
543533
remote := pr.Head.OwnerName
@@ -560,16 +550,16 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
560550
} else {
561551
headBranch := filepath.Join(g.repo.RepoPath(), "refs", "heads", pr.Head.OwnerName, pr.Head.Ref)
562552
if err := os.MkdirAll(filepath.Dir(headBranch), os.ModePerm); err != nil {
563-
return nil, err
553+
return "", err
564554
}
565555
b, err := os.Create(headBranch)
566556
if err != nil {
567-
return nil, err
557+
return "", err
568558
}
569559
_, err = b.WriteString(pr.Head.SHA)
570560
b.Close()
571561
if err != nil {
572-
return nil, err
562+
return "", err
573563
}
574564
head = pr.Head.OwnerName + "/" + pr.Head.Ref
575565
}
@@ -595,6 +585,25 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
595585
}
596586
}
597587

588+
return head, nil
589+
}
590+
591+
func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullRequest, error) {
592+
var labels []*models.Label
593+
for _, label := range pr.Labels {
594+
lb, ok := g.labels[label.Name]
595+
if ok {
596+
labels = append(labels, lb)
597+
}
598+
}
599+
600+
milestoneID := g.milestones[pr.Milestone]
601+
602+
head, err := g.updateGitForPullRequest(pr)
603+
if err != nil {
604+
return nil, fmt.Errorf("updateGitForPullRequest: %w", err)
605+
}
606+
598607
if pr.Created.IsZero() {
599608
if pr.Closed != nil {
600609
pr.Created = *pr.Closed

0 commit comments

Comments
 (0)