Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Introduce Github V4 Api For Chronological Tags (#31)
Browse files Browse the repository at this point in the history
* Introduce Github V4 Api For Chronological Tags

* Remove Consoles
  • Loading branch information
therynamo authored Mar 18, 2019
1 parent 4db9276 commit d924854
Show file tree
Hide file tree
Showing 14 changed files with 1,590 additions and 619 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"jest": true
},
"rules": {
"max-len": ["error", 150]
"max-len": ["error", 150],
"no-debugger": 0
}
}
2 changes: 2 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"regexp": null,
"github": {
"apiVersion": "v3",
"timeout": 0,
"token": "<REPLACE_ME>",
"host": "https://github.com/api/v3",
"v4Host": "https://api.github.com/graphql",
"domain": null
},
"slack": {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@octokit/graphql": "^2.0.1",
"@octokit/rest": "^15.8.1",
"@slack/client": "^4.3.1",
"common-tags": "^1.8.0",
Expand All @@ -18,7 +19,8 @@
"lodash": "^4.17.10",
"nconf": "^0.10.0",
"node-fetch": "^2.1.2",
"pino": "^4.17.3"
"pino": "^4.17.3",
"prettier": "^1.16.4"
},
"devDependencies": {
"eslint": "^4.19.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Github = require('@octokit/rest');
const config = require('../config');
const config = require('../../config');

const {
domain, host, timeout, token,
Expand Down
12 changes: 12 additions & 0 deletions src/connectors/GithubConnector/GithubConnectorV4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const config = require('../../config');

const { domain, v4Host, token } = config.get('github');

const GithubGraphQL = require('@octokit/graphql').defaults({
baseUrl: `${domain}/api` || v4Host,
headers: {
authorization: `bearer ${token}`,
},
});

module.exports = GithubGraphQL;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => () => jest.fn();
48 changes: 48 additions & 0 deletions src/connectors/GithubConnector/__tests__/GithubConnector.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const GithubConnector = require('..');

describe('GithubConnector', () => {
it('should initialize with the v3 api by default', () => {
const github = GithubConnector();

expect(Object.getOwnPropertyNames(github)).toMatchInlineSnapshot(`
Array [
"hook",
"plugin",
"request",
"authenticate",
"activity",
"apps",
"authorization",
"checks",
"enterprise",
"gists",
"gitdata",
"integrations",
"issues",
"migrations",
"misc",
"orgs",
"projects",
"pullRequests",
"reactions",
"repos",
"search",
"users",
"getFirstPage",
"getLastPage",
"getNextPage",
"getPreviousPage",
"hasFirstPage",
"hasLastPage",
"hasNextPage",
"hasPreviousPage",
]
`);
});

it('should override the initial github v3 service with v4', () => {
const github = GithubConnector(true);

expect(github.name).toEqual('newApi');
});
});
15 changes: 15 additions & 0 deletions src/connectors/GithubConnector/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const v3API = require('./GithubConnectorV3');
const v4API = require('./GithubConnectorV4');
const { apiVersion } = require('../../config').get('github');

module.exports = (overrideV4) => {
const isV4 = apiVersion.includes('v4');

let GithubConnector = v3API;

if (isV4 || overrideV4) {
GithubConnector = v4API;
}

return GithubConnector;
};
3 changes: 2 additions & 1 deletion src/handlers/getPullRequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Github: github } = require('../connectors');
const { Github } = require('../connectors');

const github = Github();
module.exports = async function getPullRequest(owner, repo, number) {
let pr = {};
try {
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/getTagDiffHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { Github: github } = require('../connectors');
const { Github } = require('../connectors');

const github = Github();

module.exports = async function getTagDiff(owner, repo, head, base) {
let tagDiff = {};
Expand Down
38 changes: 33 additions & 5 deletions src/handlers/getTagsHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
const { Github: github } = require('../connectors');
const logger = require('../logger');
const idx = require('idx');

const { Github } = require('../connectors');

const github = Github(true);

module.exports = async function getTags(owner, repo) {
let tags = [];
let repository = [];
try {
tags = await github.repos.getTags({ owner, repo });
repository = await github({
query: `query GetTags($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
refs(
refPrefix: "refs/tags/"
first: 100
orderBy: { field: TAG_COMMIT_DATE, direction: DESC }
) {
edges {
node {
name
}
}
}
}
}`,
owner,
repo,
});
} catch (e) {
throw e;
}

return tags.data;
const { repository: rep } = repository;
const response = idx(rep, _ => _.refs.edges) || [];

// We need to put the tags back into their original format so that we can be backwards compatible with
// the expected v3 response.
const tags = response.reduce((acc, edge) => [...acc, { name: idx(edge, _ => _.node.name) || '' }], []);

return tags;
};
4 changes: 3 additions & 1 deletion src/handlers/searchIssuesByCommitHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { Github: github } = require('../connectors');
const { Github } = require('../connectors');

const github = Github();

// query is required
module.exports = async function searchIssuesByCommit(query) {
Expand Down
Loading

0 comments on commit d924854

Please sign in to comment.