2019-04-08 20:08:58 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/savers/github.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: saver
|
|
|
|
|
|
|
|
Saves wiki by pushing a commit to the GitHub v3 REST API
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
2019-07-31 20:39:52 +00:00
|
|
|
/*global $tw: false */
|
2019-04-08 20:08:58 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Select the appropriate saver module and set it up
|
|
|
|
*/
|
|
|
|
var GitHubSaver = function(wiki) {
|
|
|
|
this.wiki = wiki;
|
|
|
|
};
|
|
|
|
|
|
|
|
GitHubSaver.prototype.save = function(text,method,callback) {
|
|
|
|
var self = this,
|
|
|
|
username = this.wiki.getTiddlerText("$:/GitHub/Username"),
|
|
|
|
password = $tw.utils.getPassword("github"),
|
|
|
|
repo = this.wiki.getTiddlerText("$:/GitHub/Repo"),
|
2020-01-30 17:02:14 +00:00
|
|
|
path = this.wiki.getTiddlerText("$:/GitHub/Path",""),
|
2019-04-08 20:08:58 +00:00
|
|
|
filename = this.wiki.getTiddlerText("$:/GitHub/Filename"),
|
2020-11-17 14:47:15 +00:00
|
|
|
branch = this.wiki.getTiddlerText("$:/GitHub/Branch") || "main",
|
2019-04-16 09:15:58 +00:00
|
|
|
endpoint = this.wiki.getTiddlerText("$:/GitHub/ServerURL") || "https://api.github.com",
|
2019-04-08 20:08:58 +00:00
|
|
|
headers = {
|
|
|
|
"Accept": "application/vnd.github.v3+json",
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
2020-11-27 21:37:11 +00:00
|
|
|
"Authorization": "Basic " + window.btoa(username + ":" + password),
|
|
|
|
"If-None-Match": ""
|
2019-04-08 20:08:58 +00:00
|
|
|
};
|
2019-04-11 07:21: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) {
|
2019-04-11 07:21:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-08 20:08:58 +00:00
|
|
|
// 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
|
2019-04-15 20:30:59 +00:00
|
|
|
var uri = endpoint + "/repos/" + repo + "/contents" + path;
|
2019-04-08 20:08:58 +00:00
|
|
|
// 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) {
|
2019-04-15 20:08:04 +00:00
|
|
|
var getResponseData,sha = "";
|
|
|
|
if(err && xhr.status !== 404) {
|
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 a06acc4eb8ade26cbb17b9f5911876bac1da7715
2019-07-31 20:38:52 +00:00
|
|
|
return callback(err);
|
2019-04-08 20:08:58 +00:00
|
|
|
}
|
2019-04-15 20:08:04 +00:00
|
|
|
if(xhr.status !== 404) {
|
|
|
|
getResponseData = JSON.parse(getResponseDataJson);
|
|
|
|
$tw.utils.each(getResponseData,function(details) {
|
|
|
|
if(details.name === filename) {
|
|
|
|
sha = details.sha;
|
|
|
|
}
|
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 a06acc4eb8ade26cbb17b9f5911876bac1da7715
2019-07-31 20:38:52 +00:00
|
|
|
});
|
2019-04-15 20:08:04 +00:00
|
|
|
}
|
2019-04-08 20:08:58 +00:00
|
|
|
var data = {
|
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 a06acc4eb8ade26cbb17b9f5911876bac1da7715
2019-07-31 20:38:52 +00:00
|
|
|
message: $tw.language.getRawString("ControlPanel/Saving/GitService/CommitMessage"),
|
|
|
|
content: $tw.utils.base64Encode(text),
|
|
|
|
branch: branch,
|
|
|
|
sha: sha
|
|
|
|
};
|
2019-04-08 20:08:58 +00:00
|
|
|
// Perform a PUT request to save the file
|
|
|
|
$tw.utils.httpRequest({
|
|
|
|
url: uri + filename,
|
|
|
|
type: "PUT",
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
GitHubSaver.prototype.info = {
|
|
|
|
name: "github",
|
|
|
|
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 GitHubSaver(wiki);
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|