Skip to content

Commit e2c5910

Browse files
authored
Merge pull request #31 from aha-develop/ADT-211-refactor-inline-with-other-exts
ADT-211 + DEVELOP-1305-2: Refactor github extension and empty state
2 parents 4ea235a + 991e2cf commit e2c5910

60 files changed

Lines changed: 3160 additions & 590 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/node_modules
22
.aha-cache
3+
4+
# Ignore build files in the repo root
5+
./*.gz

jest.config.cjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
},
1010
"dependencies": {
1111
"@aha-app/aha-develop-react": "^1.0.0",
12+
"@octokit/graphql": "^5.0.0",
13+
"@octokit/webhooks-types": "^6.3.2",
1214
"gql-tag": "^1.0.1",
15+
"inflected": "^2.1.0",
1316
"prettier": "^2.6.2"
1417
},
1518
"type": "module",
@@ -35,7 +38,7 @@
3538
"links": {
3639
"title": "GitHub",
3740
"host": "attribute",
38-
"entryPoint": "src/views/attribute.js",
41+
"entryPoint": "src/views/attribute.tsx",
3942
"recordTypes": [
4043
"Feature",
4144
"Epic",
@@ -45,7 +48,7 @@
4548
"prs": {
4649
"title": "My Pull Requests",
4750
"host": "page",
48-
"entryPoint": "src/views/prsPage.js",
51+
"entryPoint": "src/views/prsPage.tsx",
4952
"location": {
5053
"menu": "Work"
5154
}
@@ -59,7 +62,7 @@
5962
"commands": {
6063
"sync": {
6164
"title": "Sync pull requests",
62-
"entryPoint": "src/commands/sync.js",
65+
"entryPoint": "src/commands/sync.ts",
6366
"commandType": "recordAction",
6467
"recordTypes": [
6568
"Epic",
@@ -69,7 +72,7 @@
6972
},
7073
"addLink": {
7174
"title": "Link pull request to record",
72-
"entryPoint": "src/commands/addLink.js",
75+
"entryPoint": "src/commands/addLink.ts",
7376
"commandType": "recordAction",
7477
"recordTypes": [
7578
"Epic",
@@ -79,7 +82,7 @@
7982
},
8083
"removeLinks": {
8184
"title": "Remove linked pull requests from record",
82-
"entryPoint": "src/commands/removeLinks.js",
85+
"entryPoint": "src/commands/removeLinks.ts",
8386
"commandType": "recordAction",
8487
"recordTypes": [
8588
"Epic",
@@ -121,7 +124,12 @@
121124
}
122125
},
123126
"devDependencies": {
127+
"@types/inflected": "^1.1.29",
128+
"@types/jest": "^28.1.8",
124129
"@types/react": "^17.0.3",
125-
"@types/react-dom": "^17.0.3"
130+
"@types/react-dom": "^17.0.3",
131+
"jest": "^28.1.3",
132+
"ts-jest": "^28.0.8",
133+
"typescript": "^4.7.4"
126134
}
127135
}

src/commands/addLink.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/commands/addLink.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { updatePullRequestLinkOnRecord } from "@lib/fields";
2+
import { withGitHubApi } from "@lib/github/api";
3+
import { getPrByUrl } from "@lib/github/getPr";
4+
import { LinkableRecord } from "@lib/linkableRecord";
5+
import { validPrUrl } from "@lib/validPrUrl";
6+
7+
const AddLink: Aha.CommandExtension<{ record: LinkableRecord }> = async ({
8+
record,
9+
}) => {
10+
if (!record) return;
11+
12+
const prUrl = await aha.commandPrompt("Link URL", {
13+
placeholder: "Enter the URL to a pull request",
14+
});
15+
16+
if (!validPrUrl(prUrl)) {
17+
throw new Error("Please enter a valid pull request URL");
18+
}
19+
20+
await withGitHubApi(async (api) => {
21+
const pullRequest = await getPrByUrl(api, prUrl);
22+
await updatePullRequestLinkOnRecord(pullRequest, record);
23+
});
24+
};
25+
26+
aha.on("addLink", AddLink);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { unlinkBranches, unlinkPullRequests } from "../lib/fields";
1+
import { unlinkBranches, unlinkPullRequests } from "@lib/fields";
22

33
aha.on("removeLinks", async ({ record }) => {
44
await unlinkPullRequests(record);
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import { linkBranch, linkPullRequest } from "../lib/fields";
2-
import { withGitHubApi } from "../lib/github/api";
3-
import GithubSearchQuery from "../lib/github/GithubSearchQuery";
4-
import { searchForPr } from "../lib/github/searchForPr";
1+
import { linkBranch, getOrLinkPullRequestRecord } from "@lib/fields";
2+
import { withGitHubApi } from "@lib/github/api";
3+
import GithubSearchQuery from "@lib/github/GithubSearchQuery";
4+
import { searchForPr } from "@lib/github/searchForPr";
5+
import { LinkableRecord } from "@lib/linkableRecord";
56

6-
aha.on("sync", ({ record }, { settings }) => {
7+
const SyncCommand: Aha.CommandExtension<{ record: LinkableRecord }> = (
8+
{ record },
9+
{ settings }
10+
) => {
711
if (!record) {
812
aha.commandOutput("Open a record first to sync PRs for that record");
913
return;
1014
}
1115
console.log(`Syncing PRs for ${record.referenceNum}`);
1216
/** @type {string[]} */
13-
const repos = settings.repos;
17+
const repos = settings.repos as string[];
1418

