forked from walter-cd/walter
/
github.go
123 lines (108 loc) · 3.72 KB
/
github.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* walter: a deployment pipeline template
* Copyright (C) 2014 Recruit Technologies Co., Ltd. and contributors
* (see CONTRIBUTORS.md)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Package services provides the functionality for all supported services (GitHub)
package services
import (
"container/list"
"regexp"
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
"github.com/recruit-tech/walter/log"
)
//GitHubClient struct
type GitHubClient struct {
Repo string `config:"repo"`
From string `config:"from"`
Token string `config:"token"`
UpdateFile string `config:"update"`
TargetBranch string `config:"branch"`
}
//GetUpdateFilePath returns the update file name
func (githubClient *GitHubClient) GetUpdateFilePath() string {
if githubClient.UpdateFile != "" {
return githubClient.UpdateFile
}
return DefaultUpdateFileName
}
//RegisterResult registers the supplied result
func (githubClient *GitHubClient) RegisterResult(result Result) error {
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: githubClient.Token},
}
client := github.NewClient(t.Client())
log.Info("Submitting result")
repositories := client.Repositories
status, _, err := repositories.CreateStatus(
githubClient.From,
githubClient.Repo,
result.SHA,
&github.RepoStatus{
State: github.String(result.State),
TargetURL: github.String(""),
Description: github.String(result.Message),
Context: github.String("continuous-integraion/walter"),
})
log.Infof("Submit status: %s", status)
if err != nil {
log.Errorf("Failed to register result: %s", err)
}
return err
}
//GetCommits get a list of all the commits for the current update
func (githubClient *GitHubClient) GetCommits(update Update) (*list.List, error) {
log.Info("getting commits\n")
commits := list.New()
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: githubClient.Token},
}
client := github.NewClient(t.Client())
// get a list of pull requests with Pull Request API
pullreqs, _, err := client.PullRequests.List(
githubClient.From, githubClient.Repo,
&github.PullRequestListOptions{})
if err != nil {
log.Errorf("Failed to get pull requests")
return list.New(), err
}
re, err := regexp.Compile(githubClient.TargetBranch)
if err != nil {
log.Error("Failed to compile branch pattern...")
return list.New(), err
}
log.Infof("Size of pull reqests: %d", len(pullreqs))
for _, pullreq := range pullreqs {
log.Infof("Branch name is \"%s\"", *pullreq.Head.Ref)
if githubClient.TargetBranch != "" {
matched := re.Match([]byte(*pullreq.Head.Ref))
if matched != true {
log.Infof("Not add a branch, \"%s\" since this branch name is not match the filtering pattern", *pullreq.Head.Ref)
continue
}
}
if *pullreq.State == "open" && pullreq.UpdatedAt.After(update.Time) {
log.Infof("Adding pullrequest %d", *pullreq.Number)
commits.PushBack(pullreq)
}
}
// get the latest commit with Commit API if the commit is newer than last update
masterCommits, _, _ := client.Repositories.ListCommits(
githubClient.From, githubClient.Repo, &github.CommitsListOptions{})
if masterCommits[0].Commit.Author.Date.After(update.Time) {
commits.PushBack(masterCommits[0])
}
return commits, nil
}