Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Merged in feature/auto-cleanup-lint (pull request #33)
Browse files Browse the repository at this point in the history
Automaticlly cleanup outdated lint comments
  • Loading branch information
Lusheng Lv committed Oct 13, 2016
2 parents 5a8323c + 3db1b91 commit ad7118c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 59 deletions.
6 changes: 2 additions & 4 deletions badwolf/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@

class Context(object):
"""Badwolf build/lint context"""
def __init__(self, repository, actor, type, message, source,
target=None, rebuild=False, pr_id=None, cleanup_lint=False,
nocache=False, clone_depth=50):
def __init__(self, repository, actor, type, message, source, target=None,
rebuild=False, pr_id=None, nocache=False, clone_depth=50):
self.task_id = to_text(uuid.uuid4())
self.repository = repository
self.repo_name = repository.split('/')[-1]
Expand All @@ -32,7 +31,6 @@ def __init__(self, repository, actor, type, message, source,
self.target = target
self.rebuild = rebuild
self.pr_id = pr_id
self.cleanup_lint = cleanup_lint
# Don't use cache when build Docker image
self.nocache = nocache
self.clone_depth = clone_depth
Expand Down
38 changes: 20 additions & 18 deletions badwolf/lint/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def process(self):
description = 'No code issues found'
logger.info('No problems found when linting codes')

# Report error or cleanup lint
# Report error and cleanup outdated lint comments
self._report()

if has_error:
Expand Down Expand Up @@ -129,38 +129,31 @@ def _report(self):
logger.exception('Error fetching all comments for pull request')
comments = []

hash_set = set()
existing_comments = set()
existing_comments_ids = {}
for comment in comments:
inline = comment.get('inline')
if not inline:
continue

raw = comment['content']['raw']
if self.context.cleanup_lint and raw.startswith(':broken_heart:'):
# Delete comment
try:
self.pr.delete_comment(self.context.pr_id, comment['id'])
except BitbucketAPIError:
logger.exception('Error deleting pull request comment')
else:
filename = inline['path']
line_to = inline['to']
hash_set.add(hash('{}{}{}'.format(filename, line_to, raw)))
filename = inline['path']
line_to = inline['to']
existing_comments.add((filename, line_to, raw))
existing_comments_ids[(filename, line_to, raw)] = comment['id']

if len(self.problems) == 0:
return

revision_before = self.context.target['commit']['hash']
revision_after = self.context.source['commit']['hash']
lint_comments = set()
problem_count = 0
for problem in self.problems:
content = ':broken_heart: **{}**: {}'.format(problem.linter, problem.message)
comment_hash = hash('{}{}{}'.format(
problem.filename,
problem.line,
content,
))
if comment_hash in hash_set:
commnet_tuple = (problem.filename, problem.line, content)
lint_comments.add(commnet_tuple)
if commnet_tuple in existing_comments:
continue

try:
Expand All @@ -182,6 +175,15 @@ def _report(self):
len(self.problems),
problem_count
)

outdated_comments = existing_comments - lint_comments
logger.info('%d outdated lint comments found', len(outdated_comments))
for comment in outdated_comments:
# Delete comment
try:
self.pr.delete_comment(self.context.pr_id, existing_comments_ids[comment])
except BitbucketAPIError:
logger.exception('Error deleting pull request comment')
return problem_count

def update_build_status(self, state, description=None):
Expand Down
4 changes: 1 addition & 3 deletions badwolf/webhook/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ def handle_pull_request_comment(payload):
comment_content = comment['content']['raw'].lower()
retry = 'ci retry' in comment_content
rebuild = 'ci rebuild' in comment_content
cleanup_lint = 'cleanup lint' in comment_content
nocache = 'no cache' in comment_content
if not (retry or rebuild or cleanup_lint):
if not (retry or rebuild):
return

repo = payload['repository']
Expand All @@ -245,7 +244,6 @@ def handle_pull_request_comment(payload):
target,
rebuild=rebuild,
pr_id=pr['id'],
cleanup_lint=cleanup_lint,
nocache=nocache
)
start_pipeline.delay(context)
2 changes: 1 addition & 1 deletion docs/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ Tips
-----------

* 在 commit 的 message 中包含 `ci skip` 跳过测试
* 在评论中包含 `ci retry` 重跑测试,包含 `cleanup lint` 清理之前代码检查产生的评论
* 在评论中包含 `ci retry` 重跑测试
* 在评论或 commit message 或 Pull Request 的标题/描述中包含 `ci rebuild` 重新构建 Docker 镜像,同时包含 `no cache` 禁用 Docker 构建缓存
* 在 Pull Request 的标题/描述中包含 `merge skip` 禁用自动合并 Pull Request 功能
33 changes: 0 additions & 33 deletions tests/test_bitbucket_webhook_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,6 @@ def test_pr_commit_comment_created_ci_rebuild(mock_start_pipeline, test_client):
assert mock_start_pipeline.delay.called


@mock.patch('badwolf.webhook.views.start_pipeline')
def test_pr_commit_comment_created_cleanup_lint(mock_start_pipeline, test_client):
mock_start_pipeline.delay.return_value = None
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
'scm': 'git',
},
'comment': {
'content': {
'raw': 'please cleanup lint',
}
},
'pullrequest': {
'id': 1,
'title': 'Test PR',
'state': 'OPEN',
'source': {},
'destination': {},
},
'actor': {},
})
res = test_client.post(
url_for('webhook.webhook_push'),
data=payload,
headers={
'X-Event-Key': 'pullrequest:comment_created',
}
)
assert res.status_code == 200
assert mock_start_pipeline.delay.called


@mock.patch('badwolf.webhook.views.start_pipeline')
def test_pr_commit_comment_created_ci_retry_state_not_open(mock_start_pipeline, test_client):
mock_start_pipeline.delay.return_value = None
Expand Down

1 comment on commit ad7118c

@messense
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes #5

Please sign in to comment.