Content-Length: 799822 | pFad | http://github.com/angular/angular/commit/a4512ae8ca1e9d146b022f623e3a3c83c0789e2d

82 ci: add a script to copy cdk api files to adev (#61081) · angular/angular@a4512ae · GitHub
Skip to content

Commit a4512ae

Browse files
mmalerbaAndrewKushnir
authored andcommitted
ci: add a script to copy cdk api files to adev (#61081)
Refactors the update-cli-help script into a generic script to copy json assets, and uses the shared code to also copy the CDK apis PR Close #61081
1 parent 6c430d7 commit a4512ae

File tree

10 files changed

+271
-147
lines changed

10 files changed

+271
-147
lines changed

.github/workflows/update-cdk-apis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Update ADEV Angular CDK apis
2+
3+
on:
4+
workflow_dispatch:
5+
inputs: {}
6+
push:
7+
branches:
8+
- 'main'
9+
- '[0-9]+.[0-9]+.x'
10+
11+
# Declare default permissions as read only.
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
update_cli_help:
17+
name: Update Angular CDK apis (if necessary)
18+
if: github.repository == 'angular/angular'
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout the repository
22+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
23+
with:
24+
# Setting `persist-credentials: false` prevents the github-action account from being the
25+
# account that is attempted to be used for authentication, instead the remote is set to
26+
# an authenticated URL.
27+
persist-credentials: false
28+
# This is needed as otherwise the PR creation will fail with `shallow update not allowed` when the forked branch is not in sync.
29+
fetch-depth: 0
30+
- name: Generate CDK apis
31+
run: node adev/scripts/update-cdk-apis/index.mjs
32+
env:
33+
ANGULAR_CDK_BUILDS_READONLY_GITHUB_TOKEN: ${{ secrets.ANGULAR_CDK_BUILDS_READONLY_GITHUB_TOKEN }}
34+
- name: Create a PR (if necessary)
35+
uses: angular/dev-infra/github-actions/create-pr-for-changes@14c3d6bd2fa5c3231be7bd4b3c0bba68c9d79e94
36+
with:
37+
branch-prefix: update-cdk-apis
38+
pr-title: 'docs: update Angular CDK apis [${{github.ref_name}}]'
39+
pr-description: |
40+
Updated Angular CDK api files.
41+
pr-labels: |
42+
action: review
43+
area: docs
44+
angular-robot-token: ${{ secrets.ANGULAR_ROBOT_ACCESS_TOKEN }}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
//tslint:disable:no-console
10+
import {execSync} from 'node:child_process';
11+
import {existsSync, constants as fsConstants} from 'node:fs';
12+
import {copyFile, mkdtemp, readdir, readFile, realpath, unlink, writeFile} from 'node:fs/promises';
13+
import {tmpdir} from 'node:os';
14+
import {join} from 'node:path';
15+
16+
export async function copyJsonAssets({repo, githubApi, assetsPath, destPath}) {
17+
const buildInfoPath = join(destPath, '_build-info.json');
18+
if (!existsSync(buildInfoPath)) {
19+
throw new Error(`${buildInfoPath} does not exist.`);
20+
}
21+
22+
const branch = process.env.GITHUB_REF;
23+
const {sha: currentSha} = JSON.parse(await readFile(buildInfoPath, 'utf-8'));
24+
const latestSha = await githubApi.getShaForBranch(branch);
25+
26+
console.log(`Comparing ${currentSha}...${latestSha}.`);
27+
const affectedFiles = await githubApi.getAffectedFiles(currentSha, latestSha);
28+
const changedFiles = affectedFiles.filter((file) => file.startsWith(`${assetsPath}/`));
29+
30+
if (changedFiles.length === 0) {
31+
console.log(`No '${assetsPath}/**' files changed between ${currentSha} and ${latestSha}.`);
32+
return;
33+
}
34+
35+
console.log(
36+
`The below files changed between ${currentSha} and ${latestSha}:\n` +
37+
changedFiles.map((f) => '* ' + f).join('\n'),
38+
);
39+
40+
const temporaryDir = await realpath(await mkdtemp(join(tmpdir(), 'copy-json-assets-')));
41+
const execOptions = {cwd: temporaryDir, stdio: 'inherit'};
42+
execSync('git init', execOptions);
43+
execSync(`git remote add origen https://github.com/${repo}.git`, execOptions);
44+
// fetch a commit
45+
execSync(`git fetch origen ${latestSha}`, execOptions);
46+
// reset this repository's main branch to the commit of interest
47+
execSync('git reset --hard FETCH_HEAD', execOptions);
48+
// get sha when files where changed
49+
const shaWhenFilesChanged = execSync(`git rev-list -1 ${latestSha} "${assetsPath}/"`, {
50+
encoding: 'utf8',
51+
cwd: temporaryDir,
52+
stdio: ['ignore', 'pipe', 'ignore'],
53+
}).trim();
54+
55+
// Delete existing asset files.
56+
const apiFilesUnlink = (await readdir(destPath))
57+
.filter((f) => f.endsWith('.json'))
58+
.map((f) => unlink(join(destPath, f)));
59+
60+
await Promise.allSettled(apiFilesUnlink);
61+
62+
// Copy new asset files
63+
const tempAssetsDir = join(temporaryDir, assetsPath);
64+
const assetFilesCopy = (await readdir(tempAssetsDir)).map((f) => {
65+
const src = join(tempAssetsDir, f);
66+
const dest = join(destPath, f);
67+
68+
return copyFile(src, dest, fsConstants.COPYFILE_FICLONE);
69+
});
70+
71+
await Promise.allSettled(assetFilesCopy);
72+
73+
// Write SHA to file.
74+
await writeFile(
75+
buildInfoPath,
76+
JSON.stringify(
77+
{
78+
branchName: branch,
79+
sha: shaWhenFilesChanged,
80+
},
81+
undefined,
82+
2,
83+
),
84+
);
85+
86+
console.log('\nChanges: ');
87+
execSync(`git status --porcelain`, {stdio: 'inherit'});
88+
89+
console.log(`Successfully updated asset files in '${destPath}'.\n`);
90+
}

adev/scripts/shared/github-client.mjs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {get} from 'node:https';
10+
import {posix} from 'node:path';
11+
12+
const GITHUB_API = 'https://api.github.com/repos/';
13+
14+
export class GithubClient {
15+
#token;
16+
#ua;
17+
#api;
18+
19+
constructor(repo, token, ua) {
20+
this.#token = token;
21+
this.#ua = ua;
22+
this.#api = posix.join(GITHUB_API, repo);
23+
}
24+
25+
/**
26+
* Get the affected files.
27+
*
28+
* @param {string} baseSha
29+
* @param {string} headSha
30+
* @returns Promise<string[]>
31+
*/
32+
async getAffectedFiles(baseSha, headSha) {
33+
const {files} = JSON.parse(await this.#httpGet(`${this.#api}/compare/${baseSha}...${headSha}`));
34+
return files.map((f) => f.filename);
35+
}
36+
37+
/**
38+
* Get SHA of a branch.
39+
*
40+
* @param {string} branch
41+
* @returns Promise<string>
42+
*/
43+
async getShaForBranch(branch) {
44+
const sha = await this.#httpGet(`${this.#api}/commits/${branch}`, {
45+
headers: {Accept: 'application/vnd.github.VERSION.sha'},
46+
});
47+
48+
if (!sha) {
49+
throw new Error(`Unable to extract the SHA for '${branch}'.`);
50+
}
51+
52+
return sha;
53+
}
54+
55+
#httpGet(url, options = {}) {
56+
options.headers ??= {};
57+
options.headers['Authorization'] = `token ${this.#token}`;
58+
// User agent is required
59+
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#user-agent-required
60+
options.headers['User-Agent'] = this.#ua;
61+
62+
return new Promise((resolve, reject) => {
63+
get(url, options, (res) => {
64+
let data = '';
65+
res
66+
.on('data', (chunk) => {
67+
data += chunk;
68+
})
69+
.on('end', () => {
70+
resolve(data);
71+
});
72+
}).on('error', (e) => {
73+
reject(e);
74+
});
75+
});
76+
}
77+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Generating data for `angular.dev/api` CDK packages
2+
3+
This script updates the Angular CDK api JSON files stored in `adev/src/content/cdk`. This files are used to generate the [angular.dev api](https://angular.dev/api) pages for the CDK packages.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {dirname, resolve as resolvePath} from 'node:path';
10+
import {fileURLToPath} from 'node:url';
11+
import {copyJsonAssets} from '../shared/copy-json-assets.mjs';
12+
import {GithubClient} from '../shared/github-client.mjs';
13+
14+
const scriptDir = dirname(fileURLToPath(import.meta.url));
15+
16+
const CDK_BUILDS_REPO = 'angular/cdk-builds';
17+
const CDK_APIS_CONTENT_PATH = resolvePath(scriptDir, '../../src/content/cdk');
18+
19+
async function main() {
20+
await copyJsonAssets({
21+
repo: CDK_BUILDS_REPO,
22+
assetsPath: '_adev_assets',
23+
destPath: CDK_APIS_CONTENT_PATH,
24+
githubApi: new GithubClient(
25+
CDK_BUILDS_REPO,
26+
process.env.ANGULAR_CDK_BUILDS_READONLY_GITHUB_TOKEN,
27+
'ADEV_Angular_CDK_Sources_Update',
28+
),
29+
});
30+
}
31+
32+
main().catch((err) => {
33+
console.error(err);
34+
process.exit(1);
35+
});

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/commit/a4512ae8ca1e9d146b022f623e3a3c83c0789e2d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy