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

refactor: added Linear Connect dependency #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions package-lock.json

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

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@
"viewsWelcome": [
{
"view": "linearIssues",
"contents": "No linear api key found; [find key](https://linear.app/dyte/settings/api).\n[Add Linear Key](command:linear-flow.inputLinearApiKey)"
"contents": "Retriving session from Linear Connect..."
}
],
"commands": [
{
"command":"linear-flow.inputLinearApiKey",
"command": "linear-flow.inputLinearApiKey",
"title": "Input Linear Key"
},
{
"command":"linear-flow.showTicket",
"command": "linear-flow.showTicket",
"title": "Show Current Ticket"
},
{
"command":"linear-flow.openTicketInWebView",
"command": "linear-flow.openTicketInWebView",
"title": "Open Current TIcket in Web View"
},
{
"command":"linear-flow.refreshTicket",
"command": "linear-flow.refreshTicket",
"title": "Refresh"
}
],
"menus": {
"view/title": [
{
"command":"linear-flow.refreshTicket",
"command": "linear-flow.refreshTicket",
"when": "view == linearIssues",
"group": "navigation"
}
Expand Down Expand Up @@ -96,5 +96,8 @@
"@linear/sdk": "^7.0.1",
"dotenv": "^16.3.1",
"marked": "^7.0.3"
}
},
"extensionDependencies": [
"linear.linear-connect"
]
}
117 changes: 61 additions & 56 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@ const linearAPIStorageKey = "LINEAR_API_STORAGE_KEY";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
export async function activate(context: vscode.ExtensionContext) {
const session = await vscode.authentication.getSession(
"linear", // Linear VS Code authentication provider ID
["read"], // OAuth scopes we're requesting
{ createIfNone: true }
);

const apiKey = context.globalState.get(linearAPIStorageKey);
if (apiKey && (apiKey as string).startsWith("lin_api")) {
const linearIssueProvider = new LinearIssueProvider(apiKey as string);
if (session) {
const linearIssueProvider = new LinearIssueProvider(session.accessToken);
linearIssueProvider.linear.init().then(() => {
vscode.window.registerTreeDataProvider(
"linearIssues",
linearIssueProvider
);


vscode.commands.registerCommand(
"linear-flow.refreshTicket",
async () => {
linearIssueProvider.refresh();
}
);
vscode.commands.registerCommand("linear-flow.refreshTicket", async () => {
linearIssueProvider.refresh();
});

const folders = vscode.workspace.workspaceFolders;
/**
* Will only reach this in case there is no workspace open
*/
if (!folders) { return; }
if (!folders) {
return;
}
const pwd = folders[0].uri.path;
exec(
`cd ${pwd} && git rev-parse --abbrev-ref HEAD`,
Expand All @@ -58,67 +60,69 @@ export function activate(context: vscode.ExtensionContext) {

const issueIdentifier = branches[1].toLocaleUpperCase();

await linearIssueProvider.linear.addStartCommentToIssue(issueIdentifier);
await linearIssueProvider.linear.addEndCommentToIssue(issueIdentifier);
await linearIssueProvider.linear.addStartCommentToIssue(
issueIdentifier
);
await linearIssueProvider.linear.addEndCommentToIssue(
issueIdentifier
);

setInterval(async () => {
await linearIssueProvider.linear.updateIssueComment(issueIdentifier);
}, 30 * 1000);
setInterval(async () => {
await linearIssueProvider.linear.updateIssueComment(
issueIdentifier
);
}, 30 * 1000);
}
);
});
} else {
console.error(
"Something went wrong, could not acquire a Linear API session."
);
}

vscode.commands.registerCommand(
"linear-flow.inputLinearApiKey",
async () => {
const input = await vscode.window.showInputBox({
placeHolder: "lin_api_*",
prompt: "Enter Linear API Key",
});
const linearIssueProvider = new LinearIssueProvider(input!);
await linearIssueProvider.linear.init();

// Only set when successful
context.globalState.update(linearAPIStorageKey, input);
vscode.window.registerTreeDataProvider(
"linearIssues",
linearIssueProvider
);
vscode.commands.registerCommand(
"linear-flow.refreshTicket",
async () => {
linearIssueProvider.refresh();
}
);
}
);
vscode.commands.registerCommand("linear-flow.inputLinearApiKey", async () => {
const input = await vscode.window.showInputBox({
placeHolder: "lin_api_*",
prompt: "Enter Linear API Key",
});
const linearIssueProvider = new LinearIssueProvider(input!);
await linearIssueProvider.linear.init();

// Only set when successful
context.globalState.update(linearAPIStorageKey, input);
vscode.window.registerTreeDataProvider("linearIssues", linearIssueProvider);
vscode.commands.registerCommand("linear-flow.refreshTicket", async () => {
linearIssueProvider.refresh();
});
});

vscode.commands.registerCommand(
"linear-flow.openTicketInWebView",
"linear-flow.openTicketInWebView",
async (issue: Issue) => {

// Create and show a new webview
const panel = vscode.window.createWebviewPanel(
'ticketDetails', // Identifies the type of the webview. Used internally
'Ticket Details', // Title of the panel displayed to the user
const panel = vscode.window.createWebviewPanel(
"ticketDetails", // Identifies the type of the webview. Used internally
"Ticket Details", // Title of the panel displayed to the user
vscode.ViewColumn.Beside, // Editor column to show the new webview panel in.
{
enableScripts: true,
}
);
const [comments, state] = await Promise.all([issue.comments(), issue.state]);
const [comments, state] = await Promise.all([
issue.comments(),
issue.state,
]);
panel.webview.html = getWebviewContent(issue, comments.nodes, state);

panel.webview.onDidReceiveMessage(
(message) => {
switch (message.command) {
case "openineditor":
vscode.commands.executeCommand(
"linear-flow.openTicketInEditor",
[issue.branchName]
);
return;
case "openineditor":
vscode.commands.executeCommand("linear-flow.openTicketInEditor", [
issue.branchName,
]);
return;
}
},
undefined,
Expand All @@ -128,15 +132,16 @@ export function activate(context: vscode.ExtensionContext) {
);

vscode.commands.registerCommand(
"linear-flow.openTicketInEditor",
"linear-flow.openTicketInEditor",
async (branchName: string) => {

const folders = vscode.workspace.workspaceFolders;
/**
* Will only reach this in case there is no workspace open
*/
if (!folders) {
vscode.window.showErrorMessage('Please open your work directory before trying to open an issue!');
vscode.window.showErrorMessage(
"Please open your work directory before trying to open an issue!"
);
return;
}
const pwd = folders[0].uri.path;
Expand Down
14 changes: 3 additions & 11 deletions src/services/linear/linear.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
LinearClient,
User,
Issue,
Team,
} from "@linear/sdk";
import { LinearClient, User, Issue, Team } from "@linear/sdk";
import {
IdComparator,
NullableCycleFilter,
Expand All @@ -16,21 +11,19 @@ import * as vscode from "vscode";
// fetch the details for the sub issue like the id, checkout branch

export default class Linear {

linearClient: LinearClient;
me: User = {} as User;
teams: Team[] = [];

constructor(apiKey: string) {
this.linearClient = new LinearClient({ apiKey });
constructor(accessToken: string) {
this.linearClient = new LinearClient({ accessToken });
}

async init() {
this.me = await this.linearClient.viewer;
this.teams = (await this.me.teams()).nodes;
}


async getAllIssues(): Promise<Issue[]> {
const myIssues = await this.me.assignedIssues({
// orderBy: PaginationOrderBy.CreatedAt,
Expand Down Expand Up @@ -94,7 +87,6 @@ export default class Linear {
}

async addStartCommentToIssue(issueIdentifier: string) {

const issues = await this.me.assignedIssues({});

const trimmedIssueIdentifier = issueIdentifier.trimStart().trimEnd();
Expand Down
Loading