-
Notifications
You must be signed in to change notification settings - Fork 226
Add database configuration store #1691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
254f690
Add database configuration store
shati-patel 79473ad
Make `secexp` team codeowners for the new databases panel
shati-patel d7e30be
Abbreviate "database" -> "db"
shati-patel 3ef596f
Always re-create empty config, and a couple of simplifications
charisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| **/* @github/codeql-vscode-reviewers | ||
| **/remote-queries/ @github/code-scanning-secexp-reviewers | ||
| **/variant-analysis/ @github/code-scanning-secexp-reviewers | ||
| **/databases/ @github/code-scanning-secexp-reviewers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Databases | ||
|
|
||
| This folder contains code for the new experimental databases panel and new query run experience. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import * as fs from 'fs-extra'; | ||
| import * as path from 'path'; | ||
| import { cloneDbConfig, DbConfig } from './db-config'; | ||
|
|
||
| export class DbConfigStore { | ||
| private readonly configPath: string; | ||
|
|
||
| private config: DbConfig; | ||
|
|
||
| public constructor(workspaceStoragePath: string) { | ||
| this.configPath = path.join(workspaceStoragePath, 'dbconfig.json'); | ||
|
|
||
| this.config = this.createEmptyConfig(); | ||
| } | ||
|
|
||
| public async initialize(): Promise<void> { | ||
| await this.loadConfig(); | ||
| } | ||
|
|
||
| public getConfig(): DbConfig { | ||
| // Clone the config so that it's not modified outside of this class. | ||
| return cloneDbConfig(this.config); | ||
| } | ||
|
|
||
| private async loadConfig(): Promise<void> { | ||
| if (!await fs.pathExists(this.configPath)) { | ||
| await fs.writeJSON(this.configPath, this.createEmptyConfig(), { spaces: 2 }); | ||
| } | ||
|
|
||
| await this.readConfig(); | ||
| } | ||
|
|
||
| private async readConfig(): Promise<void> { | ||
| this.config = await fs.readJSON(this.configPath); | ||
| } | ||
|
|
||
| private createEmptyConfig(): DbConfig { | ||
| return { | ||
| remote: { | ||
| repositoryLists: [], | ||
| owners: [], | ||
| repositories: [], | ||
| } | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Contains models for the data we want to store in the database config | ||
|
|
||
| export interface DbConfig { | ||
| remote: RemoteDbConfig; | ||
| } | ||
|
|
||
| export interface RemoteDbConfig { | ||
| repositoryLists: RemoteRepositoryList[]; | ||
| owners: string[]; | ||
| repositories: string[]; | ||
| } | ||
|
|
||
| export interface RemoteRepositoryList { | ||
| name: string; | ||
| repositories: string[]; | ||
| } | ||
|
|
||
| export function cloneDbConfig(config: DbConfig): DbConfig { | ||
| return { | ||
| remote: { | ||
| repositoryLists: config.remote.repositoryLists.map((list) => ({ | ||
| name: list.name, | ||
| repositories: [...list.repositories], | ||
| })), | ||
| owners: [...config.remote.owners], | ||
| repositories: [...config.remote.repositories], | ||
| } | ||
| }; | ||
| } |
12 changes: 12 additions & 0 deletions
12
extensions/ql-vscode/test/pure-tests/databases/data/dbconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "remote": { | ||
| "repositoryLists": [ | ||
| { | ||
| "name": "repoList1", | ||
| "repositories": ["foo/bar", "foo/baz"] | ||
| } | ||
| ], | ||
| "owners": [], | ||
| "repositories": ["owner/repo1", "owner/repo2", "owner/repo3"] | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
extensions/ql-vscode/test/pure-tests/databases/db-config-store.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import * as fs from 'fs-extra'; | ||
| import * as path from 'path'; | ||
| import { DbConfigStore } from '../../../src/databases/db-config-store'; | ||
| import { expect } from 'chai'; | ||
|
|
||
|
|
||
| describe('db config store', async () => { | ||
| const workspaceStoragePath = path.join(__dirname, 'test-workspace'); | ||
| const testStoragePath = path.join(__dirname, 'data'); | ||
|
charisk marked this conversation as resolved.
|
||
|
|
||
| beforeEach(async () => { | ||
| await fs.ensureDir(workspaceStoragePath); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await fs.remove(workspaceStoragePath); | ||
| }); | ||
|
|
||
| it('should create a new config if one does not exist', async () => { | ||
| const configPath = path.join(workspaceStoragePath, 'dbconfig.json'); | ||
|
|
||
| const configStore = new DbConfigStore(workspaceStoragePath); | ||
| await configStore.initialize(); | ||
|
|
||
| expect(await fs.pathExists(configPath)).to.be.true; | ||
| const config = configStore.getConfig(); | ||
| expect(config.remote.repositoryLists).to.be.empty; | ||
| expect(config.remote.owners).to.be.empty; | ||
| expect(config.remote.repositories).to.be.empty; | ||
| }); | ||
|
|
||
| it('should load an existing config', async () => { | ||
| const configStore = new DbConfigStore(testStoragePath); | ||
| await configStore.initialize(); | ||
|
|
||
| const config = configStore.getConfig(); | ||
| expect(config.remote.repositoryLists).to.have.length(1); | ||
| expect(config.remote.repositoryLists[0]).to.deep.equal({ | ||
| 'name': 'repoList1', | ||
| 'repositories': ['foo/bar', 'foo/baz'] | ||
| }); | ||
| expect(config.remote.owners).to.be.empty; | ||
| expect(config.remote.repositories).to.have.length(3); | ||
| expect(config.remote.repositories).to.deep.equal(['owner/repo1', 'owner/repo2', 'owner/repo3']); | ||
| }); | ||
|
|
||
| it('should not allow modification of the config', async () => { | ||
| const configStore = new DbConfigStore(testStoragePath); | ||
| await configStore.initialize(); | ||
|
|
||
| const config = configStore.getConfig(); | ||
| config.remote.repositoryLists = []; | ||
|
|
||
| const reRetrievedConfig = configStore.getConfig(); | ||
| expect(reRetrievedConfig.remote.repositoryLists).to.have.length(1); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.