@@ -74,29 +74,58 @@ const getRepos = async () => {
7474 }
7575 }
7676
77-
78- const getDotNetVersions = async ( repo , keyword ) => {
77+ const getTree = async ( repo ) => {
7978 try {
80- const { data } = await octokit . request ( 'GET https://api.github.com/search/code?q={keyword}+repo:{repo}' , {
81- repo : repo . full_name ,
82- keyword : keyword ,
83- headers : {
84- accept : 'application/vnd.github.text-match+json'
85- }
79+ const { data } = await octokit . request ( 'GET /repos/{owner}/{repo}/git/trees/{default_branch}?recursive=1' , {
80+ owner : repo . full_name . split ( '/' ) [ 0 ] ,
81+ repo : repo . name ,
82+ default_branch : repo . default_branch
8683 } )
87- const matches = data . items . map ( ( i ) => {
88- const split = i . text_matches [ 0 ] . fragment . split ( keyword ) ;
89- const foundValue = split [ 1 ]
84+ return data
85+ } catch ( ex ) {
86+ throw ex
87+ }
88+ }
9089
91- const foundValue2 = foundValue . split ( keyword . substring ( 1 ) ) [ 0 ]
9290
93- const version = foundValue2 . substring ( 0 , foundValue2 . length - 2 ) ;
94- return 'C# ' + version ;
95- } ) ;
96- return matches . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ; //only unique values
91+ const getFileContent = async ( url ) => {
92+ try {
93+ const { data } = await octokit . request ( 'GET {url}' , {
94+ url : url
95+ } )
96+ const base64content = Buffer . from ( data . content , 'base64' )
97+ const fileContent = base64content . toString ( 'utf8' )
98+ return fileContent
9799 } catch ( ex ) {
98100 throw ex
99- }
101+ }
102+ }
103+
104+ function getStringBetween ( str , start , end ) {
105+ const result = str . match ( new RegExp ( start + "(.*)" + end ) ) ;
106+
107+ if ( result === undefined || result === null )
108+ {
109+ return '' ;
110+ }
111+
112+ return result [ 1 ] ;
113+ }
114+
115+ const getDotNetVersions = async ( fileTree ) => {
116+ const csprojBlobUrls = fileTree . tree . filter ( ( n ) => n . path . endsWith ( '.csproj' ) ) . map ( ( n ) => n . url ) ;
117+
118+ const csprojContentsPromises = csprojBlobUrls . map ( ( u ) => getFileContent ( u ) ) ;
119+
120+ const csprojContents = await Promise . all ( csprojContentsPromises )
121+
122+ const targetFrameworks = csprojContents . map ( ( c ) => getStringBetween ( c , '<TargetFramework>' , '</TargetFramework>' ) ) ;
123+ const targetFrameworkVersions = csprojContents . map ( ( c ) => getStringBetween ( c , '<TargetFrameworkVersion>' , '</TargetFrameworkVersion>' ) ) ;
124+ targetFrameworks . push ( ...targetFrameworkVersions ) ;
125+ //unique values + not empty + prefix with C#
126+ const versions = targetFrameworks . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) . filter ( ( v ) => v != '' ) . map ( ( v ) => 'C# ' + v ) ;
127+
128+ return versions ;
100129 }
101130
102131 const enrichTags = async ( serviceDefinition ) => {
@@ -113,14 +142,10 @@ const getRepos = async () => {
113142
114143 if ( languages . includes ( 'C#' ) )
115144 {
116- const targetFrameworkVersions = await getDotNetVersions ( serviceDefinition . _repo , '<TargetFramework>' ) ;
117- serviceDefinition . metadata . tags . push ( ...targetFrameworkVersions ) ;
118-
119- const targetFrameworkVersions2 = await getDotNetVersions ( serviceDefinition . _repo , '<TargetFrameworkVersion>' ) ;
120- serviceDefinition . metadata . tags . push ( ...targetFrameworkVersions2 ) ;
121-
122- //SLEEP NEEDED BECAUSE OF MAX CALLS/MINUTE - https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28#rate-limit
145+ const fileTree = await getTree ( serviceDefinition . _repo ) ;
123146
147+ const versions = await getDotNetVersions ( fileTree ) ;
148+ serviceDefinition . metadata . tags . push ( ...versions ) ;
124149 }
125150
126151 serviceDefinition . metadata . tags = serviceDefinition . metadata . tags . filter ( ( x , i , a ) => a . indexOf ( x ) == i ) ; //only unique values
@@ -157,7 +182,7 @@ const getRepos = async () => {
157182 core . debug ( `✋ Unable to find ${ path } in ${ repo . name } , not processing as 'push_missing' is false` )
158183 }
159184 }
160-
185+
161186 return repoData
162187 }
163188
0 commit comments