2020-03-12 15:53:55 +00:00
|
|
|
/*\
|
|
|
|
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,
|
2020-03-13 17:36:05 +00:00
|
|
|
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",
|
2020-03-12 15:53:55 +00:00
|
|
|
headers = {
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
2020-09-14 12:14:10 +00:00
|
|
|
"Authorization": "token " + password
|
2020-03-12 15:53:55 +00:00
|
|
|
};
|
|
|
|
// Bail if we don't have everything we need
|
2020-08-28 14:28:34 +00:00
|
|
|
if(!username || !password || !repo || !filename) {
|
2020-03-12 15:53:55 +00:00
|
|
|
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 = {
|
2021-07-16 11:04:05 +00:00
|
|
|
message: $tw.language.getString("ControlPanel/Saving/GitService/CommitMessage"),
|
2020-03-12 15:53:55 +00:00
|
|
|
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) {
|
2020-03-13 17:36:05 +00:00
|
|
|
callback("Please ensure the branch in the Gitea repo exists");
|
2020-03-12 15:53:55 +00:00
|
|
|
}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 = {
|
2020-03-13 17:36:05 +00:00
|
|
|
name: "Gitea",
|
2020-03-12 15:53:55 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|