Skip to content

Commit

Permalink
Issue 1862: Gitlab status upload fails with missing commit error
Browse files Browse the repository at this point in the history
This PR adds the preloading of data into the resource.PullRequest
used in the FromDisk() method.  If the pr.json file has been copied
into the ouput directory, it is unmarshalled into the resource prior
to subsequent updates from other files on disk, avoiding overwiting
the r.PR.Sha with a null value from head.json (should that situation
occur).
  • Loading branch information
dibbles authored and tekton-robot committed Jan 16, 2020
1 parent 89409e9 commit 5e7f2ac
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ github.com/jcmturner/gofork v0.0.0-20190328161633-dc7c13fece03/go.mod h1:MK8+TM0
github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=
github.com/jenkins-x/go-scm v1.5.65 h1:ieH+0JSWENObn1SDWFj2K40iV5Eia4aTl6W6bDdLwI0=
github.com/jenkins-x/go-scm v1.5.65/go.mod h1:MgGRkJScE/rJ30J/bXYqduN5sDPZqZFITJopsnZmTOw=
github.com/jenkins-x/go-scm v1.5.66 h1:xKLQd5YGW+LAvR2xlN8IMaSzYOqW7g7s6WbbLsUuV24=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
16 changes: 15 additions & 1 deletion pkg/pullrequest/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ func FromDisk(path string) (*Resource, error) {
var err error
var manifest Manifest

// If pr.json exists in output directory, preload r.PR
prPath := filepath.Join(path, "pr.json")
if _, err := os.Stat(prPath); err == nil {
b, err := ioutil.ReadFile(prPath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(b, r.PR); err != nil {
return nil, err
}
}

commentsPath := filepath.Join(path, "comments")
r.Comments, manifest, err = commentsFromDisk(commentsPath)
if err != nil {
Expand Down Expand Up @@ -198,7 +210,9 @@ func FromDisk(path string) (*Resource, error) {
return nil, err
}

r.PR.Sha = r.PR.Head.Sha
if r.PR.Head.Sha != "" {
r.PR.Sha = r.PR.Head.Sha
}

return r, nil
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/pullrequest/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,52 @@ func TestLabelsFromDisk(t *testing.T) {
})
}
}

func TestFromDiskPRShaWithNullHeadAndBase(t *testing.T) {
d, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)

expectedSha := "1a2s3d4f5g6g6h7j8k9l"
// Write some refs
base := scm.PullRequestBranch{
Repo: scm.Repository{},
Ref: "",
Sha: "",
}
head := scm.PullRequestBranch{
Repo: scm.Repository{},
Ref: "",
Sha: "",
}
pr := scm.PullRequest{
Sha: expectedSha,
Base: base,
Head: head,
}

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)
writeFile(filepath.Join(d, "pr.json"), &pr)

rsrc, err := FromDisk(d)
if err != nil {
t.Fatal(err)
}

if rsrc.PR.Sha != expectedSha {
t.Errorf("FromDisk() returned sha `%s`, expected `%s`", rsrc.PR.Sha, expectedSha)
}

}

0 comments on commit 5e7f2ac

Please sign in to comment.