Skip to content

Commit

Permalink
Feature: Added issue_number to update existing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSim93 committed Dec 26, 2023
1 parent e123c73 commit 948f891
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 61 deletions.
53 changes: 27 additions & 26 deletions README.md

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ inputs:
description: "Timezone to use in action"
required: false
EXECUTION_OUTCOME:
description: "Outcome format separated by comma. Can take values: 'markdown', 'new-issue', 'collection'"
description: "Outcome format separated by comma. Can take values: 'markdown', 'new-issue', 'collection', 'existing-issue'"
required: false
default: "new-issue"
ISSUE_NUMBER:
description: "Issue number"
required: false

outputs:
JSON_COLLECTION:
Expand Down
94 changes: 79 additions & 15 deletions build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pull-request-analytics-action",
"version": "1.10.0",
"version": "1.11.0",
"description": "Generates detailed PR analytics reports within GitHub, focusing on review efficiency and team performance.",
"main": "build/index.js",
"scripts": {
Expand Down
14 changes: 13 additions & 1 deletion src/common/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export const validate = () => {
required: false,
},
EXECUTION_OUTCOME: {
validValues: ["new-issue", "output", "collection", "markdown"],
validValues: [
"new-issue",
"output",
"collection",
"markdown",
"existing-issue",
],
required: true,
},
});
Expand All @@ -48,6 +54,12 @@ export const validate = () => {
getMultipleValuesInput("AGGREGATE_VALUE_METHODS").length === 1 &&
getMultipleValuesInput("AGGREGATE_VALUE_METHODS")[0] === "percentile",
},
ISSUE_NUMBER: {
min: 1,
isCritical:
getMultipleValuesInput("EXECUTION_OUTCOME").length === 1 &&
getMultipleValuesInput("EXECUTION_OUTCOME")[0] === "existing-issue",
},
TOP_LIST_AMOUNT: { min: 0, isCritical: false },
});

Expand Down
18 changes: 18 additions & 0 deletions src/requests/clearComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getValueAsIs } from "../common/utils";
import { octokit } from "../octokit/octokit";

export const clearComments = async (issueNumber?: string) => {
if (!issueNumber) return;
const comments = await octokit.rest.issues.listComments({
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
issue_number: parseInt(issueNumber),
});
for (let comment of comments.data) {
await octokit.rest.issues.deleteComment({
repo: getValueAsIs("GITHUB_REPO_FOR_ISSUE"),
owner: getValueAsIs("GITHUB_OWNER_FOR_ISSUE"),
comment_id: comment.id,
});
}
};
44 changes: 31 additions & 13 deletions src/requests/createIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { octokit } from "../octokit/octokit";
import { format } from "date-fns";
import { getMultipleValuesInput } from "../common/utils";

export const createIssue = async (markdown: string) => {
export const createIssue = async (markdown: string, issueNumber?: string) => {
const issueTitle =
core.getInput("ISSUE_TITLE") ||
process.env.ISSUE_TITLE ||
Expand All @@ -17,17 +17,35 @@ export const createIssue = async (markdown: string) => {
(assignee) => assignee && typeof assignee === "string"
) || [];

const result = await octokit.rest.issues.create({
repo:
core.getInput("GITHUB_REPO_FOR_ISSUE") ||
process.env.GITHUB_REPO_FOR_ISSUE!,
owner:
core.getInput("GITHUB_OWNER_FOR_ISSUE") ||
process.env.GITHUB_OWNER_FOR_ISSUE!,
title: issueTitle,
body: markdown,
labels,
assignees,
});
let result;
if (issueNumber) {
result = await octokit.rest.issues.update({
labels,
title: issueTitle,
assignees,
repo:
core.getInput("GITHUB_REPO_FOR_ISSUE") ||
process.env.GITHUB_REPO_FOR_ISSUE!,
owner:
core.getInput("GITHUB_OWNER_FOR_ISSUE") ||
process.env.GITHUB_OWNER_FOR_ISSUE!,
body: markdown,
issue_number: parseInt(issueNumber),
});
} else {
result = await octokit.rest.issues.create({
repo:
core.getInput("GITHUB_REPO_FOR_ISSUE") ||
process.env.GITHUB_REPO_FOR_ISSUE!,
owner:
core.getInput("GITHUB_OWNER_FOR_ISSUE") ||
process.env.GITHUB_OWNER_FOR_ISSUE!,
title: issueTitle,
body: markdown,
labels,
assignees,
});
}

return result;
};
1 change: 1 addition & 0 deletions src/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { clearComments } from './clearComments';
export { getOwnersRepositories } from "./utils/getOwnersRepositories";
export { getOrganizationsRepositories } from "./getOrganizationsRepositories";
export { makeComplexRequest } from "./makeComplexRequest";
Expand Down
13 changes: 9 additions & 4 deletions src/view/createOutput.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as core from "@actions/core";
import { getMultipleValuesInput } from "../common/utils";
import { getMultipleValuesInput, getValueAsIs } from "../common/utils";
import { Collection } from "../converters/types";
import { createMarkdown } from "./createMarkdown";
import { createIssue } from "../requests";
import { clearComments, createIssue } from "../requests";
import {
createTimelineMonthComparisonChart,
getDisplayUserList,
Expand All @@ -18,14 +18,19 @@ export const createOutput = async (
const users = getDisplayUserList(data);
const dates = sortCollectionsByDate(data.total);

if (outcome === "new-issue") {
if (outcome === "new-issue" || outcome === "existing-issue") {
const issueNumber =
outcome === "existing-issue" ? getValueAsIs("ISSUE_NUMBER") : undefined;
const markdown = createMarkdown(
data,
users,
["total"],
"Pull Request report total"
);
const issue = await createIssue(markdown);
if (outcome.includes("existing-issue")) {
await clearComments(issueNumber);
}
const issue = await createIssue(markdown, issueNumber);
const monthComparison = createTimelineMonthComparisonChart(
data,
dates,
Expand Down
3 changes: 3 additions & 0 deletions src/view/utils/createConfigParamsCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ EXCLUDE_LABELS: ${process.env.EXCLUDE_LABELS || core.getInput("EXCLUDE_LABELS")}
EXECUTION_OUTCOME: ${
process.env.EXECUTION_OUTCOME || core.getInput("EXECUTION_OUTCOME")
}
ISSUE_NUMBER: ${
process.env.ISSUE_NUMBER || core.getInput("ISSUE_NUMBER")
}
\`\`\`
`;
};

0 comments on commit 948f891

Please sign in to comment.