terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

github_user doesn't retrieve organization email

Open DavidGamba opened this issue 3 years ago • 3 comments

terraform version

Terraform v1.0.11 on darwin_amd64

  • provider registry.terraform.io/hashicorp/external v2.1.0
  • provider registry.terraform.io/integrations/github v4.18.2

Tested with a Full Access personal access token.

Affected Resource(s)

  • github_user

Terraform Configuration Files

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
}

# Assumes GITHUB_TOKEN is set
provider "github" {
  owner = "my-org"
}

data "github_organization_teams" "all" {}

locals {
  # members = setunion(flatten(data.github_organization_teams.all.teams.*.members))
  members = toset(["DavidGamba"])
}

data "github_user" "reviewers" {
  for_each = local.members
  username = each.key
}

output "reviewers" {
  # value = [for o in data.github_user.reviewers : o.email]
  value = data.github_user.reviewers
}

The output is the following:

 + reviewers       = {
      + DavidGamba = {
          + avatar_url   = "https://avatars.githubusercontent.com/u/xxx?v=4"
          + bio          = ""
          + blog         = ""
          + company      = ""
          + created_at   = null
          + email        = ""
          + followers    = 18
          + following    = 19
          + gpg_keys     = []
          + gravatar_id  = ""
          + id           = "xxx"
          + location     = ""
          + login        = "DavidGamba"
          + name         = "David Gamba"

Actual Behavior

Organization email doesn't show up in the github_user data block even though the org is defined in the provider.

Expected Behavior

When I query my organization for members I can clearly retrieve the email:

image

github_user should retrieve the organization email.

End Goal

I want to be able to tie organizational email to github IDs.

Additional notes

I have 3 email addresses in my account. However, the UI is smart enough to only retrieve the organization email when I query for the organization members. The github_user resource doesn't have any other attribute that is a list of emails where I could pick one if there were multiple returned.

DavidGamba avatar Dec 01 '21 21:12 DavidGamba

Yep, I get this too; I thought it was down to privileges of the GitHub App I am using, but it has the User Email ReadOnly right (amongst others).

image

richardj-bsquare avatar Mar 25 '22 09:03 richardj-bsquare

Okay, looking into this further, I have managed to get the email address, by setting the email address in the profile (which defaults to blank).

This is because we seem to be using the following API: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user

Whereas, ideally we should use this API to acquire all the email addresses: https://docs.github.com/en/rest/reference/users#list-email-addresses-for-the-authenticated-user

I'm actually after the primary sign-up email address, but doing the above would at least enable me to filter it, since the primary flag is part of the response.

richardj-bsquare avatar Mar 25 '22 09:03 richardj-bsquare

@richardj-bsquare I don't think that API endpoint will work as is only gives back the emails of the currently authenticated user, this approach would require that you use the basic auth in the API call to pass the username/password for each user you want to get this information for.

kuhlman-labs avatar Sep 12 '22 13:09 kuhlman-labs

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

github-actions[bot] avatar Jun 10 '23 02:06 github-actions[bot]