@@ -17,9 +17,14 @@ limitations under the License.
1717package bot
1818
1919import (
20+ "context"
2021 "testing"
2122
2223 "github.com/stretchr/testify/require"
24+
25+ "github.com/gravitational/shared-workflows/bot/internal/env"
26+ "github.com/gravitational/shared-workflows/bot/internal/github"
27+ "github.com/gravitational/shared-workflows/bot/internal/review"
2328)
2429
2530func TestFindBranches (t * testing.T ) {
@@ -35,3 +40,111 @@ func TestFindBranches(t *testing.T) {
3540 "master" ,
3641 })
3742}
43+
44+ func TestBackport (t * testing.T ) {
45+ buildTestBot := func (github Client ) (* Bot , context.Context ) {
46+ r , _ := review .New (& review.Config {
47+ CodeReviewers : map [string ]review.Reviewer {"dev" : review.Reviewer {
48+ Team : "core" ,
49+ }},
50+ CodeReviewersOmit : map [string ]bool {},
51+ DocsReviewers : map [string ]review.Reviewer {},
52+ DocsReviewersOmit : map [string ]bool {},
53+ Admins : []string {},
54+ })
55+
56+ return & Bot {
57+ c : & Config {
58+ Environment : & env.Environment {
59+ Organization : "foo" ,
60+ Author : "dev" ,
61+ Repository : "bar" ,
62+ Number : 42 ,
63+ UnsafeBase : "branch/v8" ,
64+ UnsafeHead : "fix" ,
65+ },
66+ GitHub : github ,
67+ Review : r ,
68+ Git : gitDryRun ,
69+ },
70+ }, context .Background ()
71+ }
72+
73+ tests := []struct {
74+ desc string
75+ github Client
76+ assertFunc require.ValueAssertionFunc
77+ }{
78+ {
79+ desc : "pr without backport labels" ,
80+ github : & fakeGithub {},
81+ assertFunc : require .Empty ,
82+ },
83+ {
84+ desc : "pr with backport label, no changelog" ,
85+ github : & fakeGithub {
86+ pull : github.PullRequest {
87+ Author : "dev" ,
88+ Repository : "Teleport" ,
89+ Number : 42 ,
90+ UnsafeTitle : "Best PR" ,
91+ UnsafeBody : "This is PR body" ,
92+ UnsafeLabels : []string {"backport/branch/v7" },
93+ },
94+ jobs : []github.Job {{Name : "Job1" , ID : 1 }},
95+ },
96+ assertFunc : func (t require.TestingT , i interface {}, i2 ... interface {}) {
97+ comments , ok := i .([]github.Comment )
98+ require .True (t , ok )
99+ require .Len (t , comments , 1 )
100+ require .Equal (t ,
101+ `
102+ @dev See the table below for backport results.
103+
104+ | Branch | Result |
105+ |--------|--------|
106+ | branch/v7 | [Create PR](https://github.com/foo/bar/compare/branch/v7...bot/backport-42-branch/v7?body=Backport+%2342+to+branch%2Fv7&expand=1&labels=no-changelog&title=%5Bv7%5D+Best+PR) |
107+ ` , comments [0 ].Body )
108+ },
109+ },
110+ {
111+ desc : "pr with backport label and with changelog" ,
112+ github : & fakeGithub {
113+ pull : github.PullRequest {
114+ Author : "dev" ,
115+ Repository : "Teleport" ,
116+ Number : 42 ,
117+ UnsafeTitle : "Best PR" ,
118+ UnsafeBody : "This is PR body\n \n changelog: important change" ,
119+ UnsafeLabels : []string {"backport/branch/v7" },
120+ },
121+ jobs : []github.Job {{Name : "Job1" , ID : 1 }},
122+ },
123+ assertFunc : func (t require.TestingT , i interface {}, i2 ... interface {}) {
124+ comments , ok := i .([]github.Comment )
125+ require .True (t , ok )
126+ require .Len (t , comments , 1 )
127+ require .Equal (t ,
128+ `
129+ @dev See the table below for backport results.
130+
131+ | Branch | Result |
132+ |--------|--------|
133+ | branch/v7 | [Create PR](https://github.com/foo/bar/compare/branch/v7...bot/backport-42-branch/v7?body=Backport+%2342+to+branch%2Fv7%0A%0Achangelog%3A+important+change%0A&expand=1&title=%5Bv7%5D+Best+PR) |
134+ ` , comments [0 ].Body )
135+ },
136+ },
137+ }
138+
139+ for _ , test := range tests {
140+ t .Run (test .desc , func (t * testing.T ) {
141+ b , ctx := buildTestBot (test .github )
142+
143+ err := b .Backport (ctx )
144+ require .NoError (t , err )
145+
146+ comments , _ := b .c .GitHub .ListComments (nil , "" , "" , 0 )
147+ test .assertFunc (t , comments )
148+ })
149+ }
150+ }
0 commit comments