Fix extra metas retrieval for push context. (#592)

This commit is contained in:
Nerivec
2024-11-02 19:55:09 +01:00
committed by GitHub
parent 0750b40e05
commit b41af97198
5 changed files with 246 additions and 6 deletions

View File

@@ -11,7 +11,7 @@ export async function getChangedOtaFiles(
core: typeof CoreApi,
context: Context,
basehead: string,
isPR: boolean,
throwIfFilesOutsideOfImages: boolean,
): Promise<string[]> {
// NOTE: includes up to 300 files, per https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits
const compare = await github.rest.repos.compareCommitsWithBasehead({
@@ -26,8 +26,12 @@ export async function getChangedOtaFiles(
const fileList = compare.data.files.filter((f) => f.filename.startsWith(`${BASE_IMAGES_DIR}/`));
if (isPR && fileList.length !== compare.data.files.length) {
throw new Error(`Detected changes in files outside of \`images\` directory. This is not allowed for a pull request with OTA files.`);
if (throwIfFilesOutsideOfImages && fileList.length !== compare.data.files.length) {
if (context.payload.pull_request) {
throw new Error(`Detected changes in files outside of \`images\` directory. This is not allowed for a pull request with OTA files.`);
} else {
throw new Error(`Cannot run with files outside of \`images\` directory.`);
}
}
return fileList.map((f) => f.filename);

View File

@@ -4,6 +4,7 @@ import type {Octokit} from '@octokit/rest';
import type {ExtraMetas, GHExtraMetas, RepoImageMeta} from './types.js';
import assert from 'assert';
import {readFileSync, renameSync} from 'fs';
import path from 'path';
@@ -37,12 +38,40 @@ function getFileExtraMetas(extraMetas: GHExtraMetas, fileName: string): ExtraMet
return extraMetas;
}
async function getPRBody(github: Octokit, core: typeof CoreApi, context: Context): Promise<string | undefined> {
assert(context.payload.pull_request || context.eventName === 'push');
if (context.payload.pull_request) {
return context.payload.pull_request.body;
} else if (context.eventName === 'push') {
const pushMsg = context.payload.head_commit.message as string;
const prMatch = pushMsg.match(/\(#(\d+)\)/);
if (prMatch) {
const prNumber = parseInt(prMatch[1], 10);
try {
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
return pr.data.body || undefined;
} catch (error) {
throw new Error(`Failed to get PR#${prNumber} for extra metas: ${error}`);
}
}
}
}
async function parsePRBodyExtraMetas(github: Octokit, core: typeof CoreApi, context: Context): Promise<GHExtraMetas> {
let extraMetas: GHExtraMetas = {};
if (context.payload.pull_request?.body) {
const prBody = await getPRBody(github, core, context);
if (prBody) {
try {
const prBody = context.payload.pull_request.body;
const metasStart = prBody.indexOf(EXTRA_METAS_PR_BODY_START_TAG);
const metasEnd = prBody.lastIndexOf(EXTRA_METAS_PR_BODY_END_TAG);

View File

@@ -11,7 +11,7 @@ import {processOtaFiles} from './ghw_process_ota_files.js';
export async function updateManifests(github: Octokit, core: typeof CoreApi, context: Context): Promise<void> {
assert(context.eventName === 'push', 'Not a push');
const filePaths = await getChangedOtaFiles(github, core, context, `${context.payload.before}...${context.payload.after}`, false);
const filePaths = await getChangedOtaFiles(github, core, context, `${context.payload.before}...${context.payload.after}`, true);
const baseManifest = readManifest(BASE_INDEX_MANIFEST_FILENAME);
const prevManifest = readManifest(PREV_INDEX_MANIFEST_FILENAME);