Skip to content

Commit

Permalink
Source Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsridharan authored Jun 22, 2020
0 parents commit ec72d4f
Show file tree
Hide file tree
Showing 7 changed files with 7,049 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:lts-alpine

RUN apk add git

COPY entrypoint.sh /entrypoint.sh

COPY node-app /github-latest-repo-node-app/

RUN chmod 777 entrypoint.sh

RUN chmod -R 777 /github-latest-repo-node-app/

ENTRYPOINT ["/entrypoint.sh"]

133 changes: 133 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<p align="center"><img src="https://cdn.svarun.dev/gh/actions.png" width="150px"/></p>

# GH Latest Repo - ***Github Action***
Github Action That Provides Latest Public GitHub Repos From A User

## ⚙️ Configuration
| Argument | Default | Description |
| -------- | :---: | ----------- |
| `GITHUB_USERNAME` | **NULL** | Provide A Valid Github Username To Fetch Latest Repo |
| `MAX_REPOS` | **6** | Number Of Latest Repo To Fetch And Provide As JSON |
| `GH_TOKEN` | **NULL** | Github Personal Access Token |
| `SAVE_LOCATION` | **/** | Path To Save The ***JSON*** File |
| `FILE_NAME` | **repos.json** | Custom Option To Set A Custom FILE NAME |

---

### [Github Personal Token](https://github.com/settings/tokens/new?description=gh-latest-repos) <small> Is required with the below scope </small>

> ![https://cdn.svarun.dev/gh/varunsridharan/action-gh-latest-repo/scope.png](https://cdn.svarun.dev/gh/varunsridharan/action-gh-latest-repo/scope.png)
#### [Click Here To Generate A Token](https://github.com/settings/tokens/new?description=gh-latest-repos)

---

## 🚀 Usage

### 1. Using It With Master Branch
* Step1 : Create A File Named **gh-latest-repo.yml** in `/.github/workflows/`
* Step2 : Copy & Paste The Below Content In That File
```yml
name: ON_PUSH

on:
schedule:
- cron: '*/60 * * * *'

jobs:
GH_Latest_Repo:
runs-on: ubuntu-latest
steps:
- name: 📩 Fetching Repository
uses: actions/checkout@v2
- name: 📝 Generating JSON
uses: varunsridharan/action-gh-latest-repo@master
with:
GH_TOKEN: ${{secrets.GH_TOKEN}}
GITHUB_USERNAME: "varunsridharan"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
### 2. Using It With Custom/GH-PAGES Branch
* Step1 : Create the branch you need.
* Step2 : Create A File Named **gh-latest-repo.yml** in `/.github/workflows/` in MASTER BRANCH
* Step3 : Copy & Paste The Below Content In That File
```yml
name: ON_PUSH
on:
schedule:
- cron: '*/60 * * * *'
jobs:
GH_Latest_Repo:
runs-on: ubuntu-latest
steps:
- name: 📩 Fetching Repository
uses: actions/checkout@v2
with:
ref: 'your-branch-here'
- name: 📝 Generating JSON
uses: varunsridharan/action-gh-latest-repo@master
with:
GH_TOKEN: ${{secrets.GH_TOKEN}}
GITHUB_USERNAME: "varunsridharan"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## 🤔 Inspired By <small>[Sindre Sorhus][sindresorhus/gh-latest-repos]<small>
First when i saw his website i was so confused on how he was able to fetch the latest github repo via the domain `https://gh-latest-repos.now.sh/`
& Also i never heared of the domain `.now.sh`

So i was trying to figure on where i can purchase and after going through lots of Website's i somehow, ended here
[sindresorhus/gh-latest-repos](https://github.com/sindresorhus/gh-latest-repos)

That's when i found out `.now.sh` is domain provided by [https://vercel.com](https://vercel.com) and its FREE!!!

So you guessed it. I jumped right into it and tried to get it up & Running.
But I failed and was not able to get it since i am not that familiar with it.

#### Well Finally Got it Working Using None Other Than Github Actions 😀 😉
Since i am familiar ***Github Actions*** i decided to convert his script into a ***Action Package***



> Thanks To Sindre Sorhus For Creating Such Wonderful Microservice

---

## 🤝 Contributing
If you would like to help, please take a look at the list of [issues](issues/) or the [To Do](#-todo) checklist.

## 📝 License
This project is licensed under **General Public License v3.0 license**. See the [LICENSE](LICENSE) file for more info.

## 📣 Feedback
If you find it useful, let me know :wink:

- Create An [🔧 Issue](issues/)
- Contact me on [🐦 Twitter][twitter]
- Send An [📧 E-Mail][email].

## 🙏 Supports
Built With ♥️By _[Varun Sridharan][twitter]_

🌟 This repository if this project helped you!

## Powered by
| [![DigitalOcean][do-image]][do-ref] | [![JetBrains][jb-image]][jb-ref] |
| --- | --- |


[twitter]: https://go.svarun.dev/sm/twitter/
[email]: https://go.svarun.dev/contact/email/
[website]: https://go.svarun.dev/website/
[do-ref]: https://go.svarun.dev/powered/digitalocean/
[jb-ref]: https://go.svarun.dev/powered/jetbrains/

[do-image]: https://cdn.svarun.dev/common/digitalocean/small.png?v=1
[jb-image]: https://cdn.svarun.dev/common/jetbrains/phpstorm/small.png?v=1
[sindresorhus/gh-latest-repos]: https://github.com/sindresorhus/gh-latest-repos
29 changes: 29 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: 'GH Latest Repo'
description: 'Github Action That Provides Latest Public GitHub Repos From A User'
author: 'varunsridharan'
branding:
icon: 'message-square'
color: 'gray-dark'
inputs:
GITHUB_USERNAME:
description: 'Github Username'
MAX_REPOS:
description: 'Max Github Repos'
default: '6'
GH_TOKEN:
description: 'Github Personal Access Token'
SAVE_LOCATION:
description: 'Path To Save The JSON File'
default: '/'
FILE_NAME:
description: 'Repo JSON File Name'
default: 'repos.json'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.GITHUB_USERNAME }}
- ${{ inputs.MAX_REPOS }}
- ${{ inputs.SAVE_LOCATION }}
- ${{ inputs.FILE_NAME }}
- ${{ inputs.GH_TOKEN }}
39 changes: 39 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh

