Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
node_modules
yarn.lock
dist.zip
dist
dist
*.pem
debug-server.js
package-lock.json
35 changes: 28 additions & 7 deletions src/GitHubAPI/Requestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,26 @@ class Requestable {
* @param {string} [AcceptHeader=v3] - the accept header for the requests
*/
constructor(auth, apiBase, AcceptHeader) {
// [Copilot review] auth is optional per JSDoc — default to {} to prevent
// TypeError when accessing auth.token / auth.username / auth.password
auth = auth || {};
const normalizedToken = auth.token
? String(auth.token)
.trim()
.replace(/^Bearer\s+/i, '')
.replace(/^token\s+/i, '')
: auth.token;

this.__apiBase = apiBase || 'https://api.github.com';
this.__auth = {
token: auth.token,
token: normalizedToken,
username: auth.username,
password: auth.password,
};
this.__AcceptHeader = AcceptHeader || 'v3';

if (auth.token) {
this.__authorizationHeader = 'token ' + auth.token;
if (normalizedToken) {
this.__authorizationHeader = 'token ' + normalizedToken;
} else if (auth.username && auth.password) {
this.__authorizationHeader =
'Basic ' + Base64.encode(auth.username + ':' + auth.password);
Expand Down Expand Up @@ -327,11 +337,22 @@ function callbackErrorOrThrow(cb, path) {
return function handler(object) {
let error;
if (object.hasOwnProperty('config')) {
const {
response: { status, statusText },
config: { method, url },
} = object;
const response = object.response || {};
const config = object.config || {};
const status = response.status;
const statusText = response.statusText || '';
const method = config.method || 'UNKNOWN';
const url = config.url || 'UNKNOWN_URL';
const ghMessage = response.data && response.data.message ? response.data.message : '';
const docsUrl = response.data && response.data.documentation_url
? ` (${response.data.documentation_url})`
: '';

let message = `${status} error making request ${method} ${url}: "${statusText}"`;
if (ghMessage) {
message += ` | GitHub: ${ghMessage}${docsUrl}`;
}

error = new ResponseError(message, path, object);
log(`${message} ${JSON.stringify(object.data)}`);
} else {
Expand Down
34 changes: 31 additions & 3 deletions src/githubFs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,33 @@ const prompt = acode.require('prompt');
const encodings = acode.require('encodings');

const test = (url) => /^gh:/.test(url);
const REGISTRY_KEY = '__acodeGithubFsTests__';

function _isZh() {
try {
const langs = [].concat(navigator.languages || [], navigator.language || []);
return langs.some((l) => /^zh(?:-|$)/i.test(String(l || '')));
} catch (_) { return false; }
}
function _t(en, zh) { return _isZh() ? zh : en; }

function getRegistry() {
if (!window[REGISTRY_KEY]) {
window[REGISTRY_KEY] = [];
}
return window[REGISTRY_KEY];
}

function removeAllGithubFsHandlers() {
const registry = getRegistry();
registry.forEach((registeredTest) => {
try { fsOperation.remove(registeredTest); } catch (_) {}
});
registry.length = 0;
}

githubFs.remove = () => {
fsOperation.remove(test);
removeAllGithubFsHandlers();
};

/**
Expand Down Expand Up @@ -41,6 +65,10 @@ githubFs.constructUrl = (type, user, repo, path, branch) => {
};

export default function githubFs(token, settings) {
// Ensure only one active gh:// handler even after plugin hot-reloads.
removeAllGithubFsHandlers();
getRegistry().push(test);

fsOperation.extend(test, (url) => {
const { user, type, repo, path, gist } = parseUrl(url);
if (type === 'repo') {
Expand Down Expand Up @@ -92,9 +120,9 @@ export default function githubFs(token, settings) {
*/
async function getCommitMessage(message) {
if (settings.askCommitMessage) {
const res = await prompt('Commit message', message, 'text');
const res = await prompt(_t('Commit message', '提交信息'), message, 'text');
if (!res) {
const error = new Error('Commit aborted');
const error = new Error(_t('Commit aborted', '提交已取消'));
error.code = 0;
error.toString = () => error.message;
throw error;
Expand Down
Loading