refactor report and utils.ts

This commit is contained in:
jpaul 2023-11-17 08:51:57 +01:00
parent d0de3476cf
commit 714a309928
7 changed files with 47 additions and 46 deletions

View File

@ -49,7 +49,7 @@ jobs:
# If index.js was different than expected, upload the expected version as an artifact # If index.js was different than expected, upload the expected version as an artifact
- uses: actions/upload-artifact@v3 - uses: actions/upload-artifact@v3
# if: ${{ failure() && steps.diff.conclusion == 'failure' }} if: ${{ failure() && steps.diff.conclusion == 'failure' }}
with: with:
name: dist name: dist
path: dist/ path: dist/

View File

@ -50,9 +50,6 @@ inputs:
gds-token: gds-token:
required: false required: false
description: 'Download token for the GraalVM Download Service. If provided, the action will set up GraalVM Enterprise Edition.' description: 'Download token for the GraalVM Download Service. If provided, the action will set up GraalVM Enterprise Edition.'
report-token:
required: false
description: 'Github-Token for add build report tp repository.'
outputs: outputs:
cache-hit: cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key' description: 'A boolean value to indicate an exact match was found for the primary key'

View File

@ -7,7 +7,7 @@
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"format": "prettier --write '**/*.ts'", "format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'", "format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts", "lint": "eslint src/**/*.ts",
"package": "ncc build -o dist/main src/main.ts && ncc build -o dist/cleanup src/cleanup.ts", "package": "ncc build -o dist/main src/main.ts && ncc build -o dist/cleanup src/cleanup.ts",
"test": "jest", "test": "jest",

View File

