internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: RedHatInsights/insights-core

Issue:
Detect rules with possible duplicate response keys
The identifier of a rule result in json responses is the base name of the module containing the rule and the key the rule supplied to `make_*`. This can lead to collisions that are almost impossible to detect until runtime, especially if rules are created in different repos and then deployed together.


Discussion:

Resolving patch:
diff --git a/insights/tools/dupkeycheck.py b/insights/tools/dupkeycheck.py
new file mode 100755
--- /dev/null
+++ b/insights/tools/dupkeycheck.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+"""
+Given a list of plugins to load, print a report of any containing rules that
+might produce the same result keys.
+"""
+from __future__ import print_function
+import argparse
+import inspect
+from collections import defaultdict
+
+from insights import dr, parse_plugins, rule
+
+
+def parse_args():
+    p = argparse.ArgumentParser(description="Find rule modules that could produce the same response keys.")
+    p.add_argument("-p",
+                   "--plugins",
+                   default="",
+                   help="Comma separated list of package(s) or module(s) containing plugins.")
+    return p.parse_args()
+
+
+def main():
+    args = parse_args()
+
+    plugins = parse_plugins(args.plugins)
+    for p in plugins:
+        dr.load_components(p, continue_on_error=False)
+
+    results = defaultdict(list)
+    for comp, delegate in dr.DELEGATES.items():
+        if isinstance(delegate, rule):
+            results[dr.get_base_module_name(comp)].append(comp)
+
+    results = dict((key, comps) for key, comps in results.items() if len(comps) > 1)
+
+    if results:
+        print("Potential key conflicts:")
+        print()
+        for key in sorted(results):
+            print("{key}:".format(key=key))
+            for comp in sorted(results[key], key=dr.get_name):
+                name = comp.__name__
+                path = inspect.getabsfile(comp)
+                print("    {name} in {path}".format(name=name, path=path))
+            print()
+
+
+if __name__ == "__main__":
+    main()
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -16,6 +16,7 @@
         'insights-run = insights:main',
         'insights = insights.command_parser:main',
         'insights-cat = insights.tools.cat:main',
