Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/azure-pipelines/common/checkDistroCommit.ts
5241 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import path from 'path';
7
import fs from 'fs';
8
import { retry } from './retry.ts';
9
10
const root = path.dirname(path.dirname(path.dirname(import.meta.dirname)));
11
12
function getEnv(name: string): string {
13
const result = process.env[name];
14
15
if (typeof result === 'undefined') {
16
throw new Error('Missing env: ' + name);
17
}
18
19
return result;
20
}
21
22
interface GitHubBranchResponse {
23
commit: {
24
sha: string;
25
};
26
}
27
28
async function getDistroBranchHead(branch: string, token: string): Promise<string> {
29
const url = `https://api.github.com/repos/microsoft/vscode-distro/branches/${encodeURIComponent(branch)}`;
30
31
const response = await fetch(url, {
32
headers: {
33
'Accept': 'application/vnd.github+json',
34
'Authorization': `Bearer ${token}`,
35
'X-GitHub-Api-Version': '2022-11-28',
36
'User-Agent': 'VSCode Build'
37
}
38
});
39
40
if (!response.ok) {
41
throw new Error(`Failed to fetch branch ${branch} from vscode-distro: ${response.status} ${response.statusText}`);
42
}
43
44
const data = await response.json() as GitHubBranchResponse;
45
return data.commit.sha;
46
}
47
48
async function checkDistroCommit(): Promise<void> {
49
// Get the distro commit from package.json
50
const packageJsonPath = path.join(root, 'package.json');
51
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
52
const distroCommit: string = packageJson.distro;
53
54
if (!distroCommit) {
55
console.log('No distro commit found in package.json, skipping check');
56
return;
57
}
58
59
console.log(`Distro commit in package.json: ${distroCommit}`);
60
61
// Get the current branch from Azure DevOps
62
// BUILD_SOURCEBRANCH is in format refs/heads/main or refs/heads/release/1.90
63
const sourceBranch = getEnv('BUILD_SOURCEBRANCH');
64
const branchMatch = sourceBranch.match(/^refs\/heads\/(.+)$/);
65
66
if (!branchMatch) {
67
console.log(`Cannot determine branch from BUILD_SOURCEBRANCH: ${sourceBranch}, skipping check`);
68
return;
69
}
70
71
const branch = branchMatch[1];
72
console.log(`Current branch: ${branch}`);
73
74
// Get the GitHub token
75
const token = getEnv('GITHUB_TOKEN');
76
77
// Fetch the HEAD of the matching branch in vscode-distro
78
let distroBranchHead: string;
79
try {
80
distroBranchHead = await retry(() => getDistroBranchHead(branch, token));
81
} catch (error) {
82
// If the branch doesn't exist in distro, that's expected for feature branches
83
console.log(`Could not fetch branch '${branch}' from vscode-distro: ${error}`);
84
console.log('This is expected for feature branches that have not been merged to distro');
85
return;
86
}
87
88
console.log(`Distro branch '${branch}' HEAD: ${distroBranchHead}`);
89
90
// Compare the commits
91
if (distroCommit === distroBranchHead) {
92
console.log(`✓ Distro commit matches branch HEAD`);
93
} else {
94
// Issue a warning using Azure DevOps logging commands
95
console.log(`##vso[task.logissue type=warning]Distro commit mismatch: package.json has ${distroCommit.substring(0, 8)} but ${branch} HEAD is ${distroBranchHead.substring(0, 8)}`);
96
console.log(`##vso[task.complete result=SucceededWithIssues;]Distro commit does not match branch HEAD`);
97
console.log('');
98
console.log(`⚠️ WARNING: Distro commit in package.json does not match the HEAD of branch '${branch}' in vscode-distro`);
99
console.log(` package.json distro: ${distroCommit}`);
100
console.log(` ${branch} HEAD: ${distroBranchHead}`);
101
console.log('');
102
console.log(' To update, run: npm run update-distro');
103
}
104
}
105
106
checkDistroCommit().then(() => {
107
console.log('Distro commit check completed');
108
process.exit(0);
109
}, err => {
110
console.error(err);
111
process.exit(1);
112
});
113
114