Skip to content

Ghe token refactoring #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2019
Merged
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
26 changes: 14 additions & 12 deletions src/renderer/store/modules/Note.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import db from '@/datastore-notes';
import converter from '@/converter';
import Octokit from '@octokit/rest';
import path from 'path';
// eslint-disable-next-line import/no-extraneous-dependencies
import { remote } from 'electron';
import packageJson from '../../../../package';

const Octokit = require('@octokit/rest');
let octokit = Octokit({
const getOctokit = ({ githubPersonalAccessToken, githubEnterpriseUrl }) => new Octokit({
auth: githubPersonalAccessToken,
userAgent: 'code-notes'.concat(packageJson.version),
mediaType: {
format: 'application/vnd.github.v3+json',
},
...(githubEnterpriseUrl && { baseUrl: githubEnterpriseUrl }),
});

const state = {
Expand Down Expand Up @@ -55,16 +57,8 @@ const actions = {
store.commit('SELECT_LOADING', true);
store.commit('LOAD_NOTES', []);

if (store.rootState.Settings.settings.githubEnterpriseUrl) {
octokit = new Octokit({
baseUrl: store.rootState.Settings.settings.githubEnterpriseUrl,
auth: store.rootState.Settings.settings.githubPersonalAccessToken,
});
} else {
octokit = new Octokit({
auth: store.rootState.Settings.settings.githubPersonalAccessToken,
});
}
const octokit = getOctokit(store.rootState.Settings.settings);

octokit.gists.list().then((res) => {
const promises = [];

Expand Down Expand Up @@ -99,6 +93,9 @@ const actions = {
},
addNote(store, note) {
store.commit('SELECT_LOADING', true);

const octokit = getOctokit(store.rootState.Settings.settings);

if (store.state.gistsSelected) {
octokit.gists.create(note).then(() => {
store.dispatch('loadNotes');
Expand All @@ -115,6 +112,8 @@ const actions = {
},
updateNote(store, note) {
if (store.state.gistsSelected) {
const octokit = getOctokit(store.rootState.Settings.settings);

octokit.gists
.update({
gist_id: note.id,
Expand All @@ -133,6 +132,9 @@ const actions = {
},
deleteNote(store, note) {
store.commit('SELECT_LOADING', true);

const octokit = getOctokit(store.rootState.Settings.settings);

if (store.state.gistsSelected) {
octokit.gists.delete({ gist_id: note.id }).then(() => {
store.commit('DELETE_NOTE', note);
Expand Down