import Client from './client';
import getScope from './get-scope.ts';
import getArgs from './get-args';

export default async (sentry, error, apiUrl, configFiles) => {
  let user = null;
  let team = null;

  try {
    const { token } = configFiles.readAuthConfigFile();
    const { currentTeam } = configFiles.readConfigFile();
    const client = new Client({ apiUrl, token, currentTeam, debug: false });
    ({ user, team } = await getScope(client));
  } catch (err) {
    // We can safely ignore this, as the error
    // reporting works even without this metadata attached.
  }

  sentry.withScope(scope => {
    if (user) {
      const spec = {
        email: user.email,
        id: user.uid
      };

      if (user.username) {
        spec.username = user.username;
      }

      if (user.name) {
        spec.name = user.name;
      }

      scope.setUser(spec);
    }

    if (team) {
      scope.setTag('currentTeam', team.id);
    }

    // Report process.argv without sensitive data
    let args
    try {
      args = getArgs(process.argv.slice(2), {});
    } catch (_) {}

    if (args) {
      const flags = ['--env', '--build-env', '--token']
      for (const flag of flags) {
        if (args[flag]) args[flag] = 'REDACTED';
      }
      if (args._.length >= 4 && args._[0].startsWith('secret') && args._[1] === 'add') {
        args._[3] = 'REDACTED';
      }
      scope.setExtra('args', args);
    } else {
      scope.setExtra('args', 'Unable to parse args');
    }

    sentry.captureException(error);
  });

  const client = sentry.getCurrentHub().getClient();

  if (client) {
    await client.close();
  }
};

@@ -171,23 +171,16 @@
 let user
- = null
 ;%0A  let 
@@ -187,15 +187,26 @@
 team
- = null
+;%0A  let scopeError
 ;%0A%0A 
@@ -575,16 +575,38 @@
 tached.%0A
+    scopeError = err;%0A
   %7D%0A%0A  s
@@ -962,24 +962,200 @@
 id);%0A    %7D%0A%0A
+    if (scopeError) %7B%0A      scope.setExtra('scopeError', %7B%0A        name: scopeError.name,%0A        message: scopeError.message,%0A        stack: scopeError.stack%0A      %7D);%0A    %7D%0A%0A
     // Repor
@@ -1156,16 +1156,17 @@
  Report 
+%60
 process.
@@ -1169,16 +1169,17 @@
 ess.argv
+%60
  without
@@ -1206,16 +1206,36 @@
 let args
+;%0A    let argsError;
 %0A    try
@@ -1303,12 +1303,42 @@
 ch (
-_) %7B
+err) %7B%0A      argsError = err;%0A    
 %7D%0A%0A 
@@ -1407,16 +1407,17 @@
 -token'%5D
+;
 %0A      f
@@ -1506,24 +1506,33 @@
 %7D%0A      if (
+%0A        
 args._.lengt
@@ -1540,16 +1540,24 @@
  %3E= 4 &&
+%0A       
  args._%5B
@@ -1582,16 +1582,24 @@
 ret') &&
+%0A       
  args._%5B
@@ -1610,16 +1610,23 @@
 == 'add'
+%0A      
 ) %7B%0A    
@@ -1720,61 +1720,314 @@
     
-scope.setExtra('args', 'Unable to parse args');
+let msg = 'Unable to parse args';%0A      if (argsError) %7B%0A        msg += %60: $%7BargsError%7D%60;%0A      %7D%0A      scope.setExtra('args', msg);%0A    %7D%0A%0A    // Report information about the version of %60node%60 being used%0A    scope.setExtra('node', %7B%0A      execPath: process.execPath,%0A      version: process.version
 %0A    %7D
+);
 %0A%0A  

Add information when team/user/args parsing fails to Sentry reports (#2635)