Skip to content

Commit 06e5880

Browse files
committed
validate git is configured with user.name & email
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 34b1e6e commit 06e5880

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/actions/onValidateSetup.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as T from 'typings'
21
import * as E from 'typings/error'
32
import { version } from '../services/dependencies'
43
import { checkWorkspaceEmpty } from '../services/workspace'
54
import { send } from '../commands'
5+
import { validateGitConfig } from '../services/git'
66

77
const onValidateSetup = async (): Promise<void> => {
88
try {
@@ -43,6 +43,27 @@ const onValidateSetup = async (): Promise<void> => {
4343
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
4444
return
4545
}
46+
47+
const isGitUserNameConfigured = await validateGitConfig('user.name')
48+
const isGitUserEmailConfigured = await validateGitConfig('user.email')
49+
if (!isGitUserNameConfigured || !isGitUserEmailConfigured) {
50+
let message = ''
51+
if (!isGitUserNameConfigured) message += 'Git user not configured.\n'
52+
if (!isGitUserEmailConfigured) message += 'Git email not configured.'
53+
const error: E.ErrorMessage = {
54+
type: 'GitUserNotConfigured',
55+
message,
56+
actions: [
57+
{
58+
label: 'Check Again',
59+
transition: 'RETRY',
60+
},
61+
],
62+
}
63+
send({ type: 'VALIDATE_SETUP_FAILED', payload: { error } })
64+
return
65+
}
66+
4667
send({ type: 'SETUP_VALIDATED' })
4768
} catch (e) {
4869
const error = {

src/services/git/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,16 @@ export async function commitsExistsByMessage(message: string): Promise<boolean>
191191
return false
192192
}
193193
}
194+
195+
export async function validateGitConfig(target: string): Promise<boolean> {
196+
try {
197+
// returns a list of commit hashes
198+
const { stdout, stderr } = await exec({ command: `git config ${target}` })
199+
if (stderr) {
200+
return false
201+
}
202+
return !!stdout.length
203+
} catch (error) {
204+
return false
205+
}
206+
}

typings/error.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type ErrorMessageView = 'FULL_PAGE' | 'NOTIFY' | 'NONE'
33
export type ErrorMessageType =
44
| 'FailedToConnectToGitRepo'
55
| 'GitNotFound'
6+
| 'GitUserNotConfigured'
67
| 'GitProjectAlreadyExists'
78
| 'GitRemoteAlreadyExists'
89
| 'MissingTutorialDependency'

web-app/src/services/errors/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"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",
33
"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.",
4+
"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```",
45
"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.",
56
"GitRemoteAlreadyExists": "### Git Project Already Exists\n\nCodeRoad requires an empty Git project.\n\nOpen a new workspace to start a tutorial.",
67
"MissingTutorialDependency": "### Missing Tutorial Dependency\n\nThe tutorial cannot run because it a dependency is not yet installed. Install the dependency and click \"Check Again\".",

0 commit comments

Comments
 (0)