@@ -81,6 +81,7 @@ function githubPrToPrLink(pr) {
8181 */
8282async 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 */
107111async 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 */
120129async 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 */
160177export 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
211239export {
212240 appendField ,
213- linkPullRequest ,
241+ getOrLinkPullRequestRecord as linkPullRequest ,
214242 linkPullRequestToRecord ,
215243 unlinkPullRequest ,
216244 unlinkPullRequests ,
0 commit comments