Path: blob/main/build/azure-pipelines/common/checkDistroCommit.ts
5241 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import path from 'path';6import fs from 'fs';7import { retry } from './retry.ts';89const root = path.dirname(path.dirname(path.dirname(import.meta.dirname)));1011function getEnv(name: string): string {12const result = process.env[name];1314if (typeof result === 'undefined') {15throw new Error('Missing env: ' + name);16}1718return result;19}2021interface GitHubBranchResponse {22commit: {23sha: string;24};25}2627async function getDistroBranchHead(branch: string, token: string): Promise<string> {28const url = `https://api.github.com/repos/microsoft/vscode-distro/branches/${encodeURIComponent(branch)}`;2930const response = await fetch(url, {31headers: {32'Accept': 'application/vnd.github+json',33'Authorization': `Bearer ${token}`,34'X-GitHub-Api-Version': '2022-11-28',35'User-Agent': 'VSCode Build'36}37});3839if (!response.ok) {40throw new Error(`Failed to fetch branch ${branch} from vscode-distro: ${response.status} ${response.statusText}`);41}4243const data = await response.json() as GitHubBranchResponse;44return data.commit.sha;45}4647async function checkDistroCommit(): Promise<void> {48// Get the distro commit from package.json49const packageJsonPath = path.join(root, 'package.json');50const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));51const distroCommit: string = packageJson.distro;5253if (!distroCommit) {54console.log('No distro commit found in package.json, skipping check');55return;56}5758console.log(`Distro commit in package.json: ${distroCommit}`);5960// Get the current branch from Azure DevOps61// BUILD_SOURCEBRANCH is in format refs/heads/main or refs/heads/release/1.9062const sourceBranch = getEnv('BUILD_SOURCEBRANCH');63const branchMatch = sourceBranch.match(/^refs\/heads\/(.+)$/);6465if (!branchMatch) {66console.log(`Cannot determine branch from BUILD_SOURCEBRANCH: ${sourceBranch}, skipping check`);67return;68}6970const branch = branchMatch[1];71console.log(`Current branch: ${branch}`);7273// Get the GitHub token74const token = getEnv('GITHUB_TOKEN');7576// Fetch the HEAD of the matching branch in vscode-distro77let distroBranchHead: string;78try {79distroBranchHead = await retry(() => getDistroBranchHead(branch, token));80} catch (error) {81// If the branch doesn't exist in distro, that's expected for feature branches82console.log(`Could not fetch branch '${branch}' from vscode-distro: ${error}`);83console.log('This is expected for feature branches that have not been merged to distro');84return;85}8687console.log(`Distro branch '${branch}' HEAD: ${distroBranchHead}`);8889// Compare the commits90if (distroCommit === distroBranchHead) {91console.log(`✓ Distro commit matches branch HEAD`);92} else {93// Issue a warning using Azure DevOps logging commands94console.log(`##vso[task.logissue type=warning]Distro commit mismatch: package.json has ${distroCommit.substring(0, 8)} but ${branch} HEAD is ${distroBranchHead.substring(0, 8)}`);95console.log(`##vso[task.complete result=SucceededWithIssues;]Distro commit does not match branch HEAD`);96console.log('');97console.log(`⚠️ WARNING: Distro commit in package.json does not match the HEAD of branch '${branch}' in vscode-distro`);98console.log(` package.json distro: ${distroCommit}`);99console.log(` ${branch} HEAD: ${distroBranchHead}`);100console.log('');101console.log(' To update, run: npm run update-distro');102}103}104105checkDistroCommit().then(() => {106console.log('Distro commit check completed');107process.exit(0);108}, err => {109console.error(err);110process.exit(1);111});112113114