Skip to content

Commit

Permalink
Merge pull request #1 from urcomputeringpal/jnewland-patch-4
Browse files Browse the repository at this point in the history
initial version
  • Loading branch information
jnewland committed May 13, 2023
2 parents dd801a7 + ca14d8d commit 3227e1b
Show file tree
Hide file tree
Showing 20 changed files with 5,307 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .github/fixtures/pull_request/main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"act": "true",
"pull_request": {
"number": 1,
"head": {
"ref": "jnewland-patch-4"
},
"base": {
"ref": "main"
}
}
}
7 changes: 7 additions & 0 deletions .github/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: {
"@actions/core": "<rootDir>/src/__mocks__/core.ts",
},
};
4,684 changes: 4,684 additions & 0 deletions .github/package-lock.json

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions .github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "ts-actions",
"version": "1.0.0",
"description": "TypeScript actions for the GitHub workflow",
"private": true,
"scripts": {
"format": "prettier --write src",
"format:check": "prettier --check src",
"build": "tsc",
"test": "jest",
"integration": "./test.sh"
},
"dependencies": {
"@actions/core": "1.6.0",
"@actions/github": "5.0.0",
"@octokit/rest": "18.12.0"
},
"devDependencies": {
"@types/jest": "29.5.1",
"@types/node": "16.11.9",
"jest": "29.5.0",
"prettier": "2.8.4",
"ts-jest": "29.1.0",
"ts-loader": "9.4.2",
"typescript": "4.5.2"
},
"importSort": {
".ts": {
"style": "module"
}
}
}
33 changes: 33 additions & 0 deletions .github/src/__mocks__/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as core from "@actions/core";

export const getInput: jest.MockedFunction<typeof core.getInput> = jest.fn();
export const setOutput: jest.MockedFunction<typeof core.setOutput> = jest.fn();
export const setFailed: jest.MockedFunction<typeof core.setFailed> = jest.fn();
export const info: jest.MockedFunction<typeof core.info> = jest.fn();
export const exportVariable: jest.MockedFunction<typeof core.exportVariable> = jest.fn();
export const setSecret: jest.MockedFunction<typeof core.setSecret> = jest.fn();
export const addPath: jest.MockedFunction<typeof core.addPath> = jest.fn();
export const getMultilineInput: jest.MockedFunction<typeof core.getMultilineInput> = jest.fn();
export const getBooleanInput: jest.MockedFunction<typeof core.getBooleanInput> = jest.fn();
export const debug: jest.MockedFunction<typeof core.debug> = jest.fn();
export const startGroup: jest.MockedFunction<typeof core.startGroup> = jest.fn();
export const endGroup: jest.MockedFunction<typeof core.endGroup> = jest.fn();
export const group: jest.MockedFunction<typeof core.group> = jest.fn();
export const saveState: jest.MockedFunction<typeof core.saveState> = jest.fn();
export const getState: jest.MockedFunction<typeof core.getState> = jest.fn();
export const setCommandEcho: jest.MockedFunction<typeof core.setCommandEcho> = jest.fn();
export const isDebug: jest.MockedFunction<typeof core.isDebug> = jest.fn();
export const error: jest.MockedFunction<typeof core.error> = jest.fn();
export const warning: jest.MockedFunction<typeof core.warning> = jest.fn();
export const getIDToken: jest.MockedFunction<typeof core.getIDToken> = jest.fn();
export const notice: jest.MockedFunction<typeof core.notice> = jest.fn();
export declare enum ExitCode {
/**
* A code indicating that the action was successful
*/
Success = 0,
/**
* A code indicating that the action was a failure
*/
Failure = 1,
}
78 changes: 78 additions & 0 deletions .github/src/getLabel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { GitHubScriptArguments } from "./types";
import { getLabel } from "./getLabel";
import { Context } from "@actions/github/lib/context";
import * as core from "./__mocks__/core";
import { Octokit } from "@octokit/rest";

jest.mock("@octokit/rest", () => {
const Octokit = class MockOctokit {
rest = {
issues: {
listLabelsOnIssue: jest.fn().mockReturnValue({
data: [{ name: "Test" }],
}),
},
};
};

return { Octokit };
});

describe("getLabel", () => {
const originalEnv = process.env;

beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
};
});

afterAll(() => {
process.env = originalEnv;
});

it("should get the provided label", async () => {
process.env.PR_NUMBER = "1234";
process.env.LABEL = "Test";

const context: Context = {
payload: {
workflow_dispatch: {
ref: "refs/heads/test",
},
},
issue: {
number: 0,
owner: "",
repo: "",
},
repo: {
owner: "",
repo: "",
},
eventName: "",
sha: "",
ref: "",
workflow: "",
action: "",
actor: "",
job: "",
runNumber: 0,
runId: 0,
apiUrl: "",
serverUrl: "",
graphqlUrl: "",
};

const args: GitHubScriptArguments = {
github: new Octokit(),
context: context,
core: core,
};

const label = await getLabel(args);

expect(label).toEqual("Test");
});
});
31 changes: 31 additions & 0 deletions .github/src/getLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GitHubScriptArguments } from "./types";

