Skip to content

Commit

Permalink
feat: add circle pr number
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfez committed Jan 2, 2023
1 parent 61c455d commit 4512cd9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/core/src/ci-environment/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import envCi from "env-ci";
import heroku from "./services/heroku";
import githubActions from "./services/github-actions";
import type { CiEnvironment, Options } from "./types";
import circleci from "./services/circleci";
import type { CiEnvironment, Options, Context } from "./types";

export { CiEnvironment };

const services = [heroku, githubActions];
const services = [heroku, githubActions, circleci];

export const getCiEnvironment = ({
env = process.env,
}: Options = {}): CiEnvironment | null => {
const ctx = { env };
const service = services.find((service) => service.detect(ctx));

// Internal service matched
if (service) {
return service.config(ctx);
}

// Fallback on env-ci detection
export const envCiDetection = (ctx: Context) => {
const ciContext = envCi(ctx);
const name = ciContext.isCi
? ciContext.name ?? null
Expand All @@ -38,3 +28,17 @@ export const getCiEnvironment = ({
? { name, commit, branch, owner, repository, jobId, runId, prNumber }
: null;
};

export const getCiEnvironment = ({
env = process.env,
}: Options = {}): CiEnvironment | null => {
const ctx = { env };
const service = services.find((service) => service.detect(ctx));

// Internal service matched
if (service) {
return service.config(ctx);
}

return envCiDetection(ctx);
};
32 changes: 32 additions & 0 deletions packages/core/src/ci-environment/services/circleci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Service, Context } from "../types";
import { envCiDetection } from "../index";

const getPrNumber = ({ env }: Context) => {
const branchRegex = /pull\/(\d+)/;
const branchMatches = branchRegex.exec(env.CIRCLE_PULL_REQUEST || "");
if (branchMatches) {
branchMatches[1];
}

return null;
};

const service: Service = {
detect: ({ env }) => Boolean(env.CIRCLECI),
config: ({ env }) => {
const ciProps = envCiDetection({ env });

return {
name: "CircleCI",
commit: ciProps?.commit || null,
branch: ciProps?.branch || null,
owner: ciProps?.owner || null,
repository: ciProps?.repository || null,
jobId: ciProps?.jobId || null,
runId: ciProps?.runId || null,
prNumber: getPrNumber({ env }),
};
},
};

export default service;

0 comments on commit 4512cd9

Please sign in to comment.