How I built the API for Hacktoberfest Validation
Tags: hacktoberfest, github, opensource, api
Published: 2020-11-18 04:51:14

If you're up for some old Queen hits, feel free to turn it up!

{% spotify spotify:artist:1dfeR4HaWDbWqFHLkxsg1d %}


### Where it all started
Online certificates have become a huge thing nowadays. With people posting certificates on LinkedIn, no one seems the bother about its authenticity or value. All this for what? to incentivize people? Hmm...  
More and more digital documents came into play. Things like Photo frames, Online ID Cards, Badges for attending events...etc. Assigning people with badges seemed like a cool thing to do. That's when we started digging and came to know about DSC OMG event. DSC OMG is a global conference for talks, workshops and events for developers. They had used a badges framework wrapped around their event. At the end people had badges in their dashboard for crazy things like attending a particular session, logging in first, staying up till the end. One could easily think of a billion ways to assign badges.


### The badges Framework

We at DSC NIT Rourkela have wrapped our [website](https://live.dscnitrourkela.org/) with the same badges framework which DSC OMG had used. Thanks to [Kautuk Kundan](https://twitter.com/Kautukkundan) (the creator of the badges framework).

After setting up the badges server, even we went crazy and started giving away badges for all sorts of activities. At the end of October, I had this thought of assigning badges to the ones who have completed the Hacktoberfest 2020 Challenge. When I first thought about it, I didn't really give much thought about it. Since it is GitHub and DigitialOcean has already done, assumed it to be fairly straightforward. And it is if you know how it is done :).


### Getting to work

Before getting into coding, I first picture the flow of all the things that need to be done. Firstly, there would be a login button on the front-end which authenticates the user and send a token to some server. Then that server fetches the PR history and does the validation check, return back with a Success True/False response.   


Setting up an OAuth GitHub app was really easy. Once that is done, I started looking into the GitHub API Docs.

### The Problem

As I was going through the Docs, I couldn't find any API which gives away the PR of an authenticated user. That really hit me hard. It didn't make sense, because DO has done it, so there must be a way. I was thinking to use search queries, even considering scrapping the page( sorry GitHub :/). No luck whatsoever.  
I pretty much gave up and thought maybe DO has some internal affair with GitHub. The big enterprises get it done. It was a big surprise to me when I got to know that the entire Hacktoberfest website, server and client was on GitHub. I felt like I hit a JackPot! It didn't even last for a second :(.

### Exploring DO's Hacktoberfest Repo

The entire codebase was on Ruby and I have no idea of Ruby. Tried going into random files in hope to figure something out, but no luck. I've used Python, JavaScript, C++ and even PHP. But Ruby was totally strange! No one I knew had a good understanding of Ruby. Out of frustration, I went to the contributors list and started mailing them!

Yes, that is what I did. Found the top contributor, her name is Frida. She had her mail on GitHub, even though I couldn't really comprehend why she would help me. Yet I mailed her, then I found a username called `MattIPv4`. This dude had his Twitter handle on GitHub, and his GitHub Profile said *Community Platform Manager @digitalocean*. Now, this is the real jackpot.

### The Magic

I texted Matt on Twitter, He asked me to drop a mail to Hacktoberfest support. As Matt suggested, I sent the same mail which I had sent to Frida. I was surprised to get a reply back within *17 mins*. 
Below is the reply I got

![Email](https://dev-to-uploads.s3.amazonaws.com/i/yu1e85a45xjfrtomf0fj.png)

As one could see, the file for the individual functionalities was clearly mentioned. This was like the ultimate tip, went right into the files, although I was not able to recognise any Ruby Code. I found out that there is a GraphQL query which fetches PR History. Yes, I was initially referring to only REST API of GitHub as I wasn't very much comfortable with GraphQL. I didn't even have to write my own GraphQL query, I copy pasted the exact query which was in the repo to my NodeJS GraphQL client.


### Actual work begins

Now that I have the data, I just had to filter the PR based on the guidelines. Quickly I fired up an Express Server with all the basic setup. I always prefer Express and NodeJS for backend, it is powerful and super fast to build. With NPM, one could do any damn thing they want to, cause there would be an NPM package for every damn thing B|.
I filtered the data according to the updated guidelines, even added support to handle PRs which counts before the guidelines were updated.

### What the API does

The API takes in the user token and responds back with a `{success:True}` if they have completed the challenge else `{success:False}` if they haven't.  Check out the Repository on GitHub to see an example Request and Response.
{% github HarishTeens/hacktoberfest-validation %}

### How the API works

Here are the steps which execute in sequence
- Get User Token
- Fetch PR History
- Filter the PRs in accordance with DO's guidelines.
- Return True/False based on the count of valid PRs.

### What more could be improved

- For now, it just works. There is literally zero error handling, if things don't go as expected, the app would definitely break. I am not super proud about that, will definitely work on it. 
- Also the filtering logic and steps could be done more efficiently. Couldn't really get that logic from DO's repo as it was in Ruby.

### Conclusion

If you have reached this point, probably you are wondering. What is so great about this? This is a piece of cake to me, I could code this in an hour. I'd say great!   
This is not a tutorial on how to build the Hacktoberfest Validation API. This is the story of how I got to it. It felt really magical to see Matt replying to a random stranger's Twitter text and a magic mail which points me to the right direction. All adding more beauty of how opensource works. Also, I got the mail back from Frida a day later! Lastly, this is not an article to praise DigitalOcean and their support.
P.S. Shhh... They didn't pay me ;)

I was really amazed that day when I realised that people are so kind and helping in the open-source community. It was a blissful experience on the whole <3.