+        'insights-dupkeycheck = insights.tools.dupkeycheck:main',
         'insights-inspect = insights.tools.insights_inspect:main',
         'insights-info = insights.tools.query:main',
         'gen_api = insights.tools.generate_api_config:main',


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Dmesg combiner always succeeds
The [Dmesg combiner has only optional dependencies](https://github.com/RedHatInsights/insights-core/blob/master/insights/combiners/dmesg.py#L51), which means it always succeeds. This is an anti-pattern.


Discussion:

Resolving patch:
diff --git a/insights/combiners/dmesg.py b/insights/combiners/dmesg.py
--- a/insights/combiners/dmesg.py
+++ b/insights/combiners/dmesg.py
@@ -48,7 +48,7 @@
 from insights.parsers.dmesg_log import DmesgLog
 
 
-@combiner(optional=[DmesgLineList, DmesgLog])
+@combiner([DmesgLineList, DmesgLog])
 class Dmesg(object):
     """
     Combiner for ``dmesg`` command and ``/var/log/dmesg`` file.


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Core doesn't detect chkconfig in services directory of sos archives
We have a [spec](https://github.com/RedHatInsights/insights-core/blob/master/insights/specs/sos_archive.py#L27) for `sos_commands/startup/chkconfig_--list` but miss `sos_commands/services/chkconfig_--list`.


Discussion:

Resolving patch:
diff --git a/insights/specs/sos_archive.py b/insights/specs/sos_archive.py
--- a/insights/specs/sos_archive.py
+++ b/insights/specs/sos_archive.py
@@ -24,7 +24,7 @@ class SosSpecs(Specs):
     ceph_osd_tree_text = simple_file("sos_commands/ceph/ceph_osd_tree")
     ceph_report = simple_file("sos_commands/ceph/ceph_report")
     ceph_health_detail = simple_file("sos_commands/ceph/ceph_health_detail_--format_json-pretty")
-    chkconfig = simple_file("sos_commands/startup/chkconfig_--list")
+    chkconfig = first_file(["sos_commands/startup/chkconfig_--list", "sos_commands/services/chkconfig_--list"])
     date = first_of([simple_file("sos_commands/general/date"), simple_file("sos_commands/date/date")])
     df__al = first_file(["sos_commands/filesys/df_-al", "sos_commands/filesys/df_-al_-x_autofs"])
     display_java = simple_file("sos_commands/java/alternatives_--display_java")


Test patch:
Repository: RedHatInsights/insights-core

Issue:
CLI dumps tracebacks instead of simple message
The CLI for insights isn't protected with a top level try / catch, so some tracebacks resulting from bad arguments get dumped to the console. It should just print the error message instead.


Discussion:

Resolving patch:
diff --git a/insights/command_parser.py b/insights/command_parser.py
--- a/insights/command_parser.py
+++ b/insights/command_parser.py
@@ -85,7 +85,10 @@ def fix_arg_dashes():
 
 def main():
     fix_arg_dashes()
-    InsightsCli()
+    try:
+        InsightsCli()
+    except BaseException as ex:
+        print(ex)
 
 
 if __name__ == "__main__":
diff --git a/insights/core/dr.py b/insights/core/dr.py
--- a/insights/core/dr.py
+++ b/insights/core/dr.py
@@ -376,8 +376,7 @@ def _import(path, continue_on_error):
     log.debug("Importing %s" % path)
     try:
         return importlib.import_module(path)
-    except Exception as ex:
-        log.exception(ex)
+    except BaseException:
         if not continue_on_error:
             raise
 


Test patch:
Repository: RedHatInsights/insights-core

Issue:
bash_version example doesn't work with json format
Running `insights run -p examples/rules -f json` results in a traceback because the `bash_version` rule puts an `InstalledRpm` object into its response:

```
TypeError: Object of type 'InstalledRpm' is not JSON serializable
```


Discussion:

Resolving patch:
diff --git a/examples/rules/bash_version.py b/examples/rules/bash_version.py
--- a/examples/rules/bash_version.py
+++ b/examples/rules/bash_version.py
@@ -11,7 +11,7 @@
 
 $ python sample_rules.py
 """
-from insights.core.plugins import make_pass, rule
+from insights.core.plugins import make_info, rule
 from insights.parsers.installed_rpms import InstalledRpms
 
 KEY = "BASH_VERSION"
@@ -21,5 +21,5 @@
 
 @rule(InstalledRpms)
 def report(rpms):
-    bash_ver = rpms.get_max('bash')
-    return make_pass(KEY, bash_version=bash_ver)
+    bash = rpms.get_max('bash')
+    return make_info(KEY, bash_version=bash.nvra)


Test patch:
Repository: RedHatInsights/insights-core

Issue:
lscpu datasource missing for sos reports
If we add a new spec, let's check if it exists in sos report and include a spec for it if it does. lscpu is in `sos_commands/processor/lscpu`.


Discussion:

Resolving patch:
diff --git a/insights/specs/sos_archive.py b/insights/specs/sos_archive.py
--- a/insights/specs/sos_archive.py
+++ b/insights/specs/sos_archive.py
@@ -60,6 +60,7 @@ class SosSpecs(Specs):
     journal_since_boot = first_of([simple_file("sos_commands/logs/journalctl_--no-pager_--boot"), simple_file("sos_commands/logs/journalctl_--no-pager_--catalog_--boot")])
     locale = simple_file("sos_commands/i18n/locale")
     lsblk = simple_file("sos_commands/block/lsblk")
+    lscpu = simple_file("sos_commands/processor/lscpu")
     lsinitrd = simple_file("sos_commands/boot/lsinitrd")
     lsof = simple_file("sos_commands/process/lsof_-b_M_-n_-l")
     lsmod = simple_file("sos_commands/kernel/lsmod")


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Specs for hostname should be `/bin/hostname` but not `/usr/bin/hostname`
> @xiangce this command should match the command in insights-core `/usr/bin/hostname`.


@bfahr - Thanks for that. But it should be `/bin/hostname` since the `/usr/bin/hostname` is not available on RHEL6. I'll fix the one in `insights-core` instead of changing the specs in uploader.json.

_Originally posted by @xiangce in https://github.com/_render_node/MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDMyMTA1MjkyMA==/comments/review_comment_


Discussion:

Resolving patch:
diff --git a/insights/specs/default.py b/insights/specs/default.py
--- a/insights/specs/default.py
+++ b/insights/specs/default.py
@@ -336,9 +336,9 @@ def dumpdev(broker):
     heat_crontab = simple_command("/usr/bin/crontab -l -u heat")
     heat_crontab_container = simple_command("docker exec heat_api_cron /usr/bin/crontab -l -u heat")
     heat_engine_log = first_file(["/var/log/containers/heat/heat-engine.log", "/var/log/heat/heat-engine.log"])
-    hostname = simple_command("/usr/bin/hostname -f")
-    hostname_default = simple_command("/usr/bin/hostname")
-    hostname_short = simple_command("/usr/bin/hostname -s")
+    hostname = simple_command("/bin/hostname -f")
+    hostname_default = simple_command("/bin/hostname")
+    hostname_short = simple_command("/bin/hostname -s")
     hosts = simple_file("/etc/hosts")
     hponcfg_g = simple_command("/sbin/hponcfg -g")
     httpd_access_log = simple_file("/var/log/httpd/access_log")
@@ -427,7 +427,7 @@ def httpd_on_nfs(broker):
     init_process_cgroup = simple_file("/proc/1/cgroup")
     interrupts = simple_file("/proc/interrupts")
     ip_addr = simple_command("/sbin/ip addr")
-    ip_addresses = simple_command("/usr/bin/hostname -I")
+    ip_addresses = simple_command("/bin/hostname -I")
     ip_route_show_table_all = simple_command("/sbin/ip route show table all")
     ip_s_link = simple_command("/sbin/ip -s link")
     ipaupgrade_log = simple_file("/var/log/ipaupgrade.log")


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Add "No files found for" to the CommandParser's blacklist
We get some data from `systemctl` by using `systemctl cat foo.service`. If the service doesn't exist, the string "No files found for foo.service" is returned instead of a valid doc.


Discussion:

Resolving patch:
diff --git a/insights/core/__init__.py b/insights/core/__init__.py
--- a/insights/core/__init__.py
+++ b/insights/core/__init__.py
@@ -514,6 +514,7 @@ class CommandParser(Parser):
             "no such file or directory",
             "command not found",
             "no module named",
+            "no files found for",
     ]
     """
     This variable contains filters for bad responses from commands defined


Test patch:
diff --git a/insights/tests/test_commandparser.py b/insights/tests/test_commandparser.py
--- a/insights/tests/test_commandparser.py
+++ b/insights/tests/test_commandparser.py
@@ -4,6 +4,7 @@
 import pytest
 
 CMF = "blah: Command not found"
+NO_FILES_FOUND = "No files found for docker.service"
 NO_SUCH_FILE = "/usr/bin/blah: No such file or directory"
 MULTI_LINE = "blah: Command not found\n" \
              "/usr/bin/blah: No such file or directory"
@@ -20,6 +21,12 @@ def test_command_not_found():
     assert "Command not found" in str(e.value)
 
 
+def test_no_files_found():
+    with pytest.raises(ContentException) as e:
+        MockParser(context_wrap(NO_FILES_FOUND))
+    assert "No files found for" in str(e.value)
+
+
 def test_no_such_file_or_directory():
     with pytest.raises(ContentException) as e:
         MockParser(context_wrap(NO_SUCH_FILE))
Repository: RedHatInsights/insights-core

Issue:
Double Check satellite_version.CapsuleVersion Combiner
CapsuleVersion Combiner currently relies on satellite-capsule package.

satellite-capsule package can be installed on satellite server t0o and plugin rules may use the combiner to wrongly identify satellite server as capsule.

"foreman" package would not be there on Capsule server and hence can be added as a check. 


Discussion:

Resolving patch:
diff --git a/insights/combiners/satellite_version.py b/insights/combiners/satellite_version.py
--- a/insights/combiners/satellite_version.py
+++ b/insights/combiners/satellite_version.py
@@ -219,7 +219,8 @@ def __init__(self, rpms):
 
         # For Capsule, ONLY 6.2 and newer are supported
         sat62_pkg = rpms.get_max('satellite-capsule')
-        if sat62_pkg:
+        # foreman package should not be there on Capsule Server
+        if sat62_pkg and 'foreman' not in rpms:
             self.full = sat62_pkg.package
             self.version = sat62_pkg.version
             self.release = sat62_pkg.release


Test patch:
diff --git a/insights/combiners/tests/test_satellite_version.py b/insights/combiners/tests/test_satellite_version.py
--- a/insights/combiners/tests/test_satellite_version.py
+++ b/insights/combiners/tests/test_satellite_version.py
@@ -43,7 +43,6 @@
 """
 
 installed_rpms_62_cap = """
-foreman-1.11.0.53-1.el7sat.noarch                           Wed May 18 14:16:25 2016
 scl-utils-20120927-27.el7_6.x86_64                          Wed May 18 14:18:16 2016
 SDL-1.2.14-6.el7.x86_64                                     Wed May 18 14:16:25 2016
 satellite-capsule-6.2.0.11-1.el7sat.noarch                  Wed May 18 14:16:25 2016
Repository: RedHatInsights/insights-core

Issue:
Add links section to text format
Update the [text based rule report](https://github.com/RedHatInsights/insights-core/blob/master/insights/formats/text.py) to automatically include links associated with rules. simple_html and html already include them.


Discussion:

Resolving patch:
diff --git a/insights/formats/text.py b/insights/formats/text.py
--- a/insights/formats/text.py
+++ b/insights/formats/text.py
@@ -5,6 +5,7 @@
 from collections import namedtuple
 
 from pprint import pprint
+from six import StringIO
 from insights import dr, datasource, rule, condition, incident, parser
 from insights.core.context import ExecutionContext
 from insights.formats import Formatter, FormatterAdapter, render
@@ -33,6 +34,24 @@ def _find_context(broker):
             return v
 
 
+def render_links(component):
+    links = dr.get_delegate(component).links or {}
+    if any(links.values()):
+        space = " " * 4
+        dbl_space = space * 2
+        output = StringIO()
+        output.write("Links:\n")
+        for key in sorted(links):
+            values = sorted(links[key])
+            if values:
+                output.write(space + key + ":\n")
+                for v in values:
+                    output.write(dbl_space + v + "\n")
+        output.seek(0)
+        return output.read()
+    return ""
+
+
 class HumanReadableFormat(Formatter):
     """
     This class prints a human readable summary of rule hits. It should be used
