Ejemplo n.º 1
0
        public bool TryProcessCommand(IExecutionContext context, string input, ContainerInfo container)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(false);
            }

            // TryParse input to Command
            ActionCommand actionCommand;

            if (!ActionCommand.TryParseV2(input, _registeredCommands, out actionCommand) &&
                !ActionCommand.TryParse(input, _registeredCommands, out actionCommand))
            {
                return(false);
            }

            if (!ActionCommandManager.EnhancedAnnotationsEnabled(context) && actionCommand.Command == "notice")
            {
                context.Debug($"Enhanced Annotations not enabled on the server: 'notice' command will not be processed.");
                return(false);
            }

            // Serialize order
            lock (_commandSerializeLock)
            {
                // Currently stopped
                if (_stopProcessCommand)
                {
                    // Resume token
                    if (!string.IsNullOrEmpty(_stopToken) &&
                        string.Equals(actionCommand.Command, _stopToken, StringComparison.OrdinalIgnoreCase))
                    {
                        context.Output(input);
                        context.Debug("Resume processing commands");
                        _registeredCommands.Remove(_stopToken);
                        _stopProcessCommand = false;
                        _stopToken          = null;
                        return(true);
                    }
                    else
                    {
                        context.Debug($"Process commands has been stopped and waiting for '##[{_stopToken}]' to resume.");
                        return(false);
                    }
                }
                // Currently processing
                else
                {
                    // Stop command
                    if (string.Equals(actionCommand.Command, _stopCommand, StringComparison.OrdinalIgnoreCase))
                    {
                        ValidateStopToken(context, actionCommand.Data);

                        _stopToken          = actionCommand.Data;
                        _stopProcessCommand = true;
                        _registeredCommands.Add(_stopToken);
                        if (_stopToken.Length > 6)
                        {
                            HostContext.SecretMasker.AddValue(_stopToken);
                        }

                        context.Output(input);
                        context.Debug("Paused processing commands until the token you called ::stopCommands:: with is received");
                        return(true);
                    }
                    // Found command
                    else if (_commandExtensions.TryGetValue(actionCommand.Command, out IActionCommandExtension extension))
                    {
                        if (context.EchoOnActionCommand && !extension.OmitEcho)
                        {
                            context.Output(input);
                        }

                        try
                        {
                            extension.ProcessCommand(context, input, actionCommand, container);
                        }
                        catch (Exception ex)
                        {
                            var commandInformation = extension.OmitEcho ? extension.Command : input;
                            context.Error($"Unable to process command '{commandInformation}' successfully.");
                            context.Error(ex);
                            context.CommandResult = TaskResult.Failed;
                        }
                    }
                    // Command not found
                    else
                    {
                        context.Warning($"Can't find command extension for ##[{actionCommand.Command}.command].");
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void ProcessCommand(IExecutionContext context, string inputLine, ActionCommand command, ContainerInfo container)
        {
            command.Properties.TryGetValue(IssueCommandProperties.File, out string file);
            command.Properties.TryGetValue(IssueCommandProperties.Line, out string line);
            command.Properties.TryGetValue(IssueCommandProperties.Column, out string column);

            if (!ActionCommandManager.EnhancedAnnotationsEnabled(context))
            {
                context.Debug("Enhanced Annotations not enabled on the server. The 'title', 'end_line', and 'end_column' fields are unsupported.");
            }

            Issue issue = new Issue()
            {
                Category = "General",
                Type     = this.Type,
                Message  = command.Data
            };

            if (!string.IsNullOrEmpty(file))
            {
                issue.Category = "Code";

                if (container != null)
                {
                    // Translate file path back from container path
                    file = container.TranslateToHostPath(file);
                    command.Properties[IssueCommandProperties.File] = file;
                }

                // Get the values that represent the server path given a local path
                string repoName = context.GetGitHubContext("repository");
                var    repoPath = context.GetGitHubContext("workspace");

                string relativeSourcePath = IOUtil.MakeRelative(file, repoPath);
                if (!string.Equals(relativeSourcePath, file, IOUtil.FilePathStringComparison))
                {
                    // add repo info
                    if (!string.IsNullOrEmpty(repoName))
                    {
                        command.Properties["repo"] = repoName;
                    }

                    if (!string.IsNullOrEmpty(relativeSourcePath))
                    {
                        // replace sourcePath with the new relative path
                        // prefer `/` on all platforms
                        command.Properties[IssueCommandProperties.File] = relativeSourcePath.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
                    }
                }
            }

            foreach (var property in command.Properties)
            {
                if (!string.Equals(property.Key, Constants.Runner.InternalTelemetryIssueDataKey, StringComparison.OrdinalIgnoreCase))
                {
                    issue.Data[property.Key] = property.Value;
                }
            }

            context.AddIssue(issue);
        }