diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index da495bba..54d5ad8a 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -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'; @@ -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'); }); @@ -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 } @@ -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'); }); }); @@ -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 } @@ -111,7 +114,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/test'); }); @@ -135,7 +138,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/tags/8.0.0'); }); @@ -159,7 +162,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/tags/8.0.0'); }); @@ -183,7 +186,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/pull/221/merge'); }); @@ -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 () => { @@ -229,7 +232,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/test'); }); @@ -253,7 +256,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/feature-branch'); }); @@ -280,7 +283,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/main'); }); @@ -307,7 +310,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/main'); }); @@ -337,7 +340,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/heads/feature'); }); @@ -370,7 +373,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/tags/v1.0.0'); }); @@ -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 () => { @@ -431,7 +434,7 @@ describe('ref', () => { }); }); - const ref = await Git.ref(); + const ref = await GitMocked.ref(); expect(ref).toEqual('refs/remotes/unusual-format'); }); @@ -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 } @@ -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 } @@ -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 } @@ -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); }); }); diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 93f9fd6a..134041df 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -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 => { - return >(repoFixture as unknown); -}); describe('repoData', () => { it('returns GitHub repo data', async () => { + jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise => { + return >(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'); diff --git a/jest.config.cjs b/jest.config.cjs index 374a9f06..ea3f5836 100644 --- a/jest.config.cjs +++ b/jest.config.cjs @@ -38,7 +38,7 @@ module.exports = { setupFiles: ['dotenv/config'], testMatch: ['**/*.test.ts'], transform: { - '^.+\\.ts$': [ + '^.+\\.[tj]s$': [ 'ts-jest', { useESM: true, @@ -46,7 +46,9 @@ module.exports = { } ] }, + transformIgnorePatterns: ['/node_modules/(?!(?:@actions/github|@octokit|universal-user-agent|before-after-hook)/)'], moduleNameMapper: { + '^@actions/github$': '/node_modules/@actions/github/lib/github.js', '^(\\.{1,2}/.*)\\.js$': '$1' }, extensionsToTreatAsEsm: ['.ts'], diff --git a/jest.config.itg.cjs b/jest.config.itg.cjs index 87547520..97ac2b4e 100644 --- a/jest.config.itg.cjs +++ b/jest.config.itg.cjs @@ -21,7 +21,7 @@ module.exports = { testMatch: ['**/*.test.itg.ts'], testTimeout: 1800000, // 30 minutes transform: { - '^.+\\.ts$': [ + '^.+\\.[tj]s$': [ 'ts-jest', { useESM: true, @@ -29,7 +29,9 @@ module.exports = { } ] }, + transformIgnorePatterns: ['/node_modules/(?!(?:@actions/github|@octokit|universal-user-agent|before-after-hook)/)'], moduleNameMapper: { + '^@actions/github$': '/node_modules/@actions/github/lib/github.js', '^(\\.{1,2}/.*)\\.js$': '$1' }, extensionsToTreatAsEsm: ['.ts'], diff --git a/package.json b/package.json index bfd231f8..a4f65de1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/git.ts b/src/git.ts index b69f50bd..7fb9fe64 100644 --- a/src/git.ts +++ b/src/git.ts @@ -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'; @@ -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 ( diff --git a/src/types/github.ts b/src/types/github.ts index af184ef6..9b4e77dc 100644 --- a/src/types/github.ts +++ b/src/types/github.ts @@ -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'; @@ -39,7 +39,8 @@ export interface GitHubContentOpts { path: string; } -export type GitHubRepo = OctoOpenApiTypes['schemas']['repository']; +type Octokit = ReturnType; +export type GitHubRepo = Awaited>['data']; export interface GitHubActionsRuntimeToken extends JwtPayload { ac?: string; diff --git a/tsconfig.test.json b/tsconfig.test.json index d78b1e5f..37e63ac8 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -2,7 +2,8 @@ "extends": "./tsconfig.json", "compilerOptions": { "rootDir": ".", - "noEmit": true + "noEmit": true, + "allowJs": true }, "include": [ "src/**/*.ts", diff --git a/yarn.lock b/yarn.lock index 9cb6c26b..b7d7490c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -105,18 +105,18 @@ __metadata: languageName: node linkType: hard -"@actions/github@npm:^7.0.0": - version: 7.0.0 - resolution: "@actions/github@npm:7.0.0" +"@actions/github@npm:^9.0.0": + version: 9.0.0 + resolution: "@actions/github@npm:9.0.0" dependencies: - "@actions/http-client": "npm:^3.0.1" - "@octokit/core": "npm:^5.0.1" - "@octokit/plugin-paginate-rest": "npm:^9.2.2" - "@octokit/plugin-rest-endpoint-methods": "npm:^10.4.0" - "@octokit/request": "npm:^8.4.1" - "@octokit/request-error": "npm:^5.1.1" - undici: "npm:^5.28.5" - checksum: 10/829c6402f927839714692f4e1f48359d306be0fd21ef3843feaf3db6541e5e7f85447b0bf794beeebad0b8f4a2644f7f92143cb4a8311541b29f5ddfc585cec1 + "@actions/http-client": "npm:^3.0.2" + "@octokit/core": "npm:^7.0.6" + "@octokit/plugin-paginate-rest": "npm:^14.0.0" + "@octokit/plugin-rest-endpoint-methods": "npm:^17.0.0" + "@octokit/request": "npm:^10.0.7" + "@octokit/request-error": "npm:^7.1.0" + undici: "npm:^6.23.0" + checksum: 10/5d3b2790f7b67cb7b0ec4416e8b666fde69621bac5fff5e59737c8233f8ad09829471b5cab2c50faea60da39554c69d5e7e51e5ffc8e2c899fba32f47db15b9b languageName: node linkType: hard @@ -1192,7 +1192,7 @@ __metadata: "@actions/cache": "npm:^5.0.5" "@actions/core": "npm:^2.0.3" "@actions/exec": "npm:^2.0.0" - "@actions/github": "npm:^7.0.0" + "@actions/github": "npm:^9.0.0" "@actions/http-client": "npm:^3.0.2" "@actions/io": "npm:^2.0.0" "@actions/tool-cache": "npm:^3.0.1" @@ -1200,8 +1200,6 @@ __metadata: "@eslint/compat": "npm:^2.0.0" "@eslint/eslintrc": "npm:^3.3.3" "@eslint/js": "npm:^9.39.2" - "@octokit/core": "npm:^5.2.2" - "@octokit/plugin-rest-endpoint-methods": "npm:^10.4.1" "@sigstore/bundle": "npm:^4.0.0" "@sigstore/rekor-types": "npm:^4.0.0" "@sigstore/sign": "npm:^4.1.0" @@ -1946,6 +1944,13 @@ __metadata: languageName: node linkType: hard +"@octokit/auth-token@npm:^6.0.0": + version: 6.0.0 + resolution: "@octokit/auth-token@npm:6.0.0" + checksum: 10/a30f5c4c984964b57193de5b6f67169f74e4779fedbe716157dd3558dd9de3ca5c105cae521b7bd8ce1ae180773a2ef01afe2306ad5a329f4fd291eba2b7c7d1 + languageName: node + linkType: hard + "@octokit/core@npm:^5.0.1": version: 5.0.1 resolution: "@octokit/core@npm:5.0.1" @@ -1961,7 +1966,7 @@ __metadata: languageName: node linkType: hard -"@octokit/core@npm:^5.2.1, @octokit/core@npm:^5.2.2": +"@octokit/core@npm:^5.2.1": version: 5.2.2 resolution: "@octokit/core@npm:5.2.2" dependencies: @@ -1976,6 +1981,31 @@ __metadata: languageName: node linkType: hard +"@octokit/core@npm:^7.0.6": + version: 7.0.6 + resolution: "@octokit/core@npm:7.0.6" + dependencies: + "@octokit/auth-token": "npm:^6.0.0" + "@octokit/graphql": "npm:^9.0.3" + "@octokit/request": "npm:^10.0.6" + "@octokit/request-error": "npm:^7.0.2" + "@octokit/types": "npm:^16.0.0" + before-after-hook: "npm:^4.0.0" + universal-user-agent: "npm:^7.0.0" + checksum: 10/852d41fc3150d2a891156427dd0575c77889f1c7a109894ee541594e3fd47c0d4e0a93fee22966c507dfd6158b522e42846c2ac46b9d896078194c95fa81f4ae + languageName: node + linkType: hard + +"@octokit/endpoint@npm:^11.0.2": + version: 11.0.2 + resolution: "@octokit/endpoint@npm:11.0.2" + dependencies: + "@octokit/types": "npm:^16.0.0" + universal-user-agent: "npm:^7.0.2" + checksum: 10/0d088747baf94eafbba69da23ba840b40cd3f5d0bfbc51c692ff9d9d78de6d81f06366e6e30df8c1783355be826c27d38ab9ab0708396af8f430b06cfa29db35 + languageName: node + linkType: hard + "@octokit/endpoint@npm:^9.0.6": version: 9.0.6 resolution: "@octokit/endpoint@npm:9.0.6" @@ -2008,6 +2038,17 @@ __metadata: languageName: node linkType: hard +"@octokit/graphql@npm:^9.0.3": + version: 9.0.3 + resolution: "@octokit/graphql@npm:9.0.3" + dependencies: + "@octokit/request": "npm:^10.0.6" + "@octokit/types": "npm:^16.0.0" + universal-user-agent: "npm:^7.0.0" + checksum: 10/7b16f281f8571dce55280b3986fbb8d15465a7236164a5f6497ded7597ff9ee95d5796924555b979903fe8c6706fe6be1b3e140d807297f85ac8edeadc28f9fe + languageName: node + linkType: hard + "@octokit/openapi-types@npm:^12.11.0": version: 12.11.0 resolution: "@octokit/openapi-types@npm:12.11.0" @@ -2043,6 +2084,24 @@ __metadata: languageName: node linkType: hard +"@octokit/openapi-types@npm:^27.0.0": + version: 27.0.0 + resolution: "@octokit/openapi-types@npm:27.0.0" + checksum: 10/5cd2cdf4e41fdf522e15e3d53f3ece8380d98dda9173a6fc905828fb2c33e8733d5f5d2a757ae3a572525f4749748e66cb40e7939372132988d8eb4ba978d54f + languageName: node + linkType: hard + +"@octokit/plugin-paginate-rest@npm:^14.0.0": + version: 14.0.0 + resolution: "@octokit/plugin-paginate-rest@npm:14.0.0" + dependencies: + "@octokit/types": "npm:^16.0.0" + peerDependencies: + "@octokit/core": ">=6" + checksum: 10/57ddd857528dad9c02431bc6254c2374c06057872cf9656a4a88b162ebe1c2bc9f34fbec360f2ccff72c940f29b120758ce14e8135bd027223d381eb1b8b6579 + languageName: node + linkType: hard + "@octokit/plugin-paginate-rest@npm:^9.2.2": version: 9.2.2 resolution: "@octokit/plugin-paginate-rest@npm:9.2.2" @@ -2074,14 +2133,14 @@ __metadata: languageName: node linkType: hard -"@octokit/plugin-rest-endpoint-methods@npm:^10.4.1": - version: 10.4.1 - resolution: "@octokit/plugin-rest-endpoint-methods@npm:10.4.1" +"@octokit/plugin-rest-endpoint-methods@npm:^17.0.0": + version: 17.0.0 + resolution: "@octokit/plugin-rest-endpoint-methods@npm:17.0.0" dependencies: - "@octokit/types": "npm:^12.6.0" + "@octokit/types": "npm:^16.0.0" peerDependencies: - "@octokit/core": 5 - checksum: 10/1090fc5a1bebb7b48c512e178f8ad69a3ef8332e583274972f3a3035e9be9200093e22a5dbfe0f71aa1a7a8817e54bb915af3c2a3f88db1311a2873cef176552 + "@octokit/core": ">=6" + checksum: 10/e9d9ad4d9755cc7fb82fdcbfa870ddea8a432180f0f76c8469095557fd1e26f8caea8cae58401209be17c4f3d8cc48c0e16a3643e37e48f4d23c39e058bf2c55 languageName: node linkType: hard @@ -2106,6 +2165,28 @@ __metadata: languageName: node linkType: hard +"@octokit/request-error@npm:^7.0.2, @octokit/request-error@npm:^7.1.0": + version: 7.1.0 + resolution: "@octokit/request-error@npm:7.1.0" + dependencies: + "@octokit/types": "npm:^16.0.0" + checksum: 10/c1d447ff7482382c69f7a4b2eaa44c672906dd111d8a9196a5d07f2adc4ae0f0e12ec4ce0063f14f9b2fb5f0cef4451c95ec961a7a711bd900e5d6441d546570 + languageName: node + linkType: hard + +"@octokit/request@npm:^10.0.6, @octokit/request@npm:^10.0.7": + version: 10.0.7 + resolution: "@octokit/request@npm:10.0.7" + dependencies: + "@octokit/endpoint": "npm:^11.0.2" + "@octokit/request-error": "npm:^7.0.2" + "@octokit/types": "npm:^16.0.0" + fast-content-type-parse: "npm:^3.0.0" + universal-user-agent: "npm:^7.0.2" + checksum: 10/eaf6d347340729b47d482b487411e3543384d1c07f9125c4e53c11ece53f22a0245b71be22dd48bd6ad16af48c7c323c7317da49a238206d07fb7cd3ef0c6e74 + languageName: node + linkType: hard + "@octokit/request@npm:^8.0.1, @octokit/request@npm:^8.0.2, @octokit/request@npm:^8.4.1": version: 8.4.1 resolution: "@octokit/request@npm:8.4.1" @@ -2154,6 +2235,15 @@ __metadata: languageName: node linkType: hard +"@octokit/types@npm:^16.0.0": + version: 16.0.0 + resolution: "@octokit/types@npm:16.0.0" + dependencies: + "@octokit/openapi-types": "npm:^27.0.0" + checksum: 10/03d5cfc29556a9b53eae8beb1bf15c0b704dc722db2c51b53f093f3c3ee6c1d8e20b682be8117a3a17034b458be7746d1b22aaefb959ceb5152ad7589b39e2c9 + languageName: node + linkType: hard + "@octokit/types@npm:^6.0.3": version: 6.41.0 resolution: "@octokit/types@npm:6.41.0" @@ -3393,6 +3483,13 @@ __metadata: languageName: node linkType: hard +"before-after-hook@npm:^4.0.0": + version: 4.0.0 + resolution: "before-after-hook@npm:4.0.0" + checksum: 10/9fd52bc0c3cca0fb115e04dacbeeaacff38fa23e1af725d62392254c31ef433b15da60efcba61552e44d64e26f25ea259f72dba005115924389e88d2fd56e19f + languageName: node + linkType: hard + "binary@npm:^0.3.0": version: 0.3.0 resolution: "binary@npm:0.3.0" @@ -4884,6 +4981,13 @@ __metadata: languageName: node linkType: hard +"fast-content-type-parse@npm:^3.0.0": + version: 3.0.0 + resolution: "fast-content-type-parse@npm:3.0.0" + checksum: 10/8616a8aa6c9b4f8f4f3c90eaa4e7bfc2240cfa6f41f0eef5b5aa2b2c8b38bd9ad435f1488b6d817ffd725c54651e2777b882ae9dd59366e71e7896f1ec11d473 + languageName: node + linkType: hard + "fast-deep-equal@npm:^3.1.1, fast-deep-equal@npm:^3.1.3": version: 3.1.3 resolution: "fast-deep-equal@npm:3.1.3" @@ -9487,6 +9591,13 @@ __metadata: languageName: node linkType: hard +"universal-user-agent@npm:^7.0.0, universal-user-agent@npm:^7.0.2": + version: 7.0.3 + resolution: "universal-user-agent@npm:7.0.3" + checksum: 10/c497e85f8b11eb8fa4dce584d7a39cc98710164959f494cafc3c269b51abb20fff269951838efd7424d15f6b3d001507f3cb8b52bb5676fdb642019dfd17e63e + languageName: node + linkType: hard + "unrs-resolver@npm:^1.7.11": version: 1.11.1 resolution: "unrs-resolver@npm:1.11.1"