Skip to content

Commit

Permalink
feat(ci-context): improve context for github, gitlab and drone
Browse files Browse the repository at this point in the history
  • Loading branch information
gperdomor committed Oct 15, 2023
1 parent e99e749 commit 5909b45
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
19 changes: 18 additions & 1 deletion packages/ci-context/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
payload: Partial<Payload>;
ref: string;
runId: number;
runNumber: number;
Expand Down
13 changes: 11 additions & 2 deletions packages/ci-context/src/lib/utils/drone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -54,6 +58,7 @@ describe('Drone Context', () => {
beforeEach(() => {
restore = mockedEnv({
DRONE_COMMIT_REF: 'refs/tags/drone-v1.0.0',
DRONE_REPO_PRIVATE: 'true',
});
});

Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion packages/ci-context/src/lib/utils/drone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
15 changes: 12 additions & 3 deletions packages/ci-context/src/lib/utils/github.ts
Original file line number Diff line number Diff line change
@@ -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<RunnerContext> {
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,
};
}
Expand Down
16 changes: 14 additions & 2 deletions packages/ci-context/src/lib/utils/gitlab.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
);
Expand All @@ -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,
Expand All @@ -56,6 +62,7 @@ describe('GitLab Context', () => {
beforeEach(() => {
restore = mockedEnv({
CI_COMMIT_TAG: 'gitlab-tag',
CI_PROJECT_VISIBILITY: 'private',
});
});

Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion packages/ci-context/src/lib/utils/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']}`,
Expand Down

0 comments on commit 5909b45

Please sign in to comment.