Skip to content

Commit

Permalink
PullRequestCreated: Enable empty jira-project (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored Dec 19, 2024
1 parent f300f9a commit c1e59e3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
49 changes: 36 additions & 13 deletions PullRequestCreated/PullRequestCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ class PullRequestCreated extends OctokitAction {
}

private async processNewJiraIssue(pr: PullRequest, newTitle: string): Promise<string> {
const projectKey = this.getInput('jira-project');
const additionalFields = this.parseAdditionalFields();
const parameters = await this.newIssueParameters(projectKey, pr, additionalFields.issuetype?.name ?? 'Task'); // Transfer issuetype name manually, because parameters should have priority due to Sub-task.
const issueId = await this.jira.createIssue(projectKey, pr.title, { ...additionalFields, ...parameters });
if (issueId == null) {
this.setFailed('Failed to create a new issue in Jira');
} else {
const issueId = await this.createJiraIssue(pr);
if (issueId) {
newTitle = `${issueId} ${newTitle}`;
await this.addLinkedIssuesToDescription(pr, [issueId]);
await this.jira.moveIssue(issueId, 'Commit'); // OPEN -> TO DO
Expand All @@ -52,6 +47,35 @@ class PullRequestCreated extends OctokitAction {
return newTitle;
}

private async createJiraIssue(pr: PullRequest): Promise<string> {
const additionalFields = this.parseAdditionalFields();
const parent = await this.findNonSubTaskParent(this.findMentionedIssues(pr));
const projectKey = this.projectKey(parent);
if (projectKey) {
const parameters = await this.newIssueParameters(projectKey, parent, additionalFields.issuetype?.name ?? 'Task'); // Transfer issuetype name manually, because parameters should have priority due to Sub-task.
const issueId = await this.jira.createIssue(projectKey, pr.title, { ...additionalFields, ...parameters });
if (issueId == null) {
this.setFailed('Failed to create a new issue in Jira');
}
return issueId;
} else {
this.log('No suitable project key found, issue will not be created');
return null;
}
}

private projectKey(parent: any): string {
const projectKey = this.getInput('jira-project');
// If projectKey is not defined (like in rspec), we want only to create only Sub-tasks in other tasks (not Epics).
if (projectKey) {
return projectKey;
} else if (parent && !["Epic", "Sub-task"].includes(parent.fields.issuetype.name)) {
return parent.fields.project.key;
} else {
return null;
}
}

private parseAdditionalFields(): any {
const inputAdditionFields = this.getInput('additional-fields');
if (inputAdditionFields) {
Expand All @@ -64,11 +88,7 @@ class PullRequestCreated extends OctokitAction {
return {};
}

private async newIssueParameters(projectKey: string, pr: PullRequest, issueType: string): Promise<IssueParameters> {
const mentionedIssues = this.findMentionedIssues(pr);
console.log('Looking for a non-Sub-task ticket');
const parent = await this.firstNonSubTask(mentionedIssues);
console.log(parent ? `Parent issue: ${parent?.key} (${parent?.fields.issuetype.name})` : 'No parent issue found');
private async newIssueParameters(projectKey: string, parent: any, issueType: string): Promise<IssueParameters> {
switch (parent?.fields.issuetype.name) {
case 'Epic':
return { issuetype: { name: issueType }, parent: { key: parent.key } };
Expand All @@ -88,13 +108,16 @@ class PullRequestCreated extends OctokitAction {
await this.updatePullRequestDescription(pr.number, `${linkedIssues.map(x => this.issueLink(x)).join('\n')}\n\n${pr.body || ''}`);
}

private async firstNonSubTask(issues: Set<string>): Promise<any> {
private async findNonSubTaskParent(issues: Set<string>): Promise<any> {
console.log('Looking for a non-Sub-task ticket');
for (const issueKey of issues) {
const issue = await this.jira.getIssue(issueKey);
if (issue?.fields.issuetype.name !== 'Sub-task') {
console.log(`Parent issue: ${issue.key} ${issue.fields.issuetype.name}`);
return issue;
}
}
console.log('No parent issue found');
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion PullRequestCreated/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Token to access the Jira API.

### `jira-project`

Jira project key that is used to create new issues.
Jira project key that is used to create new issues. If key is not specified, the action will not create tasks. It will only create a sub-task, if a parent issue is mentioned in the description.

### `additional-fields`

Expand Down
2 changes: 1 addition & 1 deletion PullRequestCreated/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inputs:
required: true
jira-project:
description: 'Jira project key'
required: true
required: false
additional-fields:
description: 'Additional fields to be added to the Jira issue'
required: false
Expand Down
51 changes: 37 additions & 14 deletions dist/PullRequestCreated/PullRequestCreated.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/PullRequestCreated/PullRequestCreated.js.map

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

0 comments on commit c1e59e3

Please sign in to comment.