Skip to content

Commit c6081f7

Browse files
Merge pull request #295 from NTTDATA-CLOUDHEDGE-POC/fix_sha_compute_for_branch_name_slash
fix: error fetching sha for branch name with slash in it
2 parents ad27f7f + 9e15378 commit c6081f7

File tree

4 files changed

+352
-0
lines changed

4 files changed

+352
-0
lines changed

scm/driver/bitbucket/git.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ type gitService struct {
1919

2020
func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *scm.Response, error) {
2121
ref = strings.TrimPrefix(ref, "heads/")
22+
23+
if strings.Index(ref, "/") > 0 {
24+
// ref is of pattern "foo/bar". FindCommit method fails to work properly (Open ticket BCLOUD-9969) in this case. Use FindBranch instead
25+
branchRef, res, err := s.FindBranch(ctx, repo, ref)
26+
27+
if err == nil {
28+
return branchRef.Sha, res, err
29+
}
30+
31+
return ref, res, err
32+
}
33+
34+
// here when ref pattern is not foo/bar
2235
commit, res, err := s.FindCommit(ctx, repo, ref)
2336
if err != nil && res == nil || (res.Status != 404 && res.Status >= 300) {
2437
return "", res, err
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
{
2+
"pullrequest": {
3+
"type": "pullrequest",
4+
"description": "made some changes",
5+
"links": {
6+
"decline": {
7+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/decline"
8+
},
9+
"commits": {
10+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/commits"
11+
},
12+
"self": {
13+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1"
14+
},
15+
"comments": {
16+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/comments"
17+
},
18+
"merge": {
19+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/merge"
20+
},
21+
"html": {
22+
"href": "https://bitbucket.org/brydzewski/foo/pull-requests/1"
23+
},
24+
"activity": {
25+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/activity"
26+
},
27+
"diff": {
28+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/diff"
29+
},
30+
"approve": {
31+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/approve"
32+
},
33+
"statuses": {
34+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/pullrequests/1/statuses"
35+
}
36+
},
37+
"title": "Awesome new feature",
38+
"close_source_branch": false,
39+
"reviewers": [],
40+
"id": 1,
41+
"destination": {
42+
"commit": {
43+
"hash": "7d1a175411ef",
44+
"links": {
45+
"self": {
46+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/commit/7d1a175411ef"
47+
}
48+
}
49+
},
50+
"branch": {
51+
"name": "master"
52+
},
53+
"repository": {
54+
"full_name": "brydzewski/foo",
55+
"type": "repository",
56+
"name": "foo",
57+
"links": {
58+
"self": {
59+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo"
60+
},
61+
"html": {
62+
"href": "https://bitbucket.org/brydzewski/foo"
63+
},
64+
"avatar": {
65+
"href": "https://bytebucket.org/ravatar/%7Bbc771cbf-829e-4c4b-b71f-a0eb3ac2b860%7D?ts=default"
66+
}
67+
},
68+
"uuid": "{bc771cbf-829e-4c4b-b71f-a0eb3ac2b860}"
69+
}
70+
},
71+
"comment_count": 0,
72+
"summary": {
73+
"raw": "made some changes",
74+
"markup": "markdown",
75+
"html": "<p>made some changes</p>",
76+
"type": "rendered"
77+
},
78+
"source": {
79+
"commit": {
80+
"hash": "507a576e59b3",
81+
"links": {
82+
"self": {
83+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo/commit/507a576e59b3"
84+
}
85+
}
86+
},
87+
"branch": {
88+
"name": "feature/develop"
89+
},
90+
"repository": {
91+
"full_name": "brydzewski/foo",
92+
"type": "repository",
93+
"name": "foo",
94+
"links": {
95+
"self": {
96+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo"
97+
},
98+
"html": {
99+
"href": "https://bitbucket.org/brydzewski/foo"
100+
},
101+
"avatar": {
102+
"href": "https://bytebucket.org/ravatar/%7Bbc771cbf-829e-4c4b-b71f-a0eb3ac2b860%7D?ts=default"
103+
}
104+
},
105+
"uuid": "{bc771cbf-829e-4c4b-b71f-a0eb3ac2b860}"
106+
}
107+
},
108+
"state": "OPEN",
109+
"author": {
110+
"username": "brydzewski",
111+
"display_name": "Brad Rydzewski",
112+
"account_id": "557058:2a6349dc-4346-4805-bd84-3abdd0812d17",
113+
"links": {
114+
"self": {
115+
"href": "https://api.bitbucket.org/2.0/users/brydzewski"
116+
},
117+
"html": {
118+
"href": "https://bitbucket.org/brydzewski/"
119+
},
120+
"avatar": {
121+
"href": "https://bitbucket.org/account/brydzewski/avatar/32/"
122+
}
123+
},
124+
"type": "user",
125+
"uuid": "{87bb15eb-47c1-49b3-9f16-ca824a2979a4}"
126+
},
127+
"created_on": "2018-07-02T21:51:39.492248+00:00",
128+
"participants": [],
129+
"reason": "",
130+
"updated_on": "2018-07-02T21:51:39.532546+00:00",
131+
"merge_commit": null,
132+
"closed_by": null,
133+
"task_count": 0
134+
},
135+
"actor": {
136+
"username": "brydzewski",
137+
"display_name": "Brad Rydzewski",
138+
"account_id": "557058:2a6349dc-4346-4805-bd84-3abdd0812d17",
139+
"links": {
140+
"self": {
141+
"href": "https://api.bitbucket.org/2.0/users/brydzewski"
142+
},
143+
"html": {
144+
"href": "https://bitbucket.org/brydzewski/"
145+
},
146+
"avatar": {
147+
"href": "https://bitbucket.org/account/brydzewski/avatar/32/"
148+
}
149+
},
150+
"type": "user",
151+
"uuid": "{87bb15eb-47c1-49b3-9f16-ca824a2979a4}"
152+
},
153+
"repository": {
154+
"scm": "git",
155+
"website": "",
156+
"name": "foo",
157+
"links": {
158+
"self": {
159+
"href": "https://api.bitbucket.org/2.0/repositories/brydzewski/foo"
160+
},
161+
"html": {
162+
"href": "https://bitbucket.org/brydzewski/foo"
163+
},
164+
"avatar": {
165+
"href": "https://bytebucket.org/ravatar/%7Bbc771cbf-829e-4c4b-b71f-a0eb3ac2b860%7D?ts=default"
166+
}
167+
},
168+
"full_name": "brydzewski/foo",
169+
"owner": {
170+
"username": "brydzewski",
171+
"display_name": "Brad Rydzewski",
172+
"account_id": "557058:2a6349dc-4346-4805-bd84-3abdd0812d17",
173+
"links": {
174+
"self": {
175+
"href": "https://api.bitbucket.org/2.0/users/brydzewski"
176+
},
177+
"html": {
178+
"href": "https://bitbucket.org/brydzewski/"
179+
},
180+
"avatar": {
181+
"href": "https://bitbucket.org/account/brydzewski/avatar/32/"
182+
}
183+
},
184+
"type": "user",
185+
"uuid": "{87bb15eb-47c1-49b3-9f16-ca824a2979a4}"
186+
},
187+
"type": "repository",
188+
"is_private": true,
189+
"uuid": "{bc771cbf-829e-4c4b-b71f-a0eb3ac2b860}"
190+
}
191+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"Action": "opened",
3+
"Repo": {
4+
"ID": "{bc771cbf-829e-4c4b-b71f-a0eb3ac2b860}",
5+
"Namespace": "brydzewski",
6+
"Name": "foo",
7+
"FullName": "brydzewski/foo",
8+
"Perm": null,
9+
"Branch": "",
10+
"Private": true,
11+
"Clone": "https://bitbucket.org/brydzewski/foo.git",
12+
"CloneSSH": "[email protected]:brydzewski/foo.git",
13+
"Link": "https://bitbucket.org/brydzewski/foo",
14+
"Created": "0001-01-01T00:00:00Z",
15+
"Updated": "0001-01-01T00:00:00Z"
16+
},
17+
"Label": {
18+
"ID": 0,
19+
"URL": "",
20+
"Name": "",
21+
"Description": "",
22+
"Color": ""
23+
},
24+
"PullRequest": {
25+
"Number": 1,
26+
"Title": "Awesome new feature",
27+
"Body": "made some changes",
28+
"Labels": null,
29+
"Sha": "507a576e59b3",
30+
"Ref": "refs/pull-requests/1/from",
31+
"Source": "feature/develop",
32+
"Target": "master",
33+
"Base": {
34+
"Ref": "master",
35+
"Sha": "",
36+
"Repo": {
37+
"ID": "{bc771cbf-829e-4c4b-b71f-a0eb3ac2b860}",
38+
"Namespace": "brydzewski",
39+
"Name": "foo",
40+
"FullName": "brydzewski/foo",
41+
"Perm": null,
42+
"Branch": "",
43+
"Private": true,
44+
"Clone": "https://bitbucket.org/brydzewski/foo.git",
45+
"CloneSSH": "[email protected]:brydzewski/foo.git",
46+
"Link": "https://bitbucket.org/brydzewski/foo",
47+
"Created": "0001-01-01T00:00:00Z",
48+
"Updated": "0001-01-01T00:00:00Z"
49+
}
50+
},
51+
"Head": {
52+
"Ref": "feature/develop",
53+
"Sha": "",
54+
"Repo": {
55+
"ID": "",
56+
"Namespace": "",
57+
"Name": "",
58+
"FullName": "",
59+
"Perm": null,
60+
"Branch": "",
61+
"Private": false,
62+
"Clone": "",
63+
"CloneSSH": "",
64+
"Link": "",
65+
"Created": "0001-01-01T00:00:00Z",
66+
"Updated": "0001-01-01T00:00:00Z"
67+
}
68+
},
69+
"Fork": "brydzewski/foo",
70+
"State": "",
71+
"Closed": false,
72+
"Draft": false,
73+
"Merged": false,
74+
"Mergeable": false,
75+
"Rebaseable": false,
76+
"MergeableState": "",
77+
"MergeSha": "",
78+
"Author": {
79+
"ID": 0,
80+
"Login": "brydzewski",
81+
"Name": "Brad Rydzewski",
82+
"Email": "",
83+
"Avatar": "https://bitbucket.org/account/brydzewski/avatar/32/",
84+
"Link": "",
85+
"Created": "0001-01-01T00:00:00Z",
86+
"Updated": "0001-01-01T00:00:00Z"
87+
},
88+
"Assignees": null,
89+
"Reviewers": null,
90+
"Milestone": {
91+
"Number": 0,
92+
"ID": 0,
93+
"Title": "",
94+
"Description": "",
95+
"Link": "",
96+
"State": "",
97+
"DueDate": null
98+
},
99+
"Created": "2018-07-02T21:51:39.492248Z",
100+
"Updated": "2018-07-02T21:51:39.532546Z",
101+
"Link": "https://bitbucket.org/brydzewski/foo/pull-requests/1",
102+
"DiffLink": ""
103+
},
104+
"Sender": {
105+
"ID": 0,
106+
"Login": "brydzewski",
107+
"Name": "Brad Rydzewski",
108+
"Email": "",
109+
"Avatar": "https://bitbucket.org/account/brydzewski/avatar/32/",
110+
"Link": "",
111+
"Created": "0001-01-01T00:00:00Z",
112+
"Updated": "0001-01-01T00:00:00Z"
113+
},
114+
"Changes": {
115+
"Base": {
116+
"Ref": {
117+
"From": ""
118+
},
119+
"Sha": {
120+
"From": ""
121+
},
122+
"Repo": {
123+
"ID": "",
124+
"Namespace": "",
125+
"Name": "",
126+
"FullName": "",
127+
"Perm": null,
128+
"Branch": "",
129+
"Private": false,
130+
"Clone": "",
131+
"CloneSSH": "",
132+
"Link": "",
133+
"Created": "0001-01-01T00:00:00Z",
134+
"Updated": "0001-01-01T00:00:00Z"
135+
}
136+
}
137+
},
138+
"GUID": "",
139+
"Installation": null
140+
}

scm/driver/bitbucket/webhook_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ func TestWebhooks(t *testing.T) {
9292
after: "testdata/webhooks/pr_created.json.golden",
9393
obj: new(scm.PullRequestHook),
9494
},
95+
// pull request created, having branch name of pattern foo/bar
96+
{
97+
sig: "71295b197fa25f4356d2fb9965df3f2379d903d7",
98+
event: "pullrequest:created",
99+
before: "testdata/webhooks/pr_created_slashbranch.json",
100+
after: "testdata/webhooks/pr_created_slashbranch.json.golden",
101+
obj: new(scm.PullRequestHook),
102+
},
95103
// pull request updated
96104
{
97105
sig: "71295b197fa25f4356d2fb9965df3f2379d903d7",

0 commit comments

Comments
 (0)