mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-23 10:07:19 +00:00
Added gitea saver (#4491)
* added gitea saver * create nonexistent file
This commit is contained in:
parent
22e6b20f50
commit
52a9f928ae
@ -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/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/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/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/Advanced/Heading: Advanced Settings
|
||||||
Saving/TiddlySpot/BackupDir: Backup Directory
|
Saving/TiddlySpot/BackupDir: Backup Directory
|
||||||
Saving/TiddlySpot/Backups: Backups
|
Saving/TiddlySpot/Backups: Backups
|
||||||
|
136
core/modules/savers/gitea.js
Normal file
136
core/modules/savers/gitea.js
Normal file
@ -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);
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
16
core/ui/ControlPanel/Saving/gitea.tid
Normal file
16
core/ui/ControlPanel/Saving/gitea.tid
Normal file
@ -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
|
||||||
|
|
||||||
|
<<lingo Description>>
|
||||||
|
|
||||||
|
|<<lingo UserName>> |<$edit-text tiddler="$:/gitea/Username" default="" tag="input"/> |
|
||||||
|
|<<lingo gitea/Password>> |<$password name="gitea"/> |
|
||||||
|
|<<lingo Repo>> |<$edit-text tiddler="$:/gitea/Repo" default="" tag="input"/> |
|
||||||
|
|<<lingo Branch>> |<$edit-text tiddler="$:/gitea/Branch" default="master" tag="input"/> |
|
||||||
|
|<<lingo Path>> |<$edit-text tiddler="$:/gitea/Path" default="" tag="input"/> |
|
||||||
|
|<<lingo Filename>> |<$edit-text tiddler="$:/gitea/Filename" default="" tag="input"/> |
|
||||||
|
|<<lingo ServerURL>> |<$edit-text tiddler="$:/gitea/ServerURL" default="https://gitea/api/v1" tag="input"/> |
|
Loading…
Reference in New Issue
Block a user