Skip to content
This repository was archived by the owner on May 9, 2019. It is now read-only.
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,4 @@ FakesAssemblies/
!.build/*.*
/src/.vs/config/applicationhost.config
/src/.vs/BugNET_WAP
/src/.vs/BugNET/v15/Server/sqlite3
80 changes: 80 additions & 0 deletions src/BugNET.GitHooks/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="BugNET.GitHooks.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<applicationSettings>
<BugNET.GitHooks.Properties.Settings>
<setting name="BugNetUsername" serializeAs="String">
<value>admin</value>
</setting>
<setting name="BugNetPassword" serializeAs="String">
<value>password</value>
</setting>
<setting name="ConnectionString" serializeAs="String">
<value>Data Source=.\SQLEXPRESS;Database=BugNET;Integrated Security=True;</value>
</setting>
<setting name="IssueIdRegEx" serializeAs="String">
<value>\[?([A-Za-z]{1,50}-(\d+))\]?</value>
</setting>
<setting name="BugNetServicesUrl" serializeAs="String">
<value>http://localhost:59847/Webservices/BugNetServices.asmx</value>
</setting>
<setting name="BugNetWindowsAuthentication" serializeAs="String">
<value>False</value>
</setting>
<setting name="SubversionBinDirectory" serializeAs="String">
<value />
</setting>
<setting name="BugNET_SubversionHooks_BugNetServices_BugNetServices"
serializeAs="String">
<value>http://localhost:59847/Webservices/BugNetServices.asmx</value>
</setting>
</BugNET.GitHooks.Properties.Settings>
</applicationSettings>
<log4net>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<target value="Console.Error"/>
<mapping>
<level value="FATAL"/>
<foreColor value="Red"/>
<backColor value="White"/>
</mapping>
<mapping>
<level value="ERROR"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<mapping>
<level value="WARN"/>
<foreColor value="Yellow"/>
</mapping>
<mapping>
<level value="INFO"/>
<foreColor value="Cyan"/>
</mapping>
<mapping>
<level value="DEBUG"/>
<foreColor value="Green"/>
</mapping>
<layout type="log4net.Layout.SimpleLayout"/>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt"/>
<appendToFile value="true"/>
<encoding value="utf-8"/>
<layout type="log4net.Layout.SimpleLayout"/>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="ColoredConsoleAppender"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>

</configuration>
121 changes: 121 additions & 0 deletions src/BugNET.GitHooks/BugNET.GitHooks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DFFF7129-FA65-4B99-BFEA-7F32CF206EE5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BugNET.GitHooks</RootNamespace>
<AssemblyName>BugNET.GitHooks</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.15.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.5\lib\net45-full\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.EnterpriseServices" />
<Reference Include="System.Web.Services" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Common\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="CommandExecutor.cs" />
<Compile Include="IssueTrackerIntegration.cs" />
<None Include="post-receive" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<DependentUpon>Settings.settings</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Web References\WebServices\Reference.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Reference.map</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<WebReferences Include="Web References\" />
</ItemGroup>
<ItemGroup>
<WebReferenceUrl Include="http://localhost:59847/Webservices/BugNetServices.asmx">
<UrlBehavior>Dynamic</UrlBehavior>
<RelPath>Web References\WebServices\</RelPath>
<UpdateFromURL>http://localhost:59847/Webservices/BugNetServices.asmx</UpdateFromURL>
<ServiceLocationURL>
</ServiceLocationURL>
<CachedDynamicPropName>
</CachedDynamicPropName>
<CachedAppSettingsObjectName>Settings</CachedAppSettingsObjectName>
<CachedSettingsPropName>BugNET_SubversionHooks_BugNetServices_BugNetServices</CachedSettingsPropName>
</WebReferenceUrl>
</ItemGroup>
<ItemGroup>
<None Include="Web References\WebServices\BugNetServices.disco" />
<None Include="Web References\WebServices\BugNetServices.wsdl" />
<None Include="Web References\WebServices\Reference.map">
<Generator>MSDiscoCodeGenerator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
131 changes: 131 additions & 0 deletions src/BugNET.GitHooks/CommandExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace BugNET.GitHooks
{
/// <summary>
///
/// </summary>
public static class CommandExecutor
{

private static readonly Dictionary<int, string> Errors = new Dictionary<int, string>();


/// <summary>
/// Runs the command.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="args">The args.</param>
/// <param name="echoCommand">if set to <c>true</c> [echo command].</param>
/// <returns></returns>
public static string RunCommand(string command, string args, bool echoCommand)
{
return RunCommand(command, args, 300, echoCommand);
}


/// <summary>
/// Runs a separate process and returns the standard output and error text. This is intended for command line apps only.
/// </summary>
/// <param name="command"></param>
/// <param name="args"></param>
/// <param name="killAfterSeconds"></param>
/// <param name="echoCommand"> </param>
/// <returns></returns>
public static string RunCommand(string command, string args, int killAfterSeconds = 300, bool echoCommand = true)
{
Process proc = null;
log4net.ILog logger = log4net.LogManager.GetLogger("CommandExecutor");

if (logger.IsDebugEnabled) logger.DebugFormat("Running Commandline: {0} {1}",command,args);

try
{
var startInfo = new ProcessStartInfo(command, args)
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = System.Text.Encoding.Default
};

proc = new Process {StartInfo = startInfo};
proc.ErrorDataReceived += CommandProcessErrorDataReceived;
proc.Start();

proc.BeginErrorReadLine();

var retVal = proc.StandardOutput.ReadToEnd();

if (!proc.WaitForExit(killAfterSeconds * 1000))
proc.Kill();

if (Errors.ContainsKey(proc.Id))
retVal += Environment.NewLine + "Error: " + Environment.NewLine + Errors[proc.Id];

if (echoCommand)
{
// hide password from being displayed
var regexObj = new Regex("--password\\s+\\S+\\s", RegexOptions.IgnoreCase);
args = regexObj.Replace(args, "--password **** ");


return command + " " + args + Environment.NewLine + retVal;
}
else
{
return retVal;
}

}
catch (Exception ex)
{
logger.ErrorFormat("An error occurred running the command line: {2} {3}\n\n {0} \n\n {1}", ex.Message, ex.StackTrace, command, args);
return string.Empty;
}
finally
{
if (proc != null)
{
if (Errors.ContainsKey(proc.Id))
Errors.Remove(proc.Id);

proc.Dispose();
}
}

}

/// <summary>
/// Event handler to capture error data. At least one of the output streams has to be read asynchronously
/// to avoid a deadlock.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void CommandProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
{
// RC: Sometimes an error occurres in here. I think the process is ending while we are getting the data, but Im not sure.
// I'm stuffing it for now.
try
{
if (sender == null) return;

if (string.IsNullOrEmpty(e.Data)) return;

var id = ((Process)sender).Id;

if (Errors.ContainsKey(id))
Errors[id] += Environment.NewLine + e.Data;
else
Errors.Add(id, e.Data);
}
catch (Exception)
{ }
}
}
}
Loading