Skip to content

Commit 71187b1

Browse files
committed
linter fixes
1 parent 5c67e72 commit 71187b1

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

nmdc_server/api.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,10 @@ async def update_submission(
12441244
if body_dict.get("status", None):
12451245
new_status = body_dict["status"]
12461246
allowed_transitions = {
1247-
SubmissionStatusEnum.UpdatesRequired.text: SubmissionStatusEnum.InProgress.text,
1248-
SubmissionStatusEnum.InProgress.text: SubmissionStatusEnum.SubmittedPendingReview.text
1247+
SubmissionStatusEnum.UpdatesRequired.text:
1248+
SubmissionStatusEnum.InProgress.text,
1249+
SubmissionStatusEnum.InProgress.text:
1250+
SubmissionStatusEnum.SubmittedPendingReview.text,
12491251
}
12501252
current_status = submission.status
12511253
if (
@@ -1266,15 +1268,18 @@ def create_github_issue(submission: schemas_submission.SubmissionMetadataSchema,
12661268
# If the settings for issue creation weren't supplied return, no need to do anything further
12671269
if gh_url is None or token is None:
12681270
return None
1269-
1271+
12701272
headers = {"Authorization": f"Bearer {token}", "Content-Type": "text/plain; charset=utf-8"}
12711273

12721274
# Check for existing issues first
12731275
existing_issue = check_existing_github_issue(submission.id, headers, gh_url, user)
12741276
if existing_issue:
1275-
logging.info(f"GitHub issue already exists for submission {submission.id}: {existing_issue['html_url']}")
1277+
logging.info(
1278+
f"GitHub issue already exists for submission {submission.id}: \
1279+
{existing_issue['html_url']}"
1280+
)
12761281
return existing_issue
1277-
1282+
12781283
else:
12791284

12801285
# Gathering the fields we want to display in the issue
@@ -1333,6 +1338,7 @@ def create_github_issue(submission: schemas_submission.SubmissionMetadataSchema,
13331338

13341339
return res
13351340

1341+
13361342
def update_github_issue_for_resubmission(existing_issue, user, headers):
13371343
"""
13381344
Update an existing GitHub issue to note that the submission was resubmitted.
@@ -1341,78 +1347,73 @@ def update_github_issue_for_resubmission(existing_issue, user, headers):
13411347
try:
13421348
issue_number = existing_issue.get("number")
13431349
issue_url = existing_issue.get("url") # API URL for the issue
1344-
1350+
13451351
if not issue_number or not issue_url:
13461352
logging.error("Could not find issue number or URL for existing GitHub issue")
13471353
return existing_issue
1348-
1354+
13491355
# Create a comment noting the resubmission
13501356
from datetime import datetime
1357+
13511358
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
1352-
1359+
13531360
comment_body = f"""
13541361
## 🔄 Submission Resubmitted
13551362
1356-
**Resubmitted by:** {user.name} ({user.orcid})
1357-
**Timestamp:** {timestamp}
1363+
**Resubmitted by:** {user.name} ({user.orcid})
1364+
**Timestamp:** {timestamp}
13581365
**Status:** {SubmissionStatusEnum.SubmittedPendingReview.text}
13591366
13601367
The submission has been updated and resubmitted for review.
13611368
""".strip()
1362-
1369+
13631370
# Add comment to the issue
13641371
comment_url = f"{issue_url}/comments"
13651372
comment_payload = {"body": comment_body}
1366-
1373+
13671374
comment_response = requests.post(
1368-
comment_url,
1369-
headers=headers,
1370-
data=json.dumps(comment_payload)
1375+
comment_url, headers=headers, data=json.dumps(comment_payload)
13711376
)
1372-
1377+
13731378
if comment_response.status_code == 201:
13741379
logging.info(f"Successfully added resubmission comment to GitHub issue #{issue_number}")
13751380
else:
13761381
logging.error(f"Failed to add comment to GitHub issue: {comment_response.status_code}")
1377-
1382+
13781383
# If the issue is closed, reopen it
13791384
if existing_issue.get("state") == "closed":
1380-
reopen_payload = {
1381-
"state": "open",
1382-
"state_reason": "reopened"
1383-
}
1384-
1385+
reopen_payload = {"state": "open", "state_reason": "reopened"}
1386+
13851387
reopen_response = requests.patch(
1386-
issue_url,
1387-
headers=headers,
1388-
data=json.dumps(reopen_payload)
1388+
issue_url, headers=headers, data=json.dumps(reopen_payload)
13891389
)
1390-
1390+
13911391
if reopen_response.status_code == 200:
13921392
logging.info(f"Successfully reopened GitHub issue #{issue_number}")
13931393
else:
13941394
logging.error(f"Failed to reopen GitHub issue: {reopen_response.status_code}")
1395-
1395+
13961396
return existing_issue
1397-
1397+
13981398
except Exception as e:
13991399
logging.error(f"Error updating GitHub issue for resubmission: {str(e)}")
14001400
return existing_issue
14011401

1402-
def check_existing_github_issue(submission_id: str, headers: dict, gh_base_url:str, user):
1402+
1403+
def check_existing_github_issue(submission_id: str, headers: dict, gh_base_url: str, user):
14031404
"""
14041405
Check if a GitHub issue already exists for the given submission ID using GitHub's search API.
14051406
"""
14061407
try:
1407-
repo_url = gh_base_url.replace('/issues', '')
1408+
repo_url = gh_base_url.replace("/issues", "")
14081409
search_url = f"{repo_url}/issues"
14091410
expected_title = f"NMDC Submission: {submission_id}"
14101411
params = {
14111412
"state": "all",
14121413
"per_page": 100,
14131414
}
14141415
response = requests.get(search_url, headers=headers, params=params)
1415-
1416+
14161417
if response.status_code == 200:
14171418
issues = response.json()
14181419

@@ -1428,11 +1429,12 @@ def check_existing_github_issue(submission_id: str, headers: dict, gh_base_url:s
14281429
else:
14291430
logging.warning(f"Failed to search GitHub issues: {response.status_code}")
14301431
return None
1431-
1432+
14321433
except Exception as e:
14331434
logging.error(f"Error searching GitHub issues: {str(e)}")
14341435
return None
14351436

1437+
14361438
@router.delete(
14371439
"/metadata_submission/{id}",
14381440
tags=["metadata_submission"],

tests/test_submission.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import json
12
from csv import DictReader
23
from datetime import UTC, datetime, timedelta
3-
4-
import json
54
from unittest.mock import Mock, patch
5+
66
import pytest
77
from fastapi.encoders import jsonable_encoder
88
from nmdc_schema.nmdc import SubmissionStatusEnum
@@ -1433,8 +1433,9 @@ def test_delete_submission_study_images_success(
14331433
def test_github_issue_resubmission_creates_comment_only(
14341434
db: Session, client: TestClient, logged_in_user
14351435
):
1436-
"""Test that when a GitHub issue already exists for a submission, only a comment is created (no new issue)."""
1437-
1436+
"""Test that when a GitHub issue already exists for a submission,
1437+
only a comment is created (no new issue)."""
1438+
14381439
# Create a submission
14391440
submission = fakes.MetadataSubmissionFactory(
14401441
author=logged_in_user,
@@ -1456,72 +1457,72 @@ def test_github_issue_resubmission_creates_comment_only(
14561457
"url": "https://api.github.com/repos/owner/repo/issues/123",
14571458
"html_url": "https://github.com/owner/repo/issues/123",
14581459
"title": f"NMDC Submission: {submission.id}",
1459-
"state": "open"
1460+
"state": "open",
14601461
}
14611462

14621463
# Mock responses for the GitHub API calls
14631464
mock_responses = []
1464-
1465+
14651466
# Mock the search for existing issues (returns the existing issue)
14661467
search_response = Mock()
14671468
search_response.status_code = 200
14681469
search_response.json.return_value = [existing_issue] # List of issues from repo issues endpoint
14691470
mock_responses.append(search_response)
1470-
1471+
14711472
# Mock the comment creation response
14721473
comment_response = Mock()
14731474
comment_response.status_code = 201
14741475
comment_response.json.return_value = {"id": 456, "body": "comment content"}
14751476
mock_responses.append(comment_response)
1476-
1477+
14771478
# Patch the requests.get and requests.post calls
1478-
with patch('nmdc_server.api.requests.get') as mock_get, \
1479-
patch('nmdc_server.api.requests.post') as mock_post, \
1480-
patch('nmdc_server.api.settings') as mock_settings:
1481-
1479+
with patch("nmdc_server.api.requests.get") as mock_get, patch(
1480+
"nmdc_server.api.requests.post"
1481+
) as mock_post, patch("nmdc_server.api.settings") as mock_settings:
1482+
14821483
# Configure settings
14831484
mock_settings.github_issue_url = "https://api.github.com/repos/owner/repo/issues"
14841485
mock_settings.github_authentication_token = "fake_token"
14851486
mock_settings.github_issue_assignee = "assignee"
14861487
mock_settings.host = "test-host"
1487-
1488+
14881489
# Set up the mock responses
14891490
mock_get.return_value = search_response
14901491
mock_post.return_value = comment_response
1491-
1492+
14921493
# Update submission status to trigger GitHub issue creation/update
14931494
payload = {
14941495
"status": SubmissionStatusEnum.SubmittedPendingReview.text,
1495-
"metadata_submission": {}
1496+
"metadata_submission": {},
14961497
}
1497-
1498+
14981499
response = client.request(
14991500
method="PATCH",
15001501
url=f"/api/metadata_submission/{submission.id}",
15011502
json=payload,
15021503
)
1503-
1504+
15041505
assert response.status_code == 200
1505-
1506+
15061507
# Verify that requests.get was called to search for existing issues
15071508
assert mock_get.call_count == 1
15081509
get_call = mock_get.call_args
15091510
assert "https://api.github.com/repos/owner/repo/issues" in get_call[0][0]
1510-
1511+
15111512
# Verify that requests.post was called to create a comment (not a new issue)
15121513
assert mock_post.call_count == 1
15131514
post_call = mock_post.call_args
1514-
1515+
15151516
# Verify the comment endpoint was called
15161517
assert post_call[0][0] == "https://api.github.com/repos/owner/repo/issues/123/comments"
1517-
1518+
15181519
# Verify the comment content includes resubmission information
1519-
comment_data = json.loads(post_call[1]['data'])
1520-
assert "Submission Resubmitted" in comment_data['body']
1521-
assert logged_in_user.name in comment_data['body']
1522-
assert logged_in_user.orcid in comment_data['body']
1523-
assert SubmissionStatusEnum.SubmittedPendingReview.text in comment_data['body']
1524-
1520+
comment_data = json.loads(post_call[1]["data"])
1521+
assert "Submission Resubmitted" in comment_data["body"]
1522+
assert logged_in_user.name in comment_data["body"]
1523+
assert logged_in_user.orcid in comment_data["body"]
1524+
assert SubmissionStatusEnum.SubmittedPendingReview.text in comment_data["body"]
1525+
15251526
# Verify headers include authorization
1526-
headers = post_call[1]['headers']
1527-
assert headers['Authorization'] == "Bearer fake_token"
1527+
headers = post_call[1]["headers"]
1528+
assert headers["Authorization"] == "Bearer fake_token"

0 commit comments

Comments
 (0)