Skip to content

resolve git config issue #521

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
Nov 6, 2021
Merged
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 .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
".vscode-test/**": true
},
"git.alwaysSignOff": true,
"typescript.tsdk": "./node_modules/typescript/lib"
"typescript.tsdk": "./node_modules/typescript/lib",
"cSpell.words": [
"coderoad"
]
}
27 changes: 21 additions & 6 deletions src/actions/onTutorialConfigNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as TT from 'typings/tutorial'
import * as E from 'typings/error'
import { satisfies } from 'semver'
import { onEvent } from '../services/telemetry'
import { version, compareVersions } from '../services/dependencies'
import { getVersion, compareVersions } from '../services/dependencies'
import Context from '../services/context/context'
import tutorialConfig from './utils/tutorialConfig'
import { send } from '../commands'
Expand Down Expand Up @@ -46,8 +46,23 @@ const onTutorialConfigNew = async (action: T.Action, context: Context): Promise<
if (dependencies && dependencies.length) {
for (const dep of dependencies) {
// check dependency is installed
const currentVersion: string | null = await version(dep.name)
if (!currentVersion) {
const { version, error: gitError } = await getVersion(dep.name)
if (gitError) {
// git config issue
const error: E.ErrorMessage = {
type: 'GitConfigError',
message: gitError.message,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
if (!version) {
// use a custom error message
const error: E.ErrorMessage = {
type: 'MissingTutorialDependency',
Expand All @@ -64,12 +79,12 @@ const onTutorialConfigNew = async (action: T.Action, context: Context): Promise<
}

// check dependency version
const satisfiedDependency = await compareVersions(currentVersion, dep.version)
const satisfiedDependency = await compareVersions(version, dep.version)

if (!satisfiedDependency) {
const error: E.ErrorMessage = {
type: 'UnmetTutorialDependency',
message: `Expected ${dep.name} to have version ${dep.version}, but found version ${currentVersion}`,
message: `Expected ${dep.name} to have version ${dep.version}, but found version ${version}`,
actions: [
{
label: 'Check Again',
Expand Down Expand Up @@ -116,7 +131,7 @@ const onTutorialConfigNew = async (action: T.Action, context: Context): Promise<

// report back to the webview that setup is complete
send({ type: 'TUTORIAL_CONFIGURED' })
} catch (e) {
} catch (e: any) {
const error = {
type: 'UnknownError',
message: `Location: EditorTutorialConfig.\n\n ${e.message}`,
Expand Down
28 changes: 23 additions & 5 deletions src/actions/onValidateSetup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as E from 'typings/error'
import { version } from '../services/dependencies'
import { getVersion } from '../services/dependencies'
import { checkWorkspaceEmpty } from '../services/workspace'
import { send } from '../commands'
import { validateGitConfig } from '../services/git'
Expand Down Expand Up @@ -28,8 +28,23 @@ const onValidateSetup = async (): Promise<void> => {
}
// check Git is installed.
// Should wait for workspace before running otherwise requires access to root folder
const isGitInstalled = await version('git')
if (!isGitInstalled) {
const { version, error: gitError } = await getVersion('git')
if (gitError) {
// git config issue
const error: E.ErrorMessage = {
type: 'GitConfigError',
message: gitError.message,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
return
}
if (!version) {
const error: E.ErrorMessage = {
type: 'GitNotFound',
message: '',
Expand All @@ -46,6 +61,9 @@ const onValidateSetup = async (): Promise<void> => {

const isGitUserNameConfigured = await validateGitConfig('user.name')
const isGitUserEmailConfigured = await validateGitConfig('user.email')
console.log(`isGitUserNameConf: ${isGitUserNameConfigured}`)
console.log(`isGitUserEmailConf: ${isGitUserEmailConfigured}`)

if (!isGitUserNameConfigured || !isGitUserEmailConfigured) {
let message = ''
if (!isGitUserNameConfigured) message += 'Git user not configured.\n'
Expand All @@ -65,9 +83,9 @@ const onValidateSetup = async (): Promise<void> => {
}

send({ type: 'SETUP_VALIDATED' })
} catch (e) {
} catch (e: any) {
const error = {
type: 'UknownError',
type: 'UnknownError',
message: e.message,
}
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
Expand Down
13 changes: 7 additions & 6 deletions src/services/dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { satisfies } from 'semver'
import { exec } from '../node'

const semverRegex = /(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(\.windows.[0-9]+)?(?=$|\s)/gi
const semverRegex =
/(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(\.windows.[0-9]+)?(?=$|\s)/gi

export const version = async (name: string): Promise<string | null> => {
export const getVersion = async (name: string): Promise<{ version: string | null; error: Error | null }> => {
try {
const { stdout, stderr } = await exec({ command: `${name} --version` })
if (!stderr) {
const match = stdout.match(semverRegex)
if (match) {
const parsedVersion = match[0].split('.').slice(0, 3).join('.')
return parsedVersion
return { version: parsedVersion, error: null }
}
}
return null
} catch (error) {
return null
return { version: null, error: null }
} catch (error: any) {
return { version: null, error }
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as TT from 'typings/tutorial'
import { exec, exists } from '../node'
import { version, compareVersions } from '../dependencies'
import { getVersion, compareVersions } from '../dependencies'
import logger from '../logger'

export const gitOrigin = 'coderoad'

const stashAllFiles = async (): Promise<never | void> => {
// stash files including untracked (eg. newly created file)
const { stdout, stderr } = await exec({ command: `git stash --include-untracked` })
const { stderr } = await exec({ command: `git stash --include-untracked` })
if (stderr) {
console.error(stderr)
throw new Error('Error stashing files')
Expand Down Expand Up @@ -71,7 +71,10 @@ export async function clear(): Promise<Error | void> {
}

async function init(): Promise<Error | void> {
const gitVersion = await version('git')
const { version: gitVersion, error: gitError } = await getVersion('git')
if (gitError) {
throw new Error(`Error: Git config error: ${gitError.message}`)
}
if (!gitVersion) {
throw new Error('Error: No git version found')
}
Expand Down
1 change: 1 addition & 0 deletions typings/error.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type ErrorMessageView = 'FULL_PAGE' | 'NOTIFY' | 'NONE'
export type ErrorMessageType =
| 'FailedToConnectToGitRepo'
| 'GitNotFound'
| 'GitConfigError'
| 'GitUserNotConfigured'
| 'GitProjectAlreadyExists'
| 'GitRemoteAlreadyExists'
Expand Down
1 change: 1 addition & 0 deletions web-app/src/services/errors/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"FailedToConnectToGitRepo": "### Failed to Connect to Git Repo\n\nThere are several possible causes:\n\n- you may not be connected to the internet or have an unstable connection.\n- you may not have access permission to the remote tutorial repo.\n- the remote tutorial repo may not exist at the provided location",
"GitNotFound": "### Git Not Found\n\nMake sure you have Git installed.\n\nSee the [Git docs](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) for help.",
"GitConfigError": "### Git Config Error\n\n There may be an issue with your git configuration. Examine your config in ~/.gitconfig",
"GitUserNotConfigured": "### Git User Not Configured\n\nThe first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:\n```shell\ngit config --global user.name \"John Doe\"\ngit config --global user.email johndoe@example.com\n```",
"GitProjectAlreadyExists": "### Git Remote Already Exists\n\nHave you started this tutorial before in this workspace? The Git remote already exists.\n\nConsider deleting your `.git` folder and restarting.",
"GitRemoteAlreadyExists": "### Git Project Already Exists\n\nCodeRoad requires an empty Git project.\n\nOpen a new workspace to start a tutorial.",
Expand Down