66import * as vscode from 'vscode' ;
77import * as PersistentState from './common/persistentState' ;
88import { BRANCH_PUBLISH , PR_SETTINGS_NAMESPACE , QUERIES } from './common/settingKeys' ;
9+ import { DefaultQueries , isAllQuery , isLocalQuery } from './view/treeNodes/categoryNode' ;
10+ import { IQueryInfo } from './view/treeNodes/workspaceFolderNode' ;
911
1012const PROMPTS_SCOPE = 'prompts' ;
1113const PROMPT_TO_CREATE_PR_ON_PUBLISH_KEY = 'createPROnPublish' ;
1214
1315export async function migrate ( context : vscode . ExtensionContext ) {
1416 await createOnPublish ( ) ;
1517 await makeQueriesScopedToRepo ( context ) ;
18+ await addDefaultQueries ( context ) ;
1619}
1720
1821async function createOnPublish ( ) {
@@ -34,11 +37,7 @@ async function makeQueriesScopedToRepo(context: vscode.ExtensionContext) {
3437 const configuration = vscode . workspace . getConfiguration ( PR_SETTINGS_NAMESPACE ) ;
3538 const settingValue = configuration . inspect ( QUERIES ) ;
3639
37- type Query = {
38- label : string ,
39- query : string ,
40- } ;
41- const addRepoScope = ( queries : Query [ ] ) => {
40+ const addRepoScope = ( queries : IQueryInfo [ ] ) => {
4241 return queries . map ( query => {
4342 return {
4443 label : query . label ,
@@ -49,13 +48,55 @@ async function makeQueriesScopedToRepo(context: vscode.ExtensionContext) {
4948
5049 // User setting
5150 if ( ! hasMigratedUserQueries && settingValue ?. globalValue ) {
52- await configuration . update ( QUERIES , addRepoScope ( settingValue . globalValue as Query [ ] ) , vscode . ConfigurationTarget . Global ) ;
53- context . globalState . update ( HAS_MIGRATED_QUERIES , true ) ;
51+ await configuration . update ( QUERIES , addRepoScope ( settingValue . globalValue as IQueryInfo [ ] ) , vscode . ConfigurationTarget . Global ) ;
52+ }
53+ context . globalState . update ( HAS_MIGRATED_QUERIES , true ) ;
54+
55+ // Workspace setting
56+ if ( ! hasMigratedWorkspaceQueries && settingValue ?. workspaceValue ) {
57+ await configuration . update ( QUERIES , addRepoScope ( settingValue . workspaceValue as IQueryInfo [ ] ) , vscode . ConfigurationTarget . Workspace ) ;
58+ }
59+ context . workspaceState . update ( HAS_MIGRATED_QUERIES , true ) ;
60+ }
61+
62+ const HAS_MIGRATED_DEFAULT_QUERIES = 'hasMigratedDefaultQueries4' ;
63+ async function addDefaultQueries ( context : vscode . ExtensionContext ) {
64+ const hasMigratedUserQueries = context . globalState . get < boolean > ( HAS_MIGRATED_DEFAULT_QUERIES , false ) ;
65+ const hasMigratedWorkspaceQueries = context . workspaceState . get < boolean > ( HAS_MIGRATED_DEFAULT_QUERIES , false ) ;
66+ if ( hasMigratedUserQueries && hasMigratedWorkspaceQueries ) {
67+ return ;
68+ }
69+
70+ const configuration = vscode . workspace . getConfiguration ( PR_SETTINGS_NAMESPACE ) ;
71+ const settingValue = configuration . inspect ( QUERIES ) ;
72+
73+ const addNewDefaultQueries = ( queries : IQueryInfo [ ] ) => {
74+ const hasLocalQuery = queries . some ( query => isLocalQuery ( query ) ) ;
75+ const hasAllQuery = queries . some ( query => isAllQuery ( query ) ) ;
76+ if ( ! hasLocalQuery ) {
77+ queries . unshift ( {
78+ label : DefaultQueries . Queries . LOCAL ,
79+ query : DefaultQueries . Values . DEFAULT ,
80+ } ) ;
81+ }
82+ if ( ! hasAllQuery ) {
83+ queries . push ( {
84+ label : DefaultQueries . Queries . ALL ,
85+ query : DefaultQueries . Values . DEFAULT ,
86+ } ) ;
87+ }
88+ return queries ;
89+ } ;
90+
91+ // User setting
92+ if ( ! hasMigratedUserQueries && settingValue ?. globalValue ) {
93+ await configuration . update ( QUERIES , addNewDefaultQueries ( settingValue . globalValue as IQueryInfo [ ] ) , vscode . ConfigurationTarget . Global ) ;
5494 }
95+ context . globalState . update ( HAS_MIGRATED_DEFAULT_QUERIES , true ) ;
5596
5697 // Workspace setting
5798 if ( ! hasMigratedWorkspaceQueries && settingValue ?. workspaceValue ) {
58- await configuration . update ( QUERIES , addRepoScope ( settingValue . workspaceValue as Query [ ] ) , vscode . ConfigurationTarget . Workspace ) ;
59- context . workspaceState . update ( HAS_MIGRATED_QUERIES , true ) ;
99+ await configuration . update ( QUERIES , addNewDefaultQueries ( settingValue . workspaceValue as IQueryInfo [ ] ) , vscode . ConfigurationTarget . Workspace ) ;
60100 }
101+ context . workspaceState . update ( HAS_MIGRATED_DEFAULT_QUERIES , true ) ;
61102}
0 commit comments