Skip to content

Commit

Permalink
Basic serverless function logic
Browse files Browse the repository at this point in the history
  • Loading branch information
datejer committed Nov 21, 2020
0 parents commit acbce3b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.vercel
.env
58 changes: 58 additions & 0 deletions api/[owner]/[repo].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const axios = require("axios");

module.exports = (req, res) => {
const { owner, repo } = req.query;

axios
.get(`https://api.github.com/repos/${owner}/${repo}/deployments`)
.then((response) => {
if (response.data.length <= 0)
return res.status(404).json({
error: {
code: "not_found",
message: "The requested repository does not have any deployments.",
},
});

const vercelDeployments = response.data.filter(
(deployment) =>
deployment.creator.login === "vercel[bot]" &&
deployment.creator.html_url === "https://github.com/apps/vercel" &&
deployment.creator.type === "Bot"
);

if (vercelDeployments.length <= 0)
return res.status(404).json({
error: {
code: "not_found",
message:
"The requested repository does not have any Vercel deployments.",
},
});

const latest = vercelDeployments[0];

axios.get(latest.statuses_url).then((response) => {
if (response.data[0].state === "success")
return res.status(200).json({
url: "https://img.shields.io/badge/vercel-passing-success",
});
else if (response.data[0].state === "failure")
return res.status(200).json({
url: "https://img.shields.io/badge/vercel-failed-critical",
});
else if (response.data[0].state === "pending")
return res.status(200).json({
url: "https://img.shields.io/badge/vercel-pending-yellow",
});
});
})
.catch((error) => {
return res.status(error.response.status).json({
error: {
code: error.response.statusText.toLowerCase().replace(/ /gi, "_"),
message: "The requested repository does not have any deployments.",
},
});
});
};
5 changes: 5 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = (req, res) => {
res.json({
message: "Hello World!",
});
};
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "vercel-badge",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.0"
}
}

1 comment on commit acbce3b

@vercel
Copy link

@vercel vercel bot commented on acbce3b Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.