Skip to content

Commit

Permalink
Added sponsor API
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Jun 14, 2024
1 parent 7d70e24 commit 85c3bcb
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/functions/http_sponsors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
app,
HttpRequest,
HttpResponseInit,
InvocationContext,
} from "@azure/functions";
import { GitHubService } from "../services/GithubService";

export async function http_sponsors(
_: HttpRequest,
__: InvocationContext
): Promise<HttpResponseInit> {
let sponsors = [];

const gitHubSponsors = await GitHubService.getSponsors();
sponsors = [...gitHubSponsors];

const ocResponse = await fetch(
`https://opencollective.com/frontmatter/members.json`
);
if (ocResponse && ocResponse.ok) {
const data = await ocResponse.json();
sponsors = [
...sponsors,
...data
.filter((s: any) => s.role === "BACKER" && s.isActive)
.map((s: any) => ({
id: s.MemberId,
name: s.name,
url: s.website,
avatarUrl: s.image,
})),
];
}

if (sponsors && sponsors.length > 0) {
return {
jsonBody: sponsors,
};
}

return {
jsonBody: null,
};
}

app.http("sponsors", {
methods: ["GET"],
authLevel: "anonymous",
handler: http_sponsors,
});
91 changes: 91 additions & 0 deletions src/services/GithubService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
export class GitHubService {
public static async verifyUser(token: string) {
if (!token) {
return;
}

const username = await GitHubService.getUser(token);
if (!username) {
return;
}

const sponsors = await GitHubService.getSponsors();
const backers = (process.env.BACKERS || "").split(",");

const sponsor = sponsors.find((s: any) => s.login === username);

if (!backers?.includes(username) && !sponsor) {
return;
}

return username;
}

public static async getUser(token: string) {
const response = await fetch(`https://api.github.com/user`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});

if (!response.ok) {
return;
}

const data = await response.json();

return data.login;
}

public static async getSponsors() {
const response = await fetch(`https://api.github.com/graphql`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `token ${process.env.GITHUB_AUTH}`,
},
body: JSON.stringify({
query: `query SponsorQuery {
viewer {
login
sponsors(first: 100) {
edges {
node {
... on User {
id
name
login
url
avatarUrl
}
... on Organization {
id
name
url
avatarUrl
}
}
}
}
}
}`,
}),
});

let sponsors = [];

if (response && response.ok) {
const data = await response.json();
sponsors = data.data.viewer.sponsors.edges.map((edge: any) => edge.node);
}

if (sponsors && sponsors.length > 0) {
return sponsors;
}

return [];
}
}

0 comments on commit 85c3bcb

Please sign in to comment.