Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create github issue when user submits metadata #1208

Merged
merged 35 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ebe1573
Add changes from branch
JamesTessmer Apr 10, 2024
6f97aef
add create_github_issue with basic functionality
Apr 9, 2024
c253f95
Add fields in config for github automation
Apr 13, 2024
36e918c
updated reference to token
Apr 18, 2024
d869544
remove logged_in cookie
Apr 18, 2024
7f73584
Remove debugging line
Apr 18, 2024
2b26566
Merge branch 'main' into automate-github-issues
Apr 18, 2024
fcc7efb
add fix for numsamples
Apr 18, 2024
90d3bd1
remove cookies ref in requests
Apr 18, 2024
c9ce2d8
Add logging capability
Apr 19, 2024
d529ddd
add response text error logging
Apr 19, 2024
0628ac2
update response.test to response.reason
Apr 19, 2024
a02dbc6
Add json dump and readability
Apr 19, 2024
0f4b94e
Some formatting to make markdown look nicer
Apr 19, 2024
0734a29
Fixed linting and formatting errors.
Apr 19, 2024
c1c680b
remove return statement used for tests
Apr 19, 2024
66721ba
Repositioning imports for lint
Apr 19, 2024
14dfcb3
reordered imports to the actual correct positions
Apr 19, 2024
fcffd0b
added typing to github url
Apr 19, 2024
02a77dd
Add github issue to project board function
May 2, 2024
f6fc626
small bug fixes with posting to project board
May 3, 2024
bae9a28
Fixed some spacing and added comments
May 3, 2024
71bab83
add logic to supply fields for issue if there is/is not data
May 3, 2024
562427a
remove testing line
May 3, 2024
673c151
remove other testing lines
May 3, 2024
e2d9908
Fixed formatting issues
May 3, 2024
7eae4bc
remove label tag in issue creation
May 3, 2024
d522925
fixed lint issues
May 3, 2024
f3c521f
removed trailing white space
May 3, 2024
af4c982
fixed black formatting error
May 3, 2024
40bf241
Implement PR suggestions
May 7, 2024
92f4648
standardize github variable names
May 7, 2024
6a32b5f
Merge branch 'main' into automate-github-issues
May 7, 2024
1464cd7
small fix after testing, everything functions now
May 7, 2024
89af2d8
Remove references to posting GH issue to project
May 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions nmdc_server/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import logging
from io import BytesIO
from typing import Any, Dict, List, Optional
from uuid import UUID

import requests
from fastapi import APIRouter, Depends, Header, HTTPException, Response, status
from fastapi.responses import JSONResponse, PlainTextResponse
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -728,12 +731,16 @@ async def update_submission(
status_code=400,
detail="This submission is currently being edited by a different user.",
)

# Create Github issue when metadata is being submitted
if (
submission.status == "in-progress"
and body_dict.get("status", None) == "Submitted- Pending Review"
):
create_github_issue(submission, user)
# Merge the submission metadata dicts
submission.metadata_submission = (
submission.metadata_submission | body_dict["metadata_submission"]
)

# Update permissions and status iff the user is an "owner"
if current_user_role and current_user_role.role == models.SubmissionEditorRole.owner:
new_permissions = body_dict.get("permissions", None)
Expand All @@ -747,6 +754,79 @@ async def update_submission(
return submission


def create_github_issue(submission, user):
settings = Settings()
gh_url = str(settings.github_issue_url)
token = settings.github_authentication_token
assignee = settings.github_issue_assignee
# If the settings for issue creation weren't supplied return, no need to do anything further
if gh_url == None or token == None:
return

# Gathering the fields we want to display in the issue
headers = {"Authorization": f"Bearer {token}", "Content-Type": "text/plain; charset=utf-8"}
studyform = submission.metadata_submission["studyForm"]
contextform = submission.metadata_submission["contextForm"]
multiomicsform = submission.metadata_submission["multiOmicsForm"]
pi = studyform["piName"]
piorcid = studyform["piOrcid"]
datagenerated = "Yes" if contextform["dataGenerated"] else "No"
omicsprocessingtypes = ", ".join(multiomicsform["omicsProcessingTypes"])
sampletype = ", ".join(submission.metadata_submission["templates"])
sampledata = submission.metadata_submission["sampleData"]
numsamples = 0
for key in sampledata:
numsamples = max(numsamples, len(sampledata[key]))

# some variable data to supply depending on if data has been generated or not
data_ids = []
if contextform["dataGenerated"]:
data_ids = [
"NCBI ID: " + multiomicsform["NCBIBioProjectId"],
"GOLD ID: " + multiomicsform["GOLDStudyId"],
"Alternative IDs/Names: " + ", ".join(multiomicsform["alternativeNames"]),
]

else:
data_ids = [
"JGI IDs: " + multiomicsform["JGIStudyId"],
"EMSL IDs: " + multiomicsform["studyNumber"],
"Alternative IDs/Names: " + ", ".join(multiomicsform["alternativeNames"]),
]

# assemble the body of the API request
body_lis = [
f"Submitter: {user.name}, {user.orcid}",
f"Submission ID: {submission.id}",
f"Has data been generated: {datagenerated}",
f"PI name: {pi}",
f"PI orcid: {piorcid}",
"Status: Submitted -Pending Review",
f"Data types: {omicsprocessingtypes}",
f"Sample type:{sampletype}",
f"Number of samples:{numsamples}",
] + data_ids
body_string = " \n ".join(body_lis)
payload_dict = {
"title": f"NMDC Submission: {submission.id}",
"body": body_string,
"assignees": [assignee],
}

payload = json.dumps(payload_dict)

# make request and log an error or success depending on reply
res = requests.post(url=gh_url, data=payload, headers=headers)
if res.status_code != 201:
logging.error(f"Github issue creation failed with code {res.status_code}")
logging.error(res.reason)
else:
logging.info(f"Github issue creation successful with code {res.status_code}")
logging.info(res.reason)

return res


@router.delete(
"/metadata_submission/{id}",
tags=["metadata_submission"],
Expand Down
5 changes: 5 additions & 0 deletions nmdc_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Settings(BaseSettings):
# CORS settings necessary for allowing request from Field Notes app
cors_allow_origins: Optional[str] = None # comma separated list of allowed origins

# Github Issue creation settings. Both are required for automated issue creation.
github_issue_url: Optional[str] = None
github_authentication_token: Optional[str] = None
github_issue_assignee: Optional[str] = None

@property
def current_db_uri(self) -> str:
if self.environment == "testing":
Expand Down
Loading