Skip to content

Commit 2342bc7

Browse files
Merge pull request #48 from form3tech-oss/fmatavelli-update-go
chore: update go to v1.25
2 parents d0a03c3 + 896c5d6 commit 2342bc7

File tree

3,383 files changed

+1051477
-63
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,383 files changed

+1051477
-63
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/** linguist-generated

.github/workflows/ci.yaml

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
name: CI
2-
on:
3-
push:
4-
branches:
5-
- master
6-
tags:
7-
- v*.*.*
8-
pull_request:
2+
3+
on: [pull_request]
4+
5+
permissions:
6+
id-token: write
7+
contents: write
8+
pull-requests: write
9+
10+
defaults:
11+
run:
12+
shell: bash
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.ref }}
16+
cancel-in-progress: true
17+
18+
env:
19+
TF_ACC: 1
20+
COMMIT_MESSAGE_PREFIX: "[skip ci]"
21+
GITHUB_TOKEN: ${{ github.token }}
22+
GITHUB_USERNAME: "github-actions[bot]"
23+
GITHUB_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"
924

1025
jobs:
11-
build_test:
26+
tests:
1227
runs-on: ubuntu-latest
13-
env:
14-
TF_ACC: 1
15-
GITHUB_TOKEN: ${{ secrets.TF_PROVIDER_GITHUBFILE_GITHUB_TOKEN }}
1628
steps:
17-
- name: Checkout Code
18-
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
19-
- name: Setup Golang
20-
uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
29+
- name: Checkout
30+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
31+
32+
- name: Set up Go
33+
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
2134
with:
22-
go-version-file: go.mod
23-
- name: Make
24-
run: make
25-
publish:
26-
if: startsWith(github.ref, 'refs/tags/')
27-
runs-on: ubuntu-latest
28-
steps:
29-
- name: Checkout Code
30-
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
31-
- name: Setup Golang
32-
uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 # v4.0.0
33-
- name: Goreleaser
34-
run: ./scripts/goreleaser.sh
35-
env:
36-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
go-version-file: 'go.mod'
36+
cache: true
37+
38+
- name: Tests
39+
run: |
40+
make test

.github/workflows/release.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: release
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
tag-release:
10+
uses: form3tech/tfe-providers-gh-workflows/.github/workflows/release.yml@v0.2.10
11+
secrets: inherit
12+
permissions:
13+
id-token: write
14+
contents: write
15+

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.idea/
22
bin/
33
dist/
4-
vendor/

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ vet:
1616

1717
.PHONY: test
1818
test:
19-
GITHUB_EMAIL=$(GITHUB_EMAIL) \
20-
GITHUB_USERNAME=$(GITHUB_USERNAME) \
21-
COMMIT_MESSAGE_PREFIX=$(COMMIT_MESSAGE_PREFIX) \
22-
TF_ACC=$(TF_ACC) \
19+
GITHUB_EMAIL="$(GITHUB_EMAIL)" \
20+
GITHUB_USERNAME="$(GITHUB_USERNAME)" \
21+
COMMIT_MESSAGE_PREFIX="$(COMMIT_MESSAGE_PREFIX)" \
22+
TF_ACC="$(TF_ACC)" \
2323
go test -count 1 -v ./...

githubfile/resource_file.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package githubfile
1717
import (
1818
"context"
1919
"fmt"
20+
"log"
2021
"strings"
2122
"time"
2223

@@ -99,6 +100,7 @@ func resourceFileCreateOrUpdate(s string, d *schema.ResourceData, m interface{})
99100
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
100101
RepoOwner: f.repositoryOwner,
101102
RepoName: f.repositoryName,
103+
Branch: f.branch,
102104
CommitMessage: formatCommitMessage(c.commitMessagePrefix, s, f.path),
103105
GpgPassphrase: c.gpgPassphrase,
104106
GpgPrivateKey: c.gpgSecretKey,
@@ -119,6 +121,18 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
119121
c := m.(*providerConfiguration)
120122
f := expandFile(d)
121123

124+
// Check if the repository is archived. If so, skip the delete operation
125+
// and just remove the resource from state, since archived repositories
126+
// cannot be modified.
127+
repo, _, err := c.githubClient.Repositories.Get(context.Background(), f.repositoryOwner, f.repositoryName)
128+
if err != nil {
129+
return fmt.Errorf("failed to retrieve repository %s/%s: %v", f.repositoryOwner, f.repositoryName, err)
130+
}
131+
if repo.GetArchived() {
132+
log.Printf("[WARN] Repository %s/%s is archived, skipping file deletion and removing %q from state", f.repositoryOwner, f.repositoryName, f.path)
133+
return nil
134+
}
135+
122136
// Check whether the file exists.
123137
fileContent, err := ghfileutils.GetFile(context.Background(),
124138
c.githubClient,
@@ -154,6 +168,7 @@ func resourceFileDelete(d *schema.ResourceData, m interface{}) error {
154168
if err := commit.CreateCommit(context.Background(), c.githubClient, &commit.CommitOptions{
155169
RepoOwner: f.repositoryOwner,
156170
RepoName: f.repositoryName,
171+
Branch: f.branch,
157172
CommitMessage: formatCommitMessage(c.commitMessagePrefix, "Delete %q.", f.path),
158173
GpgPassphrase: c.gpgPassphrase,
159174
GpgPrivateKey: c.gpgSecretKey,

githubfile/resource_file_test.go

Lines changed: 176 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,106 @@ package githubfile
1717
import (
1818
"context"
1919
"fmt"
20+
"math/rand"
21+
"net/http"
22+
"net/http/httptest"
23+
"net/url"
24+
"os"
2025
"testing"
2126

27+
"github.com/form3tech-oss/go-github-utils/pkg/branch"
2228
ghfileutils "github.com/form3tech-oss/go-github-utils/pkg/file"
29+
"github.com/google/go-github/v54/github"
2330
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
31+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
2432
"github.com/hashicorp/terraform-plugin-sdk/terraform"
33+
"golang.org/x/oauth2"
2534
)
2635

2736
const (
28-
testAccFileCreate = `
37+
testRepoOwner = "form3tech-oss"
38+
testRepoName = "terraform-provider-githubfile"
39+
)
40+
41+
var testBranchName string
42+
43+
func newGitHubClient() *github.Client {
44+
token := os.Getenv("GITHUB_TOKEN")
45+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
46+
tc := oauth2.NewClient(context.Background(), ts)
47+
return github.NewClient(tc)
48+
}
49+
50+
func TestMain(m *testing.M) {
51+
testBranchName = fmt.Sprintf("acc-test-%d", rand.Int63())
52+
53+
code := m.Run()
54+
55+
// Cleanup: delete the test branch if it was created
56+
if os.Getenv("GITHUB_TOKEN") != "" {
57+
cleanupTestBranch()
58+
}
59+
60+
os.Exit(code)
61+
}
62+
63+
func cleanupTestBranch() {
64+
client := newGitHubClient()
65+
ref := fmt.Sprintf("refs/heads/%s", testBranchName)
66+
_, err := client.Git.DeleteRef(context.Background(), testRepoOwner, testRepoName, ref)
67+
if err != nil {
68+
fmt.Fprintf(os.Stderr, "failed to delete test branch %s: %v\n", testBranchName, err)
69+
}
70+
}
71+
72+
func createTestBranch(t *testing.T) {
73+
t.Helper()
74+
75+
client := newGitHubClient()
76+
ctx := context.Background()
77+
78+
// Get the SHA for the default branch (master)
79+
sha, err := branch.GetSHAForBranch(ctx, client, testRepoOwner, testRepoName, "master")
80+
if err != nil {
81+
t.Fatalf("failed to get SHA for master branch: %v", err)
82+
}
83+
84+
// Create the test branch
85+
ref := &github.Reference{
86+
Ref: github.String(fmt.Sprintf("refs/heads/%s", testBranchName)),
87+
Object: &github.GitObject{
88+
SHA: github.String(sha),
89+
},
90+
}
91+
_, _, err = client.Git.CreateRef(ctx, testRepoOwner, testRepoName, ref)
92+
if err != nil {
93+
t.Fatalf("failed to create test branch %s: %v", testBranchName, err)
94+
}
95+
}
96+
97+
func testAccFileCreateConfig() string {
98+
return fmt.Sprintf(`
2999
resource "githubfile_file" "foo" {
30-
repository_owner = "form3tech-oss"
31-
repository_name = "terraform-provider-githubfile-test"
32-
branch = "master"
33-
path = "foo/bar/baz/README.md"
34-
contents = "foo\nbar\nbaz"
100+
repository_owner = "%s"
101+
repository_name = "%s"
102+
branch = "%s"
103+
path = "foo/bar/baz/README.md"
104+
contents = "foo\nbar\nbaz"
105+
}
106+
`, testRepoOwner, testRepoName, testBranchName)
35107
}
36-
`
37-
testAccFileUpdate = `
108+
109+
func testAccFileUpdateConfig() string {
110+
return fmt.Sprintf(`
38111
resource "githubfile_file" "foo" {
39-
repository_owner = "form3tech-oss"
40-
repository_name = "terraform-provider-githubfile-test"
41-
branch = "master"
42-
path = "foo/bar/baz/README.md"
43-
contents = "foo\nbar\nqux"
112+
repository_owner = "%s"
113+
repository_name = "%s"
114+
branch = "%s"
115+
path = "foo/bar/baz/README.md"
116+
contents = "foo\nbar\nqux"
117+
}
118+
`, testRepoOwner, testRepoName, testBranchName)
44119
}
45-
`
46-
)
47120

48121
func TestAccResourceFile_basic(t *testing.T) {
49122
var (
@@ -55,18 +128,19 @@ func TestAccResourceFile_basic(t *testing.T) {
55128
resource.Test(t, resource.TestCase{
56129
PreCheck: func() {
57130
testAccPreCheck(t)
131+
createTestBranch(t)
58132
},
59133
IDRefreshName: resourceName,
60134
Providers: testAccProviders,
61135
CheckDestroy: testAccCheckFileDestroy,
62136
Steps: []resource.TestStep{
63137
{
64-
Config: testAccFileCreate,
138+
Config: testAccFileCreateConfig(),
65139
Check: resource.ComposeAggregateTestCheckFunc(
66140
testAccCheckFileExists(resourceName, &before),
67-
resource.TestCheckResourceAttr(resourceName, repositoryNameAttributeName, "terraform-provider-githubfile-test"),
68-
resource.TestCheckResourceAttr(resourceName, repositoryOwnerAttributeName, "form3tech-oss"),
69-
resource.TestCheckResourceAttr(resourceName, branchAttributeName, "master"),
141+
resource.TestCheckResourceAttr(resourceName, repositoryNameAttributeName, testRepoName),
142+
resource.TestCheckResourceAttr(resourceName, repositoryOwnerAttributeName, testRepoOwner),
143+
resource.TestCheckResourceAttr(resourceName, branchAttributeName, testBranchName),
70144
resource.TestCheckResourceAttr(resourceName, pathAttributeName, "foo/bar/baz/README.md"),
71145
resource.TestCheckResourceAttr(resourceName, contentsAttributeName, "foo\nbar\nbaz"),
72146
),
@@ -77,12 +151,12 @@ func TestAccResourceFile_basic(t *testing.T) {
77151
ImportStateVerify: true,
78152
},
79153
{
80-
Config: testAccFileUpdate,
154+
Config: testAccFileUpdateConfig(),
81155
Check: resource.ComposeAggregateTestCheckFunc(
82156
testAccCheckFileExists(resourceName, &before),
83-
resource.TestCheckResourceAttr(resourceName, repositoryNameAttributeName, "terraform-provider-githubfile-test"),
84-
resource.TestCheckResourceAttr(resourceName, repositoryOwnerAttributeName, "form3tech-oss"),
85-
resource.TestCheckResourceAttr(resourceName, branchAttributeName, "master"),
157+
resource.TestCheckResourceAttr(resourceName, repositoryNameAttributeName, testRepoName),
158+
resource.TestCheckResourceAttr(resourceName, repositoryOwnerAttributeName, testRepoOwner),
159+
resource.TestCheckResourceAttr(resourceName, branchAttributeName, testBranchName),
86160
resource.TestCheckResourceAttr(resourceName, pathAttributeName, "foo/bar/baz/README.md"),
87161
resource.TestCheckResourceAttr(resourceName, contentsAttributeName, "foo\nbar\nqux"),
88162
),
@@ -137,3 +211,82 @@ func testAccCheckFileDestroy(s *terraform.State) error {
137211
}
138212
return nil
139213
}
214+
215+
// newTestResourceData creates a schema.ResourceData with the file resource schema
216+
// populated from the given raw values, suitable for unit testing.
217+
func newTestResourceData(t *testing.T, values map[string]interface{}) *schema.ResourceData {
218+
t.Helper()
219+
return schema.TestResourceDataRaw(t, resourceFile().Schema, values)
220+
}
221+
222+
// newMockGitHubClient creates a github.Client pointing at the given httptest.Server.
223+
func newMockGitHubClient(server *httptest.Server) *github.Client {
224+
client := github.NewClient(server.Client())
225+
serverURL, _ := url.Parse(server.URL + "/")
226+
client.BaseURL = serverURL
227+
return client
228+
}
229+
230+
func TestResourceFileDelete_ArchivedRepo(t *testing.T) {
231+
// Set up a mock HTTP server that returns an archived repository
232+
mux := http.NewServeMux()
233+
mux.HandleFunc("/repos/test-owner/test-repo", func(w http.ResponseWriter, r *http.Request) {
234+
w.Header().Set("Content-Type", "application/json")
235+
fmt.Fprint(w, `{"id":1,"name":"test-repo","full_name":"test-owner/test-repo","archived":true}`)
236+
})
237+
server := httptest.NewServer(mux)
238+
defer server.Close()
239+
240+
config := &providerConfiguration{
241+
githubClient: newMockGitHubClient(server),
242+
}
243+
244+
d := newTestResourceData(t, map[string]interface{}{
245+
repositoryOwnerAttributeName: "test-owner",
246+
repositoryNameAttributeName: "test-repo",
247+
branchAttributeName: "main",
248+
pathAttributeName: "some/file.txt",
249+
contentsAttributeName: "some content",
250+
})
251+
d.SetId("test-owner/test-repo:main:some/file.txt")
252+
253+
// Delete should succeed without error — archived repo skips deletion
254+
err := resourceFileDelete(d, config)
255+
if err != nil {
256+
t.Fatalf("expected no error for archived repo deletion, got: %v", err)
257+
}
258+
}
259+
260+
func TestResourceFileDelete_NonArchivedRepo_FileNotFound(t *testing.T) {
261+
// Set up a mock HTTP server that returns a non-archived repository
262+
// and a 404 for the file content (file doesn't exist)
263+
mux := http.NewServeMux()
264+
mux.HandleFunc("/repos/test-owner/test-repo", func(w http.ResponseWriter, r *http.Request) {
265+
w.Header().Set("Content-Type", "application/json")
266+
fmt.Fprint(w, `{"id":1,"name":"test-repo","full_name":"test-owner/test-repo","archived":false}`)
267+
})
268+
mux.HandleFunc("/repos/test-owner/test-repo/contents/some/file.txt", func(w http.ResponseWriter, r *http.Request) {
269+
w.WriteHeader(http.StatusNotFound)
270+
})
271+
server := httptest.NewServer(mux)
272+
defer server.Close()
273+
274+
config := &providerConfiguration{
275+
githubClient: newMockGitHubClient(server),
276+
}
277+
278+
d := newTestResourceData(t, map[string]interface{}{
279+
repositoryOwnerAttributeName: "test-owner",
280+
repositoryNameAttributeName: "test-repo",
281+
branchAttributeName: "main",
282+
pathAttributeName: "some/file.txt",
283+
contentsAttributeName: "some content",
284+
})
285+
d.SetId("test-owner/test-repo:main:some/file.txt")
286+
287+
// Delete should succeed — file not found is treated as already deleted
288+
err := resourceFileDelete(d, config)
289+
if err != nil {
290+
t.Fatalf("expected no error when file not found on non-archived repo, got: %v", err)
291+
}
292+
}

0 commit comments

Comments
 (0)