From 780d9bd23a90ddf5282f737a4b42aa1253f62d4a Mon Sep 17 00:00:00 2001 From: Kevin McDermott Date: Mon, 2 Dec 2019 21:59:45 +0000 Subject: [PATCH] Handle error opening missing pull-request parts. When uploading a pullrequest resource, if there are no comments (or statuses, or labels), then this is treated as a fatal error, and the upload terminates. This changes the behaviour to test the error, and not return the error if the directory does not exist. --- cmd/pullrequest-init/disk.go | 13 +++++++++ cmd/pullrequest-init/disk_test.go | 46 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/cmd/pullrequest-init/disk.go b/cmd/pullrequest-init/disk.go index bc6cfab766d..dba981a233f 100644 --- a/cmd/pullrequest-init/disk.go +++ b/cmd/pullrequest-init/disk.go @@ -231,6 +231,9 @@ func manifestFromDisk(path string) (Manifest, error) { func commentsFromDisk(path string) ([]*scm.Comment, Manifest, error) { fis, err := ioutil.ReadDir(path) + if isNotExistError(err) { + return nil, nil, nil + } if err != nil { return nil, nil, err } @@ -261,6 +264,9 @@ func commentsFromDisk(path string) ([]*scm.Comment, Manifest, error) { func labelsFromDisk(path string) ([]*scm.Label, Manifest, error) { fis, err := ioutil.ReadDir(path) + if isNotExistError(err) { + return nil, nil, nil + } if err != nil { return nil, nil, err } @@ -286,6 +292,9 @@ func labelsFromDisk(path string) ([]*scm.Label, Manifest, error) { func statusesFromDisk(path string) (*scm.CombinedStatus, error) { fis, err := ioutil.ReadDir(path) + if isNotExistError(err) { + return nil, nil + } if err != nil { return nil, err } @@ -318,3 +327,7 @@ func refFromDisk(path, name string) (scm.PullRequestBranch, error) { } return ref, nil } + +func isNotExistError(err error) bool { + return err != nil && os.IsNotExist(err) +} diff --git a/cmd/pullrequest-init/disk_test.go b/cmd/pullrequest-init/disk_test.go index 697ba28b202..692596c4adf 100644 --- a/cmd/pullrequest-init/disk_test.go +++ b/cmd/pullrequest-init/disk_test.go @@ -194,6 +194,52 @@ func TestToDisk(t *testing.T) { } } +func TestFromDiskWithoutComments(t *testing.T) { + d, err := ioutil.TempDir("", "") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(d) + + // Write some refs + base := scm.PullRequestBranch{ + Repo: scm.Repository{Name: "repo1"}, + Ref: "refs/heads/branch1", + Sha: "sha1", + } + head := scm.PullRequestBranch{ + Repo: scm.Repository{Name: "repo2"}, + Ref: "refs/heads/branch2", + Sha: "sha2", + } + + writeFile := func(p string, v interface{}) { + b, err := json.Marshal(v) + if err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(p, b, 0700); err != nil { + t.Fatal(err) + } + } + writeFile(filepath.Join(d, "base.json"), &base) + writeFile(filepath.Join(d, "head.json"), &head) + + rsrc, err := FromDisk(d) + if err != nil { + t.Fatal(err) + } + + // Check the refs + if diff := cmp.Diff(rsrc.PR.Base, base); diff != "" { + t.Errorf("Get Base: -want +got: %s", diff) + } + if diff := cmp.Diff(rsrc.PR.Head, head); diff != "" { + t.Errorf("Get Head: -want +got: %s", diff) + } + +} + func TestFromDisk(t *testing.T) { d, err := ioutil.TempDir("", "") if err != nil {