Autofork is a Python & Flask project that uses GitHub API & OAuth to provide
an alternative way to fork its own repostory.
Designed with simplicity in mind, and as an example of serverless application
suitable for deployment on ZEIT Now.
- Use the live Autofork application or
fork the original repository manually.
This will make your own copy of the Autofork repository in your GitHub account. - Clone the repository to download the code to your local machine.
The simplest way to publish this project is by leveraging serverless solutions. Zeit Now is a web platform that makes serverless application deployment easy. It allows to deploy project from local console as well as a GitHub CI/CD integration.
- Sign up with GitHub on ZEIT Now and follow the instructions.
You will be asked to login into your GitHub account and authorize the application to access your info. - Configure Now GitHub application by allowing
All repositoriesaccess.
This allows Now to rebuild your repository on each commit. - Install Now CLI on your system.
If you're on Mac/Windows, installing Recommended package would provide both GUI & CLI apps.
Since autofork's goal is to create repository forks, it needs credentials to access GitHub. OAuth allows users to easily login with their account into autofork, while application gains access to their shared data.
-
Navigate to Settings - Developer Settings - OAuth Apps - New OAuth App on GitHub.
-
Fill the fields with the following info and click
Register application:- Application Name:
Autofork - Homepage URL:
https://autofork.<now_username>.now.sh - Authorization callback URL:
https://autofork.<now_username>.now.sh/login/authorized
Where
now_usernameis Your Username on ZEIT Now from step 3. - Application Name:
-
Now you have your own credentials. Note the created
Client ID&Client Secretvalues, and keep them secret.
Before deploying your own copy of the application, you need to configure it with your credentials.
-
Generate a secret key by running the following command in terminal:
python -c 'import os, binascii; print(binascii.hexlify(os.urandom(16)).decode())'Or you can come up with a passphrase of your choice, at least 10-characters long. It will be used to securely store cookies used by your application.
-
Run the following commands to add secret keys to your Now account:
now secrets add autofork-id <client_id> now secrets add autofork-secret <client_secret> now secrets add autofork-key <client_key>Where
<client_id>,<client_secret>are your GitHub OAuth credentials from step 8, and<client_key>is the passphrase from the step 9.
When you're done with all the previous steps, you're ready to deploy your Autofork copy as a serverless application.
-
Simply run
nowin your project directory.
This will build and deploy your application automatically, returning the deployment URLs. -
You're done!
Visit the link to your application and celebrate :)
Note: By default, Autofork is configured to fork the original repository. If you wish to make it fork your own copy, there are two ways to handle this:
- In
now.jsonmodify the line"GITHUB_ORIGIN": <url>to point to the repository remote URL. - Run deployment as
now -e GITHUB_ORIGIN=<url>
Where<url>is the clone URL of the GitHub repository.
Autofork is written in Python3.6, and uses these awesome libraries:
-
Flask microframework for web applications, that handles http requests.
-
Flask-Dance library to perform authorization via GitHub OAuth.
Autofork provides a basic frontend UI that allows user to perform actions needed to sign in/out and fork the repo.
-
First, user has to Sign in with their GitHub account and allow access to public data.
This allows Autofork to call the GitHub API to act on the shared data. -
After sign in, user is presented with two buttons: fork the repo or sign out.
The user session is stored in the cookie, which keeps the user logged in. -
Clicking the Fork button will create the repository fork in user's account.
-
Clicking the Sign out button will clear the session and revoke GitHub access.
To modify or revoke access to their data, user can also check GitHub application settings.
In case of runtime errors, user is presented with the page that contains error message, and mostly technical description.
If you need to tweak the application code, the easiest way to setup the development environment with pipenv.
-
Run
pipenv installin the project directory to install all needed dependencies in a virtual python environment. -
Use
flask runto run the local server with the application on http://127.0.0.1:5000/ -
Create
.envfile in your project directory with the used environment variables:SECRET_KEY=<client_key> GITHUB_OAUTH_CLIENT_ID=<client_id> GITHUB_OAUTH_CLIENT_SECRET=<client_secret> GITHUB_ORIGIN=<url> # allow app reload on code changes FLASK_ENV="development" # allow non-https callback URL OAUTHLIB_INSECURE_TRANSPORT=1 -
After pushing your changes to the GitHub, Now will rebuild the code and deploy the project automatically.
Note: To use OAuth locally, you need to change "Authorization callback URL" in your credentials to your local server address.
If you have questions or encounter any issues, feel free to open up an issue.