Skip to content

Commit

Permalink
Handle error opening missing pull-request parts.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bigkevmcd authored and tekton-robot committed Dec 3, 2019
1 parent f4ce23c commit 780d9bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/pullrequest-init/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
46 changes: 46 additions & 0 deletions cmd/pullrequest-init/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 780d9bd

Please sign in to comment.