Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic when gitea commit object contains nil pointers #237

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gitea/integration_repositories_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ var _ = Describe("Gitea Provider", func() {
Content: &content,
},
}
_, err = userRepo.Commits().Create(ctx, branchName, "added config file", files)
commit, err := userRepo.Commits().Create(ctx, branchName, "added config file", files)
Expect(err).ToNot(HaveOccurred())
Expect(commit.Get().Sha).ToNot(BeEmpty())

pr, err := userRepo.PullRequests().Create(ctx, "Added config file", branchName, defaultBranch, "added config file")
Expect(err).ToNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion gitea/integration_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var (
c gitprovider.Client

giteaUser string
giteaBaseUrl = "http://try.gitea.io"
giteaBaseUrl = "https://try.gitea.io"
testOrgName = "fluxcd-testing"
testTeamName = "fluxcd-testing-2"
// placeholders, will be randomized and created.
Expand Down
12 changes: 9 additions & 3 deletions gitea/resource_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ func (c *commitType) APIObject() interface{} {
}

func commitFromAPI(apiObj *gitea.Commit) gitprovider.CommitInfo {
return gitprovider.CommitInfo{
info := gitprovider.CommitInfo{
Sha: apiObj.SHA,
TreeSha: apiObj.RepoCommit.Tree.SHA,
Author: apiObj.Author.UserName,
Message: apiObj.RepoCommit.Message,
CreatedAt: apiObj.Author.Created,
URL: apiObj.URL,
}

if apiObj.RepoCommit != nil {
if apiObj.RepoCommit.Tree != nil {
info.TreeSha = apiObj.RepoCommit.Tree.SHA
}
info.Message = apiObj.RepoCommit.Message
}
return info
}
129 changes: 129 additions & 0 deletions gitea/resource_commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright 2023 The Flux CD contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gitea

import (
"testing"
"time"

"code.gitea.io/sdk/gitea"
"github.com/fluxcd/go-git-providers/gitprovider"
"github.com/google/go-cmp/cmp"
)

func Test_CommitFromAPI(t *testing.T) {
genTime, err := time.Parse(time.RFC3339, "2021-01-01T00:00:00Z")
if err != nil {
t.Errorf("failed to parse time: %v", err)
}
testCases := []struct {
name string
apiObj *gitea.Commit
want gitprovider.CommitInfo
}{
{
name: "full",
apiObj: &gitea.Commit{
CommitMeta: &gitea.CommitMeta{
SHA: "sha",
URL: "commitURL",
},
Author: &gitea.User{
UserName: "username",
Created: genTime,
},
Committer: &gitea.User{
UserName: "username",
Created: genTime,
},
RepoCommit: &gitea.RepoCommit{
Message: "message",
Tree: &gitea.CommitMeta{
SHA: "treesha",
},
},
},
want: gitprovider.CommitInfo{
Sha: "sha",
Author: "username",
CreatedAt: genTime,
URL: "commitURL",
Message: "message",
TreeSha: "treesha",
},
},
{
name: "nil repo commit",
apiObj: &gitea.Commit{
CommitMeta: &gitea.CommitMeta{
SHA: "sha",
URL: "commitURL",
},
Author: &gitea.User{
UserName: "username",
Created: genTime,
},
Committer: &gitea.User{
UserName: "username",
Created: genTime,
},
},
want: gitprovider.CommitInfo{
Sha: "sha",
Author: "username",
CreatedAt: genTime,
URL: "commitURL",
},
},
{
name: "nil tree",
apiObj: &gitea.Commit{
CommitMeta: &gitea.CommitMeta{
SHA: "sha",
URL: "commitURL",
},
Author: &gitea.User{
UserName: "username",
Created: genTime,
},
Committer: &gitea.User{
UserName: "username",
Created: genTime,
},
RepoCommit: &gitea.RepoCommit{
Message: "message",
},
},
want: gitprovider.CommitInfo{
Sha: "sha",
Author: "username",
CreatedAt: genTime,
URL: "commitURL",
Message: "message",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := commitFromAPI(tc.apiObj)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("commitFromAPI() mismatch (-want +got):\n%s", diff)
}
})
}
}