Skip to content

Commit

Permalink
feat(generateGitlabPush): generate git push with gitlab specific op…
Browse files Browse the repository at this point in the history
…tions (#32)
  • Loading branch information
Ilanaya authored Nov 8, 2022
1 parent db94d60 commit ccbce18
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
81 changes: 81 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@
{
"command": "openReferencesInView",
"title": "Open References in View"
},
{
"command": "generateGitlabPush",
"title": "Generate push with gitlab options"
},
{
"command": "generateGitlabPushAccept",
"title": "Accept created push sequence"
}
],
"configuration": {
Expand Down Expand Up @@ -468,6 +476,69 @@
"type": "boolean",
"description": "Wether to display statusbar with all occurences count (including selected) in the following format: E (case matching occurences)",
"default": false
},
"generateGitlabPush": {
"type": "object",
"description": "Generate `git push` with gitlab specific options",
"properties": {
"dynamicPushOptions": {
"type": "object",
"properties": {
"merge_request.target": {
"type": "string"
},
"merge_request.title": {
"type": "string",
"default": ""
},
"merge_request.description": {
"type": "string",
"default": ""
},
"merge_request.milestone": {
"type": "string",
"default": ""
},
"merge_request.label": {
"type": "string",
"default": ""
},
"merge_request.unlabel": {
"type": "string",
"default": ""
},
"merge_request.assign": {
"type": "string",
"default": ""
},
"merge_request.unassign": {
"type": "string",
"default": ""
}
}
},
"staticPushOptions": {
"type": "object",
"properties": {
"merge_request.create": {
"type": "boolean",
"default": false
},
"merge_request.merge_when_pipeline_succeeds":{
"type": "boolean",
"default": false
},
"merge_request.remove_source_branch": {
"type": "boolean",
"default": false
},
"merge_request.draft": {
"type": "boolean",
"default": false
}
}
}
}
}
}
},
Expand Down Expand Up @@ -508,6 +579,10 @@
{
"command": "renameVariablePartsPartMoveDown",
"when": "false"
},
{
"command": "generateGitlabPushAccept",
"when": "false"
}
]
},
Expand Down Expand Up @@ -553,6 +628,12 @@
"key": "ctrl+alt+k",
"mac": "cmd+shift+k",
"when": "zardoyExperiments.renameVariablePartsOpened"
},
{
"command": "zardoyExperiments.generateGitlabPushAccept",
"key": "ctrl+o",
"mac": "cmd+o",
"when": "zardoyExperiments.generateGitlabPushOpened"
}
]
},
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import tsHighlightedKeywordsReferences from './features/tsHighlightedKeywordsRef
import autoRenameJsx from './features/autoRenameJsx'
import openReferencesInView from './features/openReferencesInView'
import statusbarOccurrencesCount from './features/statusbarOccurrencesCount'
import generateGitlabPush from './features/generateGitlabPush'

export const activate = () => {
void initGitApi()
Expand Down Expand Up @@ -131,6 +132,7 @@ export const activate = () => {
autoRenameJsx()
openReferencesInView()
statusbarOccurrencesCount()
generateGitlabPush()

registerExtensionCommand('openUrl', async (_, url: string) => {
// to test: https://regex101.com/?regex=.%2B%3A.%2B%3B?&flags=gi
Expand Down
101 changes: 101 additions & 0 deletions src/features/generateGitlabPush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as vscode from 'vscode'
import { getExtensionContributionsPrefix, getExtensionSetting, registerExtensionCommand, RegularCommands } from 'vscode-framework'

export default () => {
registerExtensionCommand('generateGitlabPush', async () => {
// https://docs.gitlab.com/ee/user/project/push_options.html

const quickPick = vscode.window.createQuickPick()
const { dynamicPushOptions = {}, staticPushOptions = {} } = getExtensionSetting('generateGitlabPush')

let editingIndex: number | undefined

const normallizeStaticOptions = () =>
Object.keys(staticPushOptions)
.map(key => ` -o ${key}`)
.join('')
const normallizeDynamicOptions = () =>
Object.entries(dynamicPushOptions)
.filter(([, value]) => value)
.map(([key, value]) => ` -o ${key}=${value!}`)
.join('')

const getOptionsNames = () => [...Object.keys(dynamicPushOptions), ...Object.keys(staticPushOptions)]

const updateMainTitle = () => {
quickPick.title = `git push${normallizeDynamicOptions()}${normallizeStaticOptions()}`
}

const setActiveItem = (existingIndex: number) => {
// do it in next loop after items update (most probably)
setTimeout(() => {
quickPick.activeItems = [quickPick.items[existingIndex]!]
})
}

const resetItems = () => {
// Currently remove static options from quick pick as managing them looks tricky
// quickPick.items = [...Object.keys(dynamicPushOptions), ...Object.keys(staticPushOptions)].map(option => ({ label: option }))
quickPick.items = Object.keys(dynamicPushOptions).map(option => ({ label: option }))
if (editingIndex !== undefined) setActiveItem(editingIndex)
updateMainTitle()
editingIndex = undefined
}

const updatePushOption = (option: string, value: string) => {
if (option in dynamicPushOptions) {
if (!(value.startsWith('"') && value.endsWith('"'))) value = `"${value}"`
dynamicPushOptions[option] = value
}
}

const registerCommand = (command: keyof RegularCommands, handler: () => void) =>
vscode.commands.registerCommand(`${getExtensionContributionsPrefix()}${command}`, handler)

const mainDisposable = vscode.Disposable.from(
quickPick,
registerCommand('generateGitlabPushAccept', () => {
if (!quickPick.title) return

const terminal = vscode.window.createTerminal()
terminal.sendText(quickPick.title)
terminal.show()
// dispose terminal in success cases?
// terminal.dispose()
}),
{
dispose() {
void vscode.commands.executeCommand('setContext', 'zardoyExperiments.generateGitlabPushOpened', false)
},
},
)

quickPick.onDidAccept(() => {
const activeItem = quickPick.activeItems[0]!
if (editingIndex === undefined) {
if (!activeItem) return
editingIndex = quickPick.items.indexOf(activeItem)
const { label } = activeItem
const editingOption = [...Object.entries(dynamicPushOptions), ...Object.entries(dynamicPushOptions)].find(
([option, value]) => option === label,
)!
quickPick.items = []
quickPick.title = `Changing option: ${label}`
quickPick.value = editingOption[1]!
return
}

const changingOption = getOptionsNames()[editingIndex]!
updatePushOption(changingOption, quickPick.value)
resetItems()
quickPick.value = ''
})

quickPick.onDidHide(() => {
mainDisposable.dispose()
})
await vscode.commands.executeCommand('setContext', 'zardoyExperiments.generateGitlabPushOpened', true)
resetItems()
quickPick.show()
})
}

0 comments on commit ccbce18

Please sign in to comment.