Skip to content

Commit

Permalink
add slack channel mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
cdignam-segment committed May 10, 2024
1 parent c5ad1ba commit 47cb8e7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"files.associations": {
"test/data/**/*": "codeowners"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[codeowners]": {
// never autoformat test files
"editor.defaultFormatter": null
Expand Down
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]

## 3.4.0 - 2024-04-10

### Added

- Slack channel mapping via `github-code-owners.team-mapping.slack` setting.

## 3.3.0 - 2023-11-15

### 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.

48 changes: 47 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": "3.3.0",
"version": "3.4.0",
"icon": "images/logo256.png",
"homepage": "https://github.com/chdsbd/vscode-github-code-owners/blob/master/README.md",
"keywords": [
Expand Down Expand Up @@ -72,6 +72,52 @@
"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": {
"type": "array",
"default": [],
"markdownDescription": "Map GitHub teams to Slack channels.",
"items": {
"type": "object",
"properties": {
"team": {
"type": "string",
"examples": [
"@acme-corp/frontend"
],
"markdownDescription": "GitHub team",
"pattern": "^@",
"patternErrorMessage": "GitHub teams 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
}
},
"required": true,
"examples": [
{
"team": "@acme-corp/frontend",
"domain": "acme-corp.slack.com",
"channel": "#eng-frontend"
}
]
}
}
}
}
Expand Down
41 changes: 39 additions & 2 deletions src/github-usernames-link-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ function getGitHubUrl(): string {
return setting
}

type SlackMappingConfigurationItem = {
domain: string
channel: string
team: string
}

function getTeamMappingSlack() {
const setting =
vscode.workspace
.getConfiguration()
.get<Array<SlackMappingConfigurationItem>>(
"github-code-owners.team-mapping.slack",
) ?? []
const mapping: Record<string, SlackMappingConfigurationItem | undefined> = {}
for (const team of setting) {
mapping[team.team] = team
}
return mapping
}

/**
* Add links to usernames in CODEOWNERS file that open on GitHub.
*/
Expand All @@ -38,6 +58,7 @@ export class GitHubUsernamesLinkProvider
provideDocumentLinks(
document: vscode.TextDocument,
): vscode.ProviderResult<vscode.DocumentLink[]> {
const slackTeamMapping = getTeamMappingSlack()
const links = []
for (const range of findUsernameRanges(document)) {
if (range) {
Expand All @@ -52,9 +73,25 @@ export class GitHubUsernamesLinkProvider
range,
githubUserToUrl(username.replace(/^@/, "")),
)
link.tooltip = `View ${username} on Github`

link.tooltip = `Open ${username} on Github`
links.push(link)

const slackMapping = slackTeamMapping[username]
if (slackMapping) {
const linkslack = new vscode.DocumentLink(
range,
// https://api.slack.com/reference/deep-linking
// https://acme-corp.slack.com/channels/eng-frontend
vscode.Uri.parse(
`https://${
slackMapping.domain
}/app_redirect?channel=${slackMapping.channel.replace(/^#/, "")}`,
),
)
linkslack.tooltip = `Open ${slackMapping.channel} on Slack`

links.push(linkslack)
}
}
}
return links
Expand Down

0 comments on commit 47cb8e7

Please sign in to comment.