1519
if (!repos || repos.length === 0) {
1620
throw new Error(
@@ -28,14 +32,23 @@ aha.on("sync", ({ record }, { settings }) => {
2832
withGitHubApi(async (api) => {
2933
const search = await searchForPr(api, { query });
3034

35+
if (search.edges.length === 0) {
36+
aha.commandOutput(
37+
`No pull requests found with ${record.referenceNum} in the title`
38+
);
39+
return;
40+
}
41+
3142
for (let prNode of search.edges) {
3243
const pr = prNode.node;
3344

34-
await linkPullRequest(pr);
45+
await getOrLinkPullRequestRecord(pr);
3546

3647
if (pr.headRef) {
3748
await linkBranch(pr.headRef.name, pr.repository.url);
3849
}
3950
}
4051
});
41-
});
52+
};
53+
54+
aha.on("sync", SyncCommand);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Styles } from "./Styles";
55
/**
66
* Set up the styles and auth provider
77
*/
8-
export const ExtensionRoot = ({ children }) => {
8+
export const ExtensionRoot: React.FC = ({ children }) => {
99
return (
1010
<>
1111
<Styles />
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import React from "react";
22

3-
/**
4-
* @param {string} urlString
5-
*/
6-
function isValidExternalLink(urlString) {
3+
function isValidExternalLink(urlString: string) {
74
try {
85
const url = new URL(urlString);
96
return url.protocol === "https:";
107
} catch (e) {
118
return false;
129
}
13-
};
10+
}
1411

1512
/**
1613
* Ensures that Github target _blank links have the required noopener noreferrer properties
1714
* and that they point to Github. Use like a normal <a> tag
18-
*
19-
* @type {React.FC<{href: string}>}
2015
*/
21-
export const ExternalLink = (props) => {
16+
export const ExternalLink: React.FC<{ href: string }> = (props) => {
2217
if (isValidExternalLink(props.href)) {
2318
return (
2419
<a {...props} target="_blank" rel="noopener noreferrer">
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import React from "react";
22

33
/**
44
* Display the pull request labels as pills with the correct colors
5-
*
6-
* @type {React.FC<{pr: Github.PrWithLabels}>}
75
*/
8-
export const PrLabels = ({ pr }) => {
6+
export const PrLabels: React.FC<{ pr: Github.PrWithLabels }> = ({ pr }) => {
97
const labels = pr.labels.nodes.map(({ name, color }, idx) => (
108
<span key={idx} className="pr-label">
119
<aha-pill color={"#" + color}>{name}</aha-pill>

0 commit comments

Comments
 (0)