-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve urls with either the github or octolinker API
- Loading branch information
Stefan Buck
committed
Feb 15, 2019
1 parent
f6fc910
commit 29e62ce
Showing
2 changed files
with
88 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,101 @@ | ||
function createStore(json, payload) { | ||
const store = global.__ocotlinker_cache || {}; | ||
import { fetchTree } from '@octolinker/helper-github-api'; | ||
import { bulkAction } from '@octolinker/helper-octolinker-api'; | ||
|
||
payload.forEach(({ registry, target }, index) => { | ||
store[registry] = store[registry] || {}; | ||
store[registry][target] = json[index]; | ||
}); | ||
function matchFor({ urls }, types) { | ||
return urls.every(url => url.type.includes(types)); | ||
} | ||
|
||
global.__ocotlinker_cache = store; | ||
function filterLiveResolver(matches) { | ||
return matches.reduce( | ||
(memo, match) => { | ||
if (matchFor(match, ['registry', 'ping'])) { | ||
// Match contains just external urls | ||
memo.external.push(match, ['internal-link']); | ||
} else if (matchFor(match)) { | ||
// Match contains just internal urls | ||
memo.internal.push(match); | ||
} else { | ||
// Match contains both internal and external urls | ||
memo.internal.push(match); | ||
memo.external.push(match); | ||
} | ||
|
||
return memo; | ||
}, | ||
{ | ||
external: [], | ||
internal: [], | ||
}, | ||
); | ||
} | ||
function getRepoMetadata(data) { | ||
// Find first internal links which contains the information we need | ||
const result = data.find(item => | ||
item.urls.find(({ type }) => type === 'internal-link'), | ||
); | ||
|
||
async function runLiveQuery(matches) { | ||
if (!matches.length) { | ||
return []; | ||
if (result && result.urls[0]) { | ||
const { user, repo, branch } = result.urls[0]; | ||
return { user, repo, branch }; | ||
} | ||
|
||
const payload = [].concat(...matches.map(match => match.urls)); | ||
return {}; | ||
} | ||
|
||
const response = await fetch('https://githublinker.herokuapp.com/bulk', { | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: new Headers({ | ||
'Content-Type': 'application/json', | ||
}), | ||
}); | ||
const json = await response.json(); | ||
async function resolveInternalLinks(data) { | ||
const { user, repo, branch } = getRepoMetadata(data); | ||
|
||
createStore(json, payload); | ||
} | ||
let tree = []; | ||
if (user && repo && branch) { | ||
tree = await fetchTree({ user, repo, branch }); | ||
} | ||
|
||
function matchContainsOnlyRegistyMatches(match) { | ||
return match.urls.every(url => url.type === 'registry'); | ||
} | ||
for (const { urls, link } of data) { | ||
if (link.href) { | ||
// Return early if link is already set | ||
return; | ||
} | ||
|
||
function filterLiveResolver(matches) { | ||
return matches.reduce((memo, match) => { | ||
if (matchContainsOnlyRegistyMatches(match)) { | ||
memo.push(match); | ||
for (const item of urls) { | ||
if (item.type === 'internal-link' && tree.includes(item.path)) { | ||
link.href = item.url; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
async function resolveExternalLinks(data) { | ||
const response = await bulkAction(data); | ||
|
||
return memo; | ||
}, []); | ||
data.forEach(({ link, urls }) => { | ||
urls.forEach(url => { | ||
if (link.href) { | ||
// Return early if link is already set | ||
return; | ||
} | ||
|
||
try { | ||
const finalUrl = response.find( | ||
({ type, target }) => | ||
(type === url.registry || type === url.type) && | ||
target === url.target, | ||
); | ||
|
||
if (finalUrl && finalUrl.result) { | ||
link.href = finalUrl.result; | ||
} | ||
} catch (error) { | ||
// error | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export default function(matches) { | ||
const registryMatch = filterLiveResolver(matches); | ||
runLiveQuery(registryMatch); | ||
export default async function(matches) { | ||
const { external, internal } = filterLiveResolver(matches); | ||
console.log(matches); | ||
|
||
resolveExternalLinks(external); | ||
resolveInternalLinks(internal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters