Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Walton committed Feb 14, 2019
0 parents commit 3fc2b3f
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GITHUB_USERNAME=<YOUR GITHUB USERNAME>
GITHUB_ACCESS_TOKEN=<YOUR GITHUB ACCESS TOKEN>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
yarn-error.log
.env
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Which? Github Manager

A small Node app for automating common tasks for managing Which?'s Github repos.

## Setup

* Clone the repo and run `yarn`.
* Create a .env file containing your Github username and access token. Access tokens can be generated [here](https://github.com/settings/tokens). *NB*: User must have permissions to create repos within the [whichdigital](https://github.com/whichdigital) Github org.

## Usage

* Run `node index.js` passing in the command and arguments as detailed below.

### Commands

#### newWhichRepo

Creates a new repository with the given name. Creates a master and develop branch. Sets develop as the default branch. Adds protection to both master and develop branches.

| Argument | Required | Default |
| ------------ | ----------- |---------|
| Repo name | Y | n/a |

#### createRepo

Creates a new repository with the given name.

| Argument | Required | Default |
| ------------ | ----------- |---------|
| Repo name | Y | n/a |

#### createBranchFromMaster

Creates a new branch off the master branch.

| Argument | Required | Default |
| ------------ | ----------- |---------|
| Repo name | Y | n/a |
| Branch name | Y | n/a |

#### setDefaultBranch

Sets the default branch to the specified branch.

| Argument | Required | Default |
| -------------------- | ----------- |---------|
| Repo name | Y | n/a |
| Default Branch name | N | develop |

#### protectBranch

Adds branch protection to the specified branch.

| Argument | Required | Default |
| ------------ | ----------- |---------|
| Repo name | Y | n/a |
| Branch name | Y | n/a |
110 changes: 110 additions & 0 deletions github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const fetch = require('node-fetch')

const ORGANISATION = 'whichdigital'
const { GITHUB_USERNAME, GITHUB_ACCESS_TOKEN } = process.env
const BASE_URL = `https://${GITHUB_USERNAME}:${GITHUB_ACCESS_TOKEN}@api.github.com/`

const createRepo = name => {
if (!name) {
throw new Error('Repo name required to create a repo')
}

const url = `${BASE_URL}orgs/${ORGANISATION}/repos`
const options = {
name,
auto_init: true,
private: true,
has_issues: true,
has_projects: true,
has_wiki: true
}
const body = JSON.stringify(options)

return fetch(url, {
method: 'POST',
body
})
.then(response => response.json())
}

const createBranchFromMaster = (repo, branch) => {
if (!repo || !branch) {
throw new Error('Repo name and branch name required to create a branch')
}

const url = `${BASE_URL}repos/${ORGANISATION}/${repo}/git/refs`
return getRepoMasterRef(repo)
.then(response => response.json())
.then(data => fetch(url, {
method: 'POST',
body: JSON.stringify({
ref: `refs/heads/${branch}`,
sha: data.object.sha
})
}))
.then(response => response.json())
}

const getRepoMasterRef = repo => {
const url = `${BASE_URL}repos/${ORGANISATION}/${repo}/git/refs/heads/master`
return fetch(url)
}

const setDefaultBranch = (repo, branch = 'develop') => {
if (!repo) {
throw new Error('Repo name required to set the default branch')
}

const url = `${BASE_URL}repos/${ORGANISATION}/${repo}`
return fetch(url, {
method: 'PATCH',
body: JSON.stringify({
name: repo,
default_branch: branch,
})
})
.then(response => response.json())
}

const protectBranch = (repo, branch) => {
if (!branch) {
throw new Error('branch name required to protect a branch')
}

const url = `${BASE_URL}repos/${ORGANISATION}/${repo}/branches/${branch}/protection`
return fetch(url, {
method: 'PUT',
headers: {
Accept: 'application/vnd.github.luke-cage-preview+json'
},
body: JSON.stringify({
required_status_checks: null,
enforce_admins: true,
required_pull_request_reviews: {
required_approving_review_count: 1,
},
restrictions: {
users: [],
teams: ['qa']
}
})
})
.then(response => response.json())
}

const newWhichRepo = repo => {
return createRepo(repo)
.then(() => createBranchFromMaster(repo, 'develop'))
.then(() => setDefaultBranch(repo, 'develop'))
.then(() => protectBranch(repo, 'master'))
.then(() => protectBranch(repo, 'develop'))
.then(() => Promise.resolve(`Repository '${repo}' successfully created`))
}

module.exports = {
createRepo,
createBranchFromMaster,
setDefaultBranch,
protectBranch,
newWhichRepo,
}
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('dotenv').config()
const github = require('./github')

const args = process.argv.slice(2)
const [ command, repo, branch ] = args

if (!github.hasOwnProperty(command)) {
throw new Error('Command not recognised')
}

github[command](repo, branch)
.then(response => console.log(response))
.catch(error => console.log(error))

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "github-manager",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"dotenv": "^6.2.0",
"node-fetch": "^2.3.0"
}
}
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


dotenv@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064"
integrity sha512-HygQCKUBSFl8wKQZBSemMywRWcEDNidvNbjGVyZu3nbZ8qq9ubiPoGLMdRDpfSrpkkm9BXYFkpKxxFX38o/76w==

node-fetch@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

0 comments on commit 3fc2b3f

Please sign in to comment.