@@ -17,7 +17,7 @@ const CODEFAIR_DOMAIN = process.env.CODEFAIR_APP_DOMAIN;
1717 * @returns {boolean } - Returns true if a license is found in the repository, false otherwise
1818 */
1919export async function checkForLicense ( context , owner , repoName ) {
20- const readmeFilesTypes = [
20+ const licenseFilesTypes = [
2121 "LICENSE.md" ,
2222 "LICENSE.txt" ,
2323 "LICENSE" ,
@@ -29,23 +29,38 @@ export async function checkForLicense(context, owner, repoName) {
2929 ".github/LICENSE" ,
3030 ] ;
3131
32- for ( const filePath of readmeFilesTypes ) {
33- const readme = await checkForFile ( context , owner , repoName , filePath ) ;
34- if ( readme ) {
35- const content = await context . octokit . repos . getContent ( {
32+ for ( const filePath of licenseFilesTypes ) {
33+ const file = await checkForFile ( context , owner , repoName , filePath ) ;
34+ if ( file ) {
35+ // Get the actual file content from the repository
36+ const fileContent = await context . octokit . rest . repos . getContent ( {
3637 owner,
3738 repo : repoName ,
3839 path : filePath ,
3940 } ) ;
40- const contentData = Buffer . from ( content . data . content , "base64" ) . toString (
41- "utf-8"
42- ) ;
41+
42+ const contentData = Buffer . from (
43+ fileContent . data . content ,
44+ "base64"
45+ ) . toString ( "utf-8" ) ;
46+
47+ // Try to get the detected license information for the repository
48+ let spdxId = null ;
49+ try {
50+ const repoLicense = await context . octokit . rest . licenses . getForRepo ( {
51+ owner,
52+ repo : repoName ,
53+ } ) ;
54+ spdxId = repoLicense . data ?. license ?. spdx_id || null ;
55+ } catch ( error ) {
56+ logwatch . warn ( `Could not detect license SPDX ID: ${ error . message } ` ) ;
57+ }
4358
4459 return {
4560 status : true ,
4661 path : filePath ,
4762 content : contentData ,
48- spdx_id : content . data ?. license ?. spdx_id || null ,
63+ spdx_id : spdxId ,
4964 } ;
5065 }
5166 }
0 commit comments