bitbucket-repository-provider 4.0.1 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bitbucket-repository-provider",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -31,16 +31,17 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "content-entry": "^5.0.0",
34
+ "fetch-rate-limit-util": "^2.8.0",
34
35
  "matching-iterator": "^2.0.4",
35
36
  "node-fetch": "^3.2.3",
36
37
  "one-time-execution-method": "^2.0.13",
37
- "repository-provider": "^28.0.0"
38
+ "repository-provider": "^28.1.1"
38
39
  },
39
40
  "devDependencies": {
40
41
  "ava": "^4.2.0",
41
42
  "c8": "^7.11.2",
42
43
  "documentation": "^13.2.5",
43
- "repository-provider-test-support": "^2.0.4",
44
+ "repository-provider-test-support": "^2.1.2",
44
45
  "semantic-release": "^19.0.2"
45
46
  },
46
47
  "engines": {
@@ -25,17 +25,13 @@ export class BitbucketBranch extends Branch {
25
25
  };
26
26
  }
27
27
 
28
- fetch(...args) {
29
- return this.provider.fetch(...args);
30
- }
31
-
32
28
  /**
33
29
  * {@link https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src/%7Bnode%7D/%7Bpath%7D}
34
30
  * @param {string} name
35
31
  * @return {Promise<ContentEntry>}
36
32
  */
