From 1246d438a21301cd3a134751fb350906187f1385 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 4 Mar 2020 09:40:08 -0600 Subject: [PATCH] fix: getting gist doesn't require token fixes #281 --- lib/location/gist.js | 12 ++++++++---- spec/integration-spec.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/location/gist.js b/lib/location/gist.js index 672c7102..096809f6 100644 --- a/lib/location/gist.js +++ b/lib/location/gist.js @@ -145,12 +145,16 @@ and set the environment variable \`GITHUB_TOKEN\` or enter it in the settings.`. return promise } -async function getPersonalAccessToken (required) { +async function getPersonalAccessToken (allowEmpty) { let token = atom.config.get('sync-settings.personalAccessToken') || process.env.GITHUB_TOKEN if (token) { return token.trim() } + if (allowEmpty) { + return '' + } + token = await invalidPersonalAccessToken() if (token) { return token.trim() @@ -262,10 +266,10 @@ module.exports = { * @return {Promise} Returns object with `files` and `time` on success. Falsey value on silent error. */ async get () { - let personalAccessToken let gistId try { - personalAccessToken = await getPersonalAccessToken() + // getting gists doesn't require a token + const personalAccessToken = await getPersonalAccessToken(true) gistId = await getGistId() const res = await createClient(personalAccessToken).gists.get({ gist_id: gistId }) @@ -285,7 +289,7 @@ module.exports = { } } catch (err) { if (err) { - return displayError(err, 'getting backup', () => this.get(), gistId, personalAccessToken) + return displayError(err, 'getting backup', () => this.get(), gistId) } } }, diff --git a/spec/integration-spec.js b/spec/integration-spec.js index f55e71fd..d5740d65 100644 --- a/spec/integration-spec.js +++ b/spec/integration-spec.js @@ -144,6 +144,20 @@ describe('integration', () => { expect(readme).toBe('# Generated by Sync Settings for Atom\n\n') }, (process.env.CI ? 60 : 10) * 1000) + it('restores without token', async () => { + const GITHUB_TOKEN = process.env.GITHUB_TOKEN + try { + process.env.GITHUB_TOKEN = '' + atom.config.set('sync-settings.extraFiles', ['README']) + await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:restore') + const readme = await readFile(path.resolve(atom.getConfigDirPath(), 'README'), { encoding: 'utf8' }) + + expect(readme).toBe('# Generated by Sync Settings for Atom\n\n') + } finally { + process.env.GITHUB_TOKEN = GITHUB_TOKEN + } + }, (process.env.CI ? 60 : 10) * 1000) + it('backs up and restores paths with slash', async () => { atom.config.set('sync-settings.extraFiles', ['../test.tmp']) const tmpPath = path.resolve(atom.getConfigDirPath(), '../test.tmp')