From bcbdf4606b237b88c40355403ef42f727c838c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= Date: Tue, 10 Oct 2023 16:35:39 +0800 Subject: [PATCH] feat: Sync Issue --- CHANGELOG.md | 10 ++++++++++ README.md | 8 +++++--- l10n/bundle.l10n.json | 9 ++++++--- l10n/bundle.l10n.zh-cn.json | 9 ++++++--- package.json | 24 ++++++++++++++++++++++-- package.nls.json | 1 + package.nls.zh-cn.json | 1 + src/commands.ts | 26 ++++++++++++++++++-------- src/extension.ts | 3 ++- src/file.ts | 4 ++-- src/github.ts | 22 ++++++++++++++++++++++ 11 files changed, 95 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a165b1..4ae3b9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## [Unreleased] +## [0.0.2] + +### Added + +- Github Issue Blog: Sync Issue + +### Fixed + +- fix: The top background color on the web page is causing the logo to not be displayed. + ## [0.0.1] First version. diff --git a/README.md b/README.md index ccc2953..34990fb 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ A vscode plugin to create blogs with issue. ## Install - Search for `github issue blog` in the VS Code extension marketplace. +- Click on the [link](https://marketplace.visualstudio.com/items?itemName=wtto00.github-issue-blog), go to the official extension marketplace, and click on download to install. ## Configuration @@ -20,6 +21,7 @@ If this configuration is not set, when executing the command, it will prompt the - Github Issue Blog: Create Issue - Github Issue Blog: Update Issue +- Github Issue Blog: Sync Issue 1. Global command @@ -59,6 +61,6 @@ issue_number: 24 | labels | string[] | Labels of issue | | issue_number | number | Number of issue | -- `title` is necessary. -- `labels` are not mandatory, they will be automatically created if they do not exist. -- `issue_number` is required when updating an issue. When creating an issue, the `issue_number` will be automatically updated to the number of the issue that is created. +- `title` is required when updating and creating an issue. +- `labels` are not mandatory, they will be automatically created if they do not exist when updating and creating an issue. +- `issue_number` is required when updating and synchronizing an issue. When creating an issue, the `issue_number` will be automatically updated to the number of the issue that is created. diff --git a/l10n/bundle.l10n.json b/l10n/bundle.l10n.json index 3d7e32a..09c02c7 100644 --- a/l10n/bundle.l10n.json +++ b/l10n/bundle.l10n.json @@ -3,20 +3,23 @@ "requestAuth": "Request Github authorization", "unauthorized": "Unauthorized access", "getRepository": "Get blog repository", - "createIssue": "Create issue", + "createIssue": "Creating issue", "createSuccess": "Issue creation successful", "viewInBrowser": "View in browser", - "updateIssue": "Update issue", + "updateIssue": "Updating issue", "updateSuccess": "Updated Issue successfully", + "syncIssue": "Syncing issue", + "syncSuccess": "Synchronized issue successfully", "noFileOpened": "No open files", "currentNotMD": "The current file is not of the Markdown type", "fileNotFound": "File not found: ", "titleEmpty": "The title cannot be empty", - "unkonwnIssue": "Unknown Issue", + "unkonwnIssue": "Unknown issue, please set the issue_number.", "createFail": "Failed to create issue", "updateFail": "Failed to update issue", + "syncFail": "Failed to get issue", "getRepositoryFail": "Failed to retrieve the list of repositories", "noRepository": "You have not created a GitHub repository yet", diff --git a/l10n/bundle.l10n.zh-cn.json b/l10n/bundle.l10n.zh-cn.json index 875fb78..60b8a72 100644 --- a/l10n/bundle.l10n.zh-cn.json +++ b/l10n/bundle.l10n.zh-cn.json @@ -3,20 +3,23 @@ "requestAuth": "请求Github授权", "unauthorized": "未获得授权", "getRepository": "获取博客仓库", - "createIssue": "创建Issue", + "createIssue": "正在创建Issue", "createSuccess": "创建Issue成功", "viewInBrowser": "前往浏览器查看", - "updateIssue": "更新Issue", + "updateIssue": "正在更新Issue", "updateSuccess": "更新Issue成功", + "syncIssue": "正在同步issue", + "syncSuccess": "同步Issue成功", "noFileOpened": "没有打开的文件", "currentNotMD": "当前文件不是Markdown类型", "fileNotFound": "没有找到文件:", "titleEmpty": "标题不能为空", - "unkonwnIssue": "未知的Issue", + "unkonwnIssue": "未知的Issue,请设置issue_number", "createFail": "创建Issue失败", "updateFail": "更新Issue失败", + "syncFail": "获取Issue失败", "getRepositoryFail": "获取仓库列表失败", "noRepository": "您还没有创建Github仓库", diff --git a/package.json b/package.json index 483f6c9..7909067 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "github-issue-blog", "displayName": "Github Issue Blog", "description": "A vscode plugin to create a blog with issue", - "version": "0.0.1", + "version": "0.0.2", "author": "wtto00", "publisher": "wtto00", "license": "MIT", @@ -28,7 +28,8 @@ ], "activationEvents": [ "onCommand:github-issue-blog.create", - "onCommand:github-issue-blog.update" + "onCommand:github-issue-blog.update", + "onCommand:github-issue-blog.sync" ], "main": "./out/extension.js", "l10n": "./l10n", @@ -41,6 +42,10 @@ { "command": "github-issue-blog.update", "title": "%commands.update%" + }, + { + "command": "github-issue-blog.sync", + "title": "%commands.sync%" } ], "menus": { @@ -54,6 +59,11 @@ "command": "github-issue-blog.update", "group": "1_modification", "when": "resourceLangId == markdown" + }, + { + "command": "github-issue-blog.sync", + "group": "1_modification", + "when": "resourceLangId == markdown" } ], "editor/context": [ @@ -66,6 +76,11 @@ "command": "github-issue-blog.update", "group": "1_modification", "when": "resourceLangId == markdown" + }, + { + "command": "github-issue-blog.sync", + "group": "1_modification", + "when": "resourceLangId == markdown" } ], "editor/title/context": [ @@ -78,6 +93,11 @@ "command": "github-issue-blog.update", "group": "1_modification", "when": "resourceLangId == markdown" + }, + { + "command": "github-issue-blog.sync", + "group": "1_modification", + "when": "resourceLangId == markdown" } ] }, diff --git a/package.nls.json b/package.nls.json index a346d4b..786a41b 100644 --- a/package.nls.json +++ b/package.nls.json @@ -1,6 +1,7 @@ { "commands.create": "Github Issue Blog: Create Issue", "commands.update": "Github Issue Blog: Update Issue", + "commands.sync": "Github Issue Blog: Sync Issue", "configuration.repo": "repository name of Github (eg. wtto00/github-issue-blog)" } diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index bd0061d..c8a374f 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -1,6 +1,7 @@ { "commands.create": "Github Issue Blog: 创建Issue", "commands.update": "Github Issue Blog: 更新Issue", + "commands.sync": "Github Issue Blog: 同步Issue", "configuration.repo": "博客所属的Github仓库名称(例如:wtto00/github-issue-blog)" } diff --git a/src/commands.ts b/src/commands.ts index 5249b30..5f96b99 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -54,10 +54,7 @@ export class Command { const url = await this.github!.createIssue(); this.statusBarItem.hide(); - const btn = await vscode.window.showInformationMessage( - l10n.t("createSuccess"), - l10n.t("viewInBrowser") - ); + const btn = await vscode.window.showInformationMessage(l10n.t("createSuccess"), l10n.t("viewInBrowser")); if (btn) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -78,10 +75,7 @@ export class Command { const url = await this.github!.updateIssue(); this.statusBarItem.hide(); - const btn = await vscode.window.showInformationMessage( - l10n.t("updateSuccess"), - l10n.t("viewInBrowser") - ); + const btn = await vscode.window.showInformationMessage(l10n.t("updateSuccess"), l10n.t("viewInBrowser")); if (btn) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -93,6 +87,22 @@ export class Command { } }; + syncIssue = async (uri: vscode.Uri) => { + try { + this.statusBarItem.show(); + await this.prepare(uri); + + this.statusBarItem.text = `$(sync~spin) ${l10n.t("syncIssue")}...`; + await this.github!.syncIssue(); + + this.statusBarItem.hide(); + vscode.window.showInformationMessage(l10n.t("syncSuccess")); + } catch (error) { + vscode.window.showErrorMessage((error as Error).message); + this.statusBarItem.hide(); + } + }; + getIssueList = async (uri: vscode.Uri) => { try { this.statusBarItem.show(); diff --git a/src/extension.ts b/src/extension.ts index 69551a9..0cb8bc7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,8 +9,9 @@ export async function activate(context: vscode.ExtensionContext) { const _createIssue = vscode.commands.registerCommand("github-issue-blog.create", command.createIssue); const _updateIssue = vscode.commands.registerCommand("github-issue-blog.update", command.updateIssue); + const _syncIssue = vscode.commands.registerCommand("github-issue-blog.sync", command.syncIssue); - context.subscriptions.push(_createIssue, _updateIssue); + context.subscriptions.push(_createIssue, _updateIssue, _syncIssue); } export function deactivate() { diff --git a/src/file.ts b/src/file.ts index 8d8fff9..8e97de3 100644 --- a/src/file.ts +++ b/src/file.ts @@ -53,7 +53,7 @@ export class FileUtil { this.issueData = matter(md); } - updateMDContent = async (overrideMatterData: MatterData) => { + updateMDContent = async (overrideMatterData: MatterData, content?: string) => { if (vscode.window.activeTextEditor?.document.fileName !== this.uri.fsPath) { const document = await vscode.workspace.openTextDocument(this.uri); await vscode.window.showTextDocument(document); @@ -64,7 +64,7 @@ export class FileUtil { const originText = activeTextEditor.document.getText(); this.issueData = matter(originText); const md = matter.stringify( - { content: this.issueData.content }, + { content: "\n" + (content ?? this.issueData.content).trimStart() }, { ...this.issueData.data, ...overrideMatterData } ); editBuilder.replace( diff --git a/src/github.ts b/src/github.ts index 8a1fa5c..925570a 100644 --- a/src/github.ts +++ b/src/github.ts @@ -75,6 +75,28 @@ export class Github { return res.data.html_url; } + async syncIssue() { + const { data = {} } = this.file.issueData; + const { issue_number = 0 } = data; + this.checkId(); + const res = await this.octokit.rest.issues.get({ + owner: this.repo.owner, + repo: this.repo.repo, + issue_number: issue_number, + }); + if (!res.data) throw Error(l10n.t("syncFail")); + this.file.updateMDContent( + { + title: res.data.title, + issue_number, + labels: res.data.labels + .map((label) => (typeof label === "string" ? label : label.name!)) + .filter((label) => label), + }, + res.data.body || "" + ); + } + async getIssueList() { const res = await this.octokit.rest.issues.listForRepo({ owner: this.repo.owner,