1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add GitLab saver, apply common lingo to Git savers (#3931)

* Transform GitHub saver to work with GitLab as well

You can choose which provider you want to use, the data is given in the
same place.

I tried to avoid code duplication, so service providers' unique
properties are in separate files, the settings of the selected provider
are loaded.

In two fields I am not sure that it fits into the current structure:

* module-type: gitservice
  Which module is a `gitservice` type, it will be listed in the
  drop-down menu.
* default-api-url: https://gitlab.com/api/v4
  The default URL to access the provider's API.

This is just a sketch, not a final version, suggestions for modification
are welcome!

* Rename saver from GitHub to GitService, update docs

* Split GitHub and GitLab to separate savers, apply common lingo

Sadly, it doesn't seem to make much sense to search for common parts in
the code, because there might be a Git service that is very different
from the GitHub API (such as BitBucket). Therefore, I feel that Git
savers are not able to share other than the translations.

I deleted the defaults values from the translations and set it to the
text entry because they should not depend on the translations.

* Add more information about the password field

It is not clear how to create a personal access token, thus added a link
to the help pages. In addition, GitLab only accepts personal access
token, GitHub also accepts the password, so I made this clear.

* Extract commit message to lingo

* Fix indentation

* Use improved base64 encoder

Fix conflict with a06acc4eb8
This commit is contained in:
Bimba Laszlo 2019-07-31 22:38:52 +02:00 committed by Jeremy Ruston
parent 3afaa9de9a
commit b5653babdf
7 changed files with 187 additions and 46 deletions

View File

@ -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 <<service-name>>
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

View File

@ -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,

View File

@ -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);
};
})();

View File

@ -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
<<lingo Description>>
|<<lingo UserName>> |<$edit-text tiddler="$:/GitHub/Username" default="" tag="input"/> |
|<<lingo Password>> |<$password name="github"/> |
|<<lingo GitHub/Password>> |<$password name="github"/> |
|<<lingo Repo>> |<$edit-text tiddler="$:/GitHub/Repo" default="" tag="input"/> |
|<<lingo Branch>> |<$edit-text tiddler="$:/GitHub/Branch" default="" tag="input"/> |
|<<lingo Branch>> |<$edit-text tiddler="$:/GitHub/Branch" default="master" tag="input"/> |
|<<lingo Path>> |<$edit-text tiddler="$:/GitHub/Path" default="" tag="input"/> |
|<<lingo Filename>> |<$edit-text tiddler="$:/GitHub/Filename" default="" tag="input"/> |
|<<lingo ServerURL>> |<$edit-text tiddler="$:/GitHub/ServerURL" default="" tag="input"/> |
|<<lingo ServerURL>> |<$edit-text tiddler="$:/GitHub/ServerURL" default="https://api.github.com" tag="input"/> |

View File

@ -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
<<lingo Description>>
|<<lingo UserName>> |<$edit-text tiddler="$:/GitLab/Username" default="" tag="input"/> |
|<<lingo GitLab/Password>> |<$password name="gitlab"/> |
|<<lingo Repo>> |<$edit-text tiddler="$:/GitLab/Repo" default="" tag="input"/> |
|<<lingo Branch>> |<$edit-text tiddler="$:/GitLab/Branch" default="master" tag="input"/> |
|<<lingo Path>> |<$edit-text tiddler="$:/GitLab/Path" default="" tag="input"/> |
|<<lingo Filename>> |<$edit-text tiddler="$:/GitLab/Filename" default="" tag="input"/> |
|<<lingo ServerURL>> |<$edit-text tiddler="$:/GitLab/ServerURL" default="https://gitlab.com/api/v4" tag="input"/> |

View File

@ -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

View File

@ -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