Skip to content

Commit

Permalink
Merge pull request #16 from ericsciple/users/ericsciple/m262ghes
Browse files Browse the repository at this point in the history
Use @actions/github to invoke GraphQL for GHES compatibility
  • Loading branch information
trent-j authored May 15, 2020
2 parents 9bf8ee6 + f4a606f commit 84f259d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 68 deletions.
21 changes: 7 additions & 14 deletions __tests__/version/graphql.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@ import {
RequestParameters
} from '@octokit/graphql/dist-types/types'

import * as Graphql from '@octokit/graphql'
import * as Graphql from '../../src/version/graphql'
import {GetVersionsQueryResponse} from '../../src/version'

import SpyInstance = jest.SpyInstance

export function mockGraphql(): SpyInstance<
Promise<GraphQlQueryResponseData>,
[string, (RequestParameters | undefined)?]
> {
return jest.spyOn(Graphql, 'graphql')
}

export function getMockedOldestQueryResponse(
numVersions: number
): GetVersionsQueryResponse {
Expand Down Expand Up @@ -49,8 +40,10 @@ export function getMockedOldestQueryResponse(

export function mockOldestQueryResponse(
numVersions: number
): ReturnType<typeof mockGraphql> {
return mockGraphql().mockResolvedValue(
getMockedOldestQueryResponse(numVersions)
)
) {
const response = new Promise((resolve) => {
resolve(getMockedOldestQueryResponse(numVersions))
}) as Promise<GraphQlQueryResponseData>
jest.spyOn(Graphql, 'graphql').mockImplementation(
(token: string, query: string, parameters: RequestParameters) => response)
}
100 changes: 52 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/version/delete-version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {from, Observable, merge, throwError, of} from 'rxjs'
import {graphql} from '@octokit/graphql'
import {catchError, map, tap} from 'rxjs/operators'
import {GraphQlQueryResponse} from '@octokit/graphql/dist-types/types'
import {graphql} from './graphql'

export interface DeletePackageVersionMutationResponse {
deletePackageVersion: {
Expand All @@ -21,10 +21,9 @@ export function deletePackageVersion(
token: string
): Observable<boolean> {
return from(
graphql(mutation, {
graphql(token, mutation, {
packageVersionId,
headers: {
authorization: `token ${token}`,
Accept: 'application/vnd.github.package-deletes-preview+json'
}
}) as Promise<DeletePackageVersionMutationResponse>
Expand Down
5 changes: 2 additions & 3 deletions src/version/get-versions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {graphql} from '@octokit/graphql'
import {GraphQlQueryResponse} from '@octokit/graphql/dist-types/types'
import {Observable, from, throwError} from 'rxjs'
import {catchError, map} from 'rxjs/operators'
import {graphql} from './graphql'

export interface VersionInfo {
id: string
Expand Down Expand Up @@ -52,13 +52,12 @@ export function queryForOldestVersions(
token: string
): Observable<GetVersionsQueryResponse> {
return from(
graphql(query, {
graphql(token, query, {
owner,
repo,
package: packageName,
last: numVersions,
headers: {
authorization: `token ${token}`,
Accept: 'application/vnd.github.packages-preview+json'
}
}) as Promise<GetVersionsQueryResponse>
Expand Down
19 changes: 19 additions & 0 deletions src/version/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {GitHub} from '@actions/github'
import {GraphQlQueryResponseData} from '@octokit/graphql/dist-types/types'
import {RequestParameters} from '@octokit/types/dist-types/RequestParameters'

/**
* Sends a GraphQL query request based on endpoint options
*
* @param {string} token Auth token
* @param {string} query GraphQL query. Example: `'query { viewer { login } }'`.
* @param {object} parameters URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
export async function graphql(
token: string,
query: string,
parameters: RequestParameters
): Promise<GraphQlQueryResponseData> {
const github = new GitHub(token)
return await github.graphql(query, parameters)
}

0 comments on commit 84f259d

Please sign in to comment.