From add81423309d4c9efc98e4df50582f615e3e3223 Mon Sep 17 00:00:00 2001 From: David Bruant Date: Thu, 18 Jan 2024 17:40:55 +0100 Subject: [PATCH 1/2] Premier jet de GraphQL --- assets/scripts/oauth-services-api/github.js | 46 ++++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/assets/scripts/oauth-services-api/github.js b/assets/scripts/oauth-services-api/github.js index 6d918e89..4b987476 100644 --- a/assets/scripts/oauth-services-api/github.js +++ b/assets/scripts/oauth-services-api/github.js @@ -4,6 +4,8 @@ import './../types.js' const GITHUB_JSON_ACCEPT_HEADER = 'application/vnd.github+json' +const GITHUB_GRAPHQL_ENDPOINT = 'https://api.github.com/graphql' + /** * @implements {OAuthServiceAPI} */ @@ -27,10 +29,21 @@ export default class GitHubAPI { } } + /** @type {OAuthServiceAPI["getAuthenticatedUser"]} */ getAuthenticatedUser() { - return this.callAPI(`${gitHubApiBaseUrl}/user`).then(response => { - return response.json() + const query = `query { + viewer { + login + email + } + }` + + return this.graphQLCall(query) + .then(resp => { + console.log('graphql resp', resp) + return resp.data.viewer }) + .catch(err => console.error('graphql err', err)) } /** @type {OAuthServiceAPI["getUserEmails"]} */ @@ -213,4 +226,33 @@ export default class GitHubAPI { return httpResp }) } + + + + /** + * + * @param {string} query + * @returns {Promise} + */ + graphQLCall(query){ + return fetch(GITHUB_GRAPHQL_ENDPOINT, { + method: 'POST', + headers: { + Authorization: 'token ' + this.accessToken, + }, + body: JSON.stringify({query: query}) + }) + .then(httpResp => { + if (httpResp.status === 404) { + throw 'NOT_FOUND' + } + + if (httpResp.status === 401) { + this.accessToken = undefined + console.debug('this accessToken : ', this.accessToken) + throw 'INVALIDATE_TOKEN' + } + return httpResp.json() + }) + } } From bfb74256c1ffa78bc79abded17050da2f9bd7234 Mon Sep 17 00:00:00 2001 From: David Bruant Date: Thu, 18 Jan 2024 18:26:42 +0100 Subject: [PATCH 2/2] =?UTF-8?q?getPagesWebsiteDeploymentStatus=20bas=C3=A9?= =?UTF-8?q?=20sur=20GraphQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/scripts/oauth-services-api/github.js | 37 ++++++++++----------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/assets/scripts/oauth-services-api/github.js b/assets/scripts/oauth-services-api/github.js index 4b987476..9a26673e 100644 --- a/assets/scripts/oauth-services-api/github.js +++ b/assets/scripts/oauth-services-api/github.js @@ -39,11 +39,7 @@ export default class GitHubAPI { }` return this.graphQLCall(query) - .then(resp => { - console.log('graphql resp', resp) - return resp.data.viewer - }) - .catch(err => console.error('graphql err', err)) + .then(resp => resp.data.viewer) } /** @type {OAuthServiceAPI["getUserEmails"]} */ @@ -144,21 +140,24 @@ export default class GitHubAPI { /** @type {OAuthServiceAPI["getPagesWebsiteDeploymentStatus"]} */ getPagesWebsiteDeploymentStatus({ repoId }) { - // TODO: We need to add the `sha` parameter to avoid the GitHub API to return - // cached data. - return this.callAPI( - `${gitHubApiBaseUrl}/repos/${repoId}/deployments?environment=github-pages`, - ) - .then(response => response.json()) - .then(json => { - const statusesUrl = json[0].statuses_url + const [owner, name] = repoId.split('/') - return this.callAPI(`${statusesUrl}?per_page=1`) - }) - .then(response => response.json()) - .then(json => { - return json[0].state - }) + const query = `query { + repository(owner: "${owner}", name: "${name}") { + deployments(last: 1) { + nodes { + statuses(first: 1){ + nodes { + state + } + } + } + } + } + }` + + return this.graphQLCall(query) + .then(resp => resp.data.repository.deployments.nodes[0].statuses.nodes[0].state.toLowerString()) } /** @type {OAuthServiceAPI["isPagesWebsiteBuilt"]} */