Skip to content

fix(scripts): BSD head portability and restore billing and Entra ID features#23

Merged
locus313 merged 2 commits into
mainfrom
fix/copilot-report-billing-entra-regressions
Jun 26, 2026
Merged

fix(scripts): BSD head portability and restore billing and Entra ID features#23
locus313 merged 2 commits into
mainfrom
fix/copilot-report-billing-entra-regressions

Conversation

@locus313

Copy link
Copy Markdown
Owner

This pull request introduces several improvements and fixes to the GitHub Copilot reporting scripts, focusing on better Azure Entra ID (Azure AD) tenant handling, improved API reliability, and more robust error reporting. The main enhancements include automatic and overrideable Entra tenant resolution, better handling of GitHub and Microsoft Graph API responses, and improved diagnostics for billing failures.

Azure Entra ID (Azure AD) Tenant Handling:

  • Added support for an entra-tenant input/option to override the Azure AD tenant ID for Microsoft Graph API lookups, with auto-resolution from the UPN domain using OIDC discovery when not explicitly set. This is reflected in both the GitHub Action and the reporting script, and is documented in the help text. [1] [2] [3] [4] [5] [6]

API Reliability and Consistency:

  • Replaced usage of head -n -1 with sed '$d' for stripping the HTTP status code from API responses, ensuring more reliable handling in all relevant functions (gh_api, _graphql_enterprise_orgs, _copilot_api). [1] [2] [3]
  • Switched Microsoft Graph user info lookups from az rest to curl with explicit bearer token and URL encoding for more robust authentication and compatibility.

GitHub Token Handling:

  • Ensured that GH_TOKEN is automatically set from GITHUB_TOKEN when using the GitHub CLI, improving compatibility with scripts that call gh api directly.

Billing API and Error Reporting:

  • Improved handling of user billing API responses: treats 404/422 as valid "no usage" (0 credits), and tracks failed lookups for better diagnostics. Adds a summary of failed users to warnings. [1] [2] [3] [4]

These changes collectively improve the reliability, usability, and diagnostics of the Copilot reporting workflow, especially in enterprise and multi-tenant Azure environments.## Description

Changes

  • New script: <domain>/github-<name>/github-<name>.sh
  • Modified: lib/github-common.sh
  • Other:

How to test

export GITHUB_TOKEN=ghp_REDACTED
export ORG=test-org
./path/to/script.sh [--dry-run]

Checklist

  • set -euo pipefail is the first executable line after the # === header
  • Script sources lib/github-common.sh via SCRIPT_DIR
  • All required env vars validated with require_env_var
  • Token validated with validate_github_token (or validate_token for secondary tokens)
  • User-supplied slugs validated with validate_slug
  • sleep added between repo-level operations to respect rate limits
  • README.md updated with the new/changed script (env var table + usage example)
  • Script header comment matches README documentation
  • Tested on a non-production org before production
  • shellcheck passes (shellcheck --severity=warning --exclude=SC2034,SC1091 --shell=bash <script>)

Patrick Lewis added 2 commits June 26, 2026 13:53
GNU head supports negative line counts (head -n -1 = all but last line)
but BSD head on macOS does not, producing 'illegal line count' errors.
Replace with sed '$d' which deletes the last line portably on both
BSD and GNU sed.

Affected: gh_api and _graphql_enterprise_orgs in lib/github-common.sh.
…actor

Three regressions introduced by the d45fe33 dual-auth refactor are fixed:

Billing API (credits used per user):
- _copilot_api used head -n -1 which fails on macOS BSD head; now uses
  sed '$d' (fixed at lib level, copied to _copilot_api in this script).
- HTTP 404 and 422 responses from the billing endpoint are valid —
  they mean no usage data this month — treat them as 0 credits instead
  of marking the user as a failure.
- Remove _billing_api() gh-CLI wrapper: now that lib syncs GH_TOKEN
  from GITHUB_TOKEN automatically, both curl and gh api use the same
  token, so the wrapper adds complexity without benefit.

Entra ID department enrichment:
- Auto-resolve the Azure AD tenant GUID from UPN_DOMAIN via OIDC
  discovery (GET login.microsoftonline.com/{domain}/.well-known/
  openid-configuration) so --entra-tenant is rarely needed.
- Switch graph_user_info from az rest to curl with the explicit
  GRAPH_TOKEN so the correct tenant's token is always used.
- Add --entra-tenant flag and $ENTRA_TENANT env var as an override
  when OIDC discovery resolves to the wrong tenant.

action.yml: update entra-tenant description to reflect auto-resolution.
@locus313 locus313 merged commit e5a17af into main Jun 26, 2026
1 check passed
@locus313 locus313 deleted the fix/copilot-report-billing-entra-regressions branch June 26, 2026 20:58
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 this pull request may close these issues.

1 participant