Skip to content

Commit

Permalink
Fetch whats-new info from new reticulum API instead of github
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpeiris committed Feb 12, 2022
1 parent 9669ef7 commit 4198a67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 70 deletions.
2 changes: 0 additions & 2 deletions .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ RETICULUM_SERVER="dev.reticulum.io"
THUMBNAIL_SERVER="nearspark-dev.reticulum.io"
NON_CORS_PROXY_DOMAINS="hubs.local,localhost"
CORS_PROXY_SERVER="hubs-proxy.com"
GITHUB_ORG="mozilla"
GITHUB_REPO="spoke"
GITHUB_PUBLIC_TOKEN="ghp_SAFEPB2zzes9TEpAOSx2McNjJLQ1GXLBES2FsfWU"
IS_MOZ="false"
2 changes: 0 additions & 2 deletions .env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ FARSPARK_SERVER="farspark.reticulum.io"
CORS_PROXY="hubs-proxy.com"
NON_CORS_PROXY_DOMAINS="hubs.mozilla.com,reticulum.io"
ROUTER_BASE_PATH="/spoke"
GITHUB_ORG="mozilla"
GITHUB_REPO="spoke"
GITHUB_PUBLIC_TOKEN="ghp_SAFEPB2zzes9TEpAOSx2McNjJLQ1GXLBES2FsfWU"
IS_MOZ="false"
12 changes: 2 additions & 10 deletions src/ui/whats-new/WhatsNewPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,13 @@ export default class WhatsNewPage extends Component {

this.state = {
updates: [],
loading: false,
hasMore: true
};

this.updatesIterator = getUpdates(30);
}

onLoadMore = async () => {
this.setState({ loading: true });

const { value: updates, done } = await this.updatesIterator.next();

let nextUpdates;
Expand All @@ -127,7 +124,7 @@ export default class WhatsNewPage extends Component {
nextUpdates = this.state.updates;
}

this.setState({ updates: nextUpdates, loading: false, hasMore: !done });
this.setState({ updates: nextUpdates, hasMore: !done });
};

render() {
Expand All @@ -142,12 +139,7 @@ export default class WhatsNewPage extends Component {
<WhatsNewContainer>
<WhatsNewContent>
<WhatsNewTitle>What&apos;s New</WhatsNewTitle>
<InfiniteScroll
pageStart={0}
loadMore={this.onLoadMore}
hasMore={!this.state.loading && this.state.hasMore}
loader={loader}
>
<InfiniteScroll loadMore={this.onLoadMore} hasMore={this.state.hasMore} loader={loader}>
{this.state.updates.map((update, i) => {
return (
<Update key={i}>
Expand Down
76 changes: 20 additions & 56 deletions src/ui/whats-new/whats-new-utils.js
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));
}

0 comments on commit 4198a67

Please sign in to comment.