Skip to content

Commit

Permalink
Rewrite test_process_pr using unittest.mock
Browse files Browse the repository at this point in the history
  • Loading branch information
iarspider committed Jun 24, 2024
1 parent 8fd02d9 commit 3c76023
Show file tree
Hide file tree
Showing 27 changed files with 541 additions and 443 deletions.
5 changes: 3 additions & 2 deletions jenkins-jobs/git/git-notify-ib-updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from github_utils import github_api
from urllib.error import HTTPError


# noinspection PyUnresolvedReferences
import libib
import shift.libib as libib

# noinspection PyUnresolvedReferences
from libib import PackageInfo, ErrorInfo
from shift.libib import PackageInfo, ErrorInfo

try:
current_shifter = libib.fetch("/SDT/shifter.txt", content_type=libib.ContentType.TEXT)
Expand Down
45 changes: 28 additions & 17 deletions process_pr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

from categories import (
CMSSW_L2,
CMSSW_L1,
Expand Down Expand Up @@ -43,7 +45,7 @@
from _py2with3compatibility import run_cmd
from json import dumps, dump, load, loads
import yaml
import sys
import sys # to test if we are doing tests or are in production mode

try:
from yaml import CLoader as Loader, CDumper as Dumper
Expand Down Expand Up @@ -238,7 +240,7 @@ def read_bot_cache(data):
res = loads_maybe_decompress(data)
for k, v in BOT_CACHE_TEMPLATE.items():
if k not in res:
res[k] = v
res[k] = copy.deepcopy(v)
collect_commit_cache(res)
return res

Expand Down Expand Up @@ -440,6 +442,19 @@ def modify_comment(comment, match, replace, dryRun):
return 0


# github_utils.set_issue_emoji -> https://github.com/PyGithub/PyGithub/blob/v1.56/github/Issue.py#L569
# github_utils.set_comment_emoji -> https://github.com/PyGithub/PyGithub/blob/v1.56/github/IssueComment.py#L149
# github_utils.delete_issue_emoji, github_utils.delete_comment_emoji -> https://github.com/PyGithub/PyGithub/blob/v1.56/github/Reaction.py#L71
def set_emoji(comment, emoji, reset_other):
if reset_other:
for e in comment.get_reactions():
login = e.user.login.encode("ascii", "ignore").decode()
if login == GH_USER and e.content != emoji:
e.delete()

comment.create_reaction(emoji)


def set_comment_emoji_cache(dryRun, bot_cache, comment, repository, emoji="+1", reset_other=True):
if dryRun:
return
Expand All @@ -449,10 +464,8 @@ def set_comment_emoji_cache(dryRun, bot_cache, comment, repository, emoji="+1",
or (bot_cache["emoji"][comment_id] != emoji)
or (comment.reactions[emoji] == 0)
):
if "Issue.Issue" in str(type(comment)):
set_issue_emoji(comment.number, repository, emoji=emoji, reset_other=reset_other)
else:
set_comment_emoji(comment.id, repository, emoji=emoji, reset_other=reset_other)

set_emoji(comment, emoji, reset_other)
bot_cache["emoji"][comment_id] = emoji
return

Expand All @@ -463,11 +476,9 @@ def has_user_emoji(bot_cache, comment, repository, emoji, user):
if (comment_id in bot_cache["emoji"]) and (comment.reactions[emoji] > 0):
e = bot_cache["emoji"][comment_id]
else:
emojis = None
if "Issue.Issue" in str(type(comment)):
emojis = get_issue_emojis(comment.number, repository)
else:
emojis = get_comment_emojis(comment.id, repository)
# github_utils.get_issue_emojis -> https://github.com/PyGithub/PyGithub/blob/v1.56/github/Issue.py#L556
# github_utils.get_comment_emojis -> https://github.com/PyGithub/PyGithub/blob/v1.56/github/IssueComment.py#L135
emojis = comment.get_reactions()
for x in emojis:
if x["user"]["login"].encode("ascii", "ignore").decode() == user:
e = x["content"]
Expand Down Expand Up @@ -847,10 +858,8 @@ def process_pr(repo_config, gh, repo, issue, dryRun, cmsbuild_user=None, force=F
if (not force) and ignore_issue(repo_config, repo, issue):
return

if "pytest" in sys.modules:
test_mode = True
else:
test_mode = False
test_mode = "pytest" in sys.modules

gh_user_char = "@"
if not notify_user(issue):
gh_user_char = ""
Expand Down Expand Up @@ -1187,7 +1196,7 @@ def process_pr(repo_config, gh, repo, issue, dryRun, cmsbuild_user=None, force=F
# Make sure bot cache has the needed keys
for k, v in BOT_CACHE_TEMPLATE.items():
if k not in bot_cache:
bot_cache[k] = v
bot_cache[k] = copy.deepcopy(v)

for comment in all_comments:
ack_comment = comment
Expand Down Expand Up @@ -1547,7 +1556,9 @@ def process_pr(repo_config, gh, repo, issue, dryRun, cmsbuild_user=None, force=F
"{2}, to re-enable processing of this PR, you can write `+commit-count` in a comment. Thanks.".format(
pr.commits,
TOO_MANY_COMMITS_WARN_THRESHOLD,
", ".join([gh_user_char + name for name in CMSSW_ISSUES_TRACKERS]),
", ".join(
sorted([gh_user_char + name for name in CMSSW_ISSUES_TRACKERS])
),
)
)
else:
Expand Down
7 changes: 6 additions & 1 deletion tests/PRActionData/TestProcessPr.test_abort.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"2056796593": "+1",
"2056801055": "+1",
"2056820593": "+1",
"2056903278": "+1"
"2056903278": "+1",
"2056930228": "+1"
},
"last_seen_sha": "dae848e38b8e387d7283a3e35818121487d9d76b",
"signatures": {
Expand Down Expand Up @@ -132,6 +133,10 @@
"context": "bot/17/jenkins"
}
},
{
"type": "edit-comment",
"data": "cms-bot internal usage<!-- bot cache: {\"commits\":{\"2a9454e30606b17e52000110972998326ce9e428\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1711538467},\"79752f053efecad55dde17732259737e621a1f3f\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1712828239},\"dae848e38b8e387d7283a3e35818121487d9d76b\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712829250},\"e4d069b76c464274bf6e7d7cf9bac2153ed9a903\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712819089}},\"emoji\":{\"2049242908\":\"+1\",\"2049536626\":\"+1\",\"2056736344\":\"+1\",\"2056739513\":\"+1\",\"2056740892\":\"+1\",\"2056796593\":\"+1\",\"2056801055\":\"+1\",\"2056820593\":\"+1\",\"2056903278\":\"+1\",\"2056930228\":\"+1\"},\"last_seen_sha\":\"dae848e38b8e387d7283a3e35818121487d9d76b\",\"signatures\":{\"2049242908\":\"2a9454e30606b17e52000110972998326ce9e428\"}} -->"
},
{
"type": "status",
"data": {
Expand Down
7 changes: 6 additions & 1 deletion tests/PRActionData/TestProcessPr.test_assign.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"2049536626": "+1",
"2056736344": "+1",
"2056739513": "+1",
"2056740892": "+1"
"2056740892": "+1",
"2056796593": "+1"
},
"last_seen_sha": "dae848e38b8e387d7283a3e35818121487d9d76b",
"signatures": {
Expand Down Expand Up @@ -92,6 +93,10 @@
"type": "remove-label",
"data": []
},
{
"type": "edit-comment",
"data": "cms-bot internal usage<!-- bot cache: {\"commits\":{\"2a9454e30606b17e52000110972998326ce9e428\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1711538467},\"79752f053efecad55dde17732259737e621a1f3f\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1712828239},\"dae848e38b8e387d7283a3e35818121487d9d76b\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712829250},\"e4d069b76c464274bf6e7d7cf9bac2153ed9a903\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712819089}},\"emoji\":{\"2049242908\":\"+1\",\"2049536626\":\"+1\",\"2056736344\":\"+1\",\"2056739513\":\"+1\",\"2056740892\":\"+1\",\"2056796593\":\"+1\"},\"last_seen_sha\":\"dae848e38b8e387d7283a3e35818121487d9d76b\",\"signatures\":{\"2049242908\":\"2a9454e30606b17e52000110972998326ce9e428\"}} -->"
},
{
"type": "status",
"data": {
Expand Down
38 changes: 0 additions & 38 deletions tests/PRActionData/TestProcessPr.test_clean_squash.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@
true
]
},
{
"type": "status",
"data": {
"commit": "35f9a4c06b006029da40ed8858e0dae4abd52cb3",
"state": "pending",
"target_url": null,
"description": "Waiting for authorized user to issue the test command.",
"context": "bot/17/jenkins"
}
},
{
"type": "add-label",
"data": [
Expand Down Expand Up @@ -175,36 +165,8 @@
"context": "cms/17/code-checks"
}
},
{
"type": "status",
"data": {
"commit": "35f9a4c06b006029da40ed8858e0dae4abd52cb3",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2056820593",
"description": "2056820593:{\"PULL_REQUESTS\": \"cms-sw/cms-bot#2134\", \"SKIP_TESTS\": \"header,static\"}",
"context": "bot/17/test_parameters"
}
},
{
"type": "emoji",
"data": [
2056820593,
"+1",
true
]
},
{
"type": "edit-comment",
"data": "cms-bot internal usage<!-- bot cache: {\"commits\":{\"2a9454e30606b17e52000110972998326ce9e428\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":true,\"time\":1711538467},\"35f9a4c06b006029da40ed8858e0dae4abd52cb3\":{\"files\":[],\"squashed\":false,\"time\":1712829250},\"79752f053efecad55dde17732259737e621a1f3f\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":true,\"time\":1712828239},\"dae848e38b8e387d7283a3e35818121487d9d76b\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":true,\"time\":1712829250},\"e4d069b76c464274bf6e7d7cf9bac2153ed9a903\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":true,\"time\":1712819089}},\"emoji\":{\"2049242908\":\"+1\",\"2049536626\":\"+1\",\"2056736344\":\"+1\",\"2056739513\":\"+1\",\"2056740892\":\"+1\",\"2056796593\":\"+1\",\"2056801055\":\"+1\",\"2056820593\":\"+1\",\"2056903278\":\"+1\",\"2056930228\":\"+1\",\"2056934192\":\"+1\",\"2056935714\":\"+1\",\"2056946596\":\"-1\",\"2056966759\":\"+1\"},\"last_seen_sha\":\"35f9a4c06b006029da40ed8858e0dae4abd52cb3\",\"signatures\":{\"2049242908\":\"2a9454e30606b17e52000110972998326ce9e428\"}} -->"
},
{
"type": "status",
"data": {
"commit": "35f9a4c06b006029da40ed8858e0dae4abd52cb3",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2056966759",
"description": "Comment by iarspider at 2024-04-15 14:14:24 UTC processed.",
"context": "bot/17/ack"
}
}
]
7 changes: 6 additions & 1 deletion tests/PRActionData/TestProcessPr.test_close.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"2056801055": "+1",
"2056820593": "+1",
"2056903278": "+1",
"2056930228": "+1"
"2056930228": "+1",
"2056934192": "+1"
},
"last_seen_sha": "dae848e38b8e387d7283a3e35818121487d9d76b",
"signatures": {
Expand Down Expand Up @@ -110,6 +111,10 @@
"type": "close",
"data": null
},
{
"type": "edit-comment",
"data": "cms-bot internal usage<!-- bot cache: {\"commits\":{\"2a9454e30606b17e52000110972998326ce9e428\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1711538467},\"79752f053efecad55dde17732259737e621a1f3f\":{\"files\":[\"Utilities/ReleaseScripts/test/BuildFile.xml\"],\"squashed\":false,\"time\":1712828239},\"dae848e38b8e387d7283a3e35818121487d9d76b\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712829250},\"e4d069b76c464274bf6e7d7cf9bac2153ed9a903\":{\"files\":[\"DQMServices/Components/test/dqmiofilecopy.sh\"],\"squashed\":false,\"time\":1712819089}},\"emoji\":{\"2049242908\":\"+1\",\"2049536626\":\"+1\",\"2056736344\":\"+1\",\"2056739513\":\"+1\",\"2056740892\":\"+1\",\"2056796593\":\"+1\",\"2056801055\":\"+1\",\"2056820593\":\"+1\",\"2056903278\":\"+1\",\"2056930228\":\"+1\",\"2056934192\":\"+1\"},\"last_seen_sha\":\"dae848e38b8e387d7283a3e35818121487d9d76b\",\"signatures\":{\"2049242908\":\"2a9454e30606b17e52000110972998326ce9e428\"}} -->"
},
{
"type": "status",
"data": {
Expand Down
127 changes: 64 additions & 63 deletions tests/PRActionData/TestProcessPr.test_code_check_approved.json
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
[{
"type": "load-bot-cache",
"data": {
"commits": {
"2a9454e30606b17e52000110972998326ce9e428": {
"files": [
"Utilities/ReleaseScripts/test/BuildFile.xml"
],
"squashed": false,
"time": 1711538467
}
},
"emoji": {},
"last_seen_sha": "2a9454e30606b17e52000110972998326ce9e428",
"signatures": {}
}
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "pending",
"target_url": null,
"description": "Waiting for authorized user to issue the test command.",
"context": "bot/17/jenkins"
}
},
{
"type": "add-label",
"data": [
"code-checks-approved"
]
},
{
"type": "remove-label",
"data": [
"code-checks-pending"
]
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2045177271",
"description": "Check details",
"context": "cms/17/code-checks"
}
},
{
"type": "create-comment",
"data": "A new Pull Request was created by @iarspider for master.\n\nIt involves the following packages:\n\n- Utilities/ReleaseScripts (**core**)\n\n\n@Dr15Jones, @iarspider, @makortel, @smuzaffar can you please review it and eventually sign? Thanks.\n@wddgit this is something you requested to watch as well.\n@iarspider you are the release manager for this.\n\ncms-bot commands are listed <a href=\"http://cms-sw.github.io/cms-bot-cmssw-cmds.html\">here</a>\n"
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2045177271",
"description": "Comment by iarspider at 2024-04-09 13:25:57 UTC processed.",
"context": "bot/17/ack"
}
}
[
{
"type": "load-bot-cache",
"data": {
"commits": {
"2a9454e30606b17e52000110972998326ce9e428": {
"files": [
"Utilities/ReleaseScripts/test/BuildFile.xml"
],
"squashed": false,
"time": 1711538467
}
},
"emoji": {},
"last_seen_sha": "2a9454e30606b17e52000110972998326ce9e428",
"signatures": {}
}
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "pending",
"target_url": null,
"description": "Waiting for authorized user to issue the test command.",
"context": "bot/17/jenkins"
}
},
{
"type": "add-label",
"data": [
"code-checks-approved"
]
},
{
"type": "remove-label",
"data": [
"code-checks-pending"
]
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2045177271",
"description": "Check details",
"context": "cms/17/code-checks"
}
},
{
"type": "create-comment",
"data": "A new Pull Request was created by @iarspider for master.\n\nIt involves the following packages:\n\n- Utilities/ReleaseScripts (**core**)\n\n\n@Dr15Jones, @iarspider, @makortel, @smuzaffar can you please review it and eventually sign? Thanks.\n@wddgit this is something you requested to watch as well.\n@iarspider you are the release manager for this.\n\ncms-bot commands are listed <a href=\"http://cms-sw.github.io/cms-bot-cmssw-cmds.html\">here</a>\n"
},
{
"type": "status",
"data": {
"commit": "2a9454e30606b17e52000110972998326ce9e428",
"state": "success",
"target_url": "https://github.com/iarspider-cmssw/cmssw/pull/17#issuecomment-2045177271",
"description": "Comment by iarspider at 2024-04-09 13:25:57 UTC processed.",
"context": "bot/17/ack"
}
}
]
Loading

0 comments on commit 3c76023

Please sign in to comment.