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 committed Jan 13, 2020
1 parent 1066bfc commit d09c16c
Show file tree
Hide file tree
Showing 3 changed files with 73 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
24 changes: 23 additions & 1 deletion pkg/pullrequest/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ func FromDisk(path string) (*Resource, error) {
var err error
var manifest Manifest

// SCM struct for gitlab pull request does not contain any data that would be written
// into head.json or base.json. Therefore we cannot rely on using that information later
// in this method to set top level properties. Therefore if the pr.json is in the output
// dir we will prepopulate r.PR and then update.
prPath := filepath.Join(path, "pr.json")
_, err = os.Stat(prPath)
if os.IsNotExist(err) {
// File does not exist, skipping preload data, might cause problems for updating
// gitlab statuses
} else {
b, err := ioutil.ReadFile(path + "/pr.json")
if err != nil {
return nil, err
}
err = json.Unmarshal(b, r.PR)
if 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 +218,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 d09c16c

Please sign in to comment.