2023-11-06 03:30:11 -07:00
import JSON5 from 'json5' ;
2022-08-24 13:41:43 -06:00
import {
2023-07-01 06:15:00 -06:00
BITBUCKET_API_USING_HOST_TYPES ,
2024-06-08 00:09:43 -06:00
BITBUCKET_SERVER_API_USING_HOST_TYPES ,
2023-08-31 03:41:39 -06:00
GITEA_API_USING_HOST_TYPES ,
2022-08-24 13:41:43 -06:00
GITHUB_API_USING_HOST_TYPES ,
GITLAB_API_USING_HOST_TYPES ,
} from '../constants' ;
2023-11-06 03:30:11 -07:00
import { logger } from '../logger' ;
2022-08-24 13:41:43 -06:00
import * as hostRules from './host-rules' ;
import { parseUrl } from './url' ;
/ * *
* Tries to detect the ` platform ` from a url .
*
* @param url the url to detect ` platform ` from
* @returns matched ` platform ` if found , otherwise ` null `
* /
2023-06-11 10:58:46 -06:00
export function detectPlatform (
2023-11-07 08:50:29 -07:00
url : string ,
2024-06-03 22:25:25 -06:00
) :
| 'azure'
| 'bitbucket'
| 'bitbucket-server'
| 'gitea'
| 'github'
| 'gitlab'
| null {
2022-08-24 13:41:43 -06:00
const { hostname } = parseUrl ( url ) ? ? { } ;
2023-06-11 10:58:46 -06:00
if ( hostname === 'dev.azure.com' || hostname ? . endsWith ( '.visualstudio.com' ) ) {
return 'azure' ;
}
2024-06-03 22:25:25 -06:00
if ( hostname === 'bitbucket.org' || hostname === 'bitbucket.com' ) {
2023-07-01 05:11:51 -06:00
return 'bitbucket' ;
}
2024-06-03 22:25:25 -06:00
if ( hostname ? . includes ( 'bitbucket' ) ) {
return 'bitbucket-server' ;
}
2023-08-31 03:41:39 -06:00
if (
hostname &&
( [ 'gitea.com' , 'codeberg.org' ] . includes ( hostname ) ||
hostname . includes ( 'gitea' ) ||
hostname . includes ( 'forgejo' ) )
) {
return 'gitea' ;
}
2023-07-01 07:48:30 -06:00
if ( hostname === 'github.com' || hostname ? . includes ( 'github' ) ) {
return 'github' ;
}
if ( hostname === 'gitlab.com' || hostname ? . includes ( 'gitlab' ) ) {
return 'gitlab' ;
}
2022-08-24 13:41:43 -06:00
2022-10-02 13:20:24 -06:00
const hostType = hostRules . hostType ( { url } ) ;
2022-08-24 13:41:43 -06:00
if ( ! hostType ) {
return null ;
}
2024-06-08 00:09:43 -06:00
if ( BITBUCKET_SERVER_API_USING_HOST_TYPES . includes ( hostType ) ) {
return 'bitbucket-server' ;
}
2023-07-01 06:15:00 -06:00
if ( BITBUCKET_API_USING_HOST_TYPES . includes ( hostType ) ) {
return 'bitbucket' ;
}
2023-08-31 03:41:39 -06:00
if ( GITEA_API_USING_HOST_TYPES . includes ( hostType ) ) {
return 'gitea' ;
}
2022-08-24 13:41:43 -06:00
if ( GITHUB_API_USING_HOST_TYPES . includes ( hostType ) ) {
return 'github' ;
}
2023-07-01 07:48:30 -06:00
if ( GITLAB_API_USING_HOST_TYPES . includes ( hostType ) ) {
return 'gitlab' ;
}
2022-08-24 13:41:43 -06:00
return null ;
}
2023-11-06 03:30:11 -07:00
2023-11-07 02:21:04 -07:00
export function noLeadingAtSymbol ( input : string ) : string {
return input . startsWith ( '@' ) ? input . slice ( 1 ) : input ;
}
2023-11-06 03:30:11 -07:00
export function parseJson ( content : string | null , filename : string ) : unknown {
if ( ! content ) {
return null ;
}
return filename . endsWith ( '.json5' )
? JSON5 . parse ( content )
: parseJsonWithFallback ( content , filename ) ;
}
export function parseJsonWithFallback (
content : string ,
2023-11-07 08:50:29 -07:00
context : string ,
2023-11-06 03:30:11 -07:00
) : unknown {
let parsedJson : unknown ;
try {
parsedJson = JSON . parse ( content ) ;
2024-08-14 04:33:02 -06:00
} catch {
2023-11-06 03:30:11 -07:00
parsedJson = JSON5 . parse ( content ) ;
logger . warn (
{ context } ,
2023-11-07 08:50:29 -07:00
'File contents are invalid JSON but parse using JSON5. Support for this will be removed in a future release so please change to a support .json5 file name or ensure correct JSON syntax.' ,
2023-11-06 03:30:11 -07:00
) ;
}
return parsedJson ;
}