37
33
  async entry(name) {
38
- const res = await this.fetch(
34
+ const res = await this.provider.fetch(
39
35
  `repositories/${this.slug}/src/${this.hash}/${name}`
40
36
  );
41
37
  if(res.ok) {
@@ -48,13 +44,11 @@ export class BitbucketBranch extends Branch {
48
44
  * @param patterns
49
45
  */
50
46
  async *entries(patterns) {
51
- const r = await this.fetch(
47
+ const { json } = await this.provider.fetchJSON(
52
48
  `repositories/${this.slug}/src/${this.hash}/?max_depth=99`
53
49
  );
54
50
 
55
- const res = await r.json();
56
-
57
- for (const entry of matcher(res.values, patterns, { name: "path" })) {
51
+ for (const entry of matcher(json.values, patterns, { name: "path" })) {
58
52
  yield entry.type === "commit_directory"
59
53
  ? new BaseCollectionEntry(entry.path)
60
54
  : new LazyBufferContentEntry(entry.path, this);
@@ -78,7 +72,7 @@ export class BitbucketBranch extends Branch {
78
72
  searchParams.set(u.name, await u.string);
79
73
  }
80
74
 
81
- return this.fetch(`repositories/${this.slug}/src`, {
75
+ return this.provider.fetch(`repositories/${this.slug}/src`, {
82
76
  method: "POST",
83
77
  headers: {
84
78
  "Content-Type": "application/x-www-form-urlencoded"
@@ -108,7 +102,7 @@ class LazyBufferContentEntry extends BufferContentEntryMixin(ContentEntry) {
108
102
  async _buffer() {
109
103
  const branch = this.branch;
110
104
 
111
- const res = await branch.fetch(
105
+ const res = await branch.provider.fetch(
112
106
  `repositories/${branch.slug}/src/${branch.hash}/${this.name}`
113
107
  );
114
108
 
@@ -1,6 +1,6 @@
1
1
  import fetch from "node-fetch";
2
2
  import { replaceWithOneTimeExecutionMethod } from "one-time-execution-method";
3
-
3
+ import { stateActionHandler } from "fetch-rate-limit-util";
4
4
  import { MultiGroupProvider } from "repository-provider";
5
5
  import { BitbucketBranch } from "./bitbucket-branch.mjs";
6
6
  import { BitbucketRepositoryGroup } from "./bitbucket-repository-group.mjs";
@@ -26,9 +26,9 @@ const domain = "bitbucket.org";
26
26
  * - owner/repo-name
27
27
  * Known environment variables
28
28
  * - BITBUCKET_API api
29
- * - BB_TOKEN api token
30
29
  * - BITBUCKET_TOKEN api token
31
30
  * - BITBUCKET_USERNAME username
31
+ * - BITBUCKET_APP_PASSWORD password
32
32
  * - BITBUCKET_PASSWORD password
33
33
  * @param {Object} config
34
34
  * @param {string} config.url provider scm base
@@ -156,24 +156,17 @@ export class BitbucketProvider extends MultiGroupProvider {
156
156
  let next = "repositories/?role=contributor";
157
157
 
158
158
  do {
159
- const response = await this.fetch(next);
160
-
161
- if (!response.ok) {
162
- console.log(response);
163
- break;
164
- }
165
-
166
- const res = await response.json();
159
+ const { json } = await this.fetchJSON(next);
167
160
 
168
- next = res.next;
169
- res.values.map(b => {
161
+ next = json.next;
162
+ json.values.map(b => {
170
163
  const groupName = b.owner.nickname || b.owner.username;
171
164
  this.addRepositoryGroup(groupName, b.owner).addRepository(b.name, b);
172
165
  });
173
166
  } while (next);
174
167
  }
175
168
 
176
- fetch(url, options = {}) {
169
+ fetch(url, options = {}, responseHandler) {
177
170
  let authorization;
178
171
 
179
172
  if (this.authentication.username) {
@@ -199,9 +192,22 @@ export class BitbucketProvider extends MultiGroupProvider {
199
192
  headers["Content-Type"] = "application/json";
200
193
  }
201
194
 
202
- return fetch(new URL(url, this.api), {
203
- ...options,
204
- headers
195
+ return stateActionHandler(
196
+ fetch,
197
+ new URL(url, this.api),
198
+ {
199
+ ...options,
200
+ headers
201
+ },
202
+ responseHandler,
203
+ undefined,
204
+ (url, ...args) => this.trace(url.toString(), ...args)
205
+ );
206
+ }
207
+
208
+ fetchJSON(url, options) {
209
+ return this.fetch(url, options, async response => {
210
+ return { response, json: await response.json() };
205
211
  });
206
212
  }
207
213
  }
@@ -31,12 +31,11 @@ export class BitbucketPullRequest extends PullRequest {
31
31
  let url = `repositories/${repository.slug}/pullrequests${query}`;
32
32
 
33
33
  do {
34
- const r = await repository.fetch(url);
35
- const res = await r.json();
36
- url = res.next;
34
+ const { json } = await repository.provider.fetchJSON(url);
35
+ url = json.next;
37
36
 
38
- if (res.values) {
39
- for (const p of res.values) {
37
+ if (json.values) {
38
+ for (const p of json.values) {
40
39
  const source = await getBranch(p.source);
41
40
 
42
41
  if (filter.source && !filter.source.equals(source)) {
@@ -73,7 +72,7 @@ export class BitbucketPullRequest extends PullRequest {
73
72
 
74
73
  const url = `repositories/${destination.slug}/pullrequests`;
75
74
 
76
- const res = await destination.fetch(url, {
75
+ const { json } = await destination.provider.fetchJSON(url, {
77
76
  method: "POST",
78
77
  data: {
79
78
  source: {
@@ -91,8 +90,6 @@ export class BitbucketPullRequest extends PullRequest {
91
90
  }
92
91
  });
93
92
 
94
- const json = await res.json();
95
-
96
93
  if (json.type === "error" && json.error) {
97
94
  throw new Error(json.error.message);
98
95
  }
@@ -109,7 +106,7 @@ export class BitbucketPullRequest extends PullRequest {
109
106
  */
110
107
  async _merge(method = "merge_commit") {
111
108
  const url = `repositories/${this.destination.slug}/pullrequests/${this.number}/merge`;
112
- return this.destination.fetch(url, {
109
+ return this.destination.provider.fetch(url, {
113
110
  type: "a type",
114
111
  message: "a message",
115
112
  method: "POST",
@@ -20,7 +20,6 @@ export class BitbucketRepositoryGroup extends RepositoryGroup {
20
20
  * @return {Repository} newly created repository
21
21
  */
22
22
  async createRepository(name, options) {
23
- //console.log(`repositories/${this.name}/${name}`);
24
23
  const response = await this.provider.fetch(
25
24
  `repositories/${this.name}/${name}`,
26
25
  {
@@ -32,8 +31,6 @@ export class BitbucketRepositoryGroup extends RepositoryGroup {
32
31
  }
33
32
  );
34
33
 
35
- //console.log(response);
36
-
37
34
  if (response.ok) {
38
35
  return this.addRepository(name, options);
39
36
  }
@@ -53,14 +53,10 @@ export class BitbucketRepository extends Repository {
53
53
  return `${this.provider.url}/${this.slug}/issues`;
54
54
  }
55
55
 
56
- fetch(...args) {
57
- return this.provider.fetch(...args);
58
- }
59
-
60
56
  /**
61
57
  * {@link https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-put}
62
58
  */
63
- async update() {
59
+ async update() {
64
60
  return this.provider.fetch(`repositories/${this.slug}`, {
65
61
  method: "PUT",
66
62
  body: JSON.stringify(
@@ -76,13 +72,11 @@ export class BitbucketRepository extends Repository {
76
72
  let url = `repositories/${this.slug}/hooks`;
77
73
 
78
74
  do {
79
- const r = await this.fetch(url);
80
- const res = await r.json();
81
- res.values.forEach(h =>
82
- this.addHook(
83
- new this.hookClass(this, h.id, new Set(h.events), h)
84
- ));
85
- url = res.next;
75
+ const { json } = await this.provider.fetchJSON(url);
76
+ json.values.forEach(h =>
77
+ this.addHook(new this.hookClass(this, h.id, new Set(h.events), h))
78
+ );
79
+ url = json.next;
86
80
  } while (url);
87
81
  }
88
82
 
@@ -90,16 +84,15 @@ export class BitbucketRepository extends Repository {
90
84
  let url = `repositories/${this.slug}/refs/branches`;
91
85
 
92
86
  do {
93
- const r = await this.fetch(url);
94
- const res = await r.json();
87
+ const { json } = await this.provider.fetchJSON(url);
95
88
 
96
- if (res.type === "error") {
89
+ if (json.type === "error") {
97
90
  break;
98
91
  }
99
92
 
100
- res.values.forEach(b => this.addBranch(b.name, b.target));
93
+ json.values.forEach(b => this.addBranch(b.name, b.target));
101
94
 
102
- url = res.next;
95
+ url = json.next;
103
96
  } while (url);
104
97
  }
105
98
 
@@ -112,23 +105,25 @@ export class BitbucketRepository extends Repository {
112
105
  * @param {string} options.message
113
106
  */
114
107
  async createBranch(name, from = this.defaultBranch, options) {
108
+ const branch = this._branches.get(name);
109
+ if (branch) {
110
+ return branch;
111
+ }
112
+
115
113
  from = await from;
116
- const res = await this.fetch(`repositories/${this.slug}/refs/branches`, {
117
- method: "POST",
118
- data: {
119
- name,
120
- target: {
121
- hash: from.hash
114
+
115
+ const { json } = await this.provider.fetchJSON(
116
+ `repositories/${this.slug}/refs/branches`,
117
+ {
118
+ method: "POST",
119
+ data: {
120
+ name,
121
+ target: {
122
+ hash: from.hash
123
+ }
122
124
  }
123
125
  }
124
- });
125
-
126
- if(!res.ok) {
127
- // TODO handle error
128
- console.log(res.ok, res.status, res.statusText);
129
- }
130
-
131
- const json = await res.json();
126
+ );
132
127
 
133
128
  return super.addBranch(name, json);
134
129
  }
@@ -139,17 +134,13 @@ export class BitbucketRepository extends Repository {
139
134
  */
140
135
  async deleteBranch(name) {
141
136
  const url = `repositories/${this.slug}/refs/branches/${name}`;
142
- // console.log(url);
143
-
144
- const res = await this.fetch(url, { method: "DELETE" });
137
+ const response = await this.provider.fetch(url, { method: "DELETE" });
145
138
 
146
- //console.log(res.ok, res.status, res.statusText);
147
- //const p = await res.json();
148
- //console.log(p);
149
-
150
- if(res.ok) {
151
- return super.deleteBranch(name);
139
+ if(!response.ok) {
140
+ throw new Error(response.statusText);
152
141
  }
142
+
143
+ return super.deleteBranch(name);
153
144
  }
154
145
  }
155
146