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_file.go
More file actions
169 lines (151 loc) · 4.48 KB
/
data_source_github_repository_file.go
File metadata and controls
169 lines (151 loc) · 4.48 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryFile() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubRepositoryFileRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "The repository name",
},
"file": {
Type: schema.TypeString,
Required: true,
Description: "The file path to manage",
},
"branch": {
Type: schema.TypeString,
Optional: true,
Description: "The branch name, defaults to the repository's default branch",
},
"ref": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the commit/branch/tag",
},
"content": {
Type: schema.TypeString,
Computed: true,
Description: "The file's content",
},
"commit_sha": {
Type: schema.TypeString,
Computed: true,
Description: "The SHA of the commit that modified the file",
},
"commit_message": {
Type: schema.TypeString,
Computed: true,
Description: "The commit message when creating or updating the file",
},
"commit_author": {
Type: schema.TypeString,
Computed: true,
Description: "The commit author name, defaults to the authenticated user's name",
},
"commit_email": {
Type: schema.TypeString,
Computed: true,
Description: "The commit author email address, defaults to the authenticated user's email address",
},
"sha": {
Type: schema.TypeString,
Computed: true,
Description: "The blob SHA of the file",
},
},
}
}
func dataSourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
diags := make(diag.Diagnostics, 0)
// checking if repo has a slash in it, which means that full_name was passed
// split and replace owner and repo
parts := strings.Split(repo, "/")
if len(parts) == 2 {
log.Printf("[DEBUG] repo has a slash, extracting owner from: %s", repo)
owner = parts[0]
repo = parts[1]
log.Printf("[DEBUG] owner: %s repo:%s", owner, repo)
}
file := d.Get("file").(string)
opts := &github.RepositoryContentGetOptions{}
if branch, ok := d.GetOk("branch"); ok {
opts.Ref = branch.(string)
}
fc, dc, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[DEBUG] Missing GitHub repository file %s/%s/%s", owner, repo, file)
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
_ = d.Set("repository", repo)
d.SetId(fmt.Sprintf("%s/%s", repo, file))
_ = d.Set("file", file)
// If the repo is a directory, then there is nothing else we can include in
// the schema.
if dc != nil {
return nil
}
content, err := fc.GetContent()
if err != nil {
return diag.FromErr(err)
}
_ = d.Set("content", content)
_ = d.Set("sha", fc.GetSHA())
parsedUrl, err := url.Parse(fc.GetURL())
if err != nil {
return diag.FromErr(err)
}
parsedQuery, err := url.ParseQuery(parsedUrl.RawQuery)
if err != nil {
return diag.FromErr(err)
}
ref := parsedQuery["ref"][0]
if err = d.Set("ref", ref); err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Unable to set ref",
Detail: fmt.Sprintf("Unable to set ref: %s", err),
})
}
log.Printf("[DEBUG] Data Source fetching commit info for repository file: %s/%s/%s", owner, repo, file)
commit, err := getFileCommit(ctx, client, owner, repo, file, ref)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Found file: %s/%s/%s, in commit SHA: %s ", owner, repo, file, commit.GetSHA())
if err = d.Set("commit_sha", commit.GetSHA()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("commit_author", commit.Commit.GetCommitter().GetName()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("commit_email", commit.Commit.GetCommitter().GetEmail()); err != nil {
return diag.FromErr(err)
}
if err = d.Set("commit_message", commit.GetCommit().GetMessage()); err != nil {
return diag.FromErr(err)
}
return diags
}