Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {beforeEach, describe, expect, it, jest} from '@jest/globals';

import {Git} from '../src/git';
import {Git as GitMocked} from '../src/git';
import {Exec} from '../src/exec';
import {ExecOutput} from '@actions/exec';

Expand Down Expand Up @@ -46,7 +46,7 @@ describe('context', () => {
exitCode: 0
});
});
const ctx = await Git.context();
const ctx = await GitMocked.context();
expect(ctx.ref).toEqual('refs/heads/test');
expect(ctx.sha).toEqual('test-sha');
});
Expand All @@ -56,7 +56,7 @@ describe('isInsideWorkTree', () => {
it('have been called', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
try {
await Git.isInsideWorkTree();
await GitMocked.isInsideWorkTree();
} catch {
// noop
}
Expand All @@ -69,9 +69,12 @@ describe('isInsideWorkTree', () => {

describe('remoteSha', () => {
it('returns sha using git ls-remote', async () => {
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
expect(await GitMocked.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
});
it('returns sha using github api', async () => {
jest.resetModules();
jest.unmock('@actions/github');
const {Git} = await import('../src/git');
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head', process.env.GITHUB_TOKEN)).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
});
});
Expand All @@ -80,7 +83,7 @@ describe('remoteURL', () => {
it('have been called', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
try {
await Git.remoteURL();
await GitMocked.remoteURL();
} catch {
// noop
}
Expand Down Expand Up @@ -111,7 +114,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/test');
});
Expand All @@ -135,7 +138,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/tags/8.0.0');
});
Expand All @@ -159,7 +162,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/tags/8.0.0');
});
Expand All @@ -183,7 +186,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/pull/221/merge');
});
Expand All @@ -207,7 +210,7 @@ describe('ref', () => {
});
});

await expect(Git.ref()).rejects.toThrow('Cannot find detached HEAD ref in "wrong, HEAD, tag: 8.0.0"');
await expect(GitMocked.ref()).rejects.toThrow('Cannot find detached HEAD ref in "wrong, HEAD, tag: 8.0.0"');
});

it('returns mocked detached branch ref', async () => {
Expand All @@ -229,7 +232,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/test');
});
Expand All @@ -253,7 +256,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/feature-branch');
});
Expand All @@ -280,7 +283,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/main');
});
Expand All @@ -307,7 +310,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/main');
});
Expand Down Expand Up @@ -337,7 +340,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/heads/feature');
});
Expand Down Expand Up @@ -370,7 +373,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/tags/v1.0.0');
});
Expand Down Expand Up @@ -403,7 +406,7 @@ describe('ref', () => {
});
});

await expect(Git.ref()).rejects.toThrow('Cannot infer ref from detached HEAD');
await expect(GitMocked.ref()).rejects.toThrow('Cannot infer ref from detached HEAD');
});

it('handles remote ref without branch pattern when inferring from remote', async () => {
Expand Down Expand Up @@ -431,7 +434,7 @@ describe('ref', () => {
});
});

const ref = await Git.ref();
const ref = await GitMocked.ref();

