Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --build option for creating users.json #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@ npm install git-iam --global
## Setup

After installation:

If you don't already have a `users.json` file:

```sh
git-iam --init https://raw.githubusercontent.com/org/repo/branch/users.json
GH_AUTH=<GitHub personal access token> git iam --build <input path>
```

This command
This command builds a JSON object in the format required for `git-iam`, and outputs it to the console.

* saves the user list URL to the global Git config `users.url`
* adds the `iam` alias to the global Git config `alias.iam`
A GitHub access token is required to retrieve user data from the GitHub API.

The `users.json` file (or whatever you decide to call it) should have this format:
The `<input path>` should point to a JSON file that has this format:

```json
{
"jane": "jane-on-github",
"joe": "joe-on-github"
}
```

Or feel free to skip this step and build your own. It should have this format:

```json
{
Expand All @@ -51,6 +61,19 @@ The `users.json` file (or whatever you decide to call it) should have this forma
}
```

Save the JSON object to a Gist, i.e. [https://raw.githubusercontent.com/org/repo/branch/users.json](#)

Once you have a URL to your `users.json` file:

```sh
git-iam --init https://raw.githubusercontent.com/org/repo/branch/users.json
```

This command

* saves the user list URL to the global Git config `users.url`
* adds the `iam` alias to the global Git config `alias.iam`

Now you can run this inside of an existing Git repository:

```sh
Expand Down
88 changes: 86 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#! /usr/bin/env node

const fs = require('fs').promises
const path = require('path')
const shell = require('shelljs')
const request = require('superagent')
const { Octokit } = require('@octokit/rest')

const alias = process.argv[2].toLowerCase()
const listingUrl = process.argv[3]
const inputPath = process.argv[3]

if (!alias) {
showUsage()
} else if (alias === '--init') {
setupExtension(listingUrl)
setupExtension(inputPath)
} else if (alias === '--build') {
createUsersJson(inputPath)
} else {
configureUser(alias)
}

function showUsage () {
console.info(`Usage:
GH_AUTH=<GitHub personal access token> git iam --build <input path>
Creates the users JSON file required for git iam, and prints it to the console.
The input path should point to a JSON file with the following format:
{
"jane": "jane-on-github",
"joe": "joe-on-github",
...
}
git-iam --init url
Sets up the extension. The url should be the address of a JSON file.
git iam user
Expand Down Expand Up @@ -61,3 +74,74 @@ async function getUserListing (url) {
}
}

async function createUsersJson (inputPath) {
const developers = await getDevData(inputPath)
const userData = await getUserData(developers)

try {
const formattedUserData = userData.map((result, idx) => {
const { id, login, name } = result.data
return {
name: name || login,
email: id + '+' + login + '@users.noreply.github.com',
alias: Object.keys(developers)[idx]
}
})
.sort((user1, user2) => {
if (user1.alias < user2.alias) {
return -1
}
if (user1.alias > user2.alias) {
return 1
}
return 0
})
.reduce((userObj, user) => {
const { alias, name, email } = user
userObj[alias] = { name, email }
return userObj
}, {})

const userJSON = JSON.stringify(formattedUserData, null, 2)
console.info(`\nSuccess! Here's your git iam JSON object. Save it to a Gist!`)
console.info(userJSON)
} catch (err) {
console.error('An error occurred while trying to create your users JSON object:')
console.error(err)
showUsage()
}
}

async function getDevData (devsPath) {
try {
const devData = await fs.readFile(path.resolve(devsPath))
return JSON.parse(devData)
} catch (err) {
console.error('An error occurred while trying to read your input JSON file:')
console.error(err)
showUsage()
process.exit(1)
}
}

async function getUserData (developers) {
const authToken = process.env.GH_AUTH

if (!authToken) {
console.error('Could not find your GitHub Personal Access Token in GH_AUTH')
console.info('You can proceed after you complete that step.')
process.exit(1)
}

const github = new Octokit({ auth: authToken })

try {
const results = await Promise.all(Object.values(developers).map(username => github.users.getByUsername({ username })))
return results
} catch (err) {
console.error('An error occurred while trying to access the user data from GitHub:')
console.error(err)
showUsage()
process.exit(1)
}
}
138 changes: 137 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-iam",
"version": "0.2.2",
"version": "0.3.0",
"description": "Git extension for setting name and email config quickly",
"repository": "https://github.com/enspiral-dev-academy/git-iam",
"author": "Don Smith <[email protected]>",
Expand All @@ -11,6 +11,7 @@
"git-iam": "bin/index.js"
},
"dependencies": {
"@octokit/rest": "^18.5.3",
"shelljs": "^0.8.4",
"superagent": "^6.1.0"
}
Expand Down