Skip to content

Commit db2d1a1

Browse files
authored
use camel case for configuration (#29)
1 parent cfac5aa commit db2d1a1

7 files changed

Lines changed: 180 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## 4.1.0 - 2024-04-13
11+
12+
### Changed
13+
14+
- Use camel case for configuration. Deprecate old configuration options (#29).
15+
1016
## 4.0.0 - 2024-04-11
1117

1218
### Added

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Quickly see GitHub Code Owners for the current file. Add syntax highlighting for CODEOWNERS files.",
55
"publisher": "chdsbd",
66
"license": "SEE LICENSE IN LICENSE",
7-
"version": "4.0.0",
7+
"version": "4.1.0",
88
"icon": "images/logo256.png",
99
"homepage": "https://github.com/chdsbd/vscode-github-code-owners/blob/master/README.md",
1010
"keywords": [
@@ -62,18 +62,93 @@
6262
"configuration": {
6363
"title": "GitHub Code Owners",
6464
"properties": {
65+
"githubCodeOwners.format.enabled": {
66+
"type": "boolean",
67+
"default": false,
68+
"order": 1,
69+
"description": "Whether or not to enable formatting."
70+
},
71+
"githubCodeOwners.format.alignmentOffset": {
72+
"type": "number",
73+
"default": 4,
74+
"minimum": 1,
75+
"order": 2,
76+
"description": "Space offset to use from the longest file pattern the first code owner when aligning."
77+
},
78+
"githubCodeOwners.teamMapping.slack": {
79+
"order": 3,
80+
"type": "array",
81+
"default": [],
82+
"markdownDescription": "Map GitHub teams to Slack channels.",
83+
"items": {
84+
"type": "object",
85+
"required": [
86+
"username",
87+
"domain",
88+
"channel"
89+
],
90+
"propertyNames": [
91+
"username",
92+
"domain",
93+
"channel"
94+
],
95+
"properties": {
96+
"username": {
97+
"type": "string",
98+
"examples": [
99+
"@acme-corp/frontend"
100+
],
101+
"markdownDescription": "GitHub username",
102+
"pattern": "^@",
103+
"patternErrorMessage": "GitHub usernames must start with @",
104+
"required": true
105+
},
106+
"domain": {
107+
"type": "string",
108+
"markdownDescription": "Slack domain",
109+
"examples": [
110+
"acme-corp.slack.com"
111+
],
112+
"required": true
113+
},
114+
"channel": {
115+
"type": "string",
116+
"markdownDescription": "Slack channel",
117+
"examples": [
118+
"#eng-frontend"
119+
],
120+
"pattern": "^#",
121+
"patternErrorMessage": "Slack channels must start with #",
122+
"required": true
123+
}
124+
},
125+
"examples": [
126+
{
127+
"username": "@acme-corp/frontend",
128+
"domain": "acme-corp.slack.com",
129+
"channel": "#eng-frontend"
130+
}
131+
]
132+
}
133+
},
65134
"github-code-owners.format.enabled": {
135+
"markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.enabled#` instead.",
136+
"deprecationMessage": "Deprecated: Please use githubCodeOwners.format.enabled instead.",
66137
"type": "boolean",
67138
"default": false,
68139
"description": "Whether or not to enable formatting."
69140
},
70141
"github-code-owners.format.alignment-offset": {
142+
"markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.format.alignmentOffset#` instead.",
143+
"deprecationMessage": "Deprecated: Please use githubCodeOwners.format.alignmentOffset instead.",
71144
"type": "number",
72145
"default": 4,
73146
"minimum": 1,
74147
"description": "Space offset to use from the longest file pattern the first code owner when aligning."
75148
},
76149
"github-code-owners.team-mapping.slack": {
150+
"markdownDeprecationMessage": "**Deprecated**: Please use `#githubCodeOwners.teamMapping.slack#` instead.",
151+
"deprecationMessage": "Deprecated: Please use githubCodeOwners.teamMapping.slack instead.",
77152
"type": "array",
78153
"default": [],
79154
"markdownDescription": "Map GitHub teams to Slack channels.",

src/align-codeowners.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import trimStart from "lodash/trimStart"
33
import trimEnd from "lodash/trimEnd"
44
import trim from "lodash/trim"
55
import range from "lodash/range"
6-
import isNumber from "lodash/isNumber"
6+
import { getAlignmentOffset, isFormatEnabled } from "./configuration"
77

88
interface LineToAlign {
99
lineNum: number
@@ -80,21 +80,15 @@ export class AlignOwnersFormattingProvider
8080
token: vscode.CancellationToken,
8181
): Promise<vscode.TextEdit[]> {
8282
// Early exit if formatting is disabled or set to a bad value
83-
if (
84-
vscode.workspace
85-
.getConfiguration()
86-
.get("github-code-owners.format.enabled") !== true
87-
) {
83+
if (!isFormatEnabled()) {
8884
return []
8985
}
90-
const alinementOffset = vscode.workspace
91-
.getConfiguration()
92-
.get("github-code-owners.format.alignment-offset")
86+
const alignmentOffset = getAlignmentOffset()
9387

9488
// Check that config value for `alinementOffset` is valid before breaking things
95-
if (!isNumber(alinementOffset) || alinementOffset < 1) {
89+
if (alignmentOffset < 1) {
9690
throw Error(
97-
`Expected number greater 1 for 'github-code-owners.format.alignment-offset' but got ${alinementOffset}!`,
91+
`Expected number greater 1 for 'githubCodeOwners.format.alignmentOffset' but got ${alignmentOffset}!`,
9892
)
9993
}
10094
// Find the `maxFilePatternLength` and which lines to potentially edit
@@ -118,7 +112,7 @@ export class AlignOwnersFormattingProvider
118112
const { lineNum, ownersStartIndex, filePatternLength } = editLine
119113
// We need the + 1 because we want `alinementOffset` spaces to be between the end of the
120114
// File pattern an before the first owner starts
121-
const newOwnersStartIndex = maxFilePatternLength + alinementOffset + 1
115+
const newOwnersStartIndex = maxFilePatternLength + alignmentOffset + 1
122116
const line = document.lineAt(lineNum)
123117
if (ownersStartIndex !== newOwnersStartIndex) {
124118
acc.push(

src/configuration.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import vscode from "vscode"
2+
3+
export function getGitHubUrl(): string {
4+
/*
5+
* When using GitHub Enterprise Server, you should have a 'github-enterprise.uri'
6+
* configuration setting.
7+
*
8+
* This configuration option is provided by built in "GitHub Authentication" extension
9+
* https://github.com/microsoft/vscode/blob/ccb95fd921349023027a0df25ed291b0992b9a18/extensions/github-authentication/src/extension.ts#L10
10+
*/
11+
const setting = vscode.workspace
12+
.getConfiguration()
13+
.get<string>("github-enterprise.uri")
14+
if (!setting) {
15+
return "https://github.com"
16+
}
17+
return setting
18+
}
19+
20+
export function isFormatEnabled(): boolean {
21+
const formatEnabled = vscode.workspace
22+
.getConfiguration()
23+
.get("githubCodeOwners.format.enabled")
24+
if (typeof formatEnabled === "boolean") {
25+
return formatEnabled
26+
}
27+
const formatEnabledDeprecated = vscode.workspace
28+
.getConfiguration()
29+
.get("github-code-owners.format.enabled")
30+
if (typeof formatEnabledDeprecated === "boolean") {
31+
return formatEnabledDeprecated
32+
}
33+
return false
34+
}
35+
36+
export function getAlignmentOffset(): number {
37+
const alignmentOffset = vscode.workspace
38+
.getConfiguration()
39+
.get("githubCodeOwners.format.alignmentOffset")
40+
if (typeof alignmentOffset === "number") {
41+
return alignmentOffset
42+
}
43+
const alignmentOffsetDeprecated = vscode.workspace
44+
.getConfiguration()
45+
.get("github-code-owners.format.alignment-offset")
46+
if (typeof alignmentOffsetDeprecated === "number") {
47+
return alignmentOffsetDeprecated
48+
}
49+
return 4
50+
}
51+
52+
export type SlackMappingConfigurationItem = {
53+
domain: string
54+
channel: string
55+
username: string
56+
}
57+
58+
export function getTeamMappingSlack(): Array<SlackMappingConfigurationItem> {
59+
const setting = vscode.workspace
60+
.getConfiguration()
61+
.get<Array<SlackMappingConfigurationItem>>(
62+
"githubCodeOwners.teamMapping.slack",
63+
)
64+
if (setting != null) {
65+
return setting
66+
}
67+
return (
68+
vscode.workspace
69+
.getConfiguration()
70+
.get<Array<SlackMappingConfigurationItem>>(
71+
"github-code-owners.team-mapping.slack",
72+
) ?? []
73+
)
74+
}

src/github-usernames-link-provider.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import vscode from "vscode"
22
import { findUsernameRanges } from "./owner-name-completion-item-provider"
3+
import {
4+
SlackMappingConfigurationItem,
5+
getGitHubUrl,
6+
getTeamMappingSlack,
7+
} from "./configuration"
38

49
function githubUserToUrl(username: string): vscode.Uri {
510
const isTeamName = username.includes("/")
@@ -12,36 +17,8 @@ function githubUserToUrl(username: string): vscode.Uri {
1217
return vscode.Uri.parse(gitHubUrl + `/${username}`)
1318
}
1419

15-
function getGitHubUrl(): string {
16-
/*
17-
* When using GitHub Enterprise Server, you should have a 'github-enterprise.uri'
18-
* configuration setting.
19-
*
20-
* This configuration option is provided by built in "GitHub Authentication" extension
21-
* https://github.com/microsoft/vscode/blob/ccb95fd921349023027a0df25ed291b0992b9a18/extensions/github-authentication/src/extension.ts#L10
22-
*/
23-
const setting = vscode.workspace
24-
.getConfiguration()
25-
.get<string>("github-enterprise.uri")
26-
if (!setting) {
27-
return "https://github.com"
28-
}
29-
return setting
30-
}
31-
32-
type SlackMappingConfigurationItem = {
33-
domain: string
34-
channel: string
35-
username: string
36-
}
37-
38-
function getTeamMappingSlack() {
39-
const setting =
40-
vscode.workspace
41-
.getConfiguration()
42-
.get<Array<SlackMappingConfigurationItem>>(
43-
"github-code-owners.team-mapping.slack",
44-
) ?? []
20+
function buildMappingSlack() {
21+
const setting = getTeamMappingSlack()
4522
const mapping: Record<string, SlackMappingConfigurationItem | undefined> = {}
4623
for (const team of setting) {
4724
mapping[team.username] = team
@@ -58,7 +35,7 @@ export class GitHubUsernamesLinkProvider
5835
provideDocumentLinks(
5936
document: vscode.TextDocument,
6037
): vscode.ProviderResult<vscode.DocumentLink[]> {
61-
const slackTeamMapping = getTeamMappingSlack()
38+
const slackTeamMapping = buildMappingSlack()
6239
const links = []
6340
for (const range of findUsernameRanges(document)) {
6441
if (range) {

test/suite/align-codeowners.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ suite("AlignOwnersFormattingProvider", () => {
2929
// Ignored anyway
3030
let mockOptions: vscode.FormattingOptions
3131
let mockToken: vscode.CancellationToken
32-
const badOffSetValues = ["invalid", -1, 0] as const
32+
const badOffSetValues = [-1, 0] as const
3333

3434
beforeEach(() => {
3535
provider = new AlignOwnersFormattingProvider()
@@ -44,14 +44,14 @@ suite("AlignOwnersFormattingProvider", () => {
4444

4545
afterEach(async () => {
4646
// restore default config
47-
const settings = vscode.workspace.getConfiguration("github-code-owners")
47+
const settings = vscode.workspace.getConfiguration("githubCodeOwners")
4848
await settings.update(
4949
"format.enabled",
5050
true,
5151
vscode.ConfigurationTarget.Global,
5252
)
5353
await settings.update(
54-
"format.alignment-offset",
54+
"format.alignmentOffset",
5555
4,
5656
vscode.ConfigurationTarget.Global,
5757
)
@@ -73,7 +73,7 @@ suite("AlignOwnersFormattingProvider", () => {
7373
})
7474

7575
test("should not edit when formatting is disabled", async () => {
76-
const settings = vscode.workspace.getConfiguration("github-code-owners")
76+
const settings = vscode.workspace.getConfiguration("githubCodeOwners")
7777
await settings.update(
7878
"format.enabled",
7979
false,
@@ -94,9 +94,9 @@ suite("AlignOwnersFormattingProvider", () => {
9494

9595
badOffSetValues.forEach((badOffSetValue) => {
9696
test(`should throw an error when alignment offset is not a positive number: ${badOffSetValue}`, async () => {
97-
const settings = vscode.workspace.getConfiguration("github-code-owners")
97+
const settings = vscode.workspace.getConfiguration("githubCodeOwners")
9898
await settings.update(
99-
"format.alignment-offset",
99+
"format.alignmentOffset",
100100
badOffSetValue,
101101
vscode.ConfigurationTarget.Global,
102102
)
@@ -138,9 +138,9 @@ suite("AlignOwnersFormattingProvider", () => {
138138
})
139139

140140
test("should create same output as 'expected-formatted-with-offset-8'", async () => {
141-
const settings = vscode.workspace.getConfiguration("github-code-owners")
141+
const settings = vscode.workspace.getConfiguration("githubCodeOwners")
142142
await settings.update(
143-
"format.alignment-offset",
143+
"format.alignmentOffset",
144144
8,
145145
vscode.ConfigurationTarget.Global,
146146
)

0 commit comments

Comments
 (0)