Skip to content

Commit

Permalink
Allow configuration of Jira branch names (#16222)
Browse files Browse the repository at this point in the history
* Add config option for custom branch names

* Implement custom branch name logic

* Update changelog

* Switch date to {PR_MERGE_DATE} placeholder

* Fix up types

* Update comment

* Lint

* README

* Update package.json

* Pull contributions

* Set name format to default if unset in settings

* Lint

* Update CHANGELOG.md and optimise images

---------

Co-authored-by: Per Nielsen Tikær <[email protected]>
Co-authored-by: raycastbot <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2025
1 parent fe0717d commit d038b34
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
3 changes: 3 additions & 0 deletions extensions/jira/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Jira Changelog

## [Add ability to configuring Git branch name format] - 2025-01-09
- Implemented the ability to configure the Git branch name format in the extension settings.

## [Add ability to open issues in other application defined in extension preferences] - 2024-12-25
- Implemented the ability to open by default in any other application (e.g. other browser) any issue.

Expand Down
10 changes: 10 additions & 0 deletions extensions/jira/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ The Jira extension supports OAuth authentication by default. But if you want, yo

1. Get an API token as described [here](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/).
2. Select any command from the Jira extension, then press `` + `K` and scroll down to `Configure Extension`. Enter your email, API token, and Jira site URL (without `https://`) on the right. Note that all three must be provided to use API tokens.

## Note

You can change the default `Git branch name` in preferences; the following keys are available:

* {issueKey}
* {issueSummary}
* {issueSummaryShort}
* {issueType}
* {projectKey}
13 changes: 12 additions & 1 deletion extensions/jira/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"horumy",
"santiago_perez",
"silv",
"EyLuismi"
"EyLuismi",
"pernielsentikaer",
"BehnH"
],
"pastContributors": [
"igor9silva"
Expand Down Expand Up @@ -153,6 +155,15 @@
"title": "Open Issues in",
"required": false,
"description": "Select the browser or application to open Issues with."
},
{
"name": "branch_name",
"title": "Git Branch Name",
"description": "The format for copying Git branch names.\nAvailable keys: {issueKey} {issueSummary} {issueSummaryShort} {issueType} {projectKey}",
"type": "textfield",
"required": false,
"default": "{issueKey}-{issueSummary}",
"placeholder": "{issueKey}-{issueSummary}"
}
],
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions extensions/jira/src/components/IssueActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getJiraCredentials } from "../api/jiraCredentials";
import { autocompleteUsers, User } from "../api/users";
import { getUserAvatar } from "../helpers/avatars";
import { getErrorMessage } from "../helpers/errors";
import { slugify } from "../helpers/string";
import { generateBranchName } from "../helpers/issues";

import CreateIssueForm from "./CreateIssueForm";
import IssueAttachments from "./IssueAttachments";
Expand Down Expand Up @@ -146,7 +146,7 @@ export default function IssueActions({
}
}

const { open_in } = getPreferenceValues<Preferences>();
const { open_in, branch_name } = getPreferenceValues<Preferences>();

return (
<ActionPanel title={issue.key}>
Expand Down Expand Up @@ -269,7 +269,7 @@ export default function IssueActions({

<Action.CopyToClipboard
title="Copy Git Branch Name"
content={`${issue.key}-${slugify(issue.fields.summary)}`}
content={generateBranchName(issue, branch_name)}
shortcut={{ modifiers: ["cmd", "shift"], key: "." }}
/>

Expand Down
18 changes: 18 additions & 0 deletions extensions/jira/src/helpers/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,21 @@ export function getCustomFieldValue(fieldSchema: CustomFieldSchema, value: unkno
return null;
}
}

export function generateBranchName(issue: Issue | IssueDetail, nameFormat?: string): string {
const issueKey = issue.key;
const issueSummary = issue.fields.summary.toLowerCase();
const issueSummaryShort = issueSummary.split(" ").slice(0, 5).join("-");

if (!nameFormat) {
nameFormat = "{issueKey}-{issueSummary}";
}

// Supported fields in the Jira UI: issue key, issue summary, issue summary short, issue type, project key
return nameFormat
.replace("{issueKey}", issueKey)
.replace("{issueSummary}", issueSummary)
.replace("{issueSummaryShort}", issueSummaryShort)
.replace("{issueType}", issue.fields.issuetype.name)
.replace("{projectKey}", issue.fields.project?.key || "");
}

1 comment on commit d038b34

@clempat
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still need to sanitize the issueSummary, which is the default. Otherwise, it will fail if there are spaces in the branch name.

Please sign in to comment.