set -eu
echo " "
MAX_REPOS="$INPUT_MAX_REPOS"
SAVE_LOCATION="$INPUT_SAVE_LOCATION"
FILE_NAME="$INPUT_FILE_NAME"
GITHUB_USERNAME="$INPUT_GITHUB_USERNAME"
GH_TOKEN="$INPUT_GH_TOKEN"
SAVE_FULL_PATH="$GITHUB_WORKSPACE/$SAVE_LOCATION"

cd $GITHUB_WORKSPACE && ls -lah
echo " "
echo "☑️ Validating Save Location"
if [[ ! -e $SAVE_FULL_PATH ]]; then
mkdir -p $SAVE_FULL_PATH
fi

echo "##[group] ⬇️ Installing Required Node Dependencies"
cd /github-latest-repo-node-app
npm install
echo "##[endgroup]"

echo " "
echo "🏃 Running Application"
node index.js "$GITHUB_USERNAME" "$MAX_REPOS" "$GH_TOKEN" "$SAVE_FULL_PATH/$FILE_NAME"

cd $GITHUB_WORKSPACE

if [ "$(git status "$SAVE_FULL_PATH/$FILE_NAME" --porcelain)" != "" ]; then
echo "##[group] 👌 Pushing To Github"
git config --local user.email "githubactionbot@gmail.com" && git config --local user.name "Github Action Bot"
git add "$SAVE_FULL_PATH/$FILE_NAME"
git commit -m "💬 #$GITHUB_RUN_NUMBER - Github Latest Repository JSON Updated/ ⚡ Triggered By $GITHUB_SHA"
git push "https://x-access-token:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY"
echo "##[endgroup]"
else
echo "✅ Nothing To Push"
fi
87 changes: 87 additions & 0 deletions node-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';
const graphqlGot = require( 'graphql-got' );
const fs = require( 'fs' );
const GITHUB_USERNAME = process.argv[ 2 ];
const MAX_REPOS = process.argv[ 3 ];
const GITHUB_TOKEN = process.argv[ 4 ];
const SAVE_LOCATION = process.argv[ 5 ];

if( !GITHUB_TOKEN ) {
throw new Error( '🛑 Please set your GitHub token in the `GITHUB_TOKEN` action config' );
}

if( !GITHUB_USERNAME ) {
throw new Error( '🛑 Please set your GitHub username in the `GITHUB_USERNAME` action config' );
}

if( !SAVE_LOCATION ) {
throw new Error( '🛑 Please set your Save Location in the `SAVE_LOCATION` action config' );
}

const query = `
query ($cursor: String) {
user(login: "${GITHUB_USERNAME}") {
repositories(
last: ${MAX_REPOS},
isFork: false,
isLocked: false,
ownerAffiliations: OWNER,
privacy: PUBLIC,
orderBy: {
field: CREATED_AT,
direction: ASC
}
before: $cursor
) {
edges {
node {
name
description
url
primaryLanguage {
name
color
}
stargazers {
totalCount
}
forks {
totalCount
}
}
cursor
}
}
}
}
`;

const fetchRepos = async( repos = [], cursor = null ) => {
const { body } = await graphqlGot( 'api.github.com/graphql', {
query,
token: GITHUB_TOKEN,
variables: { cursor }
} );
const currentRepos = body.user.repositories.edges
.filter( ( { node: repo } ) => repo.description )
.map( ( { node: repo } ) => ( {
...repo,
stargazers: repo.stargazers.totalCount,
forks: repo.forks.totalCount
} ) );

if( ( repos.length + currentRepos.length ) < MAX_REPOS ) {
return fetchRepos( repos.concat( currentRepos ), body.user.repositories.edges[ 0 ].cursor );
}

let $return = repos.concat( currentRepos.slice( repos.length - MAX_REPOS ) );

fs.writeFile( SAVE_LOCATION, JSON.stringify( $return ), function( err ) {
if( err ) {
return console.log( '🛑 Error While Saving JSON File : ' + err );
}
console.log( '✅ Latest Repository Data Downloaded' )
} );
};

fetchRepos();
Loading

0 comments on commit ec72d4f

Please sign in to comment.