Skip to content

Commit 700500b

Browse files
committed
allow to push change branch head using gh api v3
1 parent c818d86 commit 700500b

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

Gopkg.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@
3636
[[constraint]]
3737
branch = "master"
3838
name = "github.com/shurcooL/githubql"
39+
40+
[[constraint]]
41+
name = "github.com/google/go-github"
42+
version = "15.0.0"

github-flow-manager.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ func main() {
1515
}
1616
firstParentCommits := github.PickFirstParentCommits(commits)
1717

18-
for _, c := range firstParentCommits {
19-
fmt.Println(c.SHA, c.Message)
18+
masterHead := firstParentCommits[0]
19+
20+
err = gm.ChangeBranchHead("DocPlanner", "github-flow-manager-test-repo", "test", masterHead.SHA, false)
21+
if err != nil {
22+
fmt.Println(err)
2023
}
24+
25+
26+
//for _, c := range firstParentCommits {
27+
// fmt.Println(c.SHA, c.Message)
28+
//}
2129
}

github/commit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package github
22

3+
import "time"
4+
35
type Commit struct {
46
SHA string
57
Message string
68
Parents []Commit
79
StatusSuccess bool
10+
PushedDate time.Time
811
}

github/error.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package github
22

33
type Error struct {
4-
Message string
4+
Message string
5+
PreviousError error
56
}
67

78
func (e Error) Error() (string) {

github/github-query.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type githubQuery struct {
1818
}
1919
}
2020
} `graphql:"parents(first: $parentsNumber)"`
21-
Oid githubql.String
22-
Message githubql.String
21+
Oid githubql.String
22+
Message githubql.String
23+
PushedDate githubql.DateTime
2324
Status struct {
2425
Id githubql.String
2526
State githubql.String

github/manager.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"golang.org/x/oauth2"
55
"golang.org/x/net/context"
66
"github.com/shurcooL/githubql"
7+
"github.com/google/go-github/github"
8+
"net/http"
79
)
810

911
type githubManager struct {
10-
Context context.Context
11-
Client *githubql.Client
12+
Context context.Context
13+
Client *githubql.Client
14+
HttpClient *http.Client
1215
}
1316

1417
func New(githubAccessToken string) (*githubManager) {
@@ -19,7 +22,7 @@ func New(githubAccessToken string) (*githubManager) {
1922
httpClient := oauth2.NewClient(ctx, src)
2023
client := githubql.NewClient(httpClient)
2124

22-
return &githubManager{Context: ctx, Client: client}
25+
return &githubManager{Context: ctx, Client: client, HttpClient: httpClient}
2326
}
2427

2528
func (gm *githubManager) GetCommits(owner, repo, branch string, lastCommitsNumber int) ([]Commit, error) {
@@ -72,6 +75,26 @@ func PickFirstParentCommits(fullCommitsList []Commit) ([]Commit) {
7275
return firstParentCommits
7376
}
7477

78+
// TODO remove v3 client when implemented in v4
79+
func (gm *githubManager) ChangeBranchHead(owner, repo, branch, sha string, force bool) (error) {
80+
httpClient := gm.HttpClient
81+
82+
client := github.NewClient(httpClient)
83+
ref, _, err := client.Git.GetRef(gm.Context, owner, repo, "heads/"+branch)
84+
if nil != err {
85+
return &Error{Message: "Can not update branch head because: " + err.Error(), PreviousError: err}
86+
}
87+
88+
ref.GetObject().SHA = &sha
89+
90+
ref, _, err = client.Git.UpdateRef(gm.Context, owner, repo, ref, force)
91+
if nil != err {
92+
return &Error{Message: "Can not update branch head because: " + err.Error(), PreviousError: err}
93+
}
94+
95+
return nil
96+
}
97+
7598
func hydrateCommits(q *githubQuery) ([]Commit) {
7699
var fullCommitsList []Commit
77100
for _, edge := range q.Repository.Ref.Target.Commit.History.Edges {
@@ -87,6 +110,7 @@ func hydrateCommits(q *githubQuery) ([]Commit) {
87110
Message: string(edge.Node.Message),
88111
Parents: parents,
89112
StatusSuccess: bool(edge.Node.Status.State == githubql.String(githubql.StatusStateSuccess)),
113+
PushedDate: edge.Node.PushedDate.Time,
90114
})
91115
}
92116

0 commit comments

Comments
 (0)