From 52a9f928ae3535cfd735ede0f1da740ce25fabda Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Thu, 12 Mar 2020 16:53:55 +0100 Subject: [PATCH] Added gitea saver (#4491) * added gitea saver * create nonexistent file --- core/language/en-GB/ControlPanel.multids | 2 + core/modules/savers/gitea.js | 136 +++++++++++++++++++++++ core/ui/ControlPanel/Saving/gitea.tid | 16 +++ 3 files changed, 154 insertions(+) create mode 100644 core/modules/savers/gitea.js create mode 100644 core/ui/ControlPanel/Saving/gitea.tid diff --git a/core/language/en-GB/ControlPanel.multids b/core/language/en-GB/ControlPanel.multids index 99a526b9f..de19a1229 100644 --- a/core/language/en-GB/ControlPanel.multids +++ b/core/language/en-GB/ControlPanel.multids @@ -107,6 +107,8 @@ 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) +Saving/GitService/gitea/Caption: gitea saver +Saving/GitService/gitea/Password: Personal access token for API (via Gitea’s web interface: `Settings | Applications | Generate New Token`) Saving/TiddlySpot/Advanced/Heading: Advanced Settings Saving/TiddlySpot/BackupDir: Backup Directory Saving/TiddlySpot/Backups: Backups diff --git a/core/modules/savers/gitea.js b/core/modules/savers/gitea.js new file mode 100644 index 000000000..5e2b57152 --- /dev/null +++ b/core/modules/savers/gitea.js @@ -0,0 +1,136 @@ +/*\ +title: $:/core/modules/savers/gitea.js +type: application/javascript +module-type: saver + +Saves wiki by pushing a commit to the gitea + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +/* +Select the appropriate saver module and set it up +*/ +var GiteaSaver = function(wiki) { + this.wiki = wiki; +}; + +GiteaSaver.prototype.save = function(text,method,callback) { + var self = this, + username = this.wiki.getTiddlerText("$:/gitea/Username"), + password = $tw.utils.getPassword("gitea"), + repo = this.wiki.getTiddlerText("$:/gitea/Repo"), + path = this.wiki.getTiddlerText("$:/gitea/Path",""), + filename = this.wiki.getTiddlerText("$:/gitea/Filename"), + branch = this.wiki.getTiddlerText("$:/gitea/Branch") || "master", + endpoint = this.wiki.getTiddlerText("$:/gitea/ServerURL") || "https://gitea", + headers = { + "Accept": "application/json", + "Content-Type": "application/json;charset=UTF-8", + "Authorization": "Basic " + window.btoa(username + ":" + 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 + "/repos/" + repo + "/contents" + path; + // Perform a get request to get the details (inc shas) of files in the same path as our file + $tw.utils.httpRequest({ + url: uri, + type: "GET", + headers: headers, + data: { + ref: branch + }, + callback: function(err,getResponseDataJson,xhr) { + var getResponseData,sha = ""; + if(err && xhr.status !== 404) { + return callback(err); + } + var use_put = true; + if(xhr.status !== 404) { + getResponseData = JSON.parse(getResponseDataJson); + $tw.utils.each(getResponseData,function(details) { + if(details.name === filename) { + sha = details.sha; + } + }); + if(sha === ""){ + use_put = false; + } + } + var data = { + message: $tw.language.getRawString("ControlPanel/Saving/GitService/CommitMessage"), + content: $tw.utils.base64Encode(text), + sha: sha + }; + $tw.utils.httpRequest({ + url: endpoint + "/repos/" + repo + "/branches/" + branch, + type: "GET", + headers: headers, + callback: function(err,getResponseDataJson,xhr) { + if(xhr.status === 404) { + callback("Please ensure the branch in the gitea repo exists"); + }else{ + data["branch"] = branch; + self.upload(uri + filename, use_put?"PUT":"POST", headers, data, callback); + } + } + }); + } + }); + return true; +}; + +GiteaSaver.prototype.upload = function(uri,method,headers,data,callback) { + $tw.utils.httpRequest({ + url: uri, + type: method, + headers: headers, + data: JSON.stringify(data), + callback: function(err,putResponseDataJson,xhr) { + if(err) { + return callback(err); + } + var putResponseData = JSON.parse(putResponseDataJson); + callback(null); + } + }); +}; + +/* +Information about this saver +*/ +GiteaSaver.prototype.info = { + name: "gitea", + 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 GiteaSaver(wiki); +}; + +})(); diff --git a/core/ui/ControlPanel/Saving/gitea.tid b/core/ui/ControlPanel/Saving/gitea.tid new file mode 100644 index 000000000..2bf00396f --- /dev/null +++ b/core/ui/ControlPanel/Saving/gitea.tid @@ -0,0 +1,16 @@ +title: $:/core/ui/ControlPanel/Saving/gitea +tags: $:/tags/ControlPanel/Saving +caption: {{$:/language/ControlPanel/Saving/GitService/gitea/Caption}} + +\define lingo-base() $:/language/ControlPanel/Saving/GitService/ +\define service-name() ~gitea + +<> + +|<> |<$edit-text tiddler="$:/gitea/Username" default="" tag="input"/> | +|<> |<$password name="gitea"/> | +|<> |<$edit-text tiddler="$:/gitea/Repo" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/gitea/Branch" default="master" tag="input"/> | +|<> |<$edit-text tiddler="$:/gitea/Path" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/gitea/Filename" default="" tag="input"/> | +|<> |<$edit-text tiddler="$:/gitea/ServerURL" default="https://gitea/api/v1" tag="input"/> |