Skip to content

Commit

Permalink
use camel case for configuration (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
chdsbd authored May 14, 2024
1 parent cfac5aa commit db2d1a1
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 54 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 76 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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.",
Expand Down
18 changes: 6 additions & 12 deletions src/align-codeowners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,21 +80,15 @@ export class AlignOwnersFormattingProvider
token: vscode.CancellationToken,
): Promise<vscode.TextEdit[]> {
// 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
Expand All @@ -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(
Expand Down
74 changes: 74 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -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<string>("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<SlackMappingConfigurationItem> {
const setting = vscode.workspace
.getConfiguration()
.get<Array<SlackMappingConfigurationItem>>(
"githubCodeOwners.teamMapping.slack",
)
if (setting != null) {
return setting
}
return (
vscode.workspace
.getConfiguration()
.get<Array<SlackMappingConfigurationItem>>(
"github-code-owners.team-mapping.slack",
) ?? []
)
}
39 changes: 8 additions & 31 deletions src/github-usernames-link-provider.ts
Original file line number Diff line number Diff line change
@@ -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("/")
Expand All @@ -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<string>("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<Array<SlackMappingConfigurationItem>>(
"github-code-owners.team-mapping.slack",
) ?? []
function buildMappingSlack() {
const setting = getTeamMappingSlack()
const mapping: Record<string, SlackMappingConfigurationItem | undefined> = {}
for (const team of setting) {
mapping[team.username] = team
Expand All @@ -58,7 +35,7 @@ export class GitHubUsernamesLinkProvider
provideDocumentLinks(
document: vscode.TextDocument,
): vscode.ProviderResult<vscode.DocumentLink[]> {
const slackTeamMapping = getTeamMappingSlack()
const slackTeamMapping = buildMappingSlack()
const links = []
for (const range of findUsernameRanges(document)) {
if (range) {
Expand Down
16 changes: 8 additions & 8 deletions test/suite/align-codeowners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
)
Expand All @@ -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,
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit db2d1a1

Please sign in to comment.