1
+ import json
2
+ import logging
1
3
from io import BytesIO
2
4
from typing import Any , Dict , List , Optional
3
5
from uuid import UUID
4
6
7
+ import requests
5
8
from fastapi import APIRouter , Depends , Header , HTTPException , Response , status
6
9
from fastapi .responses import JSONResponse , PlainTextResponse
7
10
from sqlalchemy .orm import Session
@@ -728,12 +731,16 @@ async def update_submission(
728
731
status_code = 400 ,
729
732
detail = "This submission is currently being edited by a different user." ,
730
733
)
731
-
734
+ # Create Github issue when metadata is being submitted
735
+ if (
736
+ submission .status == "in-progress"
737
+ and body_dict .get ("status" , None ) == "Submitted- Pending Review"
738
+ ):
739
+ create_github_issue (submission , user )
732
740
# Merge the submission metadata dicts
733
741
submission .metadata_submission = (
734
742
submission .metadata_submission | body_dict ["metadata_submission" ]
735
743
)
736
-
737
744
# Update permissions and status iff the user is an "owner"
738
745
if current_user_role and current_user_role .role == models .SubmissionEditorRole .owner :
739
746
new_permissions = body_dict .get ("permissions" , None )
@@ -747,6 +754,79 @@ async def update_submission(
747
754
return submission
748
755
749
756
757
+ def create_github_issue (submission , user ):
758
+ settings = Settings ()
759
+ gh_url = str (settings .github_issue_url )
760
+ token = settings .github_authentication_token
761
+ assignee = settings .github_issue_assignee
762
+ # If the settings for issue creation weren't supplied return, no need to do anything further
763
+ if gh_url == None or token == None :
764
+ return
765
+
766
+ # Gathering the fields we want to display in the issue
767
+ headers = {"Authorization" : f"Bearer { token } " , "Content-Type" : "text/plain; charset=utf-8" }
768
+ studyform = submission .metadata_submission ["studyForm" ]
769
+ contextform = submission .metadata_submission ["contextForm" ]
770
+ multiomicsform = submission .metadata_submission ["multiOmicsForm" ]
771
+ pi = studyform ["piName" ]
772
+ piorcid = studyform ["piOrcid" ]
773
+ datagenerated = "Yes" if contextform ["dataGenerated" ] else "No"
774
+ omicsprocessingtypes = ", " .join (multiomicsform ["omicsProcessingTypes" ])
775
+ sampletype = ", " .join (submission .metadata_submission ["templates" ])
776
+ sampledata = submission .metadata_submission ["sampleData" ]
777
+ numsamples = 0
778
+ for key in sampledata :
779
+ numsamples = max (numsamples , len (sampledata [key ]))
780
+
781
+ # some variable data to supply depending on if data has been generated or not
782
+ data_ids = []
783
+ if contextform ["dataGenerated" ]:
784
+ data_ids = [
785
+ "NCBI ID: " + multiomicsform ["NCBIBioProjectId" ],
786
+ "GOLD ID: " + multiomicsform ["GOLDStudyId" ],
787
+ "Alternative IDs/Names: " + ", " .join (multiomicsform ["alternativeNames" ]),
788
+ ]
789
+
790
+ else :
791
+ data_ids = [
792
+ "JGI IDs: " + multiomicsform ["JGIStudyId" ],
793
+ "EMSL IDs: " + multiomicsform ["studyNumber" ],
794
+ "Alternative IDs/Names: " + ", " .join (multiomicsform ["alternativeNames" ]),
795
+ ]
796
+
797
+ # assemble the body of the API request
798
+ body_lis = [
799
+ f"Submitter: { user .name } , { user .orcid } " ,
800
+ f"Submission ID: { submission .id } " ,
801
+ f"Has data been generated: { datagenerated } " ,
802
+ f"PI name: { pi } " ,
803
+ f"PI orcid: { piorcid } " ,
804
+ "Status: Submitted -Pending Review" ,
805
+ f"Data types: { omicsprocessingtypes } " ,
806
+ f"Sample type:{ sampletype } " ,
807
+ f"Number of samples:{ numsamples } " ,
808
+ ] + data_ids
809
+ body_string = " \n " .join (body_lis )
810
+ payload_dict = {
811
+ "title" : f"NMDC Submission: { submission .id } " ,
812
+ "body" : body_string ,
813
+ "assignees" : [assignee ],
814
+ }
815
+
816
+ payload = json .dumps (payload_dict )
817
+
818
+ # make request and log an error or success depending on reply
819
+ res = requests .post (url = gh_url , data = payload , headers = headers )
820
+ if res .status_code != 201 :
821
+ logging .error (f"Github issue creation failed with code { res .status_code } " )
822
+ logging .error (res .reason )
823
+ else :
824
+ logging .info (f"Github issue creation successful with code { res .status_code } " )
825
+ logging .info (res .reason )
826
+
827
+ return res
828
+
829
+
750
830
@router .delete (
751
831
"/metadata_submission/{id}" ,
752
832
tags = ["metadata_submission" ],
0 commit comments