@@ -17,6 +17,7 @@ SPDX-License-Identifier: Apache-2.0
1717Copyright (c) OWASP Foundation. All Rights Reserved.
1818*/
1919
20+ import { execSync } from 'child_process'
2021import { existsSync , readFileSync } from 'fs'
2122import { dirname , extname , isAbsolute , join , parse , sep } from 'path'
2223
@@ -126,7 +127,84 @@ export function getMimeForLicenseFile (filename: string): MimeType | undefined {
126127 const { name, ext } = parse ( filename . toLowerCase ( ) )
127128 return LICENSE_FILENAME_BASE . has ( name ) && LICENSE_FILENAME_EXT . has ( ext )
128129 ? MIME_TEXT_PLAIN
129- : MAP_TEXT_EXTENSION_MIME [ ext ]
130+ : MAP_TEXT_EXTENSION_MIME [ ext ] ?? undefined
130131}
131132
132133// endregion MIME
134+
135+ export function detectBuildUrl ( ) : string | undefined {
136+ if ( process . env . GITHUB_ACTIONS === 'true' ) {
137+ return `${ process . env . GITHUB_SERVER_URL } /${ process . env . GITHUB_REPOSITORY } /actions/runs/${ process . env . GITHUB_RUN_ID } `
138+ }
139+ if ( process . env . GITLAB_CI === 'true' && isNonNullable ( process . env . CI_JOB_URL ) ) {
140+ return process . env . CI_JOB_URL
141+ }
142+ if ( isNonNullable ( process . env . CIRCLECI ) ) {
143+ return process . env . CIRCLE_BUILD_URL
144+ }
145+ if ( isNonNullable ( process . env . JENKINS_URL ) ) {
146+ return process . env . BUILD_URL
147+ }
148+ if ( isNonNullable ( process . env . TF_BUILD ) ) {
149+ return `${ process . env . SYSTEM_TEAMFOUNDATIONCOLLECTIONURI } ${ process . env . SYSTEM_TEAMPROJECT } /_build/results?buildId=${ process . env . BUILD_BUILDID } `
150+ }
151+ if ( isNonNullable ( process . env . TRAVIS ) ) {
152+ return process . env . TRAVIS_BUILD_WEB_URL
153+ }
154+ if ( isNonNullable ( process . env . BITBUCKET_BUILD_NUMBER ) ) {
155+ return process . env . BITBUCKET_GIT_HTTP_ORIGIN
156+ }
157+ if ( isNonNullable ( process . env . CODEBUILD_PUBLIC_BUILD_URL ) ) {
158+ return process . env . CODEBUILD_PUBLIC_BUILD_URL
159+ }
160+ if ( isNonNullable ( process . env . DRONE_BUILD_LINK ) ) {
161+ return process . env . DRONE_BUILD_LINK
162+ }
163+ return undefined
164+ }
165+
166+ export function detectSourceUrl ( ) : string | undefined {
167+ try {
168+ const hasGit = execSync ( 'which git' , { stdio : 'ignore' } )
169+ if ( hasGit !== null && hasGit . length > 0 ) {
170+ const gitUrl = execSync ( 'git remote get-url origin 2>/dev/null' , { encoding : 'utf8' } ) . trim ( )
171+ if ( gitUrl !== null && gitUrl !== '' ) {
172+ if ( gitUrl . startsWith ( 'git@' ) && gitUrl . endsWith ( '.git' ) ) {
173+ return gitUrl . replace ( ':' , '/' ) . replace ( 'git@' , 'https://' )
174+ }
175+ return gitUrl
176+ }
177+ }
178+ } catch ( error ) {
179+ // Fall through to environment checks if git commands fail
180+ }
181+
182+ if ( isNonNullable ( process . env . GITHUB_REPOSITORY ) ) {
183+ return `${ process . env . GITHUB_SERVER_URL } /${ process . env . GITHUB_REPOSITORY } `
184+ }
185+ if ( isNonNullable ( process . env . GITLAB_CI ) ) {
186+ return process . env . CI_REPOSITORY_URL
187+ }
188+ if ( isNonNullable ( process . env . CIRCLECI ) ) {
189+ return process . env . CIRCLE_REPOSITORY_URL
190+ }
191+ if ( isNonNullable ( process . env . JENKINS_URL ) ) {
192+ return process . env . GIT_URL
193+ }
194+ if ( isNonNullable ( process . env . TF_BUILD ) ) {
195+ return process . env . BUILD_REPOSITORY_URI
196+ }
197+ if ( isNonNullable ( process . env . BITBUCKET_GIT_HTTP_ORIGIN ) ) {
198+ return process . env . BITBUCKET_GIT_HTTP_ORIGIN
199+ }
200+ if ( isNonNullable ( process . env . BITBUCKET_GIT_SSH_ORIGIN ) ) {
201+ return process . env . BITBUCKET_GIT_SSH_ORIGIN
202+ }
203+ if ( isNonNullable ( process . env . CODEBUILD_BUILD_ID ) ) {
204+ return process . env . CODEBUILD_SOURCE_REPO_URL
205+ }
206+ if ( isNonNullable ( process . env . DRONE_REPO_LINK ) ) {
207+ return process . env . DRONE_REPO_LINK
208+ }
209+ return undefined
210+ }
0 commit comments