forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_ref_status.go
70 lines (55 loc) · 1.73 KB
/
github_ref_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"fmt"
"github.com/google/go-github/github"
)
type refStatusState string
// These statuses model the statuses mentioned here: https://developer.github.com/v3/repos/statuses/#create-a-status
const (
RSPending refStatusState = "pending"
RSSuccess refStatusState = "success"
RSError refStatusState = "error"
RSFailure refStatusState = "failure"
)
func setRefStatus(token, owner, repo, ref string, state refStatusState, statusURL string, description string) error {
client, err := getClientForToken(token)
if err != nil {
return err
}
stateStr := string(state)
statusBefore := &github.RepoStatus{State: &stateStr, TargetURL: &statusURL, Description: &description}
repoStatus, res, err := client.Repositories.CreateStatus(owner, repo, ref, statusBefore)
if err != nil {
return err
}
if repoStatus == nil {
return fmt.Errorf("github client returned nil for repo status")
}
if repoStatus.ID == nil {
return fmt.Errorf("github client returned nil for repo status id")
}
return verifyHTTPCreated(res)
}
func getMergeCommitSha(token, owner, repo string, number int64) (string, error) {
client, err := getClientForToken(token)
if err != nil {
return "", err
}
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, int(number))
req, err := client.NewRequest("GET", u, nil)
if err != nil {
return "", err
}
pull := new(pullRequest)
_, err = client.Do(req, pull)
if err != nil {
return "", err
}
if pull == nil {
return "", fmt.Errorf("github client returned nil for pull request")
}
return pull.MergeCommitSha, nil
}