@@ -116,6 +116,85 @@ export function readEnterpriseReleases(
116116 return releases ;
117117}
118118
119+ /** Adds `weeks`-many weeks to the UTC date of `date`. */
120+ export function addWeeks ( date : Date , weeks : number ) : Date {
121+ const result = new Date ( date ) ;
122+ result . setUTCDate ( date . getUTCDate ( ) + weeks * 7 ) ;
123+ return result ;
124+ }
125+
126+ /** Determines the current range of GHES versions we should support. */
127+ export function determineSupportedRange (
128+ apiCompatibilityData : ApiCompatibility ,
129+ releases : EnterpriseReleases ,
130+ ) : ApiCompatibility {
131+ // Our goal is to identify the oldest and newest GHES release we should support.
132+ // We begin with `oldestSupportRelease = undefined` so that we determine the
133+ // minimum from scratch and don't stick to `apiCompatibilityData.minimumVersion`
134+ // when it is no longer supported.
135+ // For `newestSupportedRelease`, we assume that `apiCompatibilityData.maximumVersion`
136+ // is guaranteed to not be outdated.
137+ let oldestSupportedRelease : SemVer | undefined ;
138+ let newestSupportedRelease = parseEnterpriseVersion (
139+ apiCompatibilityData . maximumVersion ,
140+ ) ;
141+
142+ if ( newestSupportedRelease === null ) {
143+ throw new Error (
144+ `${ apiCompatibilityData . maximumVersion } is not a valid semantic version.` ,
145+ ) ;
146+ }
147+
148+ const today = new Date ( ) ;
149+
150+ // NOTE: We deliberately omit including any data from `releases` in the error messages below.
151+
152+ for ( const [ releaseVersionString , releaseData ] of Object . entries ( releases ) ) {
153+ const releaseVersion = parseEnterpriseVersion ( releaseVersionString ) ;
154+
155+ if ( releaseVersion === null ) {
156+ throw new Error ( "Invalid enterprise release version." ) ;
157+ }
158+
159+ // Ignore GHES releases older than `FIRST_SUPPORTED_RELEASE`.
160+ if ( semver . compare ( releaseVersion , FIRST_SUPPORTED_RELEASE ) < 0 ) {
161+ continue ;
162+ }
163+
164+ // If the GHES release is newer than the current, newest release we support,
165+ // check whether at least two weeks have passed since the feature freeze date
166+ // so we don't set `newestSupportedRelease` too early.
167+ if ( semver . compare ( releaseVersion , newestSupportedRelease ) > 0 ) {
168+ const featureFreezeDate = new Date ( releaseData . feature_freeze ) ;
169+ if ( featureFreezeDate < addWeeks ( today , 2 ) ) {
170+ newestSupportedRelease = releaseVersion ;
171+ }
172+ }
173+
174+ if (
175+ oldestSupportedRelease === undefined ||
176+ semver . compare ( releaseVersion , oldestSupportedRelease ) < 0
177+ ) {
178+ const endOfLifeDate = new Date ( releaseData . end ) ;
179+ // The GHES version is not actually end of life until the end of the day
180+ // specified by `endOfLifeDate`. Wait an extra week to be safe.
181+ const isEndOfLife = today > addWeeks ( endOfLifeDate , 1 ) ;
182+ if ( ! isEndOfLife ) {
183+ oldestSupportedRelease = releaseVersion ;
184+ }
185+ }
186+ }
187+
188+ if ( ! oldestSupportedRelease ) {
189+ throw new Error ( "Could not determine oldest supported release." ) ;
190+ }
191+
192+ return {
193+ maximumVersion : printEnterpriseVersion ( newestSupportedRelease ) ,
194+ minimumVersion : printEnterpriseVersion ( oldestSupportedRelease ) ,
195+ } ;
196+ }
197+
119198function main ( ) {
120199 const enterpriseReleasesPath = process . env [ EnvVar . ENTERPRISE_RELEASES_PATH ] ;
121200 if ( ! enterpriseReleasesPath ) {
@@ -129,6 +208,30 @@ function main() {
129208
130209 // Get the GHES release information.
131210 const releases = readEnterpriseReleases ( enterpriseReleasesPath ) ;
211+
212+ // Determine the supported range.
213+ const newCompatibilityData : ApiCompatibility = determineSupportedRange (
214+ apiCompatibilityData ,
215+ releases ,
216+ ) ;
217+
218+ // If the version range has changed, write the updates to `API_COMPATIBILITY_FILE`.
219+ if (
220+ newCompatibilityData . minimumVersion !==
221+ apiCompatibilityData . minimumVersion ||
222+ newCompatibilityData . maximumVersion !== apiCompatibilityData . maximumVersion
223+ ) {
224+ const data = JSON . stringify ( newCompatibilityData ) ;
225+ fs . writeFileSync ( API_COMPATIBILITY_FILE , `${ data } \n` ) ;
226+
227+ console . log (
228+ `Updated '${ path . basename ( API_COMPATIBILITY_FILE ) } ': ${ newCompatibilityData . minimumVersion } - ${ newCompatibilityData . maximumVersion } ` ,
229+ ) ;
230+ } else {
231+ console . log (
232+ `No changes, not writing to '${ path . basename ( API_COMPATIBILITY_FILE ) } '.` ,
233+ ) ;
234+ }
132235}
133236
134237// Only call `main` if this script was run directly.
0 commit comments