Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add github_token input #16

Merged
merged 1 commit into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ jobs:
with:
# Optional. Post a issue comment just before closing a pull request.
comment: "We do not accept PRs. If you have any questions, please feel free to contact us."
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## Inputs
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ inputs:
comment:
description: "Post a issue comment just before closing a pull request."
required: false
github_token:
description: "Token used to close a pull request and create an issue comment"
required: false
default: ${{ github.token }}
runs:
using: "node12"
main: "dist/index.js"
Expand Down
9 changes: 6 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4039,15 +4039,18 @@ const run = async () => {
throw errors.ignoreEvent;
}

const token = process.env["GITHUB_TOKEN"] || "";
let token = process.env["GITHUB_TOKEN"] || "";

if (token === "") {
throw errors.noToken;
token = core.getInput("github_token");
} else {
core.warning("GITHUB_TOKEN environment variable is deprecated.");
core.warning("GitHub Token is passed automatically, so no longer needs to be set.");
}

const client = new github.GitHub(token); // *Optional*. Post an issue comment just before closing a pull request.

const body = core.getInput("comment");
const body = core.getInput("comment") || "";

if (body.length > 0) {
core.info("Creating a comment");
Expand Down
11 changes: 8 additions & 3 deletions src/close-pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ export const run = async () => {
throw errors.ignoreEvent;
}

const token = process.env["GITHUB_TOKEN"] || "";
let token = process.env["GITHUB_TOKEN"] || "";
if (token === "") {
throw errors.noToken;
token = core.getInput("github_token");
} else {
core.warning("GITHUB_TOKEN environment variable is deprecated.");
core.warning(
"GitHub Token is passed automatically, so no longer needs to be set."
);
}

const client = new github.GitHub(token);

// *Optional*. Post an issue comment just before closing a pull request.
const body = core.getInput("comment");
const body = core.getInput("comment") || "";
if (body.length > 0) {
core.info("Creating a comment");
await client.issues.createComment({
Expand Down
37 changes: 27 additions & 10 deletions tests/close-pull-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import * as errors from "../src/errors";
describe("Close Pull Request", () => {
let update;
let createComment;
let inputs;

beforeEach(() => {
process.env.GITHUB_TOKEN = "token";
inputs = { github_token: "token" };
core.getInput = jest.fn().mockImplementation(name => {
return inputs[name];
});

update = jest.fn().mockResolvedValue();
createComment = jest.fn().mockResolvedValue();
Expand Down Expand Up @@ -39,6 +43,10 @@ describe("Close Pull Request", () => {
GitHub.mockImplementation(() => github);
});

afterEach(() => {
jest.clearAllMocks();
});

it("should update a pull request", async () => {
await run();

Expand All @@ -59,26 +67,35 @@ describe("Close Pull Request", () => {
});
});

describe("when GITHUB_TOKEN env variable is not set", () => {
describe("when GITHUB_TOKEN env variable is set", () => {
let warnSpy;

beforeEach(() => {
process.env.GITHUB_TOKEN = "token";
warnSpy = jest.spyOn(core, "warning");
});

afterEach(() => {
delete process.env.GITHUB_TOKEN;
});

it("should throw 'no token' error", async () => {
await expect(run()).rejects.toEqual(errors.noToken);
await run();

expect(warnSpy).toHaveBeenCalled();
expect(update).toHaveBeenCalledWith({
...context.repo,
pull_number: context.issue.number,
state: "closed"
});
});
});

describe("when 'comment' input is passed", () => {
let comment = "comment";
const comment = "comment";

beforeEach(() => {
core.getInput = jest.fn().mockImplementation(name => {
if (name === "comment") {
return comment;
}
return "";
});
inputs["comment"] = comment;
});

it("should create a comment", async () => {
Expand Down