export async function getLabel(args: GitHubScriptArguments): Promise<String> {
const { github, context, core } = args;
if (github === undefined || context === undefined || core === undefined) {
throw new Error("Need github");
}

if (process.env.PR_NUMBER === undefined || process.env.PR_NUMBER === "") {
throw new Error("Need PR_NUMBER env variable");
}
console.log(`PR number: ${process.env.PR_NUMBER}`);

if (process.env.LABEL === undefined) {
throw new Error("Need LABEL env variable");
}

const response = await github.rest.issues.listLabelsOnIssue({
...context.repo,
issue_number: parseInt(process.env.PR_NUMBER),
});
const labels = response.data.filter(label => label.name.toLowerCase() == process.env.LABEL!.toLowerCase());

if (labels.length == 0) {
console.log(`No matching label`);
return "";
} else {
console.log(`Found matching label`);
return labels[0].name;
}
}
2 changes: 2 additions & 0 deletions .github/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { prNumber } from "./prNumber";
export { getLabel } from "./getLabel";
100 changes: 100 additions & 0 deletions .github/src/prNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { GitHubScriptArguments } from "./types";
import { prNumber } from "./prNumber";
import { Context } from "@actions/github/lib/context";
import * as core from "./__mocks__/core";
import { Octokit } from "@octokit/rest";

jest.mock("@octokit/rest", () => {
const Octokit = class MockOctokit {
rest = {
pulls: {
list: jest.fn().mockReturnValue({
data: [{ number: 5678 }],
}),
},
};
};

return { Octokit };
});

describe("prNumber", () => {
it("should set the prNumber from the context", async () => {
const context: Context = {
payload: {
pull_request: {
number: 1234,
},
},
issue: {
number: 1234,
owner: "",
repo: "",
},
repo: {
owner: "",
repo: "",
},
eventName: "",
sha: "",
ref: "",
workflow: "",
action: "",
actor: "",
job: "",
runNumber: 0,
runId: 0,
apiUrl: "",
serverUrl: "",
graphqlUrl: "",
};

const args: GitHubScriptArguments = {
context: context,
core: core,
};

const stringNumber = await prNumber(args);

expect(stringNumber).toEqual("1234");
});

it("or from the API if not present", async () => {
const context: Context = {
payload: {
pull_request: undefined,
},
issue: {
number: 0,
owner: "",
repo: "",
},
repo: {
owner: "",
repo: "",
},
eventName: "",
sha: "",
ref: "",
workflow: "",
action: "",
actor: "",
job: "",
runNumber: 0,
runId: 0,
apiUrl: "",
serverUrl: "",
graphqlUrl: "",
};

const args: GitHubScriptArguments = {
context: context,
github: new Octokit(),
core: core,
};

const stringNumber = await prNumber(args);

expect(stringNumber).toEqual("5678");
});
});
34 changes: 34 additions & 0 deletions .github/src/prNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { GitHubScriptArguments } from "./types";

export async function prNumber(args: GitHubScriptArguments): Promise<String> {
const { github, context, core } = args;
if (context === undefined || core === undefined) {
throw new Error("Need core and context to run this script");
}

if (context.payload.pull_request !== undefined) {
console.log(`PR number: ${context.payload.pull_request.number}`);
return `${context.payload.pull_request.number}`;
}

if (github === undefined) {
throw new Error("Need github");
}

const branchName = context.ref.replace("refs/heads/", "");

const prs = await github.rest.pulls.list({
...context.repo,
state: "open",
head: `${context.repo.owner}:${branchName}`,
});

if (prs.data.length === 0) {
console.log(`No PR found for branch ${branchName}`);
process.exit(1);
}

const pr = prs.data[0];
console.log(`Found PR number: ${pr.number}`);
return `${pr.number}`;
}
9 changes: 9 additions & 0 deletions .github/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as core from "@actions/core";
import { Context } from "@actions/github/lib/context";
import { Octokit } from "@octokit/rest";

export interface GitHubScriptArguments {
github?: Octokit;
context?: Context;
core?: typeof core;
}
16 changes: 16 additions & 0 deletions .github/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

gh extension install https://github.com/nektos/gh-act || gh extension upgrade nektos/gh-act || true

cd ${SCRIPT_DIR}/..

gh act -l

gh act pull_request \
-s GITHUB_TOKEN=${GITHUB_TOKEN} \
-W .github/workflows/test.yaml \
-e .github/fixtures/pull_request/main.json
9 changes: 9 additions & 0 deletions .github/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES5",
"strict": true,
"outDir": "dist"
},
"include": ["src/*.ts"]
}
Loading

0 comments on commit 3227e1b

Please sign in to comment.