forked from raycast/extensions
/
issue_actions.tsx
122 lines (116 loc) · 3.73 KB
/
issue_actions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
ActionPanel,
closeMainWindow,
Color,
CopyToClipboardAction,
Icon,
KeyboardShortcut,
PushAction,
showToast,
ToastStyle,
} from "@raycast/api";
import React from "react";
import { gitlab } from "../common";
import { Issue, Label } from "../gitlabapi";
import { GitLabIcons } from "../icons";
import { LabelList } from "./label";
import { IssueMRCreateForm } from "./mr_create";
export function CloseIssueAction(props: { issue: Issue }) {
const issue = props.issue;
async function handleAction() {
try {
await closeMainWindow();
await gitlab.post(`projects/${issue.project_id}/issues/${issue.iid}/notes`, { body: "/close" });
} catch (error: any) {
showToast(ToastStyle.Failure, "Failed to close issue", error instanceof Error ? error.message : error.toString());
}
}
return (
<ActionPanel.Item
title="Close Issue"
icon={{ source: Icon.XmarkCircle, tintColor: Color.Red }}
onAction={handleAction}
/>
);
}
export function CreateMRAction({ issue }: { issue: Issue }): JSX.Element {
return (
<PushAction
icon={Icon.Pencil}
title="Create Merge Request"
shortcut={{ modifiers: ["cmd", "shift"], key: "m" }}
target={<IssueMRCreateForm issue={issue} projectID={issue.project_id} title={`Draft: Resolve: ${issue.title}`} />}
/>
);
}
export function ReopenIssueAction(props: { issue: Issue }) {
const issue = props.issue;
async function handleAction() {
try {
await closeMainWindow();
await gitlab.post(`projects/${issue.project_id}/issues/${issue.iid}/notes`, { body: "/reopen" });
} catch (error: any) {
showToast(
ToastStyle.Failure,
"Failed to reopen issue",
error instanceof Error ? error.message : error.toString()
);
}
}
return <ActionPanel.Item title="Reopen Issue" icon={{ source: Icon.ExclamationMark }} onAction={handleAction} />;
}
function ShowIssueLabelsAction(props: { labels: Label[] }) {
if (props.labels.length <= 0) {
return null;
}
return (
<PushAction
title="Show attached Labels"
target={<LabelList labels={props.labels} />}
shortcut={{ modifiers: ["cmd"], key: "l" }}
icon={{ source: GitLabIcons.labels, tintColor: Color.PrimaryText }}
/>
);
}
export function CreateIssueTodoAction(props: { issue: Issue; shortcut?: KeyboardShortcut }) {
const issue = props.issue;
async function handleAction() {
try {
await gitlab.post(`projects/${issue.project_id}/issues/${issue.iid}/todo`);
showToast(ToastStyle.Success, "To do created");
} catch (error: any) {
showToast(
ToastStyle.Failure,
"Failed to add as to do",
error instanceof Error ? error.message : error.toString()
);
}
}
if (issue.state === "opened") {
return (
<ActionPanel.Item
title="Add a to do"
shortcut={props.shortcut}
icon={{ source: GitLabIcons.todo, tintColor: Color.PrimaryText }}
onAction={handleAction}
/>
);
} else {
return null;
}
}
export function IssueItemActions(props: { issue: Issue }) {
const issue = props.issue;
return (
<React.Fragment>
<CreateIssueTodoAction issue={issue} shortcut={{ modifiers: ["cmd"], key: "t" }} />
<ShowIssueLabelsAction labels={issue.labels} />
{issue.state == "opened" && <CreateMRAction issue={issue} />}
{issue.state == "opened" && <CloseIssueAction issue={issue} />}
{issue.state == "closed" && <ReopenIssueAction issue={issue} />}
<CopyToClipboardAction title="Copy Issue Number" content={issue.iid} />
<CopyToClipboardAction title="Copy Issue URL" content={issue.web_url} />
<CopyToClipboardAction title="Copy Issue Title" content={issue.title} />
</React.Fragment>
);
}