Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/actions/checkout-4
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Sep 11, 2023
2 parents 5170404 + a361b15 commit 60b3bcf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/immediate-response.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: echo "NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"

- name: Respond to issue or PR
uses: peter-evans/create-or-update-comment@94ff3426b71db76bdf47e8a2f6446d88727c7443
uses: peter-evans/create-or-update-comment@223779bc560943cb8f2aa0484a7c225c1585c597
with:
issue-number: ${{ steps.extract.outputs.NUMBER }}
body: >
Expand Down
38 changes: 38 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package github
import (
"fmt"
"log"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -335,6 +338,14 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
token = appToken
}

if token == "" {
ghAuthToken, err := tokenFromGhCli(baseURL)
if err != nil {
return nil, fmt.Errorf("gh auth token: %w", err)
}
token = ghAuthToken
}

writeDelay := d.Get("write_delay_ms").(int)
if writeDelay <= 0 {
return nil, fmt.Errorf("write_delay_ms must be greater than 0ms")
Expand Down Expand Up @@ -378,3 +389,30 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return meta, nil
}
}

// See https://github.com/integrations/terraform-provider-github/issues/1822
func tokenFromGhCli(baseURL string) (string, error) {
ghCliPath := os.Getenv("GH_PATH")
if ghCliPath == "" {
ghCliPath = "gh"
}
hostname := ""
if baseURL == "" {
hostname = "api.github.com"
} else {
parsedURL, err := url.Parse(baseURL)
if err != nil {
return "", fmt.Errorf("parse %s: %w", baseURL, err)
}
hostname = parsedURL.Host
}
out, err := exec.Command(ghCliPath, "auth", "token", "--hostname", hostname).Output()
if err != nil {
// GH CLI is either not installed or there was no `gh auth login` command issued,
// which is fine. don't return the error to keep the flow going
return "", nil
}

log.Printf("[INFO] Using the token from GitHub CLI")
return strings.TrimSpace(string(out)), nil
}
2 changes: 1 addition & 1 deletion github/resource_github_organization_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
ConflictsWith: []string{"conditions.0.repository_id"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"inlcude": {
"include": {
Type: schema.TypeList,
Required: true,
Description: "Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts `~ALL` to include all repositories.",
Expand Down
4 changes: 4 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ resource "github_membership" "membership_for_user_x" {

The GitHub provider offers multiple ways to authenticate with GitHub API.

### GitHub CLI

The GitHub provider taps into [GitHub CLI](https://cli.github.com/) authentication, where it picks up the token issued by [`gh auth login`](https://cli.github.com/manual/gh_auth_login) command. It is possible to specify the path to the `gh` executable in the `GH_PATH` environment variable, which is useful for when the GitHub Terraform provider can not properly determine its the path to GitHub CLI such as in the cygwin terminal.

### OAuth / Personal Access Token

To authenticate using OAuth tokens, ensure that the `token` argument or the `GITHUB_TOKEN` environment variable is set.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/organization_ruleset.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ One of `repository_id` and `repository_name` must be set for the rule to target

* `exclude` - (Required) (List of String) Array of repository names or patterns to exclude. The condition will not pass if any of these patterns match.

* `inlcude` - (Required) (List of String) Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts `~ALL` to include all repositories.
* `include` - (Required) (List of String) Array of repository names or patterns to include. One of these patterns must match for the condition to pass. Also accepts `~ALL` to include all repositories.

## Attributes Reference

Expand Down

0 comments on commit 60b3bcf

Please sign in to comment.