Skip to content

Commit fd13028

Browse files
authored
Merge pull request #27 from aha-develop/DEVELOP-1137-1-automation-triggers
DEVELOP-1137-1 automation triggers
2 parents 4ccd6fb + 891b4fe commit fd13028

4 files changed

Lines changed: 92 additions & 17 deletions

File tree

package.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@aha-develop/github",
33
"description": "GitHub",
4-
"version": "1.4.1",
4+
"version": "1.5.0",
55
"author": "Aha! (support@aha.io)",
66
"repository": {
77
"type": "git",
@@ -91,9 +91,26 @@
9191
"endpoints": {
9292
"webhook": {
9393
"title": "Webhook from Github",
94-
"entryPoint": "src/webhooks/webhook.js",
94+
"entryPoint": "src/webhooks/webhook.ts",
9595
"public": true
9696
}
97+
},
98+
"automationTriggers": {
99+
"draftPrOpened": {
100+
"title": "Draft PR opened"
101+
},
102+
"prOpened": {
103+
"title": "PR opened"
104+
},
105+
"prMerged": {
106+
"title": "PR merged"
107+
},
108+
"prClosed": {
109+
"title": "PR closed"
110+
},
111+
"prReopened": {
112+
"title": "PR Re-opened"
113+
}
97114
}
98115
}
99116
},

src/lib/fields.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function githubPrToPrLink(pr) {
8181
*/
8282
async function linkPullRequestToRecord(pr, record) {
8383
await appendField(record, PULL_REQUESTS_FIELD, githubPrToPrLink(pr));
84+
await aha.account.setExtensionField(IDENTIFIER, pr.url, record.referenceNum);
8485

8586
if (pr.headRef) {
8687
await linkBranchToRecord(pr.headRef.name, pr.repository.url, record);
@@ -90,11 +91,14 @@ async function linkPullRequestToRecord(pr, record) {
9091
/**
9192
* @param {Github.PrForLink} pr
9293
*/
93-
async function linkPullRequest(pr) {
94-
const record = await referenceToRecord(pr.title);
94+
async function getOrLinkPullRequestRecord(pr) {
95+
let record = await referenceFromPr(pr);
9596

96-
if (record) {
97-
await linkPullRequestToRecord(pr, record);
97+
if (!record) {
98+
record = await referenceToRecord(pr.title);
99+
if (record) {
100+
await linkPullRequestToRecord(pr, record);
101+
}
98102
}
99103

100104
return record;
@@ -105,19 +109,32 @@ async function linkPullRequest(pr) {
105109
* @param {*} number
106110
*/
107111
async function unlinkPullRequest(record, number) {
108-
await replaceField(record, PULL_REQUESTS_FIELD, (prs) => {
109-
if (prs) {
110-
return prs.filter((pr) => pr.id != number);
111-
} else {
112-
return [];
113-
}
114-
});
112+
/** @type {null|Github.PrForLink[]} */
113+
const prs = await record.getExtensionField(IDENTIFIER, PULL_REQUESTS_FIELD);
114+
const pr = prs?.find((pr) => pr.id === number);
115+
await record.setExtensionField(
116+
IDENTIFIER,
117+
PULL_REQUESTS_FIELD,
118+
prs?.filter((pr) => pr.id != number) || []
119+
);
120+
121+
if (pr) {
122+
await aha.account.clearExtensionField(IDENTIFIER, pr.url);
123+
}
115124
}
116125

117126
/**
118127
* @param {Aha.ReferenceInterface & Aha.HasExtensionFields} record
119128
*/
120129
async function unlinkPullRequests(record) {
130+
/** @type {null|Github.PrForLink[]} */
131+
const prs = await record.getExtensionField(IDENTIFIER, PULL_REQUESTS_FIELD);
132+
if (prs) {
133+
for (let pr of prs) {
134+
await aha.account.clearExtensionField(IDENTIFIER, pr.url);
135+
}
136+
}
137+
121138
await record.setExtensionField(IDENTIFIER, PULL_REQUESTS_FIELD, []);
122139
}
123140

@@ -158,7 +175,7 @@ async function unlinkBranches(record) {
158175
* @returns {Promise<(Aha.HasExtensionFields & Aha.ReferenceInterface)|null>}
159176
*/
160177
export async function referenceToRecord(str) {
161-
const ahaReference = extractReference(str);
178+
const ahaReference = extractReferenceFromName(str);
162179
if (!ahaReference) {
163180
return null;
164181
}
@@ -177,10 +194,21 @@ export async function referenceToRecord(str) {
177194
);
178195
}
179196

197+
/**
198+
* @param {Github.PrForLink | PrLink} pr
199+
*/
200+
export async function referenceFromPr(pr) {
201+
const prLink = githubPrToPrLink(pr);
202+
const ref = await aha.account.getExtensionField(IDENTIFIER, prLink.url);
203+
if (!ref) return null;
204+
205+
return referenceToRecord(ref);
206+
}
207+
180208
/**
181209
* @param {string} name
182210
*/
183-
function extractReference(name) {
211+
function extractReferenceFromName(name) {
184212
let matches;
185213

186214
// Requirement
@@ -210,7 +238,7 @@ function extractReference(name) {
210238

211239
export {
212240
appendField,
213-
linkPullRequest,
241+
getOrLinkPullRequestRecord as linkPullRequest,
214242
linkPullRequestToRecord,
215243
unlinkPullRequest,
216244
unlinkPullRequests,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IDENTIFIER } from "../extension.js";
12
import {
23
linkPullRequest,
34
linkBranch,
@@ -22,6 +23,34 @@ aha.on("webhook", async ({ headers, payload }) => {
2223
}
2324
});
2425

26+
async function triggerAutomation(payload, record) {
27+
if (!payload?.pull_request) return;
28+
29+
// Check the record is a supported type
30+
if (!["Epic", "Feature", "Requirement"].includes(record.typename)) {
31+
return;
32+
}
33+
34+
const triggers: Record<string, (pr: any) => string> = {
35+
closed: (pr) => (pr.merged ? "prMerged" : "prClosed"),
36+
opened: (pr) => (pr.draft ? "draftPrOpened" : "prOpened"),
37+
reopened: () => "prReopened",
38+
};
39+
40+
const trigger = (triggers[payload.action] || (() => null))(
41+
payload.pull_request
42+
);
43+
44+
if (trigger) {
45+
console.log("Automation trigger");
46+
await aha.triggerAutomationOn(
47+
record,
48+
[IDENTIFIER, trigger].join("."),
49+
true
50+
);
51+
}
52+
}
53+
2554
async function handlePullRequest(payload) {
2655
const pr = payload.pull_request;
2756

@@ -35,6 +64,7 @@ async function handlePullRequest(payload) {
3564
}
3665

3766
await triggerEvent("pr", payload, record);
67+
await triggerAutomation(payload, record);
3868
} else {
3969
await triggerEvent("pr", payload, null);
4070
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"moduleResolution": "node",
1212
"target": "ES6"
1313
},
14-
"include": [".aha-cache/aha.d.ts", "src"]
14+
"include": [".aha-cache/types", "src"]
1515
}

0 commit comments

Comments
 (0)