Skip to content

Commit

Permalink
fix: getting gist doesn't require token
Browse files Browse the repository at this point in the history
fixes #281
  • Loading branch information
UziTech committed Mar 4, 2020
1 parent 72e5255 commit 1246d43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/location/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 })

Expand All @@ -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)
}
}
},
Expand Down
14 changes: 14 additions & 0 deletions spec/integration-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ describe('integration', () => {
expect(readme).toBe('# Generated by Sync Settings for Atom\n\n<https://github.com/atom-community/sync-settings>')
}, (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<https://github.com/atom-community/sync-settings>')
} 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')
Expand Down

0 comments on commit 1246d43

Please sign in to comment.