From e846b54d36a7b968998c22cd696719076082503c Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Wed, 2 Oct 2024 10:36:27 +0300 Subject: [PATCH] feat(#13): --graphql --- src/graph.js | 98 ++---------------------------------- src/index.js | 3 +- test/graph.test.js | 39 ++++++++++++++ test/resources/query.graphql | 1 + 4 files changed, 47 insertions(+), 94 deletions(-) create mode 100644 test/graph.test.js create mode 100644 test/resources/query.graphql diff --git a/src/graph.js b/src/graph.js index 89ef7e3..a921380 100644 --- a/src/graph.js +++ b/src/graph.js @@ -21,98 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -const graph = `query ($searchQuery: String!, $first: Int, $after: String) { - search(query: $searchQuery, type: REPOSITORY, first: $first, after: $after) { - repositoryCount - nodes { - ... on Repository { - nameWithOwner - description - defaultBranchRef { - name - } - object(expression: "HEAD:.github/workflows/") { - ... on Tree { - entries { - name - object { - ... on Blob { - byteSize - } - } - } - } - } - primaryLanguage { - name - } - createdAt - refs(refPrefix: "refs/heads/") { - totalCount - } - defaultBranchRef { - name - target { - repository { - object(expression: "HEAD:README.md") { - ... on Blob { - text - } - } - } - ... on Commit { - history(first: 1) { - totalCount - edges { - node { - committedDate - } - } - } - } - } - } - mentionableUsers { - totalCount - } - latestRelease { - createdAt - } - releases(first:1) { - edges { - node { - id - } - } - totalCount - } - stargazerCount - forkCount - pullRequests { - totalCount - } - issues(states: [OPEN]) { - totalCount - } - licenseInfo { - spdxId - } - repositoryTopics(first: 10) { - edges { - node { - topic { - name - } - } - } - } - } - } - pageInfo { - endCursor - hasNextPage - } - } -}`; + +const fs = require('fs'); +const graph = (path) => { + return fs.readFileSync(path, "utf-8"); +}; module.exports = graph; diff --git a/src/index.js b/src/index.js index dc98d78..a652d9a 100644 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,7 @@ const startDate = argv.start || '2008-01-01'; const endDate = argv.end || now; const dateType = argv.date || 'created'; const print = argv.json || false; +const gpath = argv.graphql || "ghminer.graphql"; let tokens; if (argv.tokens) { @@ -96,7 +97,7 @@ async function fetchResultsBatch( Authorization: `Bearer ${nextToken()}` } }); - const data = await client.request(query, { + const data = await client.request(query(gpath), { searchQuery, first: batchsize, after: cursor diff --git a/test/graph.test.js b/test/graph.test.js new file mode 100644 index 0000000..b919a11 --- /dev/null +++ b/test/graph.test.js @@ -0,0 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Aliaksei Bialiauski + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +const assert = require('assert'); +const graph = require('../src/graph'); + +describe('Test case for graph.js', function() { + it('reads GraphQL query from file', function() { + const query = graph( + 'test/resources/query.graphql' + ); + const expected = 'query ($searchQuery: String!, $first: Int, $after: String) {}\n'; + assert.equal( + query, + expected, + `found query ${query} does not match with expected ${expected}` + ); + }); +}); diff --git a/test/resources/query.graphql b/test/resources/query.graphql new file mode 100644 index 0000000..f9b16cb --- /dev/null +++ b/test/resources/query.graphql @@ -0,0 +1 @@ +query ($searchQuery: String!, $first: Int, $after: String) {}