Skip to content

Commit

Permalink
create-issue: add action
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongRuoyu committed Sep 18, 2024
1 parent c4fb4dc commit 87995b6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
18 changes: 18 additions & 0 deletions create-issue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Create Issue

A very simple action to create an issue in a repository.

Intended for internal use only.

## Usage

```yaml
- uses: Homebrew/actions/create-issue@master
with:
token: ${{ github.token }} # defaults to this
repository: ${{ github.repository }} # defaults to this
title: Issue title
body: Issue body
labels: label1,label2
assignees: user1,user2
```
30 changes: 30 additions & 0 deletions create-issue/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Create issue
description: Create an issue in a repository
author: ZhongRuoyu
branding:
icon: alert-circle
color: blue
inputs:
token:
description: GitHub token
required: false
default: ${{github.token}}
repository:
description: Repository to send the issue to
required: false
default: ${{github.repository}}
title:
description: The title of the issue
required: true
body:
description: The body of the issue
required: true
labels:
description: Comma-separated list of labels to add to the issue
required: false
assignees:
description: Comma-separated list of users to assign the issue to
required: false
runs:
using: node20
main: main.mjs
40 changes: 40 additions & 0 deletions create-issue/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import core from "@actions/core";
import github from "@actions/github";

async function main() {
try {
const token = core.getInput("token", { required: true });
const [owner, repo] = core.getInput("repository", { required: true }).split("/");

const title = core.getInput("title", { required: true });
const body = core.getInput("body", { required: true });

const labelsInput = core.getInput("labels");
const labels = labelsInput ? labelsInput.split(",") : [];
const assigneesInput = core.getInput("assignees");
const assignees = assigneesInput ? assigneesInput.split(",") : [];

const client = github.getOctokit(token);

const response = await client.rest.issues.create({
owner,
repo,
title,
body,
labels,
assignees,
});
const issueNumber = response.data.number;
const issueNodeId = response.data.node_id;
const issueUrl = response.data.html_url;

core.info(`Issue created: ${issueUrl}`);

core.setOutput("number", issueNumber);
core.setOutput("node_id", issueNodeId);
} catch (error) {
core.setFailed(error);
}
}

await main();
40 changes: 40 additions & 0 deletions create-issue/main.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import util from "node:util";

test("create-issue", async () => {
const mockPool = githubMockPool();

const token = "fake-token";
const title = "Issue title";
const body = "Issue body.\nLorem ipsum dolor sit amet.";
const labels = "label1,label2";
const assignees = "assignee1,assignee2";

const issueNumber = 12345;

mockInput("token", token);
mockInput("repository", GITHUB_REPOSITORY);
mockInput("title", title);
mockInput("body", body);
mockInput("labels", labels);
mockInput("assignees", assignees);

mockPool.intercept({
method: "POST",
path: `/repos/${GITHUB_REPOSITORY}/issues`,
headers: {
Authorization: `token ${token}`,
},
body: (htmlBody) => util.isDeepStrictEqual(JSON.parse(htmlBody), {
title,
body,
labels: labels.split(","),
assignees: assignees.split(","),
}),
}).defaultReplyHeaders({
"Content-Type": "application/json",
}).reply(200, {
number: issueNumber
});

await loadMain();
});

0 comments on commit 87995b6

Please sign in to comment.