1- import { logger } from '@redocly/openapi-core ' ;
1+ import { exitWithError } from 'cli/src/utils/error.js ' ;
22
3- import type { RemoteScorecardAndPlugins , Organization , Project , PaginatedList } from '../types.js' ;
3+ import type { RemoteScorecardAndPlugins , Project } from '../types.js' ;
44
55export async function fetchRemoteScorecardAndPlugins (
66 projectUrl : string ,
7- accessToken : string
7+ auth : string
88) : Promise < RemoteScorecardAndPlugins | undefined > {
99 const parsedProjectUrl = parseProjectUrl ( projectUrl ) ;
1010
1111 if ( ! parsedProjectUrl ) {
12- logger . warn ( `Invalid project URL format: ${ projectUrl } ` ) ;
13- return ;
12+ exitWithError ( `Invalid project URL format: ${ projectUrl } ` ) ;
1413 }
1514
1615 const { residency, orgSlug, projectSlug } = parsedProjectUrl ;
17-
18- const organization = await fetchOrganizationBySlug ( residency , orgSlug , accessToken ) ;
19-
20- if ( ! organization ) {
21- logger . warn ( `Organization not found: ${ orgSlug } ` ) ;
22- return ;
23- }
24-
25- const project = await fetchProjectBySlug ( residency , organization . id , projectSlug , accessToken ) ;
26-
27- if ( ! project ) {
28- logger . warn ( `Project not found: ${ projectSlug } ` ) ;
29- return ;
30- }
31-
32- const scorecard = project ?. config . scorecard ;
33-
34- if ( ! scorecard ) {
35- logger . warn ( 'No scorecard configuration found in the remote project.' ) ;
36- return ;
16+ const apiKey = process . env . REDOCLY_AUTHORIZATION ;
17+
18+ try {
19+ const project = await fetchProjectConfigBySlugs ( residency , orgSlug , projectSlug , apiKey , auth ) ;
20+ const scorecard = project ?. config . scorecard ;
21+
22+ if ( ! scorecard ) {
23+ throw new Error ( 'No scorecard configuration found.' ) ;
24+ }
25+
26+ const plugins = project . config . pluginsUrl
27+ ? await fetchPlugins ( project . config . pluginsUrl )
28+ : undefined ;
29+
30+ return {
31+ scorecard,
32+ plugins ,
33+ } ;
34+ } catch ( error ) {
35+ exitWithError ( error . message ) ;
3736 }
38-
39- const plugins = project . config . pluginsUrl
40- ? await fetchPlugins ( project . config . pluginsUrl )
41- : undefined ;
42-
43- return {
44- scorecard,
45- plugins,
46- } ;
4737}
4838
4939function parseProjectUrl (
@@ -65,47 +55,29 @@ function parseProjectUrl(
6555 } ;
6656}
6757
68- async function fetchOrganizationBySlug (
58+ async function fetchProjectConfigBySlugs (
6959 residency : string ,
7060 orgSlug : string ,
71- accessToken : string
72- ) : Promise < Organization | undefined > {
73- const orgsUrl = new URL ( `${ residency } /api/orgs` ) ;
74- orgsUrl . searchParams . set ( 'filter' , `slug:${ orgSlug } ` ) ;
75- orgsUrl . searchParams . set ( 'limit' , '1' ) ;
76-
77- const authHeaders = createAuthHeaders ( accessToken ) ;
78- const organizationResponse = await fetch ( orgsUrl , { headers : authHeaders } ) ;
79-
80- if ( organizationResponse . status !== 200 ) {
81- return ;
82- }
83-
84- const organizations : PaginatedList < Organization > = await organizationResponse . json ( ) ;
85-
86- return organizations . items [ 0 ] ;
87- }
88-
89- async function fetchProjectBySlug (
90- residency : string ,
91- orgId : string ,
9261 projectSlug : string ,
62+ apiKey : string | undefined ,
9363 accessToken : string
9464) : Promise < Project | undefined > {
95- const projectsUrl = new URL ( `${ residency } /api/orgs/${ orgId } /projects` ) ;
96- projectsUrl . searchParams . set ( 'filter' , `slug:${ projectSlug } ` ) ;
97- projectsUrl . searchParams . set ( 'limit' , '1' ) ;
65+ const authHeaders = createAuthHeaders ( apiKey , accessToken ) ;
66+ const projectUrl = new URL ( `${ residency } /api/orgs/${ orgSlug } /projects/${ projectSlug } ` ) ;
9867
99- const authHeaders = createAuthHeaders ( accessToken ) ;
100- const projectsResponse = await fetch ( projectsUrl , { headers : authHeaders } ) ;
68+ const projectResponse = await fetch ( projectUrl , { headers : authHeaders } ) ;
10169
102- if ( projectsResponse . status !== 200 ) {
103- return ;
70+ if ( projectResponse . status === 401 || projectResponse . status === 403 ) {
71+ throw new Error (
72+ `Unauthorized access to project: ${ projectSlug } . Please check your credentials.`
73+ ) ;
10474 }
10575
106- const projects : PaginatedList < Project > = await projectsResponse . json ( ) ;
76+ if ( projectResponse . status !== 200 ) {
77+ throw new Error ( `Failed to fetch project: ${ projectSlug } . Status: ${ projectResponse . status } ` ) ;
78+ }
10779
108- return projects . items [ 0 ] ;
80+ return projectResponse . json ( ) ;
10981}
11082
11183async function fetchPlugins ( pluginsUrl : string ) : Promise < string | undefined > {
@@ -118,6 +90,13 @@ async function fetchPlugins(pluginsUrl: string): Promise<string | undefined> {
11890 return pluginsResponse . text ( ) ;
11991}
12092
121- function createAuthHeaders ( accessToken : string ) {
93+ function createAuthHeaders (
94+ apiKey : string | undefined ,
95+ accessToken : string
96+ ) : Record < string , string > {
97+ if ( apiKey ) {
98+ return { Authorization : `Bearer ${ apiKey } ` } ;
99+ }
100+
122101 return { Cookie : `accessToken=${ accessToken } ` } ;
123102}
0 commit comments