@@ -186,6 +186,8 @@ describe('modules/platform/bitbucket-server/index', () => {
186186 name : username ,
187187 emailAddress : 'abc@def.com' ,
188188 displayName : 'Abc Def' ,
189+ active : true ,
190+ slug : 'username' ,
189191 } ;
190192
191193 async function initRepo ( config = { } ) : Promise < httpMock . Scope > {
@@ -985,6 +987,57 @@ describe('modules/platform/bitbucket-server/index', () => {
985987 await expect ( bitbucket . addReviewers ( 5 , [ 'name' ] ) ) . toResolve ( ) ;
986988 } ) ;
987989
990+ it ( 'deals correctly with resolving reviewers' , async ( ) => {
991+ const scope = await initRepo ( ) ;
992+ scope
993+ . get (
994+ `${ urlPath } /rest/api/1.0/projects/SOME/repos/repo/pull-requests/5` ,
995+ )
996+ . twice ( )
997+ . reply ( 200 , prMock ( url , 'SOME' , 'repo' ) ) ;
998+
999+ scope
1000+ . put (
1001+ `${ urlPath } /rest/api/1.0/projects/SOME/repos/repo/pull-requests/5` ,
1002+ ( body ) => {
1003+ const reviewers = body . reviewers . map (
1004+ ( r : { user : { name : any } } ) => r . user . name ,
1005+ ) ;
1006+ return (
1007+ Array . isArray ( reviewers ) &&
1008+ reviewers . length === 3 &&
1009+ reviewers . includes ( 'name' ) &&
1010+ reviewers . includes ( 'userName2' ) &&
1011+ reviewers . includes ( 'usernamefoundbyemail' )
1012+ ) ;
1013+ } ,
1014+ )
1015+ . reply ( 200 ) ;
1016+
1017+ scope
1018+ // User by email
1019+ . get ( `${ urlPath } /rest/api/1.0/users` )
1020+ . query (
1021+ ( q ) =>
1022+ q . filter === 'test@test.com' &&
1023+ q [ 'permission.1' ] === 'REPO_READ' &&
1024+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1025+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1026+ )
1027+ . reply ( 200 , [
1028+ {
1029+ slug : 'usernamefoundbyemail' ,
1030+ active : true ,
1031+ displayName : 'Not relevant' ,
1032+ emailAddress : 'test@test.com' ,
1033+ } ,
1034+ ] ) ;
1035+
1036+ await expect (
1037+ bitbucket . addReviewers ( 5 , [ 'name' , 'userName2' , 'test@test.com' ] ) ,
1038+ ) . toResolve ( ) ;
1039+ } ) ;
1040+
9881041 it ( 'throws' , async ( ) => {
9891042 const scope = await initRepo ( ) ;
9901043 scope
@@ -1002,6 +1055,135 @@ describe('modules/platform/bitbucket-server/index', () => {
10021055 } ) ;
10031056 } ) ;
10041057
1058+ describe ( 'getUserSlugsByEmail' , ( ) => {
1059+ it ( 'throws when lookup fails' , async ( ) => {
1060+ const scope = await initRepo ( ) ;
1061+ scope
1062+ // User by email
1063+ . get ( `${ urlPath } /rest/api/1.0/users` )
1064+ . query (
1065+ ( q ) =>
1066+ q . filter === 'e-mail@test.com' &&
1067+ q [ 'permission.1' ] === 'REPO_READ' &&
1068+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1069+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1070+ )
1071+ . reply ( 500 , [ ] ) ;
1072+
1073+ await expect (
1074+ bitbucket . getUserSlugsByEmail ( 'e-mail@test.com' ) ,
1075+ ) . rejects . toThrow ( 'Response code 500 (Internal Server Error)' ) ;
1076+ } ) ;
1077+
1078+ it ( 'return empty array when no results found' , async ( ) => {
1079+ const scope = await initRepo ( ) ;
1080+ scope
1081+ // User by email
1082+ . get ( `${ urlPath } /rest/api/1.0/users` )
1083+ . query (
1084+ ( q ) =>
1085+ q . filter === 'e-mail@test.com' &&
1086+ q [ 'permission.1' ] === 'REPO_READ' &&
1087+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1088+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1089+ )
1090+ . reply ( 200 , [ ] ) ;
1091+
1092+ const actual = await bitbucket . getUserSlugsByEmail ( 'e-mail@test.com' ) ;
1093+ expect ( actual ) . toBeEmptyArray ( ) ;
1094+ } ) ;
1095+
1096+ it ( 'return only active users' , async ( ) => {
1097+ const scope = await initRepo ( ) ;
1098+ scope
1099+ // User by email
1100+ . get ( `${ urlPath } /rest/api/1.0/users` )
1101+ . query (
1102+ ( q ) =>
1103+ q . filter === 'e-mail@test.com' &&
1104+ q [ 'permission.1' ] === 'REPO_READ' &&
1105+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1106+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1107+ )
1108+ . reply ( 200 , [
1109+ {
1110+ slug : 'usernamefoundbyemail' ,
1111+ active : false ,
1112+ displayName : 'Not relevant' ,
1113+ emailAddress : 'e-mail@test.com' ,
1114+ } ,
1115+ ] ) ;
1116+
1117+ const actual = await bitbucket . getUserSlugsByEmail ( 'e-mail@test.com' ) ;
1118+ expect ( actual ) . toBeEmptyArray ( ) ;
1119+ } ) ;
1120+
1121+ it ( 'only returns exact matches' , async ( ) => {
1122+ const scope = await initRepo ( ) ;
1123+ scope
1124+ // User by email
1125+ . get ( `${ urlPath } /rest/api/1.0/users` )
1126+ . query (
1127+ ( q ) =>
1128+ q . filter === 'mail@test.com' &&
1129+ q [ 'permission.1' ] === 'REPO_READ' &&
1130+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1131+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1132+ )
1133+ . reply ( 200 , [
1134+ {
1135+ slug : 'usernamefoundbyemail' ,
1136+ active : true ,
1137+ displayName : 'Not relevant' ,
1138+ emailAddress : 'e-mail@test.com' ,
1139+ } ,
1140+ {
1141+ slug : 'usernamefoundbyemailtoo' ,
1142+ active : true ,
1143+ displayName : 'Not relevant' ,
1144+ emailAddress : 'e-mail@test.com' ,
1145+ } ,
1146+ ] ) ;
1147+
1148+ const actual = await bitbucket . getUserSlugsByEmail ( 'mail@test.com' ) ;
1149+ expect ( actual ) . toBeEmptyArray ( ) ;
1150+ } ) ;
1151+
1152+ it ( 'returns multiple exact matches' , async ( ) => {
1153+ const scope = await initRepo ( ) ;
1154+ scope
1155+ // User by email
1156+ . get ( `${ urlPath } /rest/api/1.0/users` )
1157+ . query (
1158+ ( q ) =>
1159+ q . filter === 'e-mail@test.com' &&
1160+ q [ 'permission.1' ] === 'REPO_READ' &&
1161+ q [ 'permission.1.repositorySlug' ] === 'repo' &&
1162+ q [ 'permission.1.projectKey' ] === 'SOME' ,
1163+ )
1164+ . reply ( 200 , [
1165+ {
1166+ slug : 'usernamefoundbyemail' ,
1167+ active : true ,
1168+ displayName : 'Not relevant' ,
1169+ emailAddress : 'e-mail@test.com' ,
1170+ } ,
1171+ {
1172+ slug : 'usernamefoundbyemailtoo' ,
1173+ active : true ,
1174+ displayName : 'Not relevant' ,
1175+ emailAddress : 'e-mail@test.com' ,
1176+ } ,
1177+ ] ) ;
1178+
1179+ const actual = await bitbucket . getUserSlugsByEmail ( 'e-mail@test.com' ) ;
1180+ expect ( actual ) . toStrictEqual ( [
1181+ 'usernamefoundbyemail' ,
1182+ 'usernamefoundbyemailtoo' ,
1183+ ] ) ;
1184+ } ) ;
1185+ } ) ;
1186+
10051187 describe ( 'deleteLAbel()' , ( ) => {
10061188 it ( 'does not throw' , async ( ) => {
10071189 expect ( await bitbucket . deleteLabel ( 5 , 'renovate' ) ) . toMatchSnapshot ( ) ;
0 commit comments