@@ -7,6 +7,7 @@ import Gitlab from 'gitlab';
7
7
8
8
import { UserDataGroups } from './authcache' ;
9
9
import { AuthCache , UserData } from './authcache' ;
10
+ import { GitlabCache } from "./gitlabcache" ;
10
11
11
12
export type VerdaccioGitlabAccessLevel = '$guest' | '$reporter' | '$developer' | '$maintainer' | '$owner' ;
12
13
@@ -42,13 +43,16 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
42
43
private options : PluginOptions < VerdaccioGitlabConfig > ;
43
44
private config : VerdaccioGitlabConfig ;
44
45
private authCache ?: AuthCache ;
46
+ private gitlabCache : GitlabCache ;
45
47
private logger : Logger ;
46
48
private publishLevel : VerdaccioGitlabAccessLevel ;
47
49
48
50
public constructor ( config : VerdaccioGitlabConfig , options : PluginOptions < VerdaccioGitlabConfig > ) {
49
51
this . logger = options . logger ;
50
52
this . config = config ;
51
53
this . options = options ;
54
+ this . gitlabCache = new GitlabCache ( this . logger , this . config . authCache ?. ttl ) ;
55
+
52
56
this . logger . info ( `[gitlab] url: ${ this . config . url } ` ) ;
53
57
54
58
if ( ( this . config . authCache || { } ) . enabled === false ) {
@@ -89,7 +93,19 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
89
93
token : password ,
90
94
} ) ;
91
95
92
- GitlabAPI . Users . current ( )
96
+ // Check if we already have a stored promise
97
+ let promise = this . gitlabCache . getPromise ( user , password , 'user' ) ;
98
+ if ( ! promise ) {
99
+ this . logger . trace ( `[gitlab] querying gitlab user: ${ user } ` ) ;
100
+
101
+ promise = GitlabAPI . Users . current ( ) as Promise < any > ;
102
+
103
+ this . gitlabCache . storePromise ( user , password , 'user' , promise ) ;
104
+ } else {
105
+ this . logger . trace ( `[gitlab] using cached promise for user: ${ user } ` ) ;
106
+ }
107
+
108
+ promise
93
109
. then ( response => {
94
110
if ( user . toLowerCase ( ) !== response . username . toLowerCase ( ) ) {
95
111
return cb ( getUnauthorized ( 'wrong gitlab username' ) ) ;
@@ -102,15 +118,31 @@ export default class VerdaccioGitLab implements IPluginAuth<VerdaccioGitlabConfi
102
118
// - for publish, the logged in user id and all the groups they can reach as configured with access level `$auth.gitlab.publish`
103
119
const gitlabPublishQueryParams = { min_access_level : publishLevelId } ;
104
120
105
- this . logger . trace ( '[gitlab] querying gitlab user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
121
+ let groupsPromise = this . gitlabCache . getPromise ( user , password , 'groups' ) ;
122
+ if ( ! groupsPromise ) {
123
+ this . logger . trace ( '[gitlab] querying gitlab user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
124
+
125
+ groupsPromise = GitlabAPI . Groups . all ( gitlabPublishQueryParams ) . then ( groups => {
126
+ return groups . filter ( group => group . path === group . full_path ) . map ( group => group . path ) ;
127
+ } ) ;
128
+
129
+ this . gitlabCache . storePromise ( user , password , 'groups' , groupsPromise ) ;
130
+ } else {
131
+ this . logger . trace ( '[gitlab] using cached promise for user groups with params:' , gitlabPublishQueryParams . toString ( ) ) ;
132
+ }
106
133
107
- const groupsPromise = GitlabAPI . Groups . all ( gitlabPublishQueryParams ) . then ( groups => {
108
- return groups . filter ( group => group . path === group . full_path ) . map ( group => group . path ) ;
109
- } ) ;
134
+ let projectsPromise = this . gitlabCache . getPromise ( user , password , 'projects' ) ;
135
+ if ( ! projectsPromise ) {
136
+ this . logger . trace ( '[gitlab] querying gitlab user projects with params:' , gitlabPublishQueryParams . toString ( ) ) ;
110
137
111
- const projectsPromise = GitlabAPI . Projects . all ( gitlabPublishQueryParams ) . then ( projects => {
112
- return projects . map ( project => project . path_with_namespace ) ;
113
- } ) ;
138
+ projectsPromise = GitlabAPI . Projects . all ( gitlabPublishQueryParams ) . then ( projects => {
139
+ return projects . map ( project => project . path_with_namespace ) ;
140
+ } ) ;
141
+
142
+ this . gitlabCache . storePromise ( user , password , 'projects' , projectsPromise ) ;
143
+ } else {
144
+ this . logger . trace ( '[gitlab] using cached promise for user projects with params:' , gitlabPublishQueryParams . toString ( ) ) ;
145
+ }
114
146
115
147
Promise . all ( [ groupsPromise , projectsPromise ] )
116
148
. then ( ( [ groups , projectGroups ] ) => {
0 commit comments