Skip to content

Commit

Permalink
fix remotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Hassanein committed Mar 29, 2020
1 parent 9524018 commit 40decc0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const IPC_PUSH_BRANCH = 'IPC_PUSH_BRANCH'
const IPC_CANCEL_SELECT = 'IPC_CANCEL_SELECT'
const IPC_HIDE_SELECT = 'IPC_HIDE_SELECT'
const IPC_REPO_SELECT = 'IPC_REPO_SELECT'
const IPC_GET_GIT_REMOTE = 'IPC_GET_GIT_REMOTE'

export {
IPC_SELECTOR,
Expand All @@ -24,4 +25,5 @@ export {
IPC_CANCEL_SELECT,
IPC_REPO_SELECT,
IPC_HIDE_SELECT,
IPC_GET_GIT_REMOTE,
}
15 changes: 15 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
deleteBranch,
rebaseLocalBranch,
checkoutLocalBranch,
getRepoFromPath,
getRemote,
} from '../plugins/git'
// import { getInfo } from './plugins/jenkins'

Expand All @@ -41,6 +43,7 @@ import {
IPC_REFRESH_TICKETS,
IPC_REFRESH_GIT,
IPC_REFRESH_PRS,
IPC_GET_GIT_REMOTE,
} from '../constants'

const logger = electronTimber.create({ name: 'index' })
Expand Down Expand Up @@ -96,6 +99,18 @@ function registerShortcuts() {
)
}

ipcMain.on(IPC_GET_GIT_REMOTE, async (e, path: string) => {
const gitRepo = await getRepoFromPath(path)

if (gitRepo) {
const remote = await getRemote(gitRepo)

e.returnValue = remote
} else {
e.returnValue = false
}
})

ipcMain.on(IPC_CREATE_BRANCH, async (e, key) => {
const result = await createBranchFromTicketId(key)

Expand Down
45 changes: 15 additions & 30 deletions src/plugins/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,48 +259,33 @@ const checkoutLocalBranch = async (repoId: string, branchName: string) => {
}
}

type RepoRemote = {
export type RepoRemote = {
remoteName: string
repoId: string
orgID: string
}

const getRemote = async (
repoOrRepoID: string | git.SimpleGit,
gitRepo: git.SimpleGit,
): Promise<RepoRemote | false> => {
let gitRepo: git.SimpleGit

if (typeof repoOrRepoID === 'string') {
const repoSettings = getRepoSettingsFromId(repoOrRepoID)

gitRepo = git(okk(repoSettings.path))
} else {
gitRepo = repoOrRepoID
}

let remotes: RemoteWithRefs[] | false

try {
remotes = await gitRepo.getRemotes(true)
} catch (error) {
logger.error('getRemote', error)
const result = await gitRepo.raw(['remote', '--verbose'])
const firstSplit = result.split('\n')[0].split('\t')
const remoteName = firstSplit[0]

remotes = false
}

if (remotes && remotes.length) {
const remoteName = remotes[0].name

const fetchRemote = okk(remotes[0].refs.fetch)
const secondSplit = firstSplit[1]
.split('\n')[0]
.split('\t')[0]
.split(' ')[0]
.split(':')[1]
.split('/')

const orgID = okk(fetchRemote.split(':')[1].split('/')[0])

const repoId = okk(fetchRemote.split(':')[1].split('/')[1].split('.')[0])
const orgID = secondSplit[0]
const repoId = secondSplit[1].split('.')[0]

return { remoteName, orgID, repoId }
} else {
logger.error('getRemote', 'no remotes!')

} catch (error) {
logger.log(error)
return false
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { composeWithDevTools } from 'redux-devtools-extension'
import { settingsReducer } from './settings/reducers'
import { ticketsReducer } from './tickets/reducers'
import { branchesReducer } from './branches/reducers'
import { IRepoSetting } from './settings/types'
import { okk } from '../helpers/helpers'

const rootReducer = combineReducers({
Expand All @@ -22,10 +21,13 @@ const middleWareEnhancer = applyMiddleware(...middlewares)
const store = createStore(rootReducer, composeWithDevTools(middleWareEnhancer))

const getRepoSettingsFromId = (repoId: string) => {
const repo = okk(
store.getState().settings.reposList.find((repo) => repo.repoId === repoId),
)
return repo
const state = store.getState()

const repo = state.settings.reposList.find((repo) => repo.repoId === repoId)

console.log('getRepoSettingsFromId', { state, repo, repoId })

return okk(repo)
}

export { store, getRepoSettingsFromId }
37 changes: 19 additions & 18 deletions src/windows/modals/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import { FieldArray } from 'react-final-form-arrays'
import { TAppState } from '../../store'
import { jsx } from '@emotion/core'
import css from '@emotion/css'
import { shell } from 'electron'
import { shell, ipcRenderer } from 'electron'
import { folderPicker } from '../../plugins/dialogs'
import { getRepoFromPath, getRemote } from '../../plugins/git'
import { RepoRemote } from '../../plugins/git'
import {
TextFieldWrapper,
Error,
Label,
SupportLink,
textColor,
} from '../components/styles'
import { IPC_GET_GIT_REMOTE } from '../../constants'

const mapState = (state: TAppState) => ({
settings: state.settings,
Expand Down Expand Up @@ -267,25 +268,25 @@ const settings: React.FC<TProps> = ({ settings, saveSettingsAction }) => (
type="button"
onClick={async () => {
const folder = await folderPicker()

if (!folder.canceled) {
const path = folder.filePaths[0]
const gitRepo = await getRepoFromPath(path)
if (gitRepo) {
const remote = await getRemote(gitRepo)

if (remote) {
form.mutators.push('reposList', {
path,
orgID: remote.orgID,
remoteName: remote.remoteName,
repoId: remote.repoId,
enableAutoRefresh: true,
})
} else {
alert("couldn't get git remote details")
}

const remote: RepoRemote | false = ipcRenderer.sendSync(
IPC_GET_GIT_REMOTE,
path,
)

if (remote) {
form.mutators.push('reposList', {
path,
orgID: remote.orgID,
remoteName: remote.remoteName,
repoId: remote.repoId,
enableAutoRefresh: true,
})
} else {
alert('not a git repo')
alert("couldn't get git remote details")
}
}
}}
Expand Down

0 comments on commit 40decc0

Please sign in to comment.