github: move artifact and summary logic to dedicated classes
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
43
__tests__/github/artifact.test.itg.ts
Normal file
43
__tests__/github/artifact.test.itg.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright 2026 actions-toolkit authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {describe, expect, it} from '@jest/globals';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
import {GitHubArtifact} from '../../src/github/artifact';
|
||||
import {Util} from '../../src/util';
|
||||
|
||||
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
||||
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'github-itg-'));
|
||||
|
||||
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;
|
||||
|
||||
maybe('upload', () => {
|
||||
it('uploads an artifact', async () => {
|
||||
const filename = path.join(tmpDir, `github-repo-${Util.generateRandomString()}.json`);
|
||||
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
|
||||
const res = await GitHubArtifact.upload({
|
||||
filename: filename,
|
||||
mimeType: 'application/json',
|
||||
retentionDays: 1
|
||||
});
|
||||
expect(res).toBeDefined();
|
||||
console.log('uploadArtifactResponse', res);
|
||||
expect(res?.url).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -19,10 +19,12 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
import {GitHub} from '../src/github';
|
||||
import {GitHubRepo} from '../src/types/github';
|
||||
import {GitHub} from '../../src/github/github';
|
||||
import {GitHubRepo} from '../../src/types/github/github';
|
||||
|
||||
import repoFixture from './.fixtures/github-repo.json';
|
||||
import repoFixture from '../.fixtures/github-repo.json';
|
||||
|
||||
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
||||
|
||||
describe('repoData', () => {
|
||||
it('returns GitHub repo data', async () => {
|
||||
@@ -49,7 +51,7 @@ describe('repoData (api)', () => {
|
||||
try {
|
||||
jest.resetModules();
|
||||
jest.unmock('@actions/github');
|
||||
const {GitHub} = await import('../src/github');
|
||||
const {GitHub} = await import('../../src/github/github');
|
||||
const github = new GitHub({token: process.env.GITHUB_TOKEN});
|
||||
const repo = await github.repoData();
|
||||
const fullName = repo.full_name ?? `${repo.owner?.login}/${repo.name}`;
|
||||
@@ -172,10 +174,7 @@ describe('actionsRuntimeToken', () => {
|
||||
}).toThrow();
|
||||
});
|
||||
it('fixture', async () => {
|
||||
process.env.ACTIONS_RUNTIME_TOKEN = fs
|
||||
.readFileSync(path.join(__dirname, '.fixtures', 'runtimeToken.txt'))
|
||||
.toString()
|
||||
.trim();
|
||||
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(fixturesDir, 'runtimeToken.txt')).toString().trim();
|
||||
const runtimeToken = GitHub.actionsRuntimeToken;
|
||||
expect(runtimeToken?.ac).toEqual('[{"Scope":"refs/heads/master","Permission":3}]');
|
||||
expect(runtimeToken?.iss).toEqual('vstoken.actions.githubusercontent.com');
|
||||
@@ -203,10 +202,7 @@ describe('printActionsRuntimeTokenACs', () => {
|
||||
});
|
||||
it('refs/heads/master', async () => {
|
||||
const infoSpy = jest.spyOn(core, 'info');
|
||||
process.env.ACTIONS_RUNTIME_TOKEN = fs
|
||||
.readFileSync(path.join(__dirname, '.fixtures', 'runtimeToken.txt'))
|
||||
.toString()
|
||||
.trim();
|
||||
process.env.ACTIONS_RUNTIME_TOKEN = fs.readFileSync(path.join(fixturesDir, 'runtimeToken.txt')).toString().trim();
|
||||
await GitHub.printActionsRuntimeTokenACs();
|
||||
expect(infoSpy).toHaveBeenCalledTimes(1);
|
||||
expect(infoSpy).toHaveBeenCalledWith(`refs/heads/master: read/write`);
|
||||
@@ -19,34 +19,19 @@ import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
import {Buildx} from '../src/buildx/buildx';
|
||||
import {Bake} from '../src/buildx/bake';
|
||||
import {Build} from '../src/buildx/build';
|
||||
import {Exec} from '../src/exec';
|
||||
import {GitHub} from '../src/github';
|
||||
import {History} from '../src/buildx/history';
|
||||
import {Util} from '../src/util';
|
||||
import {Buildx} from '../../src/buildx/buildx';
|
||||
import {Bake} from '../../src/buildx/bake';
|
||||
import {Build} from '../../src/buildx/build';
|
||||
import {Exec} from '../../src/exec';
|
||||
import {GitHubArtifact} from '../../src/github/artifact';
|
||||
import {GitHubSummary} from '../../src/github/summary';
|
||||
import {History} from '../../src/buildx/history';
|
||||
|
||||
const fixturesDir = path.join(__dirname, '.fixtures');
|
||||
const fixturesDir = path.join(__dirname, '..', '.fixtures');
|
||||
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'github-itg-'));
|
||||
|
||||
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;
|
||||
|
||||
maybe('uploadArtifact', () => {
|
||||
it('uploads an artifact', async () => {
|
||||
const filename = path.join(tmpDir, `github-repo-${Util.generateRandomString()}.json`);
|
||||
fs.copyFileSync(path.join(fixturesDir, `github-repo.json`), filename);
|
||||
const res = await GitHub.uploadArtifact({
|
||||
filename: filename,
|
||||
mimeType: 'application/json',
|
||||
retentionDays: 1
|
||||
});
|
||||
expect(res).toBeDefined();
|
||||
console.log('uploadArtifactResponse', res);
|
||||
expect(res?.url).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
maybe('writeBuildSummary', () => {
|
||||
// prettier-ignore
|
||||
test.each([
|
||||
@@ -98,7 +83,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(exportRes?.dockerbuildSize).toBeDefined();
|
||||
expect(exportRes?.summaries).toBeDefined();
|
||||
|
||||
const uploadRes = await GitHub.uploadArtifact({
|
||||
const uploadRes = await GitHubArtifact.upload({
|
||||
filename: exportRes?.dockerbuildFilename,
|
||||
mimeType: 'application/gzip',
|
||||
retentionDays: 1
|
||||
@@ -106,7 +91,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(uploadRes).toBeDefined();
|
||||
expect(uploadRes?.url).toBeDefined();
|
||||
|
||||
await GitHub.writeBuildSummary({
|
||||
await GitHubSummary.writeBuildSummary({
|
||||
exportRes: exportRes,
|
||||
uploadRes: uploadRes,
|
||||
inputs: {
|
||||
@@ -178,7 +163,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(exportRes?.dockerbuildSize).toBeDefined();
|
||||
expect(exportRes?.summaries).toBeDefined();
|
||||
|
||||
const uploadRes = await GitHub.uploadArtifact({
|
||||
const uploadRes = await GitHubArtifact.upload({
|
||||
filename: exportRes?.dockerbuildFilename,
|
||||
mimeType: 'application/gzip',
|
||||
retentionDays: 1
|
||||
@@ -186,7 +171,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(uploadRes).toBeDefined();
|
||||
expect(uploadRes?.url).toBeDefined();
|
||||
|
||||
await GitHub.writeBuildSummary({
|
||||
await GitHubSummary.writeBuildSummary({
|
||||
exportRes: exportRes,
|
||||
uploadRes: uploadRes,
|
||||
inputs: {
|
||||
@@ -233,7 +218,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(exportRes?.dockerbuildSize).toBeDefined();
|
||||
expect(exportRes?.summaries).toBeDefined();
|
||||
|
||||
const uploadRes = await GitHub.uploadArtifact({
|
||||
const uploadRes = await GitHubArtifact.upload({
|
||||
filename: exportRes?.dockerbuildFilename,
|
||||
mimeType: 'application/gzip',
|
||||
retentionDays: 1
|
||||
@@ -241,7 +226,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(uploadRes).toBeDefined();
|
||||
expect(uploadRes?.url).toBeDefined();
|
||||
|
||||
await GitHub.writeBuildSummary({
|
||||
await GitHubSummary.writeBuildSummary({
|
||||
exportRes: exportRes,
|
||||
uploadRes: uploadRes,
|
||||
inputs: {
|
||||
@@ -288,7 +273,7 @@ maybe('writeBuildSummary', () => {
|
||||
expect(exportRes?.dockerbuildSize).toBeDefined();
|
||||
expect(exportRes?.summaries).toBeDefined();
|
||||
|
||||
await GitHub.writeBuildSummary({
|
||||
await GitHubSummary.writeBuildSummary({
|
||||
exportRes: exportRes,
|
||||
inputs: {
|
||||
context: fixturesDir,
|
||||
Reference in New Issue
Block a user