2018-08-07 14:46:54 -06:00
|
|
|
import { API as GithubAPI } from 'netlify-cms-backend-github';
|
|
|
|
import { APIError } from 'netlify-cms-lib-util';
|
2018-06-11 19:03:43 -07:00
|
|
|
|
|
|
|
export default class API extends GithubAPI {
|
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
|
|
|
this.api_root = config.api_root;
|
|
|
|
this.tokenPromise = config.tokenPromise;
|
|
|
|
this.commitAuthor = config.commitAuthor;
|
2018-08-07 14:46:54 -06:00
|
|
|
this.repoURL = '';
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hasWriteAccess() {
|
|
|
|
return this.getBranch()
|
|
|
|
.then(() => true)
|
|
|
|
.catch(error => {
|
|
|
|
if (error.status === 401) {
|
2018-08-07 14:46:54 -06:00
|
|
|
if (error.message === 'Bad credentials') {
|
|
|
|
throw new APIError(
|
|
|
|
'Git Gateway Error: Please ask your site administrator to reissue the Git Gateway token.',
|
|
|
|
error.status,
|
|
|
|
'Git Gateway',
|
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-07 14:46:54 -06:00
|
|
|
} else if (
|
|
|
|
error.status === 404 &&
|
|
|
|
(error.message === undefined || error.message === 'Unable to locate site configuration')
|
|
|
|
) {
|
|
|
|
throw new APIError(
|
|
|
|
`Git Gateway Error: Please make sure Git Gateway is enabled on your site.`,
|
|
|
|
error.status,
|
|
|
|
'Git Gateway',
|
|
|
|
);
|
2018-06-11 19:03:43 -07:00
|
|
|
} else {
|
2018-08-07 14:46:54 -06:00
|
|
|
console.error('Problem fetching repo data from Git Gateway');
|
2018-06-11 19:03:43 -07:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestHeaders(headers = {}) {
|
2018-08-07 14:46:54 -06:00
|
|
|
return this.tokenPromise().then(jwtToken => {
|
2018-06-11 19:03:43 -07:00
|
|
|
const baseHeader = {
|
2018-08-07 14:46:54 -06:00
|
|
|
Authorization: `Bearer ${jwtToken}`,
|
|
|
|
'Content-Type': 'application/json',
|
2018-06-11 19:03:43 -07:00
|
|
|
...headers,
|
|
|
|
};
|
|
|
|
|
|
|
|
return baseHeader;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
urlFor(path, options) {
|
|
|
|
const cacheBuster = new Date().getTime();
|
2018-08-07 14:46:54 -06:00
|
|
|
const params = [`ts=${cacheBuster}`];
|
2018-06-11 19:03:43 -07:00
|
|
|
if (options.params) {
|
|
|
|
for (const key in options.params) {
|
2018-08-07 14:46:54 -06:00
|
|
|
params.push(`${key}=${encodeURIComponent(options.params[key])}`);
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (params.length) {
|
2018-08-07 14:46:54 -06:00
|
|
|
path += `?${params.join('&')}`;
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
return this.api_root + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
user() {
|
|
|
|
return Promise.resolve(this.commitAuthor);
|
|
|
|
}
|
|
|
|
|
2019-09-27 02:35:52 +10:00
|
|
|
request(path, options = {}, parseResponse = response => this.parseResponse(response)) {
|
2018-06-11 19:03:43 -07:00
|
|
|
const url = this.urlFor(path, options);
|
|
|
|
let responseStatus;
|
|
|
|
return this.getRequestHeaders(options.headers || {})
|
2018-08-07 14:46:54 -06:00
|
|
|
.then(headers => fetch(url, { ...options, headers }))
|
|
|
|
.then(response => {
|
|
|
|
responseStatus = response.status;
|
2019-09-27 02:35:52 +10:00
|
|
|
return parseResponse(response);
|
2018-08-07 14:46:54 -06:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw new APIError(error.message || error.msg, responseStatus, 'Git Gateway');
|
|
|
|
});
|
2018-06-11 19:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
commit(message, changeTree) {
|
|
|
|
const commitParams = {
|
|
|
|
message,
|
|
|
|
tree: changeTree.sha,
|
|
|
|
parents: changeTree.parentSha ? [changeTree.parentSha] : [],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.commitAuthor) {
|
|
|
|
commitParams.author = {
|
|
|
|
...this.commitAuthor,
|
|
|
|
date: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
return this.request('/git/commits', {
|
|
|
|
method: 'POST',
|
2018-06-11 19:03:43 -07:00
|
|
|
body: JSON.stringify(commitParams),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|