diff --git a/packages/ci-context/src/lib/interfaces.ts b/packages/ci-context/src/lib/interfaces.ts index a03933c4..c26a55e6 100644 --- a/packages/ci-context/src/lib/interfaces.ts +++ b/packages/ci-context/src/lib/interfaces.ts @@ -5,12 +5,29 @@ export type RepoMetadata = Pick< 'default_branch' | 'description' | 'html_url' | 'license' | 'name' >; +export interface Payload { + base_ref: string; + number: number; + pull_request: { + head: { + sha: string; + }; + base: { + ref: string; + }; + }; + repository: { + default_branch?: string; + private?: boolean; + }; +} + export interface RunnerContext { name: string; actor: string; eventName: string; job: string; - payload: Record; + payload: Partial; ref: string; runId: number; runNumber: number; diff --git a/packages/ci-context/src/lib/utils/drone.spec.ts b/packages/ci-context/src/lib/utils/drone.spec.ts index 864104cd..2f6a5a66 100644 --- a/packages/ci-context/src/lib/utils/drone.spec.ts +++ b/packages/ci-context/src/lib/utils/drone.spec.ts @@ -39,7 +39,11 @@ describe('Drone Context', () => { actor: 'drone-actor', eventName: 'drone-event-name', job: 'drone-job', - payload: {}, + payload: { + repository: { + private: false, + }, + }, ref: 'refs/heads/drone-ref', runId: 100, runNumber: 100, @@ -54,6 +58,7 @@ describe('Drone Context', () => { beforeEach(() => { restore = mockedEnv({ DRONE_COMMIT_REF: 'refs/tags/drone-v1.0.0', + DRONE_REPO_PRIVATE: 'true', }); }); @@ -69,7 +74,11 @@ describe('Drone Context', () => { actor: 'drone-actor', eventName: 'drone-event-name', job: 'drone-job', - payload: {}, + payload: { + repository: { + private: true, + }, + }, ref: 'refs/tags/drone-v1.0.0', runId: 100, runNumber: 100, diff --git a/packages/ci-context/src/lib/utils/drone.ts b/packages/ci-context/src/lib/utils/drone.ts index d5077c65..b3fd9fb7 100644 --- a/packages/ci-context/src/lib/utils/drone.ts +++ b/packages/ci-context/src/lib/utils/drone.ts @@ -8,7 +8,11 @@ export class Drone { actor: process.env['DRONE_COMMIT_AUTHOR']!, eventName: process.env['DRONE_BUILD_EVENT']!, job: process.env['DRONE_STAGE_NAME']!, - payload: {}, + payload: { + repository: { + private: process.env['DRONE_REPO_PRIVATE'] === 'true', + }, + }, ref: process.env['DRONE_COMMIT_REF']!, runId: parseInt(process.env['DRONE_BUILD_NUMBER']!, 10), runNumber: parseInt(process.env['DRONE_BUILD_NUMBER']!, 10), diff --git a/packages/ci-context/src/lib/utils/github.ts b/packages/ci-context/src/lib/utils/github.ts index a152ad81..1bd26e1f 100644 --- a/packages/ci-context/src/lib/utils/github.ts +++ b/packages/ci-context/src/lib/utils/github.ts @@ -1,22 +1,31 @@ import * as github from '@actions/github'; import { Context } from '@actions/github/lib/context'; -import { RepoMetadata, RunnerContext } from '../interfaces'; +import { logger } from '@nx-tools/core'; +import { Payload, RepoMetadata, RunnerContext } from '../interfaces'; export class Github { public static async context(): Promise { const ctx = new Context(); const { actor, eventName, job, ref, runId, runNumber, sha, serverUrl, payload } = ctx; + let repoUrl = ''; + + try { + repoUrl = `${serverUrl}/${ctx.repo.owner}/${ctx.repo.repo}`; + } catch (err) { + logger.warn(err); + } + return { name: 'GITHUB', actor, eventName, job, - payload, + payload: payload as unknown as Payload, ref, runId, runNumber, - repoUrl: `${serverUrl}/${ctx.repo.owner}/${ctx.repo.repo}`, + repoUrl, sha, }; } diff --git a/packages/ci-context/src/lib/utils/gitlab.spec.ts b/packages/ci-context/src/lib/utils/gitlab.spec.ts index 206527a1..6bc149c3 100644 --- a/packages/ci-context/src/lib/utils/gitlab.spec.ts +++ b/packages/ci-context/src/lib/utils/gitlab.spec.ts @@ -22,6 +22,7 @@ describe('GitLab Context', () => { CI_DEFAULT_BRANCH: 'main', CI_PROJECT_URL: 'https://gitlab.com/gperdomor/nx-tools', CI_PROJECT_NAME: 'nx-tools', + CI_PROJECT_VISIBILITY: 'public', }, { clear: true } ); @@ -41,7 +42,12 @@ describe('GitLab Context', () => { actor: 'gitlab-actor', eventName: 'gitlab-event-name', job: 'gitlab-job', - payload: {}, + payload: { + repository: { + default_branch: 'main', + private: false, + }, + }, ref: 'refs/heads/gitlab-ref-slug', runId: 100, runNumber: 10, @@ -56,6 +62,7 @@ describe('GitLab Context', () => { beforeEach(() => { restore = mockedEnv({ CI_COMMIT_TAG: 'gitlab-tag', + CI_PROJECT_VISIBILITY: 'private', }); }); @@ -71,7 +78,12 @@ describe('GitLab Context', () => { actor: 'gitlab-actor', eventName: 'gitlab-event-name', job: 'gitlab-job', - payload: {}, + payload: { + repository: { + default_branch: 'main', + private: true, + }, + }, ref: 'refs/tags/gitlab-tag', runId: 100, runNumber: 10, diff --git a/packages/ci-context/src/lib/utils/gitlab.ts b/packages/ci-context/src/lib/utils/gitlab.ts index 193a8135..82367ea1 100644 --- a/packages/ci-context/src/lib/utils/gitlab.ts +++ b/packages/ci-context/src/lib/utils/gitlab.ts @@ -8,7 +8,12 @@ export class Gitlab { actor: process.env['GITLAB_USER_LOGIN']!, eventName: process.env['CI_PIPELINE_SOURCE']!, job: process.env['CI_JOB_NAME']!, - payload: {}, + payload: { + repository: { + default_branch: process.env['CI_DEFAULT_BRANCH']!, + private: process.env['CI_PROJECT_VISIBILITY'] === 'private', + }, + }, ref: process.env['CI_COMMIT_TAG'] ? `refs/tags/${process.env['CI_COMMIT_TAG']}` : `refs/heads/${process.env['CI_COMMIT_REF_SLUG']}`,