Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 83a0aeb

Browse files
authored
Merge pull request #11 from codiga/chore/update-ruleset-queries
chore: update get rulesets query
2 parents 42ef6e8 + c67c28d commit 83a0aeb

3 files changed

Lines changed: 158 additions & 14 deletions

File tree

codiga/git_hook.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import docopt
3434

3535
from .constants import BLANK_SHA, API_TOKEN_ENVIRONMENT_VARIABLE
36-
from .graphql.rosie import get_rulesets
36+
from .graphql.rosie import graphql_get_rulesets
3737
from .model.rosie_rule import RosieRule, convert_rules_to_rosie_rules
3838
from .model.violation import Violation
3939
from .rosie.api import analyze_rosie
@@ -111,6 +111,7 @@ def analyze_files(files_with_language: Dict[str, str],
111111

112112
return result
113113

114+
114115
def print_violations(files_with_violations: Dict[str, List[Violation]]):
115116
"""
116117
Print all the violations from all the files.
@@ -166,7 +167,7 @@ def check_push(local_sha: str, remote_sha: str, max_timeout_secs: int):
166167

167168
log.info("using the following rulesets %s", rulesets)
168169

169-
rules = get_rulesets(api_token, rulesets)
170+
rules = graphql_get_rulesets(api_token, rulesets)
170171
rosie_rules: typing.List[RosieRule] = convert_rules_to_rosie_rules(rules)
171172

172173
log.info("found %s rules", len(rosie_rules))
@@ -226,7 +227,6 @@ def main(argv=None):
226227
log.error('%s environment variable not defined!', API_TOKEN_ENVIRONMENT_VARIABLE)
227228
sys.exit(1)
228229

229-
230230
if not remote_sha:
231231
log.error('remote_sha not defined')
232232
sys.exit(1)

codiga/graphql/rosie.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
import typing
2+
23
"""
34
All the GraphQL queries for Rosie
45
"""
56

67
from codiga.graphql.common import do_graphql_query
78

89

9-
def get_rulesets(api_token: str, ruleset_names: typing.List[str]):
10-
return list(map(lambda x: get_ruleset(api_token, x), ruleset_names))
10+
def get_rulesets_name_string(ruleset_names: typing.List[str]):
11+
"""
12+
Converts a List[str] into a string for graphql requests
13+
:param ruleset_names: List[str]
14+
:return: str
15+
"""
16+
return "[\"" + ("\", \"".join(ruleset_names)) + "\"]"
1117

1218

13-
def get_ruleset(api_token: str, ruleset_name: str):
19+
def graphql_get_rulesets(api_token: str, ruleset_names: typing.List[str]):
1420
"""
15-
Get a ruleset by its name
21+
Get rulesets by their names
1622
1723
:param api_token: the API token to access the GraphQL API
18-
:param ruleset_name: the name of all ruleset to fetch
24+
:param ruleset_names: the names of all rulesets to fetch
1925
"""
20-
if not ruleset_name or not api_token:
26+
if not ruleset_names or not api_token:
2127
raise ValueError
28+
29+
ruleset_names_string = get_rulesets_name_string(ruleset_names)
2230
query = """
2331
{
24-
ruleSet(name: \""""+ruleset_name+"""\"){
32+
ruleSetsForClient(names: """ + ruleset_names_string + """){
2533
id
2634
name
2735
rules(howmany: 10000, skip: 0){
@@ -35,11 +43,10 @@ def get_ruleset(api_token: str, ruleset_name: str):
3543
elementChecked
3644
}
3745
}
38-
}
39-
"""
46+
}"""
4047
data = do_graphql_query(api_token, {"query": query})
41-
if 'ruleSet' in data:
42-
return data['ruleSet']
48+
if 'ruleSetsForClient' in data:
49+
return data['ruleSetsForClient']
4350
return None
4451

4552

tests/graphql/test_rosie.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""
2+
Test for methods in graphql/rosie.py
3+
"""
4+
5+
import unittest
6+
from unittest.mock import patch
7+
8+
from codiga.graphql.rosie import graphql_get_rulesets, get_rulesets_name_string
9+
10+
11+
class TestRosie(unittest.TestCase):
12+
"""
13+
Tests for graphql/rosie.py
14+
"""
15+
16+
def setUp(self):
17+
pass
18+
19+
def tearDown(self):
20+
pass
21+
22+
def test_graphql_get_rulesets_invalid_argument(self):
23+
"""
24+
Check that invalid arguments raise an exception
25+
:return:
26+
"""
27+
with self.assertRaises(ValueError):
28+
graphql_get_rulesets(None, ["daniel-ruleset", "real-ruleset"])
29+
with self.assertRaises(ValueError):
30+
graphql_get_rulesets("blo", [])
31+
32+
@patch('codiga.graphql.rosie.do_graphql_query')
33+
def test_graphql_get_rulesets_valid_argument(self, do_graphql_query_mock):
34+
"""
35+
Check the value returned when called with valid arguments
36+
:return:
37+
"""
38+
fake_data = [
39+
{
40+
"id": 1,
41+
"name": "daniel-ruleset",
42+
"rules": [
43+
{
44+
"id": 1,
45+
"name": "rule-1",
46+
"content": "cHJpbnQoIkhlbGxvIFdvcmxkISIp",
47+
"language": "Python",
48+
"ruleType": "Ast",
49+
"pattern": None,
50+
"patternMultiline": None,
51+
"elementChecked": "FunctionCall"
52+
}
53+
]
54+
},
55+
{
56+
"id": 2,
57+
"name": "real-ruleset",
58+
"rules": [
59+
{
60+
"id": 3,
61+
"name": "rule-3",
62+
"content": "cHJpbnQoIkhlbGxvIFdvcmxkISIp",
63+
"language": "Python",
64+
"ruleType": "Ast",
65+
"pattern": None,
66+
"patternMultiline": None,
67+
"elementChecked": "FunctionCall"
68+
}
69+
]
70+
}
71+
]
72+
do_graphql_query_mock.return_value = {"ruleSetsForClient": fake_data}
73+
data = graphql_get_rulesets("api_token", ["daniel-ruleset", "real-ruleset"])
74+
do_graphql_query_mock.assert_called_with("api_token", {"query": """
75+
{
76+
ruleSetsForClient(names: """ + get_rulesets_name_string(["daniel-ruleset", "real-ruleset"]) + """){
77+
id
78+
name
79+
rules(howmany: 10000, skip: 0){
80+
id
81+
name
82+
content
83+
language
84+
ruleType
85+
pattern
86+
patternMultiline
87+
elementChecked
88+
}
89+
}
90+
}"""})
91+
self.assertEqual(fake_data, data)
92+
93+
@patch('codiga.graphql.rosie.do_graphql_query')
94+
def test_graphql_get_rulesets_call(self, do_graphql_query_mock):
95+
"""
96+
Check that we call the right method when arguments are valid
97+
:return:
98+
"""
99+
graphql_get_rulesets("api_token", ["daniel-ruleset", "real-ruleset"])
100+
query1 = """
101+
{
102+
ruleSetsForClient(names: """ + get_rulesets_name_string(["daniel-ruleset", "real-ruleset"]) + """){
103+
id
104+
name
105+
rules(howmany: 10000, skip: 0){
106+
id
107+
name
108+
content
109+
language
110+
ruleType
111+
pattern
112+
patternMultiline
113+
elementChecked
114+
}
115+
}
116+
}"""
117+
do_graphql_query_mock.assert_called_with("api_token", {"query": query1})
118+
119+
graphql_get_rulesets("api_token", ["real-ruleset"])
120+
query2 = """
121+
{
122+
ruleSetsForClient(names: """ + get_rulesets_name_string(["real-ruleset"]) + """){
123+
id
124+
name
125+
rules(howmany: 10000, skip: 0){
126+
id
127+
name
128+
content
129+
language
130+
ruleType
131+
pattern
132+
patternMultiline
133+
elementChecked
134+
}
135+
}
136+
}"""
137+
do_graphql_query_mock.assert_called_with("api_token", {"query": query2})

0 commit comments

Comments
 (0)