未验证 提交 88c12297 编写于 作者: F Fatih Acet

Implemented create snippet feature.

Closes #25
上级 ec25e7aa
# CHANGELOG # CHANGELOG
## [0.5.0] - 2018-02-25
### Added
- [#25](https://gitlab.com/fatihacet/gitlab-vscode-extension/issues/25) Create snippet from selection or entire file.
## [0.4.3] - 2018-02-19 ## [0.4.3] - 2018-02-19
### Fixed ### Fixed
- [#19](https://gitlab.com/fatihacet/gitlab-vscode-extension/issues/19) Can't add token - [#19](https://gitlab.com/fatihacet/gitlab-vscode-extension/issues/19) Can't add token
......
...@@ -12,8 +12,9 @@ This extension integrates GitLab to VSCode by adding GitLab specific options to ...@@ -12,8 +12,9 @@ This extension integrates GitLab to VSCode by adding GitLab specific options to
- Advanced pipeline actions allows you to view pipeline on GitLab, create new pipeline, retry or cancel current pipeline. [Read more](#pipeline-actions). - Advanced pipeline actions allows you to view pipeline on GitLab, create new pipeline, retry or cancel current pipeline. [Read more](#pipeline-actions).
- Issue and MR search including simple and advanced search. [Read more](#advanced-search). - Issue and MR search including simple and advanced search. [Read more](#advanced-search).
- View MR and closing issue on GitLab with a single click from your status bar. - View MR and closing issue on GitLab with a single click from your status bar.
- View active file on GitLab with highlighting active line number and selected text block. [Read more](#open-active-file). - Create public, internal or private snippet from entire file or selection. [Read more](#create-snippet).
- Compare your branch with master and view changes on GitLab. [Read more](#compare-with-master). - Compare your branch with master and view changes on GitLab. [Read more](#compare-with-master).
- View active file on GitLab with highlighting active line number and selected text block. [Read more](#open-active-file).
- Open MR of current branch on GitLab. - Open MR of current branch on GitLab.
- Open issues assigned to you on GitLab. - Open issues assigned to you on GitLab.
- Open MRs assigned to you on GitLab. - Open MRs assigned to you on GitLab.
...@@ -111,6 +112,12 @@ If your current project is a GitLab project, extension will do the following thi ...@@ -111,6 +112,12 @@ If your current project is a GitLab project, extension will do the following thi
![_status_bar.gif](https://gitlab.com/fatihacet/gitlab-vscode-extension/raw/master/src/assets/_status-bar.gif) ![_status_bar.gif](https://gitlab.com/fatihacet/gitlab-vscode-extension/raw/master/src/assets/_status-bar.gif)
### Create snippet
You can create snippet from selection or entire file. You can also select visibility level of your snippet.
![_create-snippet.gif](https://gitlab.com/fatihacet/gitlab-vscode-extension/raw/master/src/assets/_create-snippet.gif)
### Compare with master ### Compare with master
You can see changes in your branch by comparing with `master` and see them on GitLab. You can see changes in your branch by comparing with `master` and see them on GitLab.
...@@ -118,13 +125,16 @@ You can see changes in your branch by comparing with `master` and see them on Gi ...@@ -118,13 +125,16 @@ You can see changes in your branch by comparing with `master` and see them on Gi
> Soon extension will support comparing with other branches. > Soon extension will support comparing with other branches.
### Open active file ### Open active file
This command allows you to see active file on GitLab. Extension sends active line number and selected text block to GitLab UI so you can see them highlighted. This command allows you to see active file on GitLab. Extension sends active line number and selected text block to GitLab UI so you can see them highlighted.
![_open_active_file.gif](https://gitlab.com/fatihacet/gitlab-vscode-extension/raw/master/src/assets/_open_active_file.gif) ![_open_active_file.gif](https://gitlab.com/fatihacet/gitlab-vscode-extension/raw/master/src/assets/_open_active_file.gif)
----- -----
## Configuration options ## Configuration options
**`gitlab.instanceUrl` (required: false, default: "https://gitlab.com")** **`gitlab.instanceUrl` (required: false, default: "https://gitlab.com")**
...@@ -135,21 +145,22 @@ If you are using GitLab on a custom domain, you should add this to your user set ...@@ -135,21 +145,22 @@ If you are using GitLab on a custom domain, you should add this to your user set
## What's next? ## What's next?
- `[Shipped in v0.2.2]` Pipeline link to pipeline status bar item.
- `[Shipped in v0.2.2]` View last pipeline on GitLab.
- `[Shipped in v0.3.0]` Pipeline actions menu to quickly view, retry, cancel or create a new pipeline.
- `[Shipped in v0.4.0]` Issue and MR search.
- `[Shipped in v0.4.0]` Detailed issue and MR search.
- `title:MR discussions refactor labels:frontend, discussions assignee:fatihacet`
- `[Shipped in v0.4.0]` Compare your changes with master on GitLab.
- Create snippet from selected text
- Open last commit on GitLab. - Open last commit on GitLab.
- GitLab CI config file `(.gitlab-ci.yml)` validation
- MR actions picker which will allow you to - MR actions picker which will allow you to
- Go to specific MR tab, Discussions, Commits, Pipelines, Changes. - Go to specific MR tab, Discussions, Commits, Pipelines, Changes.
- Assign MR to user. - Assign MR to user.
- View last commit. - View last commit.
- [moonshot] GitLab Dashboard tab where you can see your issues, MRs, Todos all in one place in VSCode. - [moonshot] GitLab Dashboard tab where you can see your issues, MRs, Todos all in one place in VSCode.
- [moonshot] MR diff discussions on VSCode gutter with user avatars like we have in GitLab Changes tab. - [moonshot] MR diff discussions on VSCode gutter with user avatars like we have in GitLab Changes tab.
- `[Shipped in v0.2.2]` Pipeline link to pipeline status bar item.
- `[Shipped in v0.2.2]` View last pipeline on GitLab.
- `[Shipped in v0.3.0]` Pipeline actions menu to quickly view, retry, cancel or create a new pipeline.
- `[Shipped in v0.4.0]` Issue and MR search.
- `[Shipped in v0.4.0]` Detailed issue and MR search.
- `title:MR discussions refactor labels:frontend, discussions assignee:fatihacet`
- `[Shipped in v0.4.0]` Compare your changes with master on GitLab.
- `[Shipped in v0.5.0]`Create snippet from selected text
## Contribution ## Contribution
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"name": "gitlab-workflow", "name": "gitlab-workflow",
"displayName": "GitLab Workflow", "displayName": "GitLab Workflow",
"description": "GitLab VSCode integration", "description": "GitLab VSCode integration",
"version": "0.4.3", "version": "0.5.0",
"publisher": "fatihacet", "publisher": "fatihacet",
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -91,6 +91,10 @@ ...@@ -91,6 +91,10 @@
{ {
"command": "gl.compareCurrentBranch", "command": "gl.compareCurrentBranch",
"title": "GitLab: Compare current branch with master" "title": "GitLab: Compare current branch with master"
},
{
"command": "gl.createSnippet",
"title": "GitLab: Create snippet"
} }
], ],
"configuration": { "configuration": {
......
...@@ -5,6 +5,7 @@ const tokenInput = require('./token_input'); ...@@ -5,6 +5,7 @@ const tokenInput = require('./token_input');
const gitLabService = require('./gitlab_service'); const gitLabService = require('./gitlab_service');
const pipelineActionsPicker = require('./pipeline_actions_picker'); const pipelineActionsPicker = require('./pipeline_actions_picker');
const searchInput = require('./search_input'); const searchInput = require('./search_input');
const snippetInput = require('./snippet_input');
let context = null; let context = null;
...@@ -30,6 +31,7 @@ const registerCommands = () => { ...@@ -30,6 +31,7 @@ const registerCommands = () => {
'gl.issueSearch': searchInput.showIssueSearchInput, 'gl.issueSearch': searchInput.showIssueSearchInput,
'gl.mergeRequestSearch': searchInput.showMergeRequestSearchInput, 'gl.mergeRequestSearch': searchInput.showMergeRequestSearchInput,
'gl.compareCurrentBranch': openers.compareCurrentBranch, 'gl.compareCurrentBranch': openers.compareCurrentBranch,
'gl.createSnippet': snippetInput.show,
} }
Object.keys(commands).forEach((cmd) => { Object.keys(commands).forEach((cmd) => {
......
...@@ -7,7 +7,7 @@ const statusBar = require('./status_bar'); ...@@ -7,7 +7,7 @@ const statusBar = require('./status_bar');
let glToken = null; let glToken = null;
let branchMR = null; let branchMR = null;
async function fetch(path, method = 'GET') { async function fetch(path, method = 'GET', data = null) {
const { instanceUrl } = vscode.workspace.getConfiguration('gitlab'); const { instanceUrl } = vscode.workspace.getConfiguration('gitlab');
const apiRoot = `${instanceUrl}/api/v4`; const apiRoot = `${instanceUrl}/api/v4`;
...@@ -20,9 +20,13 @@ async function fetch(path, method = 'GET') { ...@@ -20,9 +20,13 @@ async function fetch(path, method = 'GET') {
method, method,
headers: { headers: {
'PRIVATE-TOKEN': glToken, 'PRIVATE-TOKEN': glToken,
} },
}; };
if (data) {
config.formData = data;
}
const response = await request(config); const response = await request(config);
try { try {
...@@ -178,6 +182,18 @@ async function fetchMRIssues(mrId) { ...@@ -178,6 +182,18 @@ async function fetchMRIssues(mrId) {
return issues; return issues;
}; };
async function createSnippet(data) {
let snippet;
try {
snippet = await fetch(`/projects/${data.id}/snippets`, 'POST', data);
} catch (e) {
vscode.window.showInformationMessage('GitLab Workflow: Failed to create your snippet.');
}
return snippet;
};
/** /**
* @private * @private
* @param {string} token GL PAT * @param {string} token GL PAT
...@@ -193,4 +209,5 @@ exports.fetchLastPipelineForCurrentBranch = fetchLastPipelineForCurrentBranch; ...@@ -193,4 +209,5 @@ exports.fetchLastPipelineForCurrentBranch = fetchLastPipelineForCurrentBranch;
exports.fetchCurrentProject = fetchCurrentProject; exports.fetchCurrentProject = fetchCurrentProject;
exports.handlePipelineAction = handlePipelineAction; exports.handlePipelineAction = handlePipelineAction;
exports.fetchMRIssues = fetchMRIssues; exports.fetchMRIssues = fetchMRIssues;
exports.createSnippet = createSnippet;
exports._setGLToken = _setGLToken; exports._setGLToken = _setGLToken;
const vscode = require('vscode');
const opn = require('opn');
const gitLabService = require('./gitlab_service');
const visibilityOptions = [
{
label: 'Public',
type: 'public',
},
{
label: 'Internal',
type: 'internal',
},
{
label: 'Private',
type: 'private',
},
];
const contextOptions = [
{
label: 'Snippet from file',
type: 'file',
},
{
label: 'Snippet from selection',
type: 'selection',
}
]
async function createSnippet(project, editor, visibility, context) {
let content = '';
const fileName = editor.document.fileName.split('/').reverse()[0];
if (context === 'selection' && editor.selection) {
const { start, end } = editor.selection;
const endLine = start.line === end.line ? end.line + 1 : end.line;
const startPos = new vscode.Position(start.line, 0);
const endPos = new vscode.Position(endLine, 0);
const range = new vscode.Range(startPos, endPos);
content = editor.document.getText(range);
} else {
content = editor.document.getText();
}
const snippet = await gitLabService.createSnippet({
id: project.id,
title: fileName,
file_name: fileName,
code: content,
visibility,
});
opn(snippet.web_url);
}
async function showPicker() {
const editor = vscode.window.activeTextEditor;
const project = await gitLabService.fetchCurrentProject();
if (editor) {
if (project) {
const visibility = await vscode.window.showQuickPick(visibilityOptions);
if (visibility) {
const context = await vscode.window.showQuickPick(contextOptions);
if (context) {
createSnippet(project, editor, visibility.type, context.type);
}
}
} else {
vscode.window.showInformationMessage('GitLab Workflow: No GitLab project found.');
}
} else {
vscode.window.showInformationMessage('GitLab Workflow: No open file.');
}
}
exports.show = showPicker;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册