1- const defaults = require ( './defaults' ) ;
2-
31module . exports = class Lock {
42 constructor ( context , config , logger ) {
53 this . context = context ;
6- this . config = Object . assign ( { } , defaults , config ) ;
4+ this . config = config ;
75 this . logger = logger ;
86 }
97
10- async lock ( ) {
8+ async lock ( type ) {
119 const { owner, repo} = this . context . repo ( ) ;
12- const { lockComment, lockLabel} = this . config ;
10+ const lockLabel = this . getConfigValue ( type , 'lockLabel' ) ;
11+ const lockComment = this . getConfigValue ( type , 'lockComment' ) ;
1312
14- const issues = await this . getLockableIssues ( ) ;
13+ const issues = await this . getLockableIssues ( type ) ;
1514 for ( const issue of issues ) {
1615 const issueUrl = `${ owner } /${ repo } /issues/${ issue . number } ` ;
1716 if ( lockComment ) {
@@ -47,17 +46,12 @@ module.exports = class Lock {
4746 }
4847 }
4948
50- async getLockableIssues ( ) {
51- const results = await this . search ( ) ;
52- return results . data . items . filter ( issue => ! issue . locked ) ;
53- }
54-
55- search ( ) {
49+ search ( type ) {
5650 const { owner, repo} = this . context . repo ( ) ;
57- const { exemptLabels , daysUntilLock, only } = this . config ;
58- const timestamp = this . since ( daysUntilLock )
59- . toISOString ( )
60- . replace ( / \. \d { 3 } \w $ / , '' ) ;
51+ const daysUntilLock = this . getConfigValue ( type , 'daysUntilLock' ) ;
52+ const exemptLabels = this . getConfigValue ( type , 'exemptLabels' ) ;
53+
54+ const timestamp = this . getUpdatedTimestamp ( daysUntilLock ) ;
6155
6256 let query = `repo:${ owner } /${ repo } is:closed updated:<${ timestamp } ` ;
6357 if ( exemptLabels . length ) {
@@ -66,13 +60,13 @@ module.exports = class Lock {
6660 . join ( ' ' ) ;
6761 query += ` ${ queryPart } ` ;
6862 }
69- if ( only === 'issues' ) {
63+ if ( type === 'issues' ) {
7064 query += ' is:issue' ;
71- } else if ( only === 'pulls' ) {
65+ } else {
7266 query += ' is:pr' ;
7367 }
7468
75- this . logger . info ( `[${ owner } /${ repo } ] Searching` ) ;
69+ this . logger . info ( `[${ owner } /${ repo } ] Searching ${ type } ` ) ;
7670 return this . context . github . search . issues ( {
7771 q : query ,
7872 sort : 'updated' ,
@@ -81,8 +75,21 @@ module.exports = class Lock {
8175 } ) ;
8276 }
8377
84- since ( days ) {
78+ async getLockableIssues ( type ) {
79+ const results = await this . search ( type ) ;
80+ return results . data . items . filter ( issue => ! issue . locked ) ;
81+ }
82+
83+ getUpdatedTimestamp ( days ) {
8584 const ttl = days * 24 * 60 * 60 * 1000 ;
86- return new Date ( new Date ( ) - ttl ) ;
85+ const date = new Date ( new Date ( ) - ttl ) ;
86+ return date . toISOString ( ) . replace ( / \. \d { 3 } \w $ / , '' ) ;
87+ }
88+
89+ getConfigValue ( type , key ) {
90+ if ( this . config [ type ] && typeof this . config [ type ] [ key ] !== 'undefined' ) {
91+ return this . config [ type ] [ key ] ;
92+ }
93+ return this . config [ key ] ;
8794 }
8895} ;
0 commit comments