From db2d1a14773e96d2deec9b46cfea2127bf9a7a2d Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Tue, 14 May 2024 08:39:39 -0400 Subject: [PATCH] use camel case for configuration (#29) --- CHANGELOG.md | 6 +++ package-lock.json | 4 +- package.json | 77 ++++++++++++++++++++++++++- src/align-codeowners.ts | 18 +++---- src/configuration.ts | 74 +++++++++++++++++++++++++ src/github-usernames-link-provider.ts | 39 +++----------- test/suite/align-codeowners.test.ts | 16 +++--- 7 files changed, 180 insertions(+), 54 deletions(-) create mode 100644 src/configuration.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d1cdf9c..26da346 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## 4.1.0 - 2024-04-13 + +### Changed + +- Use camel case for configuration. Deprecate old configuration options (#29). + ## 4.0.0 - 2024-04-11 ### Added diff --git a/package-lock.json b/package-lock.json index 5958464..b822d89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "github-code-owners", - "version": "4.0.0", + "version": "4.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "github-code-owners", - "version": "4.0.0", + "version": "4.1.0", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@snyk/github-codeowners": "github:chdsbd/github-codeowners#chris/line-number-information", diff --git a/package.json b/package.json index 0b806f3..ece2ea5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Quickly see GitHub Code Owners for the current file. Add syntax highlighting for CODEOWNERS files.", "publisher": "chdsbd", "license": "SEE LICENSE IN LICENSE", - "version": "4.0.0", + "version": "4.1.0", "icon": "images/logo256.png", "homepage": "https://github.com/chdsbd/vscode-github-code-owners/blob/master/README.md", "keywords": [ @@ -62,18 +62,93 @@ "configuration": { "title": "GitHub Code Owners", "properties": { + "githubCodeOwners.format.enabled": { + "type": "boolean", + "default": false, + "order": 1, + "description": "Whether or not to enable formatting." + }, + "githubCodeOwners.format.alignmentOffset": { + "type": "number", + "default": 4, + "minimum": 1, + "order": 2, + "description": "Space offset to use from the longest file pattern the first code owner when aligning." + }, + "githubCodeOwners.teamMapping.slack": { + "order": 3, + "type": "array", + "default": [], + "markdownDescription": "Map GitHub teams to Slack channels.", + "items": { + "type": "object", + "required": [ + "username", + "domain", + "channel" + ], + "propertyNames": [ + "username", + "domain", + "channel" + ], + "properties": { + "username": { + "type": "string", + "examples": [ + "@acme-corp/frontend" + ], + "markdownDescription": "GitHub username", + "pattern": "^@", + "patternErrorMessage": "GitHub usernames must start with @", + "required": true + }, + "domain": { + "type": "string", + "markdownDescription": "Slack domain", + "examples": [ + "acme-corp.slack.com" + ], + "required": true + }, + "channel": { + "type": "string", + "markdownDescription": "Slack channel", + "examples": [ + "#eng-frontend" + ], + "pattern": "^#", + "patternErrorMessage": "Slack channels must start with #", + "required": true + } + }, + "examples": [ + { + "username": "@acme-corp/frontend", + "domain": "acme-corp.slack.com", + "channel": "#eng-frontend" + } + ] + } + }, "github-code-owners.format.enabled": { + "markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.enabled#` instead.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.enabled instead.", "type": "boolean", "default": false, "description": "Whether or not to enable formatting." }, "github-code-owners.format.alignment-offset": { + "markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.alignmentOffset#` instead.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.format.alignmentOffset instead.", "type": "number", "default": 4, "minimum": 1, "description": "Space offset to use from the longest file pattern the first code owner when aligning." }, "github-code-owners.team-mapping.slack": { + "markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.teamMapping.slack#` instead.", + "deprecationMessage": "Deprecated: Please use githubCodeOwners.teamMapping.slack instead.", "type": "array", "default": [], "markdownDescription": "Map GitHub teams to Slack channels.", diff --git a/src/align-codeowners.ts b/src/align-codeowners.ts index b033172..cfd594c 100644 --- a/src/align-codeowners.ts +++ b/src/align-codeowners.ts @@ -3,7 +3,7 @@ import trimStart from "lodash/trimStart" import trimEnd from "lodash/trimEnd" import trim from "lodash/trim" import range from "lodash/range" -import isNumber from "lodash/isNumber" +import { getAlignmentOffset, isFormatEnabled } from "./configuration" interface LineToAlign { lineNum: number @@ -80,21 +80,15 @@ export class AlignOwnersFormattingProvider token: vscode.CancellationToken, ): Promise { // Early exit if formatting is disabled or set to a bad value - if ( - vscode.workspace - .getConfiguration() - .get("github-code-owners.format.enabled") !== true - ) { + if (!isFormatEnabled()) { return [] } - const alinementOffset = vscode.workspace - .getConfiguration() - .get("github-code-owners.format.alignment-offset") + const alignmentOffset = getAlignmentOffset() // Check that config value for `alinementOffset` is valid before breaking things - if (!isNumber(alinementOffset) || alinementOffset < 1) { + if (alignmentOffset < 1) { throw Error( - `Expected number greater 1 for 'github-code-owners.format.alignment-offset' but got ${alinementOffset}!`, + `Expected number greater 1 for 'githubCodeOwners.format.alignmentOffset' but got ${alignmentOffset}!`, ) } // Find the `maxFilePatternLength` and which lines to potentially edit @@ -118,7 +112,7 @@ export class AlignOwnersFormattingProvider const { lineNum, ownersStartIndex, filePatternLength } = editLine // We need the + 1 because we want `alinementOffset` spaces to be between the end of the // File pattern an before the first owner starts - const newOwnersStartIndex = maxFilePatternLength + alinementOffset + 1 + const newOwnersStartIndex = maxFilePatternLength + alignmentOffset + 1 const line = document.lineAt(lineNum) if (ownersStartIndex !== newOwnersStartIndex) { acc.push( diff --git a/src/configuration.ts b/src/configuration.ts new file mode 100644 index 0000000..c258f46 --- /dev/null +++ b/src/configuration.ts @@ -0,0 +1,74 @@ +import vscode from "vscode" + +export function getGitHubUrl(): string { + /* + * When using GitHub Enterprise Server, you should have a 'github-enterprise.uri' + * configuration setting. + * + * This configuration option is provided by built in "GitHub Authentication" extension + * https://github.com/microsoft/vscode/blob/ccb95fd921349023027a0df25ed291b0992b9a18/extensions/github-authentication/src/extension.ts#L10 + */ + const setting = vscode.workspace + .getConfiguration() + .get("github-enterprise.uri") + if (!setting) { + return "https://github.com" + } + return setting +} + +export function isFormatEnabled(): boolean { + const formatEnabled = vscode.workspace + .getConfiguration() + .get("githubCodeOwners.format.enabled") + if (typeof formatEnabled === "boolean") { + return formatEnabled + } + const formatEnabledDeprecated = vscode.workspace + .getConfiguration() + .get("github-code-owners.format.enabled") + if (typeof formatEnabledDeprecated === "boolean") { + return formatEnabledDeprecated + } + return false +} + +export function getAlignmentOffset(): number { + const alignmentOffset = vscode.workspace + .getConfiguration() + .get("githubCodeOwners.format.alignmentOffset") + if (typeof alignmentOffset === "number") { + return alignmentOffset + } + const alignmentOffsetDeprecated = vscode.workspace + .getConfiguration() + .get("github-code-owners.format.alignment-offset") + if (typeof alignmentOffsetDeprecated === "number") { + return alignmentOffsetDeprecated + } + return 4 +} + +export type SlackMappingConfigurationItem = { + domain: string + channel: string + username: string +} + +export function getTeamMappingSlack(): Array { + const setting = vscode.workspace + .getConfiguration() + .get>( + "githubCodeOwners.teamMapping.slack", + ) + if (setting != null) { + return setting + } + return ( + vscode.workspace + .getConfiguration() + .get>( + "github-code-owners.team-mapping.slack", + ) ?? [] + ) +} diff --git a/src/github-usernames-link-provider.ts b/src/github-usernames-link-provider.ts index 510ad84..c9340a6 100644 --- a/src/github-usernames-link-provider.ts +++ b/src/github-usernames-link-provider.ts @@ -1,5 +1,10 @@ import vscode from "vscode" import { findUsernameRanges } from "./owner-name-completion-item-provider" +import { + SlackMappingConfigurationItem, + getGitHubUrl, + getTeamMappingSlack, +} from "./configuration" function githubUserToUrl(username: string): vscode.Uri { const isTeamName = username.includes("/") @@ -12,36 +17,8 @@ function githubUserToUrl(username: string): vscode.Uri { return vscode.Uri.parse(gitHubUrl + `/${username}`) } -function getGitHubUrl(): string { - /* - * When using GitHub Enterprise Server, you should have a 'github-enterprise.uri' - * configuration setting. - * - * This configuration option is provided by built in "GitHub Authentication" extension - * https://github.com/microsoft/vscode/blob/ccb95fd921349023027a0df25ed291b0992b9a18/extensions/github-authentication/src/extension.ts#L10 - */ - const setting = vscode.workspace - .getConfiguration() - .get("github-enterprise.uri") - if (!setting) { - return "https://github.com" - } - return setting -} - -type SlackMappingConfigurationItem = { - domain: string - channel: string - username: string -} - -function getTeamMappingSlack() { - const setting = - vscode.workspace - .getConfiguration() - .get>( - "github-code-owners.team-mapping.slack", - ) ?? [] +function buildMappingSlack() { + const setting = getTeamMappingSlack() const mapping: Record = {} for (const team of setting) { mapping[team.username] = team @@ -58,7 +35,7 @@ export class GitHubUsernamesLinkProvider provideDocumentLinks( document: vscode.TextDocument, ): vscode.ProviderResult { - const slackTeamMapping = getTeamMappingSlack() + const slackTeamMapping = buildMappingSlack() const links = [] for (const range of findUsernameRanges(document)) { if (range) { diff --git a/test/suite/align-codeowners.test.ts b/test/suite/align-codeowners.test.ts index 98cf42c..9666207 100644 --- a/test/suite/align-codeowners.test.ts +++ b/test/suite/align-codeowners.test.ts @@ -29,7 +29,7 @@ suite("AlignOwnersFormattingProvider", () => { // Ignored anyway let mockOptions: vscode.FormattingOptions let mockToken: vscode.CancellationToken - const badOffSetValues = ["invalid", -1, 0] as const + const badOffSetValues = [-1, 0] as const beforeEach(() => { provider = new AlignOwnersFormattingProvider() @@ -44,14 +44,14 @@ suite("AlignOwnersFormattingProvider", () => { afterEach(async () => { // restore default config - const settings = vscode.workspace.getConfiguration("github-code-owners") + const settings = vscode.workspace.getConfiguration("githubCodeOwners") await settings.update( "format.enabled", true, vscode.ConfigurationTarget.Global, ) await settings.update( - "format.alignment-offset", + "format.alignmentOffset", 4, vscode.ConfigurationTarget.Global, ) @@ -73,7 +73,7 @@ suite("AlignOwnersFormattingProvider", () => { }) test("should not edit when formatting is disabled", async () => { - const settings = vscode.workspace.getConfiguration("github-code-owners") + const settings = vscode.workspace.getConfiguration("githubCodeOwners") await settings.update( "format.enabled", false, @@ -94,9 +94,9 @@ suite("AlignOwnersFormattingProvider", () => { badOffSetValues.forEach((badOffSetValue) => { test(`should throw an error when alignment offset is not a positive number: ${badOffSetValue}`, async () => { - const settings = vscode.workspace.getConfiguration("github-code-owners") + const settings = vscode.workspace.getConfiguration("githubCodeOwners") await settings.update( - "format.alignment-offset", + "format.alignmentOffset", badOffSetValue, vscode.ConfigurationTarget.Global, ) @@ -138,9 +138,9 @@ suite("AlignOwnersFormattingProvider", () => { }) test("should create same output as 'expected-formatted-with-offset-8'", async () => { - const settings = vscode.workspace.getConfiguration("github-code-owners") + const settings = vscode.workspace.getConfiguration("githubCodeOwners") await settings.update( - "format.alignment-offset", + "format.alignmentOffset", 8, vscode.ConfigurationTarget.Global, )