Skip to content

Commit 0e46842

Browse files
committed
Merge pull request #18 from codebendercc/disqus-create-comments
Added support to create comments into DisqusWrapper.
2 parents 005e761 + 8735e66 commit 0e46842

File tree

5 files changed

+115
-50
lines changed

5 files changed

+115
-50
lines changed

codebender_testing/disqus.py

Lines changed: 109 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515
FORUM = 'codebender-cc'
16-
AUTHOR_NAME = 'codebender'
1716
AUTHOR_URL = 'https://codebender.cc/user/codebender'
1817
DISQUS_REQUESTS_PER_HOUR = 1000
1918
DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60) / 60
@@ -34,7 +33,9 @@ def __init__(self, log_time):
3433
'email': os.getenv('DISQUS_SSO_EMAIL', None),
3534
}
3635
self.SSO_KEY = self.get_disqus_sso(self.user)
37-
self.disqus = disqusapi.DisqusAPI(api_secret=self.DISQUS_API_SECRET, public_key=self.DISQUS_API_PUBLIC, remote_auth=self.SSO_KEY)
36+
self.disqus = disqusapi.DisqusAPI(api_secret=self.DISQUS_API_SECRET,
37+
public_key=self.DISQUS_API_PUBLIC,
38+
remote_auth=self.SSO_KEY)
3839
self.change_log = {}
3940
self.last_post = None
4041
self.last_library = None
@@ -61,16 +62,23 @@ def update_comment(self, sketch, results, current_date, log_entry, openFailFlag,
6162
if not openFailFlag:
6263
log_entry = self.handle_example_comment(sketch, results, current_date, log_entry)
6364

64-
# Comment libraries when finished with the examples
65+
# Comment libraries when finished commenting the examples
6566
library_match = re.match(r'.+\/example\/(.+)\/.+', sketch)
6667
library = None
6768
if library_match:
6869
library = library_match.group(1)
6970
if not self.last_library:
7071
self.last_library = library
71-
if library and library != self.last_library and (library not in self.examples_without_library or counter >= total_sketches-1):
72-
log_entry = self.handle_library_comment(library, current_date, log_entry)
73-
self.last_library = library
72+
73+
library_to_comment = None
74+
if self.last_library and self.last_library not in self.examples_without_library and library != self.last_library:
75+
library_to_comment = self.last_library
76+
if library and library not in self.examples_without_library and counter >= total_sketches-1:
77+
library_to_comment = library
78+
if library_to_comment:
79+
log_entry = self.handle_library_comment(library_to_comment, current_date, log_entry)
80+
81+
self.last_library = library
7482

7583
return log_entry
7684

@@ -80,77 +88,131 @@ def handle_library_comment(self, library, current_date, log):
8088
if url not in log:
8189
log[url] = {}
8290
try:
83-
paginator = disqusapi.Paginator(self.disqus.api.threads.list, forum=FORUM, thread=identifier, method='GET')
91+
log[url]['comment'] = False
92+
paginator = disqusapi.Paginator(self.disqus.api.threads.list,
93+
forum=FORUM,
94+
thread=identifier, method='GET')
8495
if paginator:
96+
comment_updated = False
97+
new_message = self.messages['library'].replace('TEST_DATE', current_date)
8598
for page in paginator:
86-
post_id, existing_message = self.get_posts(page['id'])
87-
if post_id and existing_message:
88-
new_message = self.messages['library'].replace('TEST_DATE', current_date)
89-
log[url]['comment'] = self.update_post(post_id, new_message)
90-
else:
91-
log[url]['comment'] = False
99+
post_id, existing_message = self.get_posts(page['id'])
100+
if post_id and existing_message:
101+
log[url]['comment'] = self.update_post(post_id, new_message)
102+
comment_updated = True
103+
break
104+
105+
if not comment_updated:
106+
log[url]['comment'] = self.create_post(identifier, new_message)
92107
except Exception as error:
93108
print 'Error:', error
94109
log[url]['comment'] = False
110+
95111
return log
96112

97113
def handle_example_comment(self, url, results, current_date, log):
98114
identifier = url.replace('https://codebender.cc', '')
99115
identifier = 'ident:' + identifier
100116
try:
101-
paginator = disqusapi.Paginator(self.disqus.api.threads.list, forum=FORUM, thread=identifier, method='GET')
117+
log[url]['comment'] = False
118+
paginator = disqusapi.Paginator(self.disqus.api.threads.list,
119+
forum=FORUM,
120+
thread=identifier, method='GET')
102121
if paginator:
122+
comment_updated = False
123+
boards = []
124+
unsupportedFlag = False
125+
for result in results:
126+
if result['status'] == 'success':
127+
board = result['board']
128+
if re.match(r'Arduino Mega.+', board):
129+
board = 'Arduino Mega'
130+
boards.append(board)
131+
elif result['status'] == 'unsupported':
132+
unsupportedFlag = True
133+
134+
new_message = self.messages['example_fail'].replace('TEST_DATE', current_date)
135+
if len(boards) > 0:
136+
new_message = self.messages['example_success'].replace('TEST_DATE', current_date).replace('BOARDS_LIST', ', '.join(boards))
137+
elif unsupportedFlag:
138+
new_message = self.messages['example_unsupported'].replace('TEST_DATE', current_date)
139+
103140
for page in paginator:
104-
post_id, existing_message = self.get_posts(page['id'])
105-
if post_id and existing_message:
106-
boards = []
107-
unsupportedFlag = False
108-
for result in results:
109-
if result['status'] == 'success':
110-
board = result['board']
111-
if re.match(r'Arduino Mega.+', board):
112-
board = 'Arduino Mega'
113-
boards.append(board)
114-
elif result['status'] == 'unsupported':
115-
unsupportedFlag = True
116-
117-
new_message = self.messages['example_fail'].replace('TEST_DATE', current_date)
118-
if len(boards) > 0:
119-
new_message = self.messages['example_success'].replace('TEST_DATE', current_date).replace('BOARDS_LIST', ', '.join(boards))
120-
elif unsupportedFlag:
121-
new_message = self.messages['example_unsupported'].replace('TEST_DATE', current_date)
122-
log[url]['comment'] = self.update_post(post_id, new_message)
123-
break
124-
else:
125-
log[url]['comment'] = False
141+
post_id, existing_message = self.get_posts(page['id'])
142+
if post_id and existing_message:
143+
log[url]['comment'] = self.update_post(post_id, new_message)
144+
comment_updated = True
145+
break
146+
147+
if not comment_updated:
148+
log[url]['comment'] = self.create_post(identifier, new_message)
126149
except Exception as error:
127150
print 'Error:', error
128151
log[url]['comment'] = False
152+
129153
return log
130154

131155
def get_posts(self, thread_id):
132156
post_id = None
133157
raw_message = None
134-
paginator = disqusapi.Paginator(self.disqus.api.posts.list, forum=FORUM, thread=thread_id, order='asc', method='GET')
135-
if paginator:
136-
for result in paginator:
137-
if result['author']['name'] == AUTHOR_NAME and result['author']['url'] == AUTHOR_URL:
138-
post_id = result['id']
139-
raw_message = result['raw_message']
140-
break
158+
try:
159+
paginator = disqusapi.Paginator(self.disqus.api.posts.list,
160+
forum=FORUM,
161+
thread=thread_id,
162+
order='asc', method='GET')
163+
if paginator:
164+
for result in paginator:
165+
if result['author']['name'] == self.user['username'] and result['author']['url'] == AUTHOR_URL:
166+
post_id = result['id']
167+
raw_message = result['raw_message']
168+
break
169+
except Exception as error:
170+
print 'Error:', error
171+
141172
return post_id, raw_message
142173

174+
def create_post(self, thread_id, message):
175+
if not self.last_post:
176+
self.last_post = message
177+
elif re.match(r'^.+\.$', self.last_post):
178+
message = message[:-1]
179+
self.last_post = message
180+
181+
comment_status = False
182+
183+
try:
184+
response = self.disqus.threads.list(api_secret=self.DISQUS_API_SECRET,
185+
forum=FORUM,
186+
thread=thread_id, method='GET')
187+
response = self.disqus.posts.create(api_secret=self.DISQUS_API_SECRET,
188+
remote_auth=self.SSO_KEY,
189+
thread=response[0]['id'],
190+
message=message, method='POST')
191+
if response['raw_message'] == message:
192+
comment_status = True
193+
except Exception as error:
194+
print 'Error:', error
195+
196+
return comment_status
197+
143198
def update_post(self, post_id, message):
144199
if not self.last_post:
145200
self.last_post = message
146201
elif re.match(r'^.+\.$', self.last_post):
147202
message = message[:-1]
148203
self.last_post = message
204+
205+
comment_status = False
206+
149207
try:
150-
response = self.disqus.posts.update(api_secret=self.DISQUS_API_SECRET, api_key=self.DISQUS_API_PUBLIC, remote_auth=self.SSO_KEY, access_token=self.DISQUS_ACCESS_TOKEN, post=post_id, message=message, method='POST')
208+
response = self.disqus.posts.update(api_secret=self.DISQUS_API_SECRET,
209+
access_token=self.DISQUS_ACCESS_TOKEN,
210+
remote_auth=self.SSO_KEY,
211+
post=post_id,
212+
message=message, method='POST')
151213
if response['raw_message'] == message:
152-
return True
153-
return False
214+
comment_status = True
154215
except Exception as error:
155216
print 'Error:', error
156-
return False
217+
218+
return comment_status

codebender_testing/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def report_creator(compile_type, log_entry, log_file):
130130
logs_re = re.compile(r'.+libraries_test.+')
131131
elif compile_type == 'fetch':
132132
logs_re = re.compile(r'.+libraries_fetch.+')
133+
elif compile_type == 'target_library':
134+
logs_re = re.compile(r'.+target_libraries.+')
133135

134136
logs = sorted([x for x in logs if x != '.gitignore' and logs_re.match(x)])
135137
tail = logs[-2:]
@@ -628,7 +630,7 @@ def compile_sketches(self, sketches, iframe=False, project_view=False, logfile=N
628630
test_status = 'U'
629631

630632
# Update Disqus comments
631-
if compile_type == 'library' and comment:
633+
if compile_type in ['library', 'target_library'] and comment:
632634
log_entry = disqus_wrapper.update_comment(sketch, results, current_date, log_entry, openFailFlag, counter, total_sketches)
633635

634636
# Dump the test results to `logfile`.

data/examples_without_library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"05.Control",
77
"06.Sensors",
88
"07.Display",
9-
"07.Display",
9+
"08.Strings",
1010
"09.USB",
1111
"10.StarterKit",
1212
"ArduinoISP"

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def webdriver(request, desired_capabilities):
6565

6666
webdriver = config.create_webdriver(command_executor, desired_capabilities)
6767
driver = webdriver['driver']
68+
driver.maximize_window()
6869
profile_path = webdriver['profile_path']
6970

7071
def finalizer():

tests/target_libraries/test_target_libraries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_compile_target_libraries(self):
4646
if len(urls_to_follow) > 0:
4747
"""Tests that specific library examples compile successfully."""
4848
self.compile_sketches(urls_to_follow,
49-
compile_type='library',
49+
compile_type='target_library',
5050
iframe=False, project_view=True,
5151
create_report=True, logfile=LOG_FILE,
5252
comment=True)

0 commit comments

Comments
 (0)