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 a feature that creates the users.json file #4

Open
don-smith opened this issue Jan 3, 2021 · 1 comment · May be fixed by #5
Open

Add a feature that creates the users.json file #4

don-smith opened this issue Jan 3, 2021 · 1 comment · May be fixed by #5
Assignees

Comments

@don-smith
Copy link
Contributor

don-smith commented Jan 3, 2021

Given a JSON file that is an array of GitHub usernames, a --build feature could create the users.json file. Here is an example of that JSON array of usernames.

[
  "github-username-1",
  "github-username-2"
]

It will need a GITHUB_AUTH_TOKEN environment variable defined in order to create the GitHub-specific email addresses for the users (way better than what the README currently describes). Here is some code that is very close to what we currently use outside of git-iam to automatically create the users.json file.

/* eslint-disable no-console */
const fs = require('fs')
const { Octokit } = require('@octokit/rest')

const students = [
  'student-github-username-1',
  'student-github-username-2'
]

const authToken = process.env.GITHUB_AUTH_TOKEN

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

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

Promise.all(usernames.sort().map(username => github.users.getByUsername({ username })))
  .then(results => {
    /* eslint-disable camelcase */
    const userData = results
      .map(result => {
        const user = result.data
        const id = user.id
        const username = user.login
        const name = user.name || user.login
        const created_at = user.created_at
        const email = user.id + '+' + user.login + '@users.noreply.github.com'
        return { id, name, username, email, created_at }
      })
      .reduce((userObj, user) => {
        userObj[user.username.toLowerCase()] = user
        return userObj
      }, {})

    fs.writeFile('users.json', JSON.stringify(userData, null, 2), 'utf8', console.log)
    console.log(userData)
  })
  .catch(err => {
    console.error(err)
  })
@don-smith
Copy link
Contributor Author

Better yet, if instead of an array of usernames we used objects that defined the user's name and their GitHub username, we can set the correct name of the user. This would let the user git iam my-first-name instead of git iam github-username. Obviously, we wouldn't be able to have two users with the same name though. Here is what the array could look like.

[
  { "name": "jane", "username": "jane-on-github" },
  { "name": "joe", "username": "joe-on-github" }
]

@lache-melvin lache-melvin self-assigned this Jan 4, 2021
@lache-melvin lache-melvin linked a pull request May 18, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants