Skip to content

validate git is configured with user.name & email #457

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
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/actions/onValidateSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as T from 'typings'
import * as E from 'typings/error'
import { version } from '../services/dependencies'
import { checkWorkspaceEmpty } from '../services/workspace'
import { send } from '../commands'
import { validateGitConfig } from '../services/git'

const onValidateSetup = async (): Promise<void> => {
try {
Expand Down Expand Up @@ -43,6 +43,27 @@ const onValidateSetup = async (): Promise<void> => {
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
return
}

const isGitUserNameConfigured = await validateGitConfig('user.name')
const isGitUserEmailConfigured = await validateGitConfig('user.email')
if (!isGitUserNameConfigured || !isGitUserEmailConfigured) {
let message = ''
if (!isGitUserNameConfigured) message += 'Git user not configured.\n'
if (!isGitUserEmailConfigured) message += 'Git email not configured.'
const error: E.ErrorMessage = {
type: 'GitUserNotConfigured',
message,
actions: [
{
label: 'Check Again',
transition: 'RETRY',
},
],
}
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
return
}

send({ type: 'SETUP_VALIDATED' })
} catch (e) {
const error = {
Expand Down
13 changes: 13 additions & 0 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,16 @@ export async function commitsExistsByMessage(message: string): Promise<boolean>
return false
}
}

export async function validateGitConfig(target: string): Promise<boolean> {
try {
// returns a list of commit hashes
const { stdout, stderr } = await exec({ command: `git config ${target}` })
if (stderr) {
return false
}
return !!stdout.length
} catch (error) {
return false
}
}
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'
| 'GitUserNotConfigured'
| 'GitProjectAlreadyExists'
| 'GitRemoteAlreadyExists'
| 'MissingTutorialDependency'
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.",
"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.",
"MissingTutorialDependency": "### Missing Tutorial Dependency\n\nThe tutorial cannot run because it a dependency is not yet installed. Install the dependency and click \"Check Again\".",
Expand Down