Skip to content

Commit

Permalink
problem matcher default severity (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple authored Nov 22, 2019
1 parent 159e4c5 commit 7d505f7
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 8 deletions.
62 changes: 57 additions & 5 deletions src/Runner.Worker/IssueMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public MatcherChangedEventArgs(IssueMatcherConfig config)

public sealed class IssueMatcher
{
private string _defaultSeverity;
private string _owner;
private IssuePattern[] _patterns;
private IssueMatch[] _state;

public IssueMatcher(IssueMatcherConfig config, TimeSpan timeout)
{
_owner = config.Owner;
_defaultSeverity = config.Severity;
_patterns = config.Patterns.Select(x => new IssuePattern(x , timeout)).ToArray();
Reset();
}
Expand All @@ -37,13 +39,26 @@ public string Owner
{
if (_owner == null)
{
_owner = String.Empty;
_owner = string.Empty;
}

return _owner;
}
}

public string DefaultSeverity
{
get
{
if (_defaultSeverity == null)
{
_defaultSeverity = string.Empty;
}

return _defaultSeverity;
}
}

public IssueMatch Match(string line)
{
// Single pattern
Expand All @@ -54,7 +69,7 @@ public IssueMatch Match(string line)

if (regexMatch.Success)
{
return new IssueMatch(null, pattern, regexMatch.Groups);
return new IssueMatch(null, pattern, regexMatch.Groups, DefaultSeverity);
}

return null;
Expand Down Expand Up @@ -95,7 +110,7 @@ public IssueMatch Match(string line)
}

// Return
return new IssueMatch(runningMatch, pattern, regexMatch.Groups);
return new IssueMatch(runningMatch, pattern, regexMatch.Groups, DefaultSeverity);
}
// Not the last pattern
else
Expand Down Expand Up @@ -169,7 +184,7 @@ public IssuePattern(IssuePatternConfig config, TimeSpan timeout)

public sealed class IssueMatch
{
public IssueMatch(IssueMatch runningMatch, IssuePattern pattern, GroupCollection groups)
public IssueMatch(IssueMatch runningMatch, IssuePattern pattern, GroupCollection groups, string defaultSeverity = null)
{
File = runningMatch?.File ?? GetValue(groups, pattern.File);
Line = runningMatch?.Line ?? GetValue(groups, pattern.Line);
Expand All @@ -178,6 +193,11 @@ public IssueMatch(IssueMatch runningMatch, IssuePattern pattern, GroupCollection
Code = runningMatch?.Code ?? GetValue(groups, pattern.Code);
Message = runningMatch?.Message ?? GetValue(groups, pattern.Message);
FromPath = runningMatch?.FromPath ?? GetValue(groups, pattern.FromPath);

if (string.IsNullOrEmpty(Severity) && !string.IsNullOrEmpty(defaultSeverity))
{
Severity = defaultSeverity;
}
}

public string File { get; }
Expand Down Expand Up @@ -256,6 +276,9 @@ public sealed class IssueMatcherConfig
[DataMember(Name = "owner")]
private string _owner;

[DataMember(Name = "severity")]
private string _severity;

[DataMember(Name = "pattern")]
private IssuePatternConfig[] _patterns;

Expand All @@ -265,7 +288,7 @@ public string Owner
{
if (_owner == null)
{
_owner = String.Empty;
_owner = string.Empty;
}

return _owner;
Expand All @@ -277,6 +300,24 @@ public string Owner
}
}

public string Severity
{
get
{
if (_severity == null)
{
_severity = string.Empty;
}

return _severity;
}

set
{
_severity = value;
}
}

public IssuePatternConfig[] Patterns
{
get
Expand All @@ -303,6 +344,17 @@ public void Validate()
throw new ArgumentException("Owner must not be empty");
}

// Validate severity
switch ((_severity ?? string.Empty).ToUpperInvariant())
{
case "":
case "ERROR":
case "WARNING":
break;
default:
throw new ArgumentException($"Matcher '{_owner}' contains unexpected default severity '{_severity}'");
}

// Validate at least one pattern
if (_patterns == null || _patterns.Length == 0)
{
Expand Down
82 changes: 79 additions & 3 deletions src/Test/L0/Worker/IssueMatcherL0.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using GitHub.Runner.Worker;
using GitHub.Services.WebApi;
using Xunit;
Expand Down Expand Up @@ -353,6 +350,48 @@ public void Config_Validate_PropertyOutOfRange_LessThanZero()
config.Validate();
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Matcher_MultiplePatterns_DefaultSeverity()
{
var config = JsonUtility.FromString<IssueMatchersConfig>(@"
{
""problemMatcher"": [
{
""owner"": ""myMatcher"",
""severity"": ""warning"",
""pattern"": [
{
""regexp"": ""^(ERROR)?(?: )?(.+):$"",
""severity"": 1,
""code"": 2
},
{
""regexp"": ""^(.+)$"",
""message"": 1
}
]
}
]
}
");
config.Validate();
var matcher = new IssueMatcher(config.Matchers[0], TimeSpan.FromSeconds(1));

var match = matcher.Match("ABC:");
match = matcher.Match("not-working");
Assert.Equal("warning", match.Severity);
Assert.Equal("ABC", match.Code);
Assert.Equal("not-working", match.Message);

match = matcher.Match("ERROR ABC:");
match = matcher.Match("not-working");
Assert.Equal("ERROR", match.Severity);
Assert.Equal("ABC", match.Code);
Assert.Equal("not-working", match.Message);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
Expand Down Expand Up @@ -754,6 +793,43 @@ public void Matcher_SetsOwner()
Assert.Equal("myMatcher", matcher.Owner);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
public void Matcher_SinglePattern_DefaultSeverity()
{
var config = JsonUtility.FromString<IssueMatchersConfig>(@"
{
""problemMatcher"": [
{
""owner"": ""myMatcher"",
""severity"": ""warning"",
""pattern"": [
{
""regexp"": ""^(ERROR)?(?: )?(.+): (.+)$"",
""severity"": 1,
""code"": 2,
""message"": 3
}
]
}
]
}
");
config.Validate();
var matcher = new IssueMatcher(config.Matchers[0], TimeSpan.FromSeconds(1));

var match = matcher.Match("ABC: not-working");
Assert.Equal("warning", match.Severity);
Assert.Equal("ABC", match.Code);
Assert.Equal("not-working", match.Message);

match = matcher.Match("ERROR ABC: not-working");
Assert.Equal("ERROR", match.Severity);
Assert.Equal("ABC", match.Code);
Assert.Equal("not-working", match.Message);
}

[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Worker")]
Expand Down

0 comments on commit 7d505f7

Please sign in to comment.