This site runs best with JavaScript enabled.

GatsbyJs Showcase: Contributors List For Open source Project

Youssouf El Azizi

March 11, 2020

elazizi, dev, react

Today I am sharing my journey developing a new Geeksblabla feature. Meanwhile, I think it would be the perfect way to share my passion using GatsbyJs and how I can always find a solution for my challenges using GatsbyJs APIs. This article is a GatsbyJs showcase on how we can add a contributors list to any community project website.

In the last couple of weeks, we started working on updating our Geeksblabla website design and adding some new features. During our design process, we came up with this idea to thank our wonderful project contributors by making a contributors section on the main website. We think this kind of credit will encourage the community members to contribute As they will see their names as part of the contributors' family.

GeeksBlabla is one of the DevC Casablanca community products. It's 3 to 5 people show talking about a specific dev topic or interviewing a successful Moroccan person in IT. Briefly, it's a Geeks blabla.

we agree that adding a new feature to the project should be easy to maintain and handle by anyone of the team without any issues.

The Approach

GeeksBlabla source code is hosted on GitHub under the DevC Casablanca organization. It's public, so everyone can contribute to the project.

To Make a list of all contributions we are going to implement the all-contributors project that comes with a great Github bot that helps to add people using a GitHub comment.

The all-contributors spec will create a .all-contributorsrc JSON format file in the project root with all contributors, the idea is to create a GatsbyJs resource from this file and use it as a graphQl query for our contributors section component. And to automate the workflow, we are going to setup all-contributors GitHub bot that we can trigger any time using GitHub comment.

Setup All-Contributors

All-Contributors is a specification for recognizing contributors to an open-source project in a way that rewards each and every contribution, the tool also provides an emoji key to determine the contribution type (code, design, content….)

With All-contributors we have two options to add contributors: CLI to init the project and a smart GitHub bot to automate the process of adding new contributors using GitHub comment.

To Get Started, we need to Install all-contributors CLI as development dependency for our project:

1yarn add --dev all-contributors-cli # or npm

Then we need to init the all-contributors spec using init command:

1yarn all-contributors init

This command asks a few questions and sets up the project for all-contributors. This setup includes creating a .all-contributorsrc configuration file and creating a contributor table in the files you specify, a README file in our case.

Now we can start adding contributors to our list by running the following command where username is the user's GitHub username, and contribution is a,-separated list of contributions.

1#all-contributors add <username> <contribution>
2
3all-contributors add yjose code,design

If everything goes as expected you should see a new .all-contributorsrc file in the project root with your contributor’s list :

1{
2 "projectName": "geeksblabla.com",
3 "projectOwner": "DevC-Casa",
4 "repoType": "github",
5 "repoHost": "https://github.com",
6 "files": ["README.md"],
7 "imageSize": 180,
8 "commit": false,
9 "commitConvention": "none",
10 "contributors": [
11 {
12 "login": "yjose",
13 "name": "Youssouf EL AZIZI",
14 "avatar_url": "https://avatars0.githubusercontent.com/u/11137944?v=4",
15 "profile": "https://elazizi.com",
16 "contributions": ["code", "doc", "content", "review"]
17 },
18 {
19 "login": "Amine-H",
20 "name": "Amine Hakkou",
21 "avatar_url": "https://avatars1.githubusercontent.com/u/6276978?v=4",
22 "profile": "https://hakkou.me",
23 "contributions": ["code", "content"]
24 },
25 {
26 "login": "souffanda",
27 "name": "Soufian El ",
28 "avatar_url": "https://avatars2.githubusercontent.com/u/24813026?v=4",
29 "profile": "https://github.com/souffanda",
30 "contributions": ["code", "doc", "content", "ideas"]
31 }
32 //......
33 ],
34 "contributorsPerLine": 5
35}

Learn more about all-contributor cli 👉 link

Create allContributors source

A GatsbyJs source is the simplest version of a source plugin that is essentially out of the box integration between Gatsby and various third-party systems such as file-source-plugin to pull data into your Gatsby application from a local filesystem. In our case We are going to use Gatsby’s source to bring All-contributors file data to the Gatsby system then we can transform it to create our nodes that can be fetched as graphQl data.

To create a new source we need to export sourceNodes function from our gatsby-node.js file.

First, we load the data from .all-contributorsrc file using node filesystem API.Then, we parse the data using the JSON built-in API as we know that .all-contributorsrc is a JSON format. Our last step is to create a new node for every contributor using GatsbyJs node API.

1//gatsby-node.js
2const fs = require("fs");
3// ....
4exports.sourceNodes = async ({
5 actions,
6 createNodeId,
7 createContentDigest
8}) => {
9 let data = JSON.parse(fs.readFileSync("./.all-contributorsrc", "utf-8"));
10
11 data.contributors.forEach(contributor => {
12 const node = {
13 ...contributor,
14 id: createNodeId(`contributor-${contributor.login}`),
15 internal: {
16 type: "Contributor",
17 contentDigest: createContentDigest(contributor)
18 }
19 };
20
21 // Create the actual data node
22 actions.createNode(node);
23 });
24};

Learn more on how to create a Gatsby source plugin 👉 link

Restart the server and if everything works as expected you should see the allContributor query in the graphQl playground with the correct contributor’s list.

allcontributor query

GraphQl Playground

Create All contributors Component

The Above GraphQl playground comes up with a great utility called Code Exporter which generates a snippet for our GraphQl query with the correct imports.

We are going to create a Contributor component that can be used to display all contributors in our root component like the following.

1// Contributors.js
2import React from "react";
3import { useStaticQuery, graphql } from "gatsby";
4import Contributor from "./Contributor";
5
6const contributorsQuery = graphql`
7 {
8 allContributor {
9 edges {
10 node {
11 id
12 login
13 name
14 profile
15 avatar_url
16 name
17 }
18 }
19 }
20 }
21`;
22
23export default () => {
24 const data = useStaticQuery(contributorsQuery);
25 const contributors = data.allContributor.edges;
26 return (
27 <div className="contributors">
28 <div className="container list">
29 {contributors.map(({ node: { id, ...node } }) => (
30 <Contributor {...node} key={id} />
31 ))}
32 </div>
33 </div>
34 );
35};

Setup all-contributor Github bot

First, go to this link to install All-contributors bot in your project repo,Then make sure to follow the installation steps. Once Done, You can comment on issue or pull request and ask @all-contributors to add a contributor using the following format:

1# @all-contributors please add <username> for <contributions>
2@all-contributors please add yjose for content

Workflow in Actions

To see the workflow in Actions, We are Going to choose a contribution from Mohamed (@bondif) and try to make sure the Workflow works as Expected:

Step 1 : Comment to pull Request and ask @all-Contributor to add @bondif to the contributors’ list. @all-contributor bot will reply to my comment with a link to the new PR.

Step1

Comment

Step 2 : Netlify preview will deploy a preview version of our website. We just need to Make sure our new contributor has been added to the contributors’ section.

Step2

PR with Netlify preview

Step 3 : Done! PR Merged 😄.

Github Repo : https://github.com/DevC-Casa/geeksblabla.com

Thanks for reading! I hope you found it interesting, informative, and entertaining. If you think other people should read this post, Make sure to tweet, share and follow me for upcoming stories.

Share article 👉

Liked this article? Make sure to join my Newsletter.