forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_repository_autolink_references.go
More file actions
90 lines (75 loc) · 2 KB
/
data_source_github_repository_autolink_references.go
File metadata and controls
90 lines (75 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package github
import (
"context"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryAutolinkReferences() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryAutolinkReferencesRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"autolink_references": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key_prefix": {
Type: schema.TypeString,
Computed: true,
},
"target_url_template": {
Type: schema.TypeString,
Computed: true,
},
"is_alphanumeric": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubRepositoryAutolinkReferencesRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
results := make([]map[string]any, 0)
for {
listOptions := &github.ListOptions{}
autoLinks, resp, err := client.Repositories.ListAutolinks(context.Background(), orgName, repoName, listOptions)
if err != nil {
return err
}
results = append(results, flattenAutolinkReferences(autoLinks)...)
if resp.NextPage == 0 {
break
}
listOptions.Page = resp.NextPage
}
d.SetId(repoName)
err := d.Set("autolink_references", results)
if err != nil {
return err
}
return nil
}
func flattenAutolinkReferences(autoLinks []*github.Autolink) []map[string]any {
results := make([]map[string]any, 0)
if autoLinks == nil {
return results
}
for _, autolink := range autoLinks {
linkMap := make(map[string]any)
linkMap["key_prefix"] = autolink.GetKeyPrefix()
linkMap["target_url_template"] = autolink.GetURLTemplate()
linkMap["is_alphanumeric"] = autolink.GetIsAlphanumeric()
results = append(results, linkMap)
}
return results
}