Skip to content

Commit

Permalink
Merge pull request #10 from aleksandarcolic88/feature/add-ignore-option
Browse files Browse the repository at this point in the history
Add ignore_branches option.
  • Loading branch information
Nora Söderlund authored Oct 17, 2023
2 parents 6eaf4b8 + 2fc1d61 commit 733621d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 75 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
| JIRA_KEY_MULTIPLE | Boolean | false | If true and JIRA_KEY is a project key, post a comment for every story key found. |
| JIRA_PARTIAL_KEY_SILENT_FAILURE | Boolean | false | If true, not finding a story key in a pull request if a project key is specified, only throws a silent error. |
| DISABLE_PULL_REQUEST_COMMENT | Boolean | false | If true, using the action will not create or update a pull request comment. Useful for only fetching the issue details from the output. |
| IGNORE_BRANCHES | Regex | - | If source branch name mathces this regex ignore pull request. |

### Outputs
| Output | Type | Description |
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ inputs:
DISABLE_PULL_REQUEST_COMMENT:
description: If true, using the action will not create or update a pull request comment. Useful for only fetching the issue details from the output.

IGNORE_BRANCHES:
description: If source branch name mathces this regex ignore pull request.

outputs:
title:
description: The title of the Jira story.
Expand Down
33 changes: 20 additions & 13 deletions dist/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 dist/index.js.map

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

130 changes: 69 additions & 61 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import * as adf2md from "adf-to-md";
const octokit = getOctokit(getInput("GITHUB_TOKEN"));

function getDescription(description: any) {
try {
return adf2md.convert(description).result;
}
catch {
return `*No description available.*`;
}
};
try {
return adf2md.convert(description).result;
} catch {
return `*No description available.*`;
}
}

async function execute(storyKey: string) {
console.debug("Getting the story detail from Jira...");
Expand All @@ -23,14 +22,16 @@ async function execute(storyKey: string) {

const description = getDescription(issueDetails.fields.description);

if(getInput("JIRA_KEY_MULTIPLE") !== "") {
if (getInput("JIRA_KEY_MULTIPLE") !== "") {
setOutput("title", issueDetails.fields.summary);
setOutput("description", description);
}

if(context.payload.pull_request) {
if(getInput("DISABLE_PULL_REQUEST_COMMENT") !== "") {
console.info("Not creating or update any comments because DISABLE_PULL_REQUEST_COMMENT is true.");

if (context.payload.pull_request) {
if (getInput("DISABLE_PULL_REQUEST_COMMENT") !== "") {
console.info(
"Not creating or update any comments because DISABLE_PULL_REQUEST_COMMENT is true."
);

return;
}
Expand All @@ -39,121 +40,128 @@ async function execute(storyKey: string) {

const comments = await octokit.rest.issues.listComments({
...context.repo,
issue_number: context.payload.pull_request.number
issue_number: context.payload.pull_request.number,
});

const existingComment = comments.data.find((comment) => {
if(!comment.body)
return false;
if (!comment.body) return false;

const lines = comment.body.split('\n');
const lines = comment.body.split("\n");

if(!lines.length)
return false;
if (!lines.length) return false;

if(!lines[0].startsWith(`## [${issueDetails.key}]`))
return false;
if (!lines[0].startsWith(`## [${issueDetails.key}]`)) return false;

if(!lines[1].startsWith("###"))
return false;
if (!lines[1].startsWith("###")) return false;

return true;
});

const body = [
`## [${issueDetails.key}](${getInput("JIRA_BASE_URL")}/browse/${issueDetails.key})`,
`## [${issueDetails.key}](${getInput("JIRA_BASE_URL")}/browse/${
issueDetails.key
})`,
`### ${issueDetails.fields.summary}`,
description
].join('\n');
description,
].join("\n");

if(existingComment) {
if (existingComment) {
console.debug("Existing comment exists for story.");

if(existingComment.body === body) {
console.info("Skipping updating previous comment because content is the same.");
if (existingComment.body === body) {
console.info(
"Skipping updating previous comment because content is the same."
);

return;
}

await octokit.rest.issues.updateComment({
...context.repo,
comment_id: existingComment.id,
body
body,
});
}
else {
} else {
console.debug("Creating a new comment with story summary...");

await octokit.rest.issues.createComment({
...context.repo,
issue_number: context.payload.pull_request.number,
body
body,
});
}
}
};
}

async function init() {
const jiraKey = getInput("JIRA_KEY");
if(!jiraKey.includes('-')) {
if(!context.payload.pull_request)

if (!jiraKey.includes("-")) {
if (!context.payload.pull_request)
return setFailed("Partial Jira key can only be used in pull requests!");

const pullRequest = await octokit.rest.pulls.get({
...context.repo,
pull_number: context.payload.pull_request.number,
});

const ignore_branches = getInput("IGNORE_BRANCHES");

if (ignore_branches) {

const ignore_pattern = new RegExp(ignore_branches);

const match = ignore_pattern.exec(pullRequest.data.head.ref ?? "");

if (match?.length) {
console.info("Skip adding Jira summary because source branch is to be ignored");
return;
}
}

const inputs = [
pullRequest.data.head.ref,
pullRequest.data.title,
pullRequest.data.body
pullRequest.data.body,
];

const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, 'g');
const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, "g");

const storyKeys: string[] = [];

for(let input of inputs) {
for (let input of inputs) {
const matches = regex.exec(input ?? "");

if(matches?.length) {
if (matches?.length) {
storyKeys.push(matches[0]);

continue;
}
}

if(!storyKeys.length) {
if(getInput("JIRA_PARTIAL_KEY_SILENT_FAILURE") !== "") {
if (!storyKeys.length) {
if (getInput("JIRA_PARTIAL_KEY_SILENT_FAILURE") !== "") {
console.error("Failed to find a Jira key starting with " + jiraKey);

console.info("Executing silent error because JIRA_PARTIAL_KEY_SILENT_FAILURE is true.");
}
else
console.info(
"Executing silent error because JIRA_PARTIAL_KEY_SILENT_FAILURE is true."
);
} else {
setFailed("Failed to find a Jira key starting with " + jiraKey);
}

return;
}

if(getInput("JIRA_KEY_MULTIPLE") !== "") {
for(let storyKey of storyKeys)
execute(storyKey);
}
else
execute(storyKeys[0]);
}
else
execute(jiraKey);
};
if (getInput("JIRA_KEY_MULTIPLE") !== "") {
for (let storyKey of storyKeys) execute(storyKey);
} else execute(storyKeys[0]);
} else execute(jiraKey);
}

try {
init();
}
catch(error) {
if(error instanceof Error || typeof error === "string")
setFailed(error);
else
setFailed("Unknown error: " + error);
} catch (error) {
if (error instanceof Error || typeof error === "string") setFailed(error);
else setFailed("Unknown error: " + error);
}

0 comments on commit 733621d

Please sign in to comment.