11import { ActionState } from "../action-common" ;
2- import { Env , ActionsEnvVars } from "../environment" ;
2+ import { ActionsEnvVars , ReadOnlyEnv } from "../environment" ;
33import * as errorMessages from "../error-messages" ;
44import { Feature } from "../feature-flags" ;
55import { ConfigurationError , Failure , Result , Success } from "../util" ;
@@ -23,7 +23,7 @@ export const DEFAULT_CONFIG_FILE_NAME = ".github/codeql-action.yaml";
2323export const DEFAULT_CONFIG_FILE_REF = "main" ;
2424
2525/** Extracts the owner from the `GITHUB_REPOSITORY` environment variable. */
26- function getDefaultOwner ( env : Env ) : string {
26+ function getDefaultOwner ( env : ReadOnlyEnv ) : string {
2727 const currentRepoNwo = env . getRequired ( ActionsEnvVars . GITHUB_REPOSITORY ) ;
2828 const nwoParts = currentRepoNwo . split ( "/" ) ;
2929
@@ -70,6 +70,42 @@ function parseOldRemoteFileAddress(
7070 } ) ;
7171}
7272
73+ /**
74+ * Attempts to parse `input` as a `RemoteFileAddress` using the new format.
75+ *
76+ * @param env The read-only environment to obtain the owner name from if needed.
77+ * @param configFile The input to try and parse.
78+ * @returns A `RemoteFileAddress` value if successful or `undefined` otherwise.
79+ */
80+ export function parseNewRemoteFileAddress (
81+ env : ReadOnlyEnv ,
82+ configFile : string ,
83+ ) : Result < RemoteFileAddress , undefined > {
84+ // retrieve the various parts of the config location, and ensure they're present
85+ const format = new RegExp (
86+ "^((?<owner>[^:@/]+)/)?(?<repo>[^:@/]+)(@(?<ref>[^:]+))?(:(?<path>.+))?$" ,
87+ ) ;
88+ const pieces = format . exec ( configFile . trim ( ) ) ;
89+
90+ const repo : string | undefined = pieces ?. groups ?. repo ?. trim ( ) ;
91+
92+ // Check that the regular expression matched and that we have at least the repo name.
93+ if ( ! pieces ?. groups || ! repo || repo . length === 0 ) {
94+ return new Failure ( undefined ) ;
95+ }
96+
97+ const owner : string | undefined = pieces . groups . owner ?. trim ( ) ;
98+ const path : string | undefined = pieces . groups . path ?. trim ( ) ;
99+ const ref : string | undefined = pieces . groups . ref ?. trim ( ) ;
100+
101+ return new Success ( {
102+ owner : owner || getDefaultOwner ( env ) ,
103+ repo,
104+ path : path || DEFAULT_CONFIG_FILE_NAME ,
105+ ref : ref || DEFAULT_CONFIG_FILE_REF ,
106+ } ) ;
107+ }
108+
73109/**
74110 * Attempts to parse `configFile` into an array of `RemoteFileAddress` components.
75111 *
@@ -101,15 +137,12 @@ export async function parseRemoteFileAddress(
101137 }
102138
103139 // retrieve the various parts of the config location, and ensure they're present
104- const format = new RegExp (
105- "^((?<owner>[^:@/]+)/)?(?<repo>[^:@/]+)(@(?<ref>[^:]+))?(:(?<path>.+))?$" ,
140+ const newFormatAddressResult = parseNewRemoteFileAddress (
141+ actionState . env ,
142+ configFile ,
106143 ) ;
107- const pieces = format . exec ( configFile . trim ( ) ) ;
108144
109- const repo : string | undefined = pieces ?. groups ?. repo ?. trim ( ) ;
110-
111- // Check that the regular expression matched and that we have at least the repo name.
112- if ( ! pieces ?. groups || ! repo || repo . length === 0 ) {
145+ if ( newFormatAddressResult . isFailure ( ) ) {
113146 // Neither the old format nor the new format worked. Throw an error that
114147 // explains the format we accept. We only mention the new format, since that's
115148 // what we want to be used going forward.
@@ -118,21 +151,14 @@ export async function parseRemoteFileAddress(
118151 ) ;
119152 }
120153
121- const owner : string | undefined = pieces . groups . owner ?. trim ( ) ;
122- const path : string | undefined = pieces . groups . path ?. trim ( ) ;
123- const ref : string | undefined = pieces . groups . ref ?. trim ( ) ;
154+ const address = newFormatAddressResult . value ;
124155
125156 // Ensure that the path is a relative path.
126- if ( path ? .startsWith ( "/" ) ) {
157+ if ( address . path . startsWith ( "/" ) ) {
127158 throw new ConfigurationError (
128159 `The path component of '${ configFile } ' cannot be an absolute path.` ,
129160 ) ;
130161 }
131162
132- return {
133- owner : owner || getDefaultOwner ( actionState . env ) ,
134- repo,
135- path : path || DEFAULT_CONFIG_FILE_NAME ,
136- ref : ref || DEFAULT_CONFIG_FILE_REF ,
137- } ;
163+ return address ;
138164}
0 commit comments