@@ -142,6 +161,7 @@ def printit(c, v):
             name = "%s%s%s" % (resp.color, name, Style.RESET_ALL)
             print(name, file=self.stream)
             print(underline, file=self.stream)
+            print(render_links(c), file=self.stream)
             print(render(c, v), file=self.stream)
             print(file=self.stream)
 


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Update YAMLParser to use libyaml's CSafeLoader if it's available
The [YAMLParser](https://github.com/RedHatInsights/insights-core/blob/master/insights/core/__init__.py#L685) currently uses the pure python SafeLoader of pyyaml. It'll load files about 10 times faster if libyaml is installed and we use `yaml.CSafeLoader` instead. See the [ocpshell code](https://github.com/RedHatInsights/insights-core/blob/master/insights/ocpshell.py#L11) for an example.


Discussion:

Resolving patch:
diff --git a/insights/core/__init__.py b/insights/core/__init__.py
--- a/insights/core/__init__.py
+++ b/insights/core/__init__.py
@@ -21,6 +21,11 @@
 from . import ls_parser
 from insights.util import deprecated
 
+try:
+    from yaml import CSafeLoader as SafeLoader
+except ImportError:
+    from yaml import SafeLoader
+
 import sys
 # Since XPath expression is not supported by the ElementTree in Python 2.6,
 # import insights.contrib.ElementTree when running python is prior to 2.6 for compatibility.
@@ -690,9 +695,9 @@ class YAMLParser(Parser, LegacyItemAccess):
     def parse_content(self, content):
         try:
             if type(content) is list:
-                self.data = yaml.safe_load('\n'.join(content))
+                self.data = yaml.load('\n'.join(content), Loader=SafeLoader)
             else:
-                self.data = yaml.safe_load(content)
+                self.data = yaml.load(content, Loader=SafeLoader)
 
             if not isinstance(self.data, (dict, list)):
                 raise ParseException("YAML didn't produce a dictionary or list.")


Test patch:
Repository: RedHatInsights/insights-core

Issue:
Enhance parser "satellite_installer_configurations"
As a default, the file "/etc/foreman-installer/custom-hiera.yaml" is empty, it means customers haven't done any tuning, it shouldn't be skipped.


Discussion:

Resolving patch:
diff --git a/insights/parsers/satellite_installer_configurations.py b/insights/parsers/satellite_installer_configurations.py
--- a/insights/parsers/satellite_installer_configurations.py
+++ b/insights/parsers/satellite_installer_configurations.py
@@ -12,6 +12,7 @@
 
 from insights import parser, YAMLParser
 from insights.specs import Specs
+from insights.parsers import SkipException
 
 
 @parser(Specs.satellite_custom_hiera)
@@ -25,5 +26,8 @@ class CustomHiera(YAMLParser):
         >>> custom_hiera['apache::mod::prefork::serverlimit']
         582
     """
-
-    pass
+    def parse_content(self, content):
+        try:
+            super(CustomHiera, self).parse_content(content)
+        except SkipException:
+            pass


Test patch:
diff --git a/insights/parsers/tests/test_satellite_installer_configurations.py b/insights/parsers/tests/test_satellite_installer_configurations.py
--- a/insights/parsers/tests/test_satellite_installer_configurations.py
+++ b/insights/parsers/tests/test_satellite_installer_configurations.py
@@ -10,6 +10,31 @@
 apache::mod::prefork::startservers: 10
 """
 
+CUSTOM_HIERA_CONFIG_2 = """
+---
+# This YAML file lets you set your own custom configuration in Hiera for the
+# installer puppet modules that might not be exposed to users directly through
+# installer arguments.
+#
+# For example, to set 'TraceEnable Off' in Apache, a common requirement for
+# security auditors, add this to this file:
+#
+#   apache::trace_enable: Off
+#
+# Consult the full module documentation on http://forge.puppetlabs.com,
+# or the actual puppet classes themselves, to discover options to configure.
+#
+# Do note, setting some values may have unintended consequences that affect the
+# performance or functionality of the application. Consider the impact of your
+# changes before applying them, and test them in a non-production environment
+# first.
+#
+# Here are some examples of how you tune the Apache options if needed:
+#
+# apache::mod::prefork::startservers: 8
+
+"""
+
 
 def test_custom_hiera():
     result = satellite_installer_configurations.CustomHiera(context_wrap(CUSTOM_HIERA_CONFIG))
@@ -17,6 +42,9 @@ def test_custom_hiera():
     assert result.data['apache::mod::prefork::startservers'] == 10
     assert result.data['apache::mod::prefork::maxclients'] == 582
 
+    result = satellite_installer_configurations.CustomHiera(context_wrap(CUSTOM_HIERA_CONFIG_2))
+    assert result.data is None
+
 
 def test_doc():
     env = {
Repository: RedHatInsights/insights-core

Issue:
Bond parser is not collecting data for currently active primary_slave
current parser is unable to collect data for "Primary Slave" key.

```
Bonding Mode: fault-tolerance (active-backup)
**Primary Slave: eth6 (primary_reselect failure)**
Currently Active Slave: eth7
MII Status: up
MII Polling Interval (ms): 200
Up Delay (ms): 0
Down Delay (ms): 0
```

Requested a PR: https://github.com/RedHatInsights/insights-core/pull/2465


Discussion:

Resolving patch:
diff --git a/insights/parsers/bond.py b/insights/parsers/bond.py
--- a/insights/parsers/bond.py
+++ b/insights/parsers/bond.py
@@ -102,6 +102,7 @@ def parse_content(self, content):
         self._slave_link_failure_count = []
         self._slave_speed = []
         self._slave_duplex = []
+        self._primary_slave = None
 
         for line in get_active_lines(content):
             if line.startswith("Bonding Mode: "):
@@ -135,6 +136,8 @@ def parse_content(self, content):
                 self._arp_polling_interval = line.strip().split(':', 1)[1].strip()
             elif line.strip().startswith("ARP IP target/s (n.n.n.n form):"):
                 self._arp_ip_target = line.strip().split(':', 1)[1].strip()
+            elif line.strip().startswith("Primary Slave"):
+                self._primary_slave = line.split(":", 1)[1].split()[0]
 
     @property
     def bond_mode(self):
@@ -221,3 +224,10 @@ def arp_ip_target(self):
         if no "ARP IP target/s (n.n.n.n form)" key is found.
         """
         return self._arp_ip_target
+
+    @property
+    def primary_slave(self):
+        """Returns the "Primary Slave" in the bond file if key/value exists.
+        If the key is not in the bond file, ``None`` is returned.
+        """
+        return self._primary_slave


Test patch:
diff --git a/insights/parsers/tests/test_bond.py b/insights/parsers/tests/test_bond.py
--- a/insights/parsers/tests/test_bond.py
+++ b/insights/parsers/tests/test_bond.py
@@ -223,6 +223,7 @@ def test_bond_class():
     assert bond_obj_4.file_name == 'bond0'
     assert bond_obj_4.arp_polling_interval == "1000"
     assert bond_obj_4.arp_ip_target == "10.152.1.1"
+    assert bond_obj_4.primary_slave == 'em3'
 
     with pytest.raises(ParseException) as exc:
         bond_obj = Bond(context_wrap(BONDINFO_UNKNOWN_BOND_MODE, CONTEXT_PATH))
Repository: RedHatInsights/insights-core

Issue:
YAMLParser shouldn't raise ParseException when the file is empty
Currently, YAMLParser raises ParseException when the file is empty, however it isn't parse error. It just means there is no return data for it.


Discussion:
https://pyyaml.org/wiki/PyYAMLDocumentation#YAMLError

Resolving patch:
diff --git a/insights/core/__init__.py b/insights/core/__init__.py
--- a/insights/core/__init__.py
+++ b/insights/core/__init__.py
@@ -698,9 +698,13 @@ def parse_content(self, content):
                 self.data = yaml.load('\n'.join(content), Loader=SafeLoader)
             else:
                 self.data = yaml.load(content, Loader=SafeLoader)
-
+            if self.data is None:
+                raise SkipException("There is no data")
             if not isinstance(self.data, (dict, list)):
                 raise ParseException("YAML didn't produce a dictionary or list.")
+        except SkipException as se:
+            tb = sys.exc_info()[2]
+            six.reraise(SkipException, SkipException(str(se)), tb)
         except:
             tb = sys.exc_info()[2]
             cls = self.__class__


Test patch:
diff --git a/insights/tests/test_yaml_parser.py b/insights/tests/test_yaml_parser.py
--- a/insights/tests/test_yaml_parser.py
+++ b/insights/tests/test_yaml_parser.py
@@ -1,7 +1,7 @@
 import datetime
 import pytest
 
-from insights.core import YAMLParser, ParseException
+from insights.core import YAMLParser, ParseException, SkipException
 from insights.tests import context_wrap
 
 
@@ -20,6 +20,15 @@
 """: ['Hesperiidae', 'Papilionidae', 'Apatelodidae', 'Epiplemidae']
 }
 
+empty_yaml_content = """
+---
+# This YAML file is empty
+""".strip()
+
+wrong_yaml_content = """
+"unbalanced blackets: ]["
+""".strip()
+
 
 class FakeYamlParser(YAMLParser):
     """ Class for parsing the content of ``branch_info``."""
@@ -57,3 +66,11 @@ def test_settings_yml_list():
     result = FakeYamlParser(ctx)
     assert result.data['remote_branch'] == -1
     assert result.data['remote_leaf'] == -1
+
+
+def test_empty_content():
+    ctx = context_wrap(empty_yaml_content)
+    with pytest.raises(SkipException) as ex:
+        FakeYamlParser(ctx)
+
+    assert "There is no data" in ex.value.args[0]