|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/google/go-github/v66/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func dataSourceGithubOrganizationRepositoryRole() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "Lookup a custom organization repository role.", |
| 15 | + |
| 16 | + Read: dataSourceGithubOrganizationRepositoryRoleRead, |
| 17 | + |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "role_id": { |
| 20 | + Description: "The ID of the organization repository role.", |
| 21 | + Type: schema.TypeInt, |
| 22 | + Required: true, |
| 23 | + }, |
| 24 | + "name": { |
| 25 | + Description: "The name of the organization repository role.", |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "description": { |
| 30 | + Description: "The description of the organization repository role.", |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "base_role": { |
| 35 | + Description: "The system role from which this role inherits permissions.", |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "permissions": { |
| 40 | + Description: "The permissions included in this role.", |
| 41 | + Type: schema.TypeSet, |
| 42 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func dataSourceGithubOrganizationRepositoryRoleRead(d *schema.ResourceData, meta interface{}) error { |
| 50 | + client := meta.(*Owner).v3client |
| 51 | + ctx := context.Background() |
| 52 | + orgName := meta.(*Owner).name |
| 53 | + |
| 54 | + roleId := int64(d.Get("role_id").(int)) |
| 55 | + |
| 56 | + // TODO: Use this code when go-github adds the functionality to get a custom repo role |
| 57 | + // role, _, err := client.Organizations.GetCustomRepoRole(ctx, orgName, roleId) |
| 58 | + // if err != nil { |
| 59 | + // return err |
| 60 | + // } |
| 61 | + |
| 62 | + roles, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + var role *github.CustomRepoRoles |
| 68 | + for _, r := range roles.CustomRepoRoles { |
| 69 | + if r.GetID() == roleId { |
| 70 | + role = r |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + if role == nil { |
| 75 | + return fmt.Errorf("custom organization repo role with ID %d not found", roleId) |
| 76 | + } |
| 77 | + |
| 78 | + r := map[string]any{ |
| 79 | + "role_id": role.GetID(), |
| 80 | + "name": role.GetName(), |
| 81 | + "description": role.GetDescription(), |
| 82 | + "base_role": role.GetBaseRole(), |
| 83 | + "permissions": role.Permissions, |
| 84 | + } |
| 85 | + |
| 86 | + d.SetId(strconv.FormatInt(role.GetID(), 10)) |
| 87 | + |
| 88 | + for k, v := range r { |
| 89 | + if err := d.Set(k, v); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
0 commit comments