Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 58 additions & 17 deletions assets/scripts/oauth-services-api/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -27,10 +29,17 @@ 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 => resp.data.viewer)
}

/** @type {OAuthServiceAPI["getUserEmails"]} */
Expand Down Expand Up @@ -131,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('/')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l'histoire du repoId marchait bien avec les URLs REST, mais moins bien avec les requêtes GraphQL

Voir #174 pour une solution


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())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a troqué un gloubiboulga de .then et response.json()
contre cette ligne un peu affreuse (et la requête ci-dessus qui est plutôt cool)

}

/** @type {OAuthServiceAPI["isPagesWebsiteBuilt"]} */
Expand Down Expand Up @@ -213,4 +225,33 @@ export default class GitHubAPI {
return httpResp
})
}



/**
*
* @param {string} query
* @returns {Promise<any>}
*/
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()
})
}
}