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

Extended Issue Cloning Features #2

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Clone issue to another repository

Clone an issue to a predefined repository when labeled with a specific label
This action clones an issue from the current repository to another repository when a specified label is present on the issue.

## Inputs

Expand All @@ -10,18 +10,35 @@ Clone an issue to a predefined repository when labeled with a specific label

### `targetRepo`

**Required** The repository in which to clone the issue.
**Required** The repository to clone the issue to, in the format `owner/repo`.

### `label`

**Optional** The label on which to react. Default `clone`.

## Example usage
### `prefix`
**Optional** A prefix to append to the title of the cloned issue. Default is an empty string.

### `labels`
**Optional** A comma-separated list of labels to add to the cloned issue. Default is an empty string.

### `asignees`
**Optional** A comma-separated list of assignees to add to the cloned issue. Default is an empty string.

## Outputs

### `issue_url`
The URL of the cloned issue in the target repository, which can be accessed with ${{ steps.<step-id>.outputs.issue_url }} in your workflow file.

## Example Usage

```yml
uses: dpanayotov/issue-cloner@v0.2
uses: dpanayotov/issue-cloner@v0.3
with:
label: "clone"
targetRepo: myorg/myrepo
token: ${{ secrets.CLONE_ISSUE_TOKEN }}
```
prefix: 'CLONED:'
labels: 'bug, needs-triage'
asignees: 'alice,bob'
```
38 changes: 32 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ async function start(){
const targetRepo = core.getInput('targetRepo', {required: true});
const ghToken = core.getInput('token', {required: true});

const prefix = core.getInput('prefix');
const labels = core.getInput('labels').split(',').map(label => label.trim());
const assignees = core.getInput('assignees').split(',').map(assignee => assignee.trim());

const octokit = new github.getOctokit(ghToken);
const originalIssue = await getOriginalIssue(octokit);

if (!hasLabel(label, originalIssue)){
console.log(`Label ${label} not present. Will not copy issue`)
console.log(`Label ${label} not present. Will not copy issue`);
return;
}
const clonedIssue = await cloneIssue(octokit, targetRepo, originalIssue)

await addComment(octokit, originalIssue, clonedIssue)
const clonedIssue = await cloneIssue(octokit, targetRepo, originalIssue, prefix, labels, assignees);

await addComment(octokit, originalIssue, clonedIssue);

core.setOutput('issue_url', clonedIssue.data.html_url);

console.log(`Issue cloned successfully`);
} catch (error) {
Expand All @@ -41,7 +48,7 @@ async function getOriginalIssue(octokit) {
return issue;
}

async function cloneIssue(octokit, targetRepo, original){
async function cloneIssue(octokit, targetRepo, original, prefix, labels, assignees) {
const splitted = targetRepo.split('/');
const owner = splitted[0];
const repoName = splitted[1];
Expand All @@ -54,13 +61,32 @@ async function cloneIssue(octokit, targetRepo, original){

body = `Issue cloned from ${original.data.html_url}\n\n${body}`;

const title = original.data.title;
const title = !!prefix ? `${prefix} ${original.data.title}` : original.data.title;
const result = await octokit.rest.issues.create({
owner: owner,
repo: repoName,
body: body,
title: title
});

if (labels.length > 0) {
await octokit.rest.issues.addLabels({
owner: owner,
repo: repoName,
issue_number: result.data.number,
labels: labels
});
}

if (assignees.length > 0) {
await octokit.rest.issues.addAssignees({
owner: owner,
repo: repoName,
issue_number: result.data.number,
assignees: assignees
});
}

return result;
}

Expand All @@ -82,4 +108,4 @@ function hasLabel(label, issue){
}
}
return false;
}
}