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

Adds support for Jira Server #4

Merged
merged 5 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds support for personal access tokens (#1)
  • Loading branch information
mediabounds committed Feb 2, 2023
commit 4053d356868a8db8ea85a550704c5d864132ecca
34 changes: 34 additions & 0 deletions src/JiraConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Client, { Authenticator, BasicAuth, TokenAuth } from "./Client";
import { DefaultPluginSettings } from "./JiraPluginSettings";

/**
* Convenience class for getting connected to Jira.
*/
export class JiraConnection {
/**
* Creates a new Client for making API requests.
*
* @param settings - Connection settings for the client.
* @returns A configured Client.
*/
public static getClient(settings: DefaultPluginSettings): Client {
const {domain, email: username, token: key, strategy} = settings;

if (!domain) {
throw new Error('A domain must be set');
}

if (!key) {
throw new Error('An API token must be set');
}

let authenticator: Authenticator;
if (strategy === 'PAT') {
authenticator = new TokenAuth(key);
} else {
authenticator = new BasicAuth(username, key);
}

return new Client(`https://${domain}`, authenticator);
}
}
11 changes: 11 additions & 0 deletions src/JiraPluginSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ export interface DefaultPluginSettings {
* @see https://id.atlassian.com/manage-profile/security/api-tokens
*/
token: string;

/**
* The authentication strategy to use.
*
* API tokens are for JIRA cloud.
* @see https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
*
* Personal access tokens (PAT) are for JIRA server.
* @see https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html
*/
strategy: 'APIToken' | 'PAT'
}
7 changes: 3 additions & 4 deletions src/actions/Query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DidReceiveSettingsEvent, KeyDownEvent } from "@fnando/streamdeck";
import Client, { BasicAuth } from "../Client";
import Icon, { BadgeOptions } from "../Icon";
import { JiraConnection } from "../JiraConnection";
import { BadgeType, JQLQuerySettings } from "../JiraPluginSettings";
import { PollingErrorEvent, PollingResponseEvent } from "../PollingClient";
import PollingAction, { ActionPollingContext } from "./PollingAction";
Expand Down Expand Up @@ -66,7 +66,7 @@ class Query extends PollingAction<SearchResponse, JQLQuerySettings> {
* {@inheritDoc}
*/
protected async getResponse(context: ActionPollingContext<JQLQuerySettings>): Promise<SearchResponse> {
const {domain, email: username, token: password, jql} = context.settings;
const {domain, jql} = context.settings;

if (!domain || !jql) {
return {
Expand All @@ -75,8 +75,7 @@ class Query extends PollingAction<SearchResponse, JQLQuerySettings> {
};
}

const client = new Client(`https://${domain}`, new BasicAuth(username, password));

const client = JiraConnection.getClient(context.settings);
const response = await client.request<SearchResponse>({
endpoint: 'rest/api/3/search',
query: {
Expand Down
8 changes: 8 additions & 0 deletions src/inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<input class="sdpi-item-value" type="text" id="domain" placeholder="your-domain.atlassian.net" pattern="([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}" global>
</div>

<div class="sdpi-item">
<div class="sdpi-item-label">Type</div>
<select class="sdpi-item-value" id="token-type">
<option value="APIToken" selected>Jira Cloud</option>
<option value="PAT">Jira Server</option>
</select>
</div>

<div class="sdpi-item">
<div class="sdpi-item-label">Email</div>
<input class="sdpi-item-value" type="email" id="email" global>
Expand Down
8 changes: 8 additions & 0 deletions src/inspectors/Query.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<input class="sdpi-item-value" type="text" id="domain" placeholder="your-domain.atlassian.net" pattern="([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}" global>
</div>

<div class="sdpi-item">
<div class="sdpi-item-label">Type</div>
<select class="sdpi-item-value" id="token-type">
<option value="APIToken" selected>Jira Cloud</option>
<option value="PAT">Jira Server</option>
</select>
</div>

<div class="sdpi-item">
<div class="sdpi-item-label">Email</div>
<input class="sdpi-item-value" type="email" id="email" global>
Expand Down
5 changes: 5 additions & 0 deletions src/inspectors/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class QueryActionPropertyInspector extends PollingActionInspector<JQLQuerySettin
private domain = document.getElementById('domain') as HTMLInputElement;
private email = document.getElementById('email') as HTMLInputElement;
private token = document.getElementById('token') as HTMLInputElement;
private tokenType = document.getElementById('token-type') as HTMLSelectElement;
private jql = document.getElementById('jql') as HTMLTextAreaElement;
private status = document.getElementById('status-display');
private keyAction = document.getElementById('key-action') as HTMLSelectElement;
Expand Down Expand Up @@ -44,6 +45,7 @@ class QueryActionPropertyInspector extends PollingActionInspector<JQLQuerySettin
this.email.value = settings.email;
this.token.value = settings.token;
this.jql.value = settings.jql;
this.tokenType.value = settings.strategy;

// Action settings.
this.keyActionLimit.hidden = true;
Expand Down Expand Up @@ -119,6 +121,7 @@ class QueryActionPropertyInspector extends PollingActionInspector<JQLQuerySettin
.trim(),
email: this.email.value.trim(),
token: this.token.value.trim(),
strategy: <'APIToken'|'PAT'>this.tokenType.value,
jql: this.jql.value.trim(),
keyAction: this.getKeyAction(),
pollingDelay: this.settings.pollingDelay,
Expand All @@ -136,6 +139,7 @@ class QueryActionPropertyInspector extends PollingActionInspector<JQLQuerySettin
domain: settings.domain,
email: settings.email,
token: settings.token,
strategy: settings.strategy,
});
}

Expand All @@ -148,6 +152,7 @@ class QueryActionPropertyInspector extends PollingActionInspector<JQLQuerySettin
domain: this.globalSettings.domain ?? '',
email: this.globalSettings.email ?? '',
token: this.globalSettings.token ?? '',
strategy: this.globalSettings.strategy ?? 'APIToken',
jql: '',
keyAction: {
limit: 5,
Expand Down