@ -1,4 +1,5 @@
import * as otypes from '@octokit/types' import * as otypes from '@octokit/types'
import {context} from "@actions/github";
export const INPUT_VERSION = 'version' export const INPUT_VERSION = 'version'
export const INPUT_GDS_TOKEN = 'gds-token' export const INPUT_GDS_TOKEN = 'gds-token'
@ -10,7 +11,6 @@ export const INPUT_SET_JAVA_HOME = 'set-java-home'
export const INPUT_CACHE = 'cache' export const INPUT_CACHE = 'cache'
export const INPUT_CHECK_FOR_UPDATES = 'check-for-updates' export const INPUT_CHECK_FOR_UPDATES = 'check-for-updates'
export const INPUT_NI_MUSL = 'native-image-musl' export const INPUT_NI_MUSL = 'native-image-musl'
export const INPUT_REPORT_TOKEN = 'report-token'
export const IS_LINUX = process.platform === 'linux' export const IS_LINUX = process.platform === 'linux'
export const IS_MACOS = process.platform === 'darwin' export const IS_MACOS = process.platform === 'darwin'
@ -44,12 +44,20 @@ export const EVENT_NAME_PULL_REQUEST = 'pull_request'
export const ERROR_HINT = export const ERROR_HINT =
'If you think this is a mistake, please file an issue at: https://github.com/graalvm/setup-graalvm/issues.' 'If you think this is a mistake, please file an issue at: https://github.com/graalvm/setup-graalvm/issues.'
export const METRIC_REF_PATH = 'refs/graalvm-metrics'
export const OCTOKIT_ROUTE_REF_METRICS = `GET /repos/${context.repo.owner}/${context.repo.repo}/git/ref/metrics/${baseCommitSha}`
export type LatestReleaseResponse = export type LatestReleaseResponse =
otypes.Endpoints['GET /repos/{owner}/{repo}/releases/latest']['response'] otypes.Endpoints['GET /repos/{owner}/{repo}/releases/latest']['response']
export type MatchingRefsResponse = export type MatchingRefsResponse =
otypes.Endpoints['GET /repos/{owner}/{repo}/git/matching-refs/{ref}']['response'] otypes.Endpoints['GET /repos/{owner}/{repo}/git/matching-refs/{ref}']['response']
export type LatestReleaseResponse =
otypes.Endpoints['GET /repos/{owner}/{repo}/releases/latest']['response']
function determineJDKArchitecture(): string { function determineJDKArchitecture(): string {
switch (process.arch) { switch (process.arch) {
case 'x64': { case 'x64': {

View File

@ -6,9 +6,6 @@ import {join} from 'path'
import {tmpdir} from 'os' import {tmpdir} from 'os'
import {createPRComment, createRef, createTree, getPrBaseBranchMetrics, isPREvent, toSemVer} from '../utils' import {createPRComment, createRef, createTree, getPrBaseBranchMetrics, isPREvent, toSemVer} from '../utils'
import {gte} from 'semver' import {gte} from 'semver'
import {Base64} from 'js-base64';
import { Octokit } from '@octokit/rest';
import fetch from "node-fetch";
const BUILD_OUTPUT_JSON_PATH = join(tmpdir(), 'native-image-build-output.json') const BUILD_OUTPUT_JSON_PATH = join(tmpdir(), 'native-image-build-output.json')
const BYTES_TO_KiB = 1024 const BYTES_TO_KiB = 1024
@ -23,9 +20,6 @@ const NATIVE_IMAGE_CONFIG_FILE = join(
'native-image-options.properties' 'native-image-options.properties'
) )
const NATIVE_IMAGE_CONFIG_FILE_ENV = 'NATIVE_IMAGE_CONFIG_FILE' const NATIVE_IMAGE_CONFIG_FILE_ENV = 'NATIVE_IMAGE_CONFIG_FILE'
//const { Octokit } = require("@octokit/rest");
let REPORT_TOKEN = '';
interface AnalysisResult { interface AnalysisResult {
total: number total: number
@ -119,7 +113,6 @@ export async function setUpNativeImageBuildReports(
setNativeImageOption( setNativeImageOption(
`-H:BuildOutputJSONFile=${BUILD_OUTPUT_JSON_PATH.replace(/\\/g, '\\\\')}` `-H:BuildOutputJSONFile=${BUILD_OUTPUT_JSON_PATH.replace(/\\/g, '\\\\')}`
)// Escape backslashes for Windows )// Escape backslashes for Windows
REPORT_TOKEN = reportToken
} }
export async function generateReports(): Promise<void> { export async function generateReports(): Promise<void> {
@ -139,7 +132,6 @@ export async function generateReports(): Promise<void> {
const prMetrics: BuildOutput = JSON.parse( const prMetrics: BuildOutput = JSON.parse(
await getPrBaseBranchMetrics() await getPrBaseBranchMetrics()
) )
const prBaseReport = createReport(prMetrics)
const report = createReport(buildOutput) const report = createReport(buildOutput)
if (areJobReportsEnabled()) { if (areJobReportsEnabled()) {
@ -148,9 +140,9 @@ export async function generateReports(): Promise<void> {
} }
if (arePRReportsEnabled()) { if (arePRReportsEnabled()) {
createPRComment(report) createPRComment(report)
const prBaseReport = createPRComparison(buildOutput, prMetrics)
createPRComment(prBaseReport)
} }
const prBaseReport = createPRComparison(buildOutput, prMetrics)
createPRComment(prBaseReport)
} }
} }
@ -314,7 +306,7 @@ function createReport(data: BuildOutput): string {
)} of total time)` )} of total time)`
} }
return `## GraalVM Native Image Build Report example return `## GraalVM Native Image Build Report
\`${info.name}\` generated${totalTime} as part of the '${ \`${info.name}\` generated${totalTime} as part of the '${
context.job context.job

View File

@ -19,7 +19,6 @@ async function run(): Promise<void> {
const distribution = core.getInput(c.INPUT_DISTRIBUTION) const distribution = core.getInput(c.INPUT_DISTRIBUTION)
const graalVMVersion = core.getInput(c.INPUT_VERSION) const graalVMVersion = core.getInput(c.INPUT_VERSION)
const gdsToken = core.getInput(c.INPUT_GDS_TOKEN) const gdsToken = core.getInput(c.INPUT_GDS_TOKEN)
const reportToken = core.getInput(c.INPUT_REPORT_TOKEN)
const componentsString: string = core.getInput(c.INPUT_COMPONENTS) const componentsString: string = core.getInput(c.INPUT_COMPONENTS)
const components: string[] = const components: string[] =
componentsString.length > 0 componentsString.length > 0

View File

@ -161,13 +161,10 @@ function getGitHubToken(): string {
} }
function getCommitSha(): string { function getCommitSha(): string {
return process.env.GITHUB_SHA || "default_tag" return process.env.GITHUB_SHA || "default_sha"
} }
function getPrBaseBranchSha(): string { function getPrBaseBranchSha(): string {
if (!isPREvent()) {
return ""
}
return process.env.GITHUB_BASE_REF || "default_branch" return process.env.GITHUB_BASE_REF || "default_branch"
} }
@ -190,30 +187,35 @@ export async function createPRComment(content: string): Promise<void> {
} }
export async function createRef(sha: string) { export async function createRef(sha: string) {
const commitSha = getCommitSha() try {
const ref = `refs/metrics/${commitSha}` const commitSha = getCommitSha()
console.log(`creating ref ${ref} for metrics tree ${sha}`); const ref = c.METRIC_REF_PATH + commitSha
const octokit = new Octokit({ core.info(`creating ref ${ref} for metrics tree ${sha}`);
auth: getGitHubToken(), const octokit = new Octokit({
request: { auth: getGitHubToken(),
fetch: fetch, request: {
}, fetch: fetch,
}); },
const context = github.context });
const context = github.context
const response = await octokit.request( await octokit.request(
`POST /repos/${context.repo.owner}/${context.repo.repo}/git/refs`, `POST /repos/${context.repo.owner}/${context.repo.repo}/git/refs`,
{ {
...context.repo, ...context.repo,
ref, ref,
sha, sha,
} }
); );
} catch(err) {
core.info(response.data); core.error(
`Failed to create ref. Please make sure that the referred sha '${sha}' exist.`
)
}
} }
export async function createTree(metadataJson: string): Promise<string> { export async function createTree(metadataJson: string): Promise<string> {
try {
const octokit = new Octokit({ const octokit = new Octokit({
auth: getGitHubToken(), auth: getGitHubToken(),
request: { request: {
@ -221,9 +223,7 @@ export async function createTree(metadataJson: string): Promise<string> {
}, },
}); });
const context = github.context const context = github.context
core.info(`creating tree at ${context.repo.owner}/${context.repo.repo}`); core.info(`creating tree at ${context.repo.owner}/${context.repo.repo}`);
const response = await octokit.request( const response = await octokit.request(
`POST /repos/${context.repo.owner}/${context.repo.repo}/git/trees`, `POST /repos/${context.repo.owner}/${context.repo.repo}/git/trees`,
{ {
@ -241,6 +241,11 @@ export async function createTree(metadataJson: string): Promise<string> {
core.info("Tree-sha" + response.data.sha); core.info("Tree-sha" + response.data.sha);
return response.data.sha; return response.data.sha;
} catch (err) {
core.error(
`Creating metrics tree failed.`
)
}
} }
export async function getPrBaseBranchMetrics(): Promise<string> { export async function getPrBaseBranchMetrics(): Promise<string> {
@ -280,7 +285,7 @@ async function getBaseBranchCommitSha(octokit: Octokit, context: Context): Promi
} }
async function getBlobTreeSha(octokit: Octokit, context: Context, baseCommitSha: string): Promise<string> { async function getBlobTreeSha(octokit: Octokit, context: Context, baseCommitSha: string): Promise<string> {
const { data } = await octokit.request(`GET /repos/${context.repo.owner}/${context.repo.repo}/git/ref/metrics/${baseCommitSha}`, { const { data } = await octokit.request(, {
...context.repo, ...context.repo,
headers: { headers: {
'X-GitHub-Api-Version': '2022-11-28' 'X-GitHub-Api-Version': '2022-11-28'