expect(ref).toEqual('refs/remotes/unusual-format');
});
Expand All @@ -441,7 +444,7 @@ describe('fullCommit', () => {
it('have been called', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
try {
await Git.fullCommit();
await GitMocked.fullCommit();
} catch {
// noop
}
Expand All @@ -456,7 +459,7 @@ describe('shortCommit', () => {
it('have been called', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
try {
await Git.shortCommit();
await GitMocked.shortCommit();
} catch {
// noop
}
Expand All @@ -471,7 +474,7 @@ describe('tag', () => {
it('have been called', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
try {
await Git.tag();
await GitMocked.tag();
} catch {
// noop
}
Expand All @@ -484,7 +487,7 @@ describe('tag', () => {

describe('getCommitDate', () => {
it('head', async () => {
const date = await Git.commitDate('HEAD');
const date = await GitMocked.commitDate('HEAD');
await expect(date).toBeInstanceOf(Date);
});
});
32 changes: 29 additions & 3 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,43 @@ import {GitHub} from '../src/github';
import {GitHubRepo} from '../src/types/github';

import repoFixture from './.fixtures/github-repo.json';
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
return <Promise<GitHubRepo>>(repoFixture as unknown);
});

describe('repoData', () => {
it('returns GitHub repo data', async () => {
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
return <Promise<GitHubRepo>>(repoFixture as unknown);
});
const github = new GitHub();
expect((await github.repoData()).name).toEqual('Hello-World');
});
});

describe('repoData (api)', () => {
it('returns docker/actions-toolkit', async () => {
if (!process.env.GITHUB_TOKEN) {
return;
}

const originalEnv = process.env;
process.env = {
...originalEnv,
GITHUB_REPOSITORY: 'docker/actions-toolkit'
};

try {
jest.resetModules();
jest.unmock('@actions/github');
const {GitHub} = await import('../src/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}`;
expect(fullName).toEqual('docker/actions-toolkit');
} finally {
process.env = originalEnv;
}
});
});

describe('context', () => {
it('returns repository name from payload', async () => {
expect(GitHub.context.payload.repository?.name).toEqual('test-docker-action');
Expand Down
4 changes: 3 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ module.exports = {
setupFiles: ['dotenv/config'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': [
'^.+\\.[tj]s$': [
'ts-jest',
{
useESM: true,
tsconfig: '<rootDir>/tsconfig.test.json'
}
]
},
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/github|@octokit|universal-user-agent|before-after-hook)/)'],
moduleNameMapper: {
'^@actions/github$': '<rootDir>/node_modules/@actions/github/lib/github.js',
'^(\\.{1,2}/.*)\\.js$': '$1'
},
extensionsToTreatAsEsm: ['.ts'],
Expand Down
4 changes: 3 additions & 1 deletion jest.config.itg.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ module.exports = {
testMatch: ['**/*.test.itg.ts'],
testTimeout: 1800000, // 30 minutes
transform: {
'^.+\\.ts$': [
'^.+\\.[tj]s$': [
'ts-jest',
{
useESM: true,
tsconfig: '<rootDir>/tsconfig.test.json'
}
]
},
transformIgnorePatterns: ['/node_modules/(?!(?:@actions/github|@octokit|universal-user-agent|before-after-hook)/)'],
moduleNameMapper: {
'^@actions/github$': '<rootDir>/node_modules/@actions/github/lib/github.js',
'^(\\.{1,2}/.*)\\.js$': '$1'
},
extensionsToTreatAsEsm: ['.ts'],
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@
"@actions/cache": "^5.0.5",
"@actions/core": "^2.0.3",
"@actions/exec": "^2.0.0",
"@actions/github": "^7.0.0",
"@actions/github": "^9.0.0",
"@actions/http-client": "^3.0.2",
"@actions/io": "^2.0.0",
"@actions/tool-cache": "^3.0.1",
"@azure/storage-blob": "^12.29.1",
"@octokit/core": "^5.2.2",
"@octokit/plugin-rest-endpoint-methods": "^10.4.1",
"@sigstore/bundle": "^4.0.0",
"@sigstore/sign": "^4.1.0",
"@sigstore/tuf": "^4.0.1",
Expand Down
7 changes: 2 additions & 5 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import * as core from '@actions/core';
import * as github from '@actions/github';
import {Octokit} from '@octokit/core';
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods';

import {Exec} from './exec.js';
import {GitHub} from './github.js';

Expand Down Expand Up @@ -47,9 +44,9 @@ export class Git {
// if we have a token and this is a GitHub repo we can use the GitHub API
if (token && repoMatch) {
core.setSecret(token);
const octokit = new (Octokit.plugin(restEndpointMethods).defaults({
const octokit = github.getOctokit(token, {
baseUrl: GitHub.apiURL
}))({auth: token});
});
const [owner, repoName] = repoMatch.slice(1, 3);
try {
return (
Expand Down
5 changes: 3 additions & 2 deletions src/types/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as core from '@actions/core';
import {AnnotationProperties} from '@actions/core';
import {components as OctoOpenApiTypes} from '@octokit/openapi-types';
import type {getOctokit} from '@actions/github';
import {JwtPayload} from 'jwt-decode';

import {BakeDefinition} from './buildx/bake.js';
Expand All @@ -39,7 +39,8 @@ export interface GitHubContentOpts {
path: string;
}

export type GitHubRepo = OctoOpenApiTypes['schemas']['repository'];
type Octokit = ReturnType<typeof getOctokit>;
export type GitHubRepo = Awaited<ReturnType<Octokit['rest']['repos']['get']>>['data'];

export interface GitHubActionsRuntimeToken extends JwtPayload {
ac?: string;
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"noEmit": true
"noEmit": true,
"allowJs": true
},
"include": [
"src/**/*.ts",
Expand Down
Loading
Loading