github-repository-provider 7.24.7 → 7.25.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": "github-repository-provider",
3
- "version": "7.24.7",
3
+ "version": "7.25.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -32,17 +32,17 @@
32
32
  "dependencies": {
33
33
  "content-entry": "^3.0.2",
34
34
  "fetch-link-util": "^1.0.4",
35
- "fetch-rate-limit-util": "^1.1.6",
35
+ "fetch-rate-limit-util": "^2.0.2",
36
36
  "matching-iterator": "^2.0.0",
37
37
  "node-fetch": "3.1.0",
38
38
  "one-time-execution-method": "^2.0.9",
39
- "repository-provider": "^26.0.3"
39
+ "repository-provider": "^26.1.0"
40
40
  },
41
41
  "devDependencies": {
42
42
  "ava": "^3.15.0",
43
43
  "c8": "^7.11.0",
44
44
  "documentation": "^13.2.5",
45
- "repository-provider-test-support": "^1.8.15",
45
+ "repository-provider-test-support": "^1.9.1",
46
46
  "semantic-release": "^18.0.1"
47
47
  },
48
48
  "engines": {
@@ -18,13 +18,16 @@ export class GithubBranch extends Branch {
18
18
  * @return {Promise<ConentEntry>} written content with sha values set
19
19
  */
20
20
  async writeEntry(entry) {
21
- const json = await this.provider.fetchJSON(`repos/${this.slug}/git/blobs`, {
22
- method: "POST",
23
- body: JSON.stringify({
24
- content: await entry.string,
25
- encoding: "utf8"
26
- })
27
- });
21
+ const { json } = await this.provider.fetchJSON(
22
+ `repos/${this.slug}/git/blobs`,
23
+ {
24
+ method: "POST",
25
+ body: JSON.stringify({
26
+ content: await entry.string,
27
+ encoding: "utf8"
28
+ })
29
+ }
30
+ );
28
31
 
29
32
  entry.sha = json.sha;
30
33
 
@@ -36,7 +39,7 @@ export class GithubBranch extends Branch {
36
39
  * @param {string} sha
37
40
  */
38
41
  async baseTreeSha(sha) {
39
- const json = await this.provider.fetchJSON(
42
+ const { json } = await this.provider.fetchJSON(
40
43
  `repos/${this.slug}/git/commits/${sha}`
41
44
  );
42
45
  return json.tree.sha;
@@ -66,24 +69,27 @@ export class GithubBranch extends Branch {
66
69
  const shaLatestCommit = await this.refId();
67
70
  const shaBaseTree = await this.baseTreeSha(shaLatestCommit);
68
71
 
69
- let json = await this.provider.fetchJSON(`repos/${this.slug}/git/trees`, {
70
- method: "POST",
71
- body: JSON.stringify({
72
- base_tree: shaBaseTree,
73
- tree: updates.map(u => {
74
- return {
75
- path: u.name,
76
- sha: u.sha,
77
- type: "blob",
78
- mode: "100" + u.mode.toString(8)
79
- };
72
+ let { json } = await this.provider.fetchJSON(
73
+ `repos/${this.slug}/git/trees`,
74
+ {
75
+ method: "POST",
76
+ body: JSON.stringify({
77
+ base_tree: shaBaseTree,
78
+ tree: updates.map(u => {
79
+ return {
80
+ path: u.name,
81
+ sha: u.sha,
82
+ type: "blob",
83
+ mode: "100" + u.mode.toString(8)
84
+ };
85
+ })
80
86
  })
81
- })
82
- });
87
+ }
88
+ );
83
89
 
84
90
  const shaNewTree = json.sha;
85
91
 
86
- json = await this.provider.fetchJSON(`repos/${this.slug}/git/commits`, {
92
+ const r = await this.provider.fetchJSON(`repos/${this.slug}/git/commits`, {
87
93
  method: "POST",
88
94
  body: JSON.stringify({
89
95
  message,
@@ -92,7 +98,7 @@ export class GithubBranch extends Branch {
92
98
  })
93
99
  });
94
100
 
95
- const sha = json.sha;
101
+ const sha = r.json.sha;
96
102
 
97
103
  return await this.provider.fetchJSON(
98
104
  `repos/${this.slug}/git/refs/heads/${this.name}`,
@@ -111,7 +117,7 @@ export class GithubBranch extends Branch {
111
117
  * @param {string} name
112
118
  */
113
119
  async entry(name) {
114
- const json = await this.provider.fetchJSON(
120
+ const { json } = await this.provider.fetchJSON(
115
121
  `repos/${this.slug}/contents/${name}?ref=${this.ref}`
116
122
  );
117
123
 
@@ -135,27 +141,10 @@ export class GithubBranch extends Branch {
135
141
  * @return {Object[]}
136
142
  */
137
143
  async tree(treeSha) {
138
- const url = `repos/${this.slug}/git/trees/${treeSha}?recursive=1`;
139
-
140
- let res;
141
- for (let i = 0; i < 3; i++) {
142
- try {
143
- res = await this.provider.fetch(url);
144
- if (res.ok) {
145
- const json = await res.json();
146
- return json.tree;
147
- }
148
- } catch (e) {
149
- // errno: 'ERR_STREAM_PREMATURE_CLOSE',
150
- // code: 'ERR_STREAM_PREMATURE_CLOSE',
151
-
152
- this.error(e);
153
- }
154
- }
155
-
156
- console.log(res.errno, res.code);
157
-
158
- throw new Error(res.status);
144
+ const { json } = await this.provider.fetchJSON(
145
+ `repos/${this.slug}/git/trees/${treeSha}?recursive=1`
146
+ );
147
+ return json.tree;
159
148
  }
160
149
 
161
150
  async *entries(patterns) {
@@ -204,7 +193,7 @@ class LazyBufferContentEntry extends BufferContentEntryMixin(ContentEntry) {
204
193
  }
205
194
 
206
195
  const branch = this.branch;
207
- const json = await branch.provider.fetchJSON(
196
+ const { json } = await branch.provider.fetchJSON(
208
197
  `repos/${branch.slug}/contents/${this.name}?ref=${branch.ref}`
209
198
  );
210
199
 
@@ -39,12 +39,12 @@ export class GithubOwner extends RepositoryGroup {
39
39
  );
40
40
 
41
41
  if (response.ok) {
42
- this.provider.info(`Repository ${name} created`);
42
+ this.info(`Repository ${name} created`);
43
43
  options.defaultBranchName = "main";
44
44
  return this.addRepository(name, options);
45
45
  }
46
46
 
47
- this.provider.error(`Repository ${name} creation error ${response.status}`);
47
+ this.error(`Repository ${name} creation error ${response.status}`);
48
48
  }
49
49
 
50
50
  /**
@@ -9,7 +9,7 @@ import { GithubOwner } from "./github-owner.mjs";
9
9
  import { GithubPullRequest } from "./github-pull-request.mjs";
10
10
  export { GithubRepository, GithubBranch, GithubOwner, GithubPullRequest };
11
11
 
12
- const domain = "github.com";
12
+ const host = "github.com";
13
13
 
14
14
  /**
15
15
  * <!-- skip-example -->
@@ -48,25 +48,36 @@ export class GithubProvider extends MultiGroupProvider {
48
48
  static get attributes() {
49
49
  return {
50
50
  ...super.attributes,
51
+ host: {
52
+ type: "string",
53
+ env: ["{{instanceIdentifier}}HOST", "GH_HOST"],
54
+ default: "github.com"
55
+ },
51
56
  ssh: {
52
57
  type: "url",
53
- default: `git@${domain}:`
58
+ default: `git@${host}:`
54
59
  },
55
60
  url: {
56
61
  type: "url",
57
62
  env: ["{{instanceIdentifier}}SERVER_URL"],
58
63
  set: value => (value.endsWith("/") ? value : value + "/"),
59
- default: `https://${domain}/`
64
+ default: `https://${host}/`
60
65
  },
61
66
  api: {
62
67
  type: "url",
63
68
  env: ["{{instanceIdentifier}}API_URL"],
64
69
  set: value => value.replace(/\/$/, ""),
65
- default: `https://api.${domain}`
70
+ default: `https://api.${host}`
66
71
  },
67
72
  "authentication.token": {
68
73
  type: "string",
69
- env: ["{{instanceIdentifier}}TOKEN", "GH_TOKEN"],
74
+ // @see https://cli.github.com/manual/gh_help_environment
75
+ env: [
76
+ "{{instanceIdentifier}}TOKEN",
77
+ "GH_TOKEN",
78
+ "GITHUB_ENTERPRISE_TOKEN",
79
+ "GH_ENTERPRISE_TOKEN"
80
+ ],
70
81
  additionalAttributes: { "authentication.type": "token" },
71
82
  private: true,
72
83
  mandatory: true
@@ -90,14 +101,15 @@ export class GithubProvider extends MultiGroupProvider {
90
101
 
91
102
  fetch(url, options = {}) {
92
103
  return rateLimitHandler(
93
- () =>
94
- fetch(new URL(url, this.api), {
95
- ...options,
96
- headers: {
97
- authorization: `token ${this.authentication.token}`,
98
- ...options.headers
99
- }
100
- }),
104
+ fetch,
105
+ new URL(url, this.api),
106
+ {
107
+ ...options,
108
+ headers: {
109
+ authorization: `token ${this.authentication.token}`,
110
+ ...options.headers
111
+ }
112
+ },
101
113
  (millisecondsToWait, rateLimitRemaining, nthTry, response) => {
102
114
  this.rateLimitRemaining = rateLimitRemaining;
103
115
 
@@ -117,18 +129,24 @@ export class GithubProvider extends MultiGroupProvider {
117
129
  }
118
130
 
119
131
  async fetchJSON(url, options) {
120
- let i = 1;
121
- while (true) {
132
+ for (let i = 1; ; i++) {
122
133
  try {
123
134
  const response = await this.fetch(url, options);
124
- if (!response.ok) {
135
+ if (response.ok) {
136
+ return { response, json: await response.json() };
137
+ }
138
+
139
+ if (i >= 3 || response.status == 401) {
140
+ // none repeatable
125
141
  throw new Error(
126
- `Unable to fetch ${response.url} (${response.status}) try #${i}`
142
+ `Unable to fetch ${response.url} (${response.status})`
127
143
  );
128
144
  }
129
- return await response.json();
145
+ this.info(
146
+ `Unable to fetch ${response.url} (${response.status}) try #${i}`
147
+ );
130
148
  } catch (e) {
131
- if (i++ >= 3) {
149
+ if (i >= 3) {
132
150
  throw e;
133
151
  }
134
152
  this.error(e);
@@ -140,9 +158,9 @@ export class GithubProvider extends MultiGroupProvider {
140
158
  * {@link https://developer.github.com/v3/repos/#list-repositories-for-the-authenticated-user}
141
159
  */
142
160
  async initializeRepositories() {
143
- for (let page = 1; ; page++) {
144
- try {
145
- const json = await this.fetchJSON(
161
+ try {
162
+ for (let page = 1; ; page++) {
163
+ const { json } = await this.fetchJSON(
146
164
  `user/repos?page=${page}&per_page=100`,
147
165
  {
148
166
  headers: {
@@ -162,10 +180,8 @@ export class GithubProvider extends MultiGroupProvider {
162
180
  r
163
181
  );
164
182
  });
165
- } catch (e) {
166
- return;
167
183
  }
168
- }
184
+ } catch {}
169
185
  }
170
186
 
171
187
  /**
@@ -182,9 +198,9 @@ export class GithubProvider extends MultiGroupProvider {
182
198
  return super.repositoryBases.concat([
183
199
  this.url,
184
200
  "git+" + this.url,
185
- `git+ssh://${domain}`,
186
- `git://${domain}/`,
187
- `git@${domain}:`
201
+ `git+ssh://${host}`,
202
+ `git://${host}/`,
203
+ `git@${host}:`
188
204
  ]);
189
205
  }
190
206
 
@@ -43,8 +43,8 @@ export class GithubPullRequest extends PullRequest {
43
43
 
44
44
  do {
45
45
  const provider = repository.provider;
46
- const response = await provider.fetch(next);
47
- for (const node of await response.json()) {
46
+ const { response, json } = await provider.fetchJSON(next);
47
+ for (const node of json) {
48
48
  const [source, dest] = await Promise.all(
49
49
  [node.head, node.base].map(r =>
50
50
  provider.branch([r.repo.full_name, r.ref].join("#"))
@@ -72,7 +72,7 @@ export class GithubPullRequest extends PullRequest {
72
72
  return p;
73
73
  }
74
74
 
75
- const json = await destination.provider.fetchJSON(
75
+ const { json } = await destination.provider.fetchJSON(
76
76
  `repos/${destination.repository.slug}/pulls`,
77
77
  {
78
78
  method: "POST",
@@ -37,16 +37,7 @@ export class GithubRepository extends Repository {
37
37
  let next = `repos/${this.slug}/${typeName}`;
38
38
 
39
39
  do {
40
- const response = await this.provider.fetch(next);
41
-
42
- if (!response.ok) {
43
- this.error(
44
- `Unable to fetch ${typeName} ${response.status} ${response.url}`
45
- );
46
- return;
47
- }
48
-
49
- const json = await response.json();
40
+ const { response, json } = await this.provider.fetchJSON(next);
50
41
  json.forEach(b => this[addMethodName](b.name, b));
51
42
  next = getHeaderLink(response.headers);
52
43
  } while (next);
@@ -112,7 +103,9 @@ export class GithubRepository extends Repository {
112
103
  async refId(ref) {
113
104
  ref = ref.replace(/^refs\//, "");
114
105
 
115
- const json = await this.provider.fetchJSON(`repos/${this.slug}/git/ref/${ref}`);
106
+ const { json } = await this.provider.fetchJSON(
107
+ `repos/${this.slug}/git/ref/${ref}`
108
+ );
116
109
 
117
110
  // TODO why does this happen ?
118
111
  if (!json.object.sha) {
@@ -147,7 +140,7 @@ export class GithubRepository extends Repository {
147
140
  console.log(res);
148
141
  */
149
142
  } else {
150
- const json = await this.provider.fetchJSON(
143
+ const { json } = await this.provider.fetchJSON(
151
144
  `repos/${this.slug}/git/ref/heads/${
152
145
  from === undefined ? this.defaultBranchName : from.name
153
146
  }`
@@ -204,9 +197,9 @@ export class GithubRepository extends Repository {
204
197
  let next = `repos/${this.slug}/hooks`;
205
198
 
206
199
  do {
207
- const response = await this.provider.fetch(next);
200
+ const { response, json } = await this.provider.fetchJSON(next);
208
201
 
209
- for (const h of await response.json()) {
202
+ for (const h of json) {
210
203
  const id = h.id;
211
204
  delete h.id;
212
205
  new this.hookClass(this, id, new Set(h.events), {