@@ -76,44 +76,6 @@ export interface JiraTargetFilter {
7676export type JiraProjectResponseItem =
7777 GetConnectionsResponseItem < JiraProjectConnectionState > ;
7878
79- function validateJiraConnectionState (
80- state : unknown ,
81- ) : JiraProjectConnectionState {
82- const { id, url, commandPrefix, priority } =
83- state as Partial < JiraProjectConnectionState > ;
84- if ( id !== undefined && typeof id !== "string" ) {
85- throw new ApiError ( "Expected 'id' to be a string" , ErrCode . BadValue ) ;
86- }
87- if ( url === undefined ) {
88- throw new ApiError ( "Expected a 'url' property" , ErrCode . BadValue ) ;
89- }
90- if ( commandPrefix ) {
91- if ( typeof commandPrefix !== "string" ) {
92- throw new ApiError (
93- "Expected 'commandPrefix' to be a string" ,
94- ErrCode . BadValue ,
95- ) ;
96- }
97- if ( commandPrefix . length < 2 || commandPrefix . length > 24 ) {
98- throw new ApiError (
99- "Expected 'commandPrefix' to be between 2-24 characters" ,
100- ErrCode . BadValue ,
101- ) ;
102- }
103- }
104- let { events } = state as Partial < JiraProjectConnectionState > ;
105- if ( ! events || ( events [ 0 ] as string ) == "issue.created" ) {
106- // migration
107- events = [ "issue_created" ] ;
108- } else if ( events . find ( ( ev ) => ! JiraAllowedEvents . includes ( ev ) ) ?. length ) {
109- throw new ApiError (
110- `'events' can only contain ${ JiraAllowedEvents . join ( ", " ) } ` ,
111- ErrCode . BadValue ,
112- ) ;
113- }
114- return { id, url, commandPrefix, events, priority } ;
115- }
116-
11779const log = new Logger ( "JiraProjectConnection" ) ;
11880const md = new markdownit ( ) ;
11981
@@ -138,6 +100,42 @@ export class JiraProjectConnection
138100 static botCommands : BotCommands ;
139101 static helpMessage : ( cmdPrefix ?: string ) => MatrixMessageContent ;
140102
103+ public static validateState ( state : unknown ) : JiraProjectConnectionState {
104+ const { id, url, commandPrefix, priority } =
105+ state as Partial < JiraProjectConnectionState > ;
106+ if ( id !== undefined && typeof id !== "string" ) {
107+ throw new ApiError ( "Expected 'id' to be a string" , ErrCode . BadValue ) ;
108+ }
109+ if ( url === undefined ) {
110+ throw new ApiError ( "Expected a 'url' property" , ErrCode . BadValue ) ;
111+ }
112+ if ( commandPrefix ) {
113+ if ( typeof commandPrefix !== "string" ) {
114+ throw new ApiError (
115+ "Expected 'commandPrefix' to be a string" ,
116+ ErrCode . BadValue ,
117+ ) ;
118+ }
119+ if ( commandPrefix . length < 2 || commandPrefix . length > 24 ) {
120+ throw new ApiError (
121+ "Expected 'commandPrefix' to be between 2-24 characters" ,
122+ ErrCode . BadValue ,
123+ ) ;
124+ }
125+ }
126+ let { events } = state as Partial < JiraProjectConnectionState > ;
127+ if ( ! events || ( events [ 0 ] as string ) == "issue.created" ) {
128+ // migration
129+ events = [ "issue_created" ] ;
130+ } else if ( events . find ( ( ev ) => ! JiraAllowedEvents . includes ( ev ) ) ?. length ) {
131+ throw new ApiError (
132+ `'events' can only contain ${ JiraAllowedEvents . join ( ", " ) } ` ,
133+ ErrCode . BadValue ,
134+ ) ;
135+ }
136+ return { id, url, commandPrefix, events, priority } ;
137+ }
138+
141139 static async assertUserHasAccessToProject (
142140 tokenStore : UserTokenStore ,
143141 userId : string ,
@@ -189,7 +187,7 @@ export class JiraProjectConnection
189187 ErrCode . DisabledFeature ,
190188 ) ;
191189 }
192- const validData = validateJiraConnectionState ( data ) ;
190+ const validData = JiraProjectConnection . validateState ( data ) ;
193191 log . info (
194192 `Attempting to provisionConnection for ${ roomId } ${ validData . url } on behalf of ${ userId } ` ,
195193 ) ;
@@ -236,7 +234,7 @@ export class JiraProjectConnection
236234 if ( ! config . jira ) {
237235 throw Error ( "JIRA is not configured" ) ;
238236 }
239- const connectionConfig = validateJiraConnectionState ( state . content ) ;
237+ const connectionConfig = JiraProjectConnection . validateState ( state . content ) ;
240238 return new JiraProjectConnection (
241239 roomId ,
242240 as ,
@@ -347,7 +345,7 @@ export class JiraProjectConnection
347345 }
348346
349347 protected validateConnectionState ( content : unknown ) {
350- return validateJiraConnectionState ( content ) ;
348+ return JiraProjectConnection . validateState ( content ) ;
351349 }
352350
353351 public ensureGrant ( sender ?: string ) {
@@ -737,7 +735,7 @@ export class JiraProjectConnection
737735 ) {
738736 // Apply previous state to the current config, as provisioners might not return "unknown" keys.
739737 config = { ...this . state , ...config } ;
740- const validatedConfig = validateJiraConnectionState ( config ) ;
738+ const validatedConfig = JiraProjectConnection . validateState ( config ) ;
741739 if ( ! validatedConfig . id ) {
742740 await this . updateProjectId ( validatedConfig , userId ) ;
743741 }
0 commit comments