-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch whats-new info from new reticulum API instead of github
- Loading branch information
1 parent
9669ef7
commit 4198a67
Showing
4 changed files
with
22 additions
and
70 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
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
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
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,83 +1,47 @@ | ||
import markdownit from "markdown-it"; | ||
const markdown = markdownit(); | ||
|
||
function formatBody(body) { | ||
const paragraphs = body.split("\r\n\r\n").filter(l => l.trim()); | ||
|
||
const paraAndImage = [paragraphs[0]]; | ||
if (paragraphs[1] && paragraphs[1].includes("![")) { | ||
paraAndImage.push(paragraphs[1]); | ||
} | ||
|
||
return markdown.render(paraAndImage.join("\r\n\r\n")); | ||
} | ||
|
||
function formatDate(value) { | ||
return value && new Date(value).toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric" }); | ||
} | ||
|
||
export async function* getUpdates(perPage = 30) { | ||
export async function* getUpdates() { | ||
let cursor = null; | ||
let hasMore = true; | ||
|
||
do { | ||
const cursorStr = cursor ? `, after: "${cursor}"` : ""; | ||
|
||
const query = `query { | ||
repository(owner: "${process.env.GITHUB_ORG}", name: "${process.env.GITHUB_REPO}") { | ||
pullRequests(labels: ["whats new"], states: [MERGED], first: ${perPage}, orderBy: { field: CREATED_AT, direction: DESC }${cursorStr}) { | ||
edges { | ||
node { | ||
title | ||
url | ||
mergedAt | ||
body | ||
} | ||
cursor | ||
} | ||
} | ||
} | ||
}`; | ||
const response = await fetch( | ||
`/api/v1/whats-new?source=${process.env.GITHUB_REPO}&${cursor ? `cursor=${cursor}` : ""}` | ||
); | ||
|
||
// Read-only, public access token. | ||
const token = process.env.GITHUB_PUBLIC_TOKEN.replace("SAFE", ""); | ||
let pullRequests = []; | ||
|
||
const response = await fetch("https://api.github.com/graphql", { | ||
method: "POST", | ||
body: JSON.stringify({ query }), | ||
headers: { authorization: `token ${token}` } | ||
}); | ||
try { | ||
const resp = await response.json(); | ||
|
||
const json = await response.json(); | ||
const moreCursor = resp.moreCursor; | ||
pullRequests = resp.pullRequests; | ||
|
||
if (json.errors) { | ||
let message = "Error fetching pull requests from GitHub:"; | ||
|
||
for (const error of json.errors) { | ||
message += "\n" + error.message; | ||
if (moreCursor) { | ||
cursor = moreCursor; | ||
} else { | ||
hasMore = false; | ||
} | ||
|
||
throw new Error(message); | ||
} | ||
|
||
const edges = json.data.repository.pullRequests.edges; | ||
|
||
if (edges.length < perPage) { | ||
} catch (e) { | ||
console.error("Error when fetching whats-new", e); | ||
hasMore = false; | ||
} else { | ||
cursor = edges[perPage - 1].cursor; | ||
} | ||
|
||
yield edges.map(({ node }) => ({ | ||
...node, | ||
formattedMergedAt: formatDate(node.mergedAt), | ||
formattedBody: formatBody(node.body) | ||
yield pullRequests.map(pullRequest => ({ | ||
...pullRequest, | ||
formattedMergedAt: formatDate(pullRequest.mergedAt), | ||
formattedBody: markdown.render(pullRequest.body) | ||
})); | ||
} while (hasMore); | ||
} | ||
|
||
export function getLatestUpdate() { | ||
return getUpdates(1) | ||
return getUpdates() | ||
.next() | ||
.then(result => (result.value && result.value.length > 0 ? result.value[0] : null)); | ||
} |