diff --git a/core/language/en-GB/ControlPanel.multids b/core/language/en-GB/ControlPanel.multids index c81381aea..4ccb30787 100644 --- a/core/language/en-GB/ControlPanel.multids +++ b/core/language/en-GB/ControlPanel.multids @@ -91,15 +91,18 @@ Saving/DownloadSaver/Hint: These settings apply to the HTML5-compatible download Saving/General/Caption: General Saving/General/Hint: These settings apply to all the loaded savers Saving/Hint: Settings used for saving the entire TiddlyWiki as a single file via a saver module -Saving/GitHub/Branch: Target branch for saving (defaults to `master`) -Saving/GitHub/Caption: ~GitHub Saver -Saving/GitHub/Description: These settings are only used when saving to ~GitHub -Saving/GitHub/ServerURL: Server URL (defaults to `https://api.github.com`) -Saving/GitHub/Filename: Filename of target file (e.g. `index.html`) -Saving/GitHub/Password: Password, OAUTH token, or personal access token -Saving/GitHub/Path: Path to target file (e.g. `/wiki/`) -Saving/GitHub/Repo: Target repository (e.g. `Jermolene/TiddlyWiki5`) -Saving/GitHub/UserName: Username +Saving/GitService/Branch: Target branch for saving +Saving/GitService/CommitMessage: Saved by TiddlyWiki +Saving/GitService/Description: These settings are only used when saving to <> +Saving/GitService/Filename: Filename of target file (e.g. `index.html`) +Saving/GitService/Path: Path to target file (e.g. `/wiki/`) +Saving/GitService/Repo: Target repository (e.g. `Jermolene/TiddlyWiki5`) +Saving/GitService/ServerURL: Server API URL +Saving/GitService/UserName: Username +Saving/GitService/GitHub/Caption: ~GitHub Saver +Saving/GitService/GitHub/Password: Password, OAUTH token, or personal access token (see [[GitHub help page|https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line]] for details) +Saving/GitService/GitLab/Caption: ~GitLab Saver +Saving/GitService/GitLab/Password: Personal access token for API (see [[GitLab help page|https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html for details]] for details) Saving/TiddlySpot/Advanced/Heading: Advanced Settings Saving/TiddlySpot/BackupDir: Backup Directory Saving/TiddlySpot/Backups: Backups diff --git a/core/modules/savers/github.js b/core/modules/savers/github.js index 892ba4847..d7f1b06d8 100644 --- a/core/modules/savers/github.js +++ b/core/modules/savers/github.js @@ -9,7 +9,7 @@ Saves wiki by pushing a commit to the GitHub v3 REST API (function(){ /*jslint node: true, browser: true */ -/*global $tw: false */ +/*global $tw: true */ "use strict"; /* @@ -57,7 +57,7 @@ GitHubSaver.prototype.save = function(text,method,callback) { callback: function(err,getResponseDataJson,xhr) { var getResponseData,sha = ""; if(err && xhr.status !== 404) { - return callback(err); + return callback(err); } if(xhr.status !== 404) { getResponseData = JSON.parse(getResponseDataJson); @@ -65,14 +65,14 @@ GitHubSaver.prototype.save = function(text,method,callback) { if(details.name === filename) { sha = details.sha; } - }); + }); } var data = { - message: "Saved by TiddlyWiki", - content: $tw.utils.base64Encode(text), - branch: branch, - sha: sha - }; + message: $tw.language.getRawString("ControlPanel/Saving/GitService/CommitMessage"), + content: $tw.utils.base64Encode(text), + branch: branch, + sha: sha + }; // Perform a PUT request to save the file $tw.utils.httpRequest({ url: uri + filename, diff --git a/core/modules/savers/gitlab.js b/core/modules/savers/gitlab.js new file mode 100644 index 000000000..fcb3b51e8 --- /dev/null +++ b/core/modules/savers/gitlab.js @@ -0,0 +1,120 @@ +/*\ +title: $:/core/modules/savers/gitlab.js +type: application/javascript +module-type: saver + +Saves wiki by pushing a commit to the GitLab REST API + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: true */ +"use strict"; + +/* +Select the appropriate saver module and set it up +*/ +var GitLabSaver = function(wiki) { + this.wiki = wiki; +}; + +GitLabSaver.prototype.save = function(text,method,callback) { + /* See https://docs.gitlab.com/ee/api/repository_files.html */ + var self = this, + username = this.wiki.getTiddlerText("$:/GitLab/Username"), + password = $tw.utils.getPassword("gitlab"), + repo = this.wiki.getTiddlerText("$:/GitLab/Repo"), + path = this.wiki.getTiddlerText("$:/GitLab/Path"), + filename = this.wiki.getTiddlerText("$:/GitLab/Filename"), + branch = this.wiki.getTiddlerText("$:/GitLab/Branch") || "master", + endpoint = this.wiki.getTiddlerText("$:/GitLab/ServerURL") || "https://gitlab.com/api/v4", + headers = { + "Content-Type": "application/json;charset=UTF-8", + "Private-Token": password + }; + // Bail if we don't have everything we need + if(!username || !password || !repo || !path || !filename) { + return false; + } + // Make sure the path start and ends with a slash + if(path.substring(0,1) !== "/") { + path = "/" + path; + } + if(path.substring(path.length - 1) !== "/") { + path = path + "/"; + } + // Compose the base URI + var uri = endpoint + "/projects/" + encodeURIComponent(repo) + "/repository/"; + // Perform a get request to get the details (inc shas) of files in the same path as our file + $tw.utils.httpRequest({ + url: uri + "tree/" + encodeURIComponent(path.replace(/^\/+|\/$/g, '')), + type: "GET", + headers: headers, + data: { + ref: branch + }, + callback: function(err,getResponseDataJson,xhr) { + var getResponseData,sha = ""; + if(err && xhr.status !== 404) { + return callback(err); + } + var requestType = "POST"; + if(xhr.status !== 404) { + getResponseData = JSON.parse(getResponseDataJson); + $tw.utils.each(getResponseData,function(details) { + if(details.name === filename) { + requestType = "PUT"; + sha = details.sha; + } + }); + } + var data = { + commit_message: $tw.language.getRawString("ControlPanel/Saving/GitService/CommitMessage"), + content: $tw.utils.base64Encode(text), + branch: branch, + sha: sha + }; + // Perform a request to save the file + $tw.utils.httpRequest({ + url: uri + "files/" + encodeURIComponent(path.replace(/^\/+/, '') + filename), + type: requestType, + headers: headers, + data: JSON.stringify(data), + callback: function(err,putResponseDataJson,xhr) { + if(err) { + return callback(err); + } + var putResponseData = JSON.parse(putResponseDataJson); + callback(null); + } + }); + } + }); + return true; +}; + +/* +Information about this saver +*/ +GitLabSaver.prototype.info = { + name: "gitlab", + priority: 2000, + capabilities: ["save", "autosave"] +}; + +/* +Static method that returns true if this saver is capable of working +*/ +exports.canSave = function(wiki) { + return true; +}; + +/* +Create an instance of this saver +*/ +exports.create = function(wiki) { + return new GitLabSaver(wiki); +}; + +})(); diff --git a/core/ui/ControlPanel/Saving/GitHub.tid b/core/ui/ControlPanel/Saving/GitHub.tid index 5e2bc7853..88d6f947f 100644 --- a/core/ui/ControlPanel/Saving/GitHub.tid +++ b/core/ui/ControlPanel/Saving/GitHub.tid @@ -1,15 +1,16 @@ title: $:/core/ui/ControlPanel/Saving/GitHub tags: $:/tags/ControlPanel/Saving -caption: {{$:/language/ControlPanel/Saving/GitHub/Caption}} +caption: {{$:/language/ControlPanel/Saving/GitService/GitHub/Caption}} -\define lingo-base() $:/language/ControlPanel/Saving/GitHub/ +\define lingo-base() $:/language/ControlPanel/Saving/GitService/ +\define service-name() ~GitHub <> |<> |<$edit-text tiddler="$:/GitHub/Username" default="" tag="input"/> | -|<> |<$password name="github"/> | +|<> |<$password name="github"/> | |<> |<$edit-text tiddler="$:/GitHub/Repo" default="" tag="input"/> | -|<> |<$edit-text tiddler="$:/GitHub/Branch" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitHub/Branch" default="master" tag="input"/> | |<> |<$edit-text tiddler="$:/GitHub/Path" default="" tag="input"/> | |<> |<$edit-text tiddler="$:/GitHub/Filename" default="" tag="input"/> | -|<> |<$edit-text tiddler="$:/GitHub/ServerURL" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitHub/ServerURL" default="https://api.github.com" tag="input"/> | \ No newline at end of file diff --git a/core/ui/ControlPanel/Saving/GitLab.tid b/core/ui/ControlPanel/Saving/GitLab.tid new file mode 100644 index 000000000..dbe198824 --- /dev/null +++ b/core/ui/ControlPanel/Saving/GitLab.tid @@ -0,0 +1,16 @@ +title: $:/core/ui/ControlPanel/Saving/GitLab +tags: $:/tags/ControlPanel/Saving +caption: {{$:/language/ControlPanel/Saving/GitService/GitLab/Caption}} + +\define lingo-base() $:/language/ControlPanel/Saving/GitService/ +\define service-name() ~GitLab + +<> + +|<> |<$edit-text tiddler="$:/GitLab/Username" default="" tag="input"/> | +|<> |<$password name="gitlab"/> | +|<> |<$edit-text tiddler="$:/GitLab/Repo" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitLab/Branch" default="master" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitLab/Path" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitLab/Filename" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/GitLab/ServerURL" default="https://gitlab.com/api/v4" tag="input"/> | \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/saving/Saving to GitHub.tid b/editions/tw5.com/tiddlers/saving/Saving to GitHub.tid deleted file mode 100644 index a3962e52e..000000000 --- a/editions/tw5.com/tiddlers/saving/Saving to GitHub.tid +++ /dev/null @@ -1,24 +0,0 @@ -created: 20190408173002622 -modified: 20190408173002622 -tags: Saving Android Chrome Firefox InternetExplorer iOS Linux Mac Opera Safari Windows -title: Saving to GitHub -type: text/vnd.tiddlywiki -delivery: Service -method: save -caption: GitHub Saver -description: Save changes directly to a GitHub repository - -TiddlyWiki can save changes directly to a GitHub repository in the single file configuration. - -Saving to GitHub is configured in the [[$:/ControlPanel]] in the ''~GitHub Saver'' tab under the ''Saving'' tab. The following settings are supported: - -* ''Username'' - (mandatory) the username for the GitHub account used for saving changes -* ''Password'' - (mandatory) the password, OAUTH token or personal access token for the specified account. Note that GitHub permits [[several different mechanisms|https://developer.github.com/v3/#authentication]] for authentication -* ''Repository'' - (mandatory) the name of the GitHub repository. Both the owner name and the repository name must be specified. For example `Jermolene/TiddlyWiki5` -* ''Branch'' - (optional) the name of the branch to be used within the GitHub repository. Defaults to `master` -* ''Path'' - (optional) the path to the target file. Defaults to `/` -* ''Filename'' - (mandatory) the filename of the target file - -Notes - -* The GitHub password is stored persistently in browser local storage. Be sure to clear the password if using a shared machine. Using a [[personal access token|]] for authentication offers an extra layer of security: if the access token is accidentally exposed it can be revoked without needing to reset the account password diff --git a/editions/tw5.com/tiddlers/saving/Saving to a Git service.tid b/editions/tw5.com/tiddlers/saving/Saving to a Git service.tid new file mode 100644 index 000000000..f7ad41918 --- /dev/null +++ b/editions/tw5.com/tiddlers/saving/Saving to a Git service.tid @@ -0,0 +1,25 @@ +created: 20190408173002622 +modified: 20190408173002622 +tags: Saving Android Chrome Firefox InternetExplorer iOS Linux Mac Opera Safari Windows +title: Saving to a Git service +type: text/vnd.tiddlywiki +delivery: Service +method: save +caption: Git Service Saver +description: Save changes directly to a Git repository (on GitHub, GitLab) + +TiddlyWiki can save changes directly to a GitHub repository in the single file configuration. + +Saving to a Git service is configured in the [[$:/ControlPanel]] in the ''Git Service Saver'' tab under the ''Saving'' tab. The following settings are supported: + +* ''Type'' - (mandatory) the type of the service (e.g. GitHub, GitLab) +* ''Username'' - (mandatory) the username for the Git service account used for saving changes +* ''Password'' - (mandatory) the password, OAUTH token or personal access token for the specified account. Note that GitHub permits [[several different mechanisms|https://developer.github.com/v3/#authentication]] for authentication +* ''Repository'' - (mandatory) the name of the Git repository. Both the owner name and the repository name must be specified. For example `Jermolene/TiddlyWiki5` +* ''Branch'' - (optional) the name of the branch to be used within the Git repository. Defaults to `master` +* ''Path'' - (optional) the path to the target file. Defaults to `/` +* ''Filename'' - (mandatory) the filename of the target file + +Notes + +* The Git service password is stored persistently in browser local storage. Be sure to clear the password if using a shared machine. Using a [[personal access token|]] for authentication offers an extra layer of security: if the access token is accidentally exposed it can be revoked without needing to reset the account password