@@ -1049,7 +1049,7 @@ private Response doSubmit(String json, InputStream file, FormDataContentDisposit
10491049 } catch ( Exception e ) {
10501050 log .error ("OSTI GitLab failure: " + e .getMessage ());
10511051 return ErrorResponse
1052- .internalServerError ("Unable to create OSTI Hosted project." )
1052+ .internalServerError ("Unable to create OSTI Hosted project: " + e . getMessage () )
10531053 .build ();
10541054 }
10551055
@@ -1184,7 +1184,7 @@ private Response doAnnounce(String json, InputStream file, FormDataContentDispos
11841184 } catch ( Exception e ) {
11851185 log .error ("OSTI GitLab failure: " + e .getMessage ());
11861186 return ErrorResponse
1187- .internalServerError ("Unable to create OSTI Hosted project." )
1187+ .internalServerError ("Unable to create OSTI Hosted project: " + e . getMessage () )
11881188 .build ();
11891189 }
11901190
@@ -1911,114 +1911,121 @@ private static void sendPOCNotification(DOECodeMetadata md) {
19111911 * @param md the METADATA to process GitLab for
19121912 */
19131913 private static void processOSTIGitLab (DOECodeMetadata md ) throws Exception {
1914- // only process OSTI Hosted type
1915- if (!DOECodeMetadata .Accessibility .CO .equals (md .getAccessibility ()))
1916- return ;
1917-
1918- String fileName = md .getFileName ();
1919-
1920- // sometimes getFileName returns a full path (this should be addressed overall, but temp fix here for now)
1921- fileName = fileName .substring (fileName .lastIndexOf (File .separator )+1 );
1922-
1923- String codeId = String .valueOf (md .getCodeId ());
1924- java .nio .file .Path uploadedFile = Paths .get (FILE_UPLOADS , String .valueOf (codeId ), fileName );
1925-
1926- // if no file was uploaded, fail
1927- if (!Files .exists (uploadedFile ))
1928- throw new Exception ("File not found in Uploads directory! [" + uploadedFile .toString () + "]" );
1929-
1930- // convert to base64 for GitLab
1931- String base64Content = convertBase64 (new FileInputStream (uploadedFile .toString ()));
1914+ try {
1915+ // only process OSTI Hosted type
1916+ if (!DOECodeMetadata .Accessibility .CO .equals (md .getAccessibility ()))
1917+ return ;
19321918
1933- if (base64Content == null )
1934- throw new Exception ("Base 64 Content required for OSTI Hosted project!" );
1919+ String fileName = md .getFileName ();
19351920
1936- String projectName = "dc-" + md . getCodeId ();
1937- String hostedFolder = "hosted_files" ;
1921+ // sometimes getFileName returns a full path (this should be addressed overall, but temp fix here for now)
1922+ fileName = fileName . substring ( fileName . lastIndexOf ( File . separator )+ 1 ) ;
19381923
1939- String uploadFile = hostedFolder + "/" + fileName ;
1924+ String codeId = String .valueOf (md .getCodeId ());
1925+ java .nio .file .Path uploadedFile = Paths .get (FILE_UPLOADS , String .valueOf (codeId ), fileName );
19401926
1941- GitLabAPI glApi = new GitLabAPI ();
1942- glApi .setProjectName (projectName );
1927+ // if no file was uploaded, fail
1928+ if (!Files .exists (uploadedFile ))
1929+ throw new Exception ("File not found in Uploads directory! [" + uploadedFile .toString () + "]" );
19431930
1944- // check existance of project
1945- Project project = glApi . fetchProject ( );
1931+ // convert to base64 for GitLab
1932+ String base64Content = convertBase64 ( new FileInputStream ( uploadedFile . toString ()) );
19461933
1947- Commit commit = new Commit ();
1948- if (project == null ) {
1949- // create new project, if none exists
1950- project = glApi .createProject (md );
1934+ if (base64Content == null )
1935+ throw new Exception ("Base 64 Content required for OSTI Hosted project!" );
19511936
1952- commit .setBranch (glApi .getBranch ());
1953- commit .setCommitMessage ("Adding Hosted Files: " + fileName );
1954- commit .addBase64ActionByValues ("create" , uploadFile , base64Content );
1955- }
1956- else {
1957- // edit project, if one already exists
1958- project = glApi .updateProject (md );
1937+ String projectName = "dc-" + md .getCodeId ();
1938+ String hostedFolder = "hosted_files" ;
19591939
1960- // for each file in the hosted folder, check against submitted files and process as needed
1961- GitLabFile [] files = glApi .fetchTree (hostedFolder );
1940+ String uploadFile = hostedFolder + "/" + fileName ;
19621941
1963- int adds = 0 ;
1964- int updates = 0 ;
1965- int deletes = 0 ;
1942+ GitLabAPI glApi = new GitLabAPI ();
1943+ glApi .setProjectName (projectName );
19661944
1967- if (files != null ) {
1968- for ( GitLabFile f : files ) {
1969- if (!f .getType ().equalsIgnoreCase ("tree" )) {
1970- if (f .getPath ().equals (uploadFile )) {
1971- // update, if filename exists already
1972- commit .addBase64ActionByValues ("update" , uploadFile , base64Content );
1945+ // check existance of project
1946+ Project project = glApi .fetchProject ();
19731947
1974- updates ++;
1975- }
1976- else {
1977- // delete, if file is not being submitted
1978- commit .addBase64ActionByValues ("delete" , f .getPath (), null );
1948+ Commit commit = new Commit ();
1949+ if (project == null ) {
1950+ // create new project, if none exists
1951+ project = glApi .createProject (md );
19791952
1980- deletes ++;
1953+ commit .setBranch (glApi .getBranch ());
1954+ commit .setCommitMessage ("Adding Hosted Files: " + fileName );
1955+ commit .addBase64ActionByValues ("create" , uploadFile , base64Content );
1956+ }
1957+ else {
1958+ // edit project, if one already exists
1959+ project = glApi .updateProject (md );
1960+
1961+ // for each file in the hosted folder, check against submitted files and process as needed
1962+ GitLabFile [] files = glApi .fetchTree (hostedFolder );
1963+
1964+ int adds = 0 ;
1965+ int updates = 0 ;
1966+ int deletes = 0 ;
1967+
1968+ if (files != null ) {
1969+ for ( GitLabFile f : files ) {
1970+ if (!f .getType ().equalsIgnoreCase ("tree" )) {
1971+ if (f .getPath ().equals (uploadFile )) {
1972+ // update, if filename exists already
1973+ commit .addBase64ActionByValues ("update" , uploadFile , base64Content );
1974+
1975+ updates ++;
1976+ }
1977+ else {
1978+ // delete, if file is not being submitted
1979+ commit .addBase64ActionByValues ("delete" , f .getPath (), null );
1980+
1981+ deletes ++;
1982+ }
19811983 }
19821984 }
19831985 }
1984- }
19851986
1986- // right now there is only ever one file, so if we did not updated anything, we need to create it
1987- if (updates == 0 ) {
1988- // create, if there was no matching file to update
1989- commit .addBase64ActionByValues ("create" , uploadFile , base64Content );
1987+ // right now there is only ever one file, so if we did not updated anything, we need to create it
1988+ if (updates == 0 ) {
1989+ // create, if there was no matching file to update
1990+ commit .addBase64ActionByValues ("create" , uploadFile , base64Content );
19901991
1991- adds ++;
1992- }
1992+ adds ++;
1993+ }
19931994
1994- String prefix ;
1995- String detail = "\" " + fileName + "\" " ;
1996- if (adds == 1 && updates == 0 && deletes == 0 ) {
1997- prefix = "Adding" ;
1998- }
1999- else if (adds == 0 && updates == 1 && deletes == 0 ) {
2000- prefix = "Updating" ;
2001- }
2002- else {
2003- prefix = "Modifying" ;
1995+ String prefix ;
1996+ String detail = "\" " + fileName + "\" " ;
1997+ if (adds == 1 && updates == 0 && deletes == 0 ) {
1998+ prefix = "Adding" ;
1999+ }
2000+ else if (adds == 0 && updates == 1 && deletes == 0 ) {
2001+ prefix = "Updating" ;
2002+ }
2003+ else {
2004+ prefix = "Modifying" ;
20042005
2005- String tmp = (adds == 1 ) ? "\" " + fileName + "\" " : String .valueOf (adds );
2006- detail = (adds > 0 ? "Added " + tmp : "" );
2006+ String tmp = (adds == 1 ) ? "\" " + fileName + "\" " : String .valueOf (adds );
2007+ detail = (adds > 0 ? "Added " + tmp : "" );
20072008
2008- tmp = (updates == 1 ) ? "\" " + fileName + "\" " : String .valueOf (updates );
2009- detail += (updates > 0 ? (StringUtils .isBlank (detail ) ? "" : ", " ) + "Updated " + tmp : "" );
2009+ tmp = (updates == 1 ) ? "\" " + fileName + "\" " : String .valueOf (updates );
2010+ detail += (updates > 0 ? (StringUtils .isBlank (detail ) ? "" : ", " ) + "Updated " + tmp : "" );
20102011
2011- detail += (deletes > 0 ? (StringUtils .isBlank (detail ) ? "" : ", " ) + "Deleted " + deletes : "" );
2012- }
2012+ detail += (deletes > 0 ? (StringUtils .isBlank (detail ) ? "" : ", " ) + "Deleted " + deletes : "" );
2013+ }
20132014
2014- commit .setBranch (project .getDefaultBranch ());
2015- commit .setCommitMessage (prefix + " Hosted Files: " + detail );
2016- }
2015+ commit .setBranch (project .getDefaultBranch ());
2016+ commit .setCommitMessage (prefix + " Hosted Files: " + detail );
2017+ }
20172018
2018- // commit file actions, as needed
2019- glApi .commitFiles (commit );
2019+ // commit file actions, as needed
2020+ glApi .commitFiles (commit );
20202021
2021- // override repo link, no matter what
2022- md .setRepositoryLink (project .getWebUrl ());
2022+ // override repo link, no matter what
2023+ md .setRepositoryLink (project .getWebUrl ());
2024+ } catch ( Exception e ) {
2025+ // replace non-obvious 'name' with 'software_title' for user clarity
2026+ String eMsg = e .getMessage ();
2027+ eMsg = eMsg .replaceFirst ("'name'" , "'software_title'" );
2028+ throw new Exception (eMsg );
2029+ }
20232030 }
20242031}
0 commit comments