@changesets/get-github-info
Advanced tools
Comparing version 0.4.5 to 0.5.0
# @changesets/get-github-info | ||
## 0.5.0 | ||
### Minor Changes | ||
- [#535](https://github.com/atlassian/changesets/pull/535) [`91d1ef2`](https://github.com/atlassian/changesets/commit/91d1ef2ef703be6b727650ef67a932757b97d1ef) Thanks [@mitchellhamilton](https://github.com/mitchellhamilton)! - Added `getInfoFromPullRequest` | ||
## 0.4.5 | ||
@@ -4,0 +10,0 @@ |
@@ -1,6 +0,5 @@ | ||
declare type RequestData = { | ||
export declare function getInfo(request: { | ||
commit: string; | ||
repo: string; | ||
}; | ||
export declare function getInfo(request: RequestData): Promise<{ | ||
}): Promise<{ | ||
user: string | null; | ||
@@ -14,2 +13,13 @@ pull: number | null; | ||
}>; | ||
export {}; | ||
export declare function getInfoFromPullRequest(request: { | ||
pull: number; | ||
repo: string; | ||
}): Promise<{ | ||
user: string | null; | ||
commit: string | null; | ||
links: { | ||
commit: string | null; | ||
pull: string; | ||
user: string | null; | ||
}; | ||
}>; |
@@ -13,3 +13,87 @@ 'use strict'; | ||
// @ts-ignore | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
const validRepoNameRegex = /^[\w.-]+\/[\w.-]+$/; | ||
@@ -24,3 +108,3 @@ | ||
) { | ||
${repos[repo].map(commit => `a${commit}: object(expression: ${JSON.stringify(commit)}) { | ||
${repos[repo].map(data => data.kind === "commit" ? `a${data.commit}: object(expression: ${JSON.stringify(data.commit)}) { | ||
... on Commit { | ||
@@ -45,3 +129,13 @@ commitUrl | ||
} | ||
}}`).join("\n")} | ||
}}` : `pr__${data.pull}: pullRequest(number: ${data.pull}) { | ||
url | ||
author { | ||
login | ||
url | ||
} | ||
mergeCommit { | ||
commitUrl | ||
abbreviatedOid | ||
} | ||
}`).join("\n")} | ||
}`).join("\n")} | ||
@@ -67,6 +161,8 @@ } | ||
let repos = {}; | ||
requests.forEach(({ | ||
commit, | ||
repo | ||
}) => { | ||
requests.forEach((_ref) => { | ||
let { | ||
repo | ||
} = _ref, | ||
data = _objectWithoutProperties(_ref, ["repo"]); | ||
if (repos[repo] === undefined) { | ||
@@ -76,3 +172,3 @@ repos[repo] = []; | ||
repos[repo].push(commit); | ||
repos[repo].push(data); | ||
}); | ||
@@ -94,14 +190,26 @@ const data = await fetch__default['default']("https://api.github.com/graphql", { | ||
let cleanedData = {}; | ||
let dataKeys = Object.keys(data.data); | ||
Object.keys(repos).forEach((repo, index) => { | ||
cleanedData[repo] = {}; | ||
let output = { | ||
commit: {}, | ||
pull: {} | ||
}; | ||
cleanedData[repo] = output; | ||
Object.entries(data.data[`a${index}`]).forEach(([field, value]) => { | ||
// this is "a" because that's how it was when it was first written, "a" means it's a commit not a pr | ||
// we could change it to commit__ but then we have to get new GraphQL results from the GH API to put in the tests | ||
if (field[0] === "a") { | ||
output.commit[field.substring(1)] = value; | ||
} else { | ||
output.pull[field.replace("pr__", "")] = value; | ||
} | ||
}); | ||
}); | ||
return requests.map((_ref2) => { | ||
let { | ||
repo | ||
} = _ref2, | ||
data = _objectWithoutProperties(_ref2, ["repo"]); | ||
for (let nearlyCommit in data.data[dataKeys[index]]) { | ||
cleanedData[repo][nearlyCommit.substring(1)] = data.data[dataKeys[index]][nearlyCommit]; | ||
} | ||
return cleanedData[repo][data.kind][data.kind === "pull" ? data.pull : data.commit]; | ||
}); | ||
return requests.map(({ | ||
repo, | ||
commit | ||
}) => cleanedData[repo][commit]); | ||
}); | ||
@@ -121,3 +229,5 @@ async function getInfo(request) { | ||
const data = await GHDataLoader.load(request); | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "commit" | ||
}, request)); | ||
let user = null; | ||
@@ -161,3 +271,32 @@ | ||
} | ||
async function getInfoFromPullRequest(request) { | ||
if (request.pull === undefined) { | ||
throw new Error("Please pass a pull request number"); | ||
} | ||
if (!request.repo) { | ||
throw new Error("Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo"); | ||
} | ||
if (!validRepoNameRegex.test(request.repo)) { | ||
throw new Error(`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`); | ||
} | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "pull" | ||
}, request)); | ||
let user = data === null || data === void 0 ? void 0 : data.author; | ||
let commit = data === null || data === void 0 ? void 0 : data.mergeCommit; | ||
return { | ||
user: user ? user.login : null, | ||
commit: commit ? commit.abbreviatedOid : null, | ||
links: { | ||
commit: commit ? `[\`${commit.abbreviatedOid}\`](${commit.commitUrl})` : null, | ||
pull: `[#${request.pull}](https://github.com/${request.repo}/pull/${request.pull})`, | ||
user: user ? `[@${user.login}](${user.url})` : null | ||
} | ||
}; | ||
} | ||
exports.getInfo = getInfo; | ||
exports.getInfoFromPullRequest = getInfoFromPullRequest; |
@@ -17,6 +17,55 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { | ||
return key in obj ? Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: !0, | ||
configurable: !0, | ||
writable: !0 | ||
}) : obj[key] = value, obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
enumerableOnly && (symbols = symbols.filter((function(sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}))), keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = null != arguments[i] ? arguments[i] : {}; | ||
i % 2 ? ownKeys(Object(source), !0).forEach((function(key) { | ||
_defineProperty(target, key, source[key]); | ||
})) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach((function(key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
})); | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (null == source) return {}; | ||
var key, i, target = {}, sourceKeys = Object.keys(source); | ||
for (i = 0; i < sourceKeys.length; i++) key = sourceKeys[i], excluded.indexOf(key) >= 0 || (target[key] = source[key]); | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (null == source) return {}; | ||
var key, i, target = _objectWithoutPropertiesLoose(source, excluded); | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) key = sourceSymbolKeys[i], excluded.indexOf(key) >= 0 || Object.prototype.propertyIsEnumerable.call(source, key) && (target[key] = source[key]); | ||
} | ||
return target; | ||
} | ||
const validRepoNameRegex = /^[\w.-]+\/[\w.-]+$/; | ||
function makeQuery(repos) { | ||
return `\n query {\n ${Object.keys(repos).map(((repo, i) => `a${i}: repository(\n owner: ${JSON.stringify(repo.split("/")[0])}\n name: ${JSON.stringify(repo.split("/")[1])}\n ) {\n ${repos[repo].map((commit => `a${commit}: object(expression: ${JSON.stringify(commit)}) {\n ... on Commit {\n commitUrl\n associatedPullRequests(first: 50) {\n nodes {\n number\n url\n mergedAt\n author {\n login\n url\n }\n }\n }\n author {\n user {\n login\n url\n }\n }\n }}`)).join("\n")}\n }`)).join("\n")}\n }\n `; | ||
return `\n query {\n ${Object.keys(repos).map(((repo, i) => `a${i}: repository(\n owner: ${JSON.stringify(repo.split("/")[0])}\n name: ${JSON.stringify(repo.split("/")[1])}\n ) {\n ${repos[repo].map((data => "commit" === data.kind ? `a${data.commit}: object(expression: ${JSON.stringify(data.commit)}) {\n ... on Commit {\n commitUrl\n associatedPullRequests(first: 50) {\n nodes {\n number\n url\n mergedAt\n author {\n login\n url\n }\n }\n }\n author {\n user {\n login\n url\n }\n }\n }}` : `pr__${data.pull}: pullRequest(number: ${data.pull}) {\n url\n author {\n login\n url\n }\n mergeCommit {\n commitUrl\n abbreviatedOid\n } \n }`)).join("\n")}\n }`)).join("\n")}\n }\n `; | ||
} | ||
@@ -27,4 +76,5 @@ | ||
let repos = {}; | ||
requests.forEach((({commit: commit, repo: repo}) => { | ||
void 0 === repos[repo] && (repos[repo] = []), repos[repo].push(commit); | ||
requests.forEach((_ref => { | ||
let {repo: repo} = _ref, data = _objectWithoutProperties(_ref, [ "repo" ]); | ||
void 0 === repos[repo] && (repos[repo] = []), repos[repo].push(data); | ||
})); | ||
@@ -41,7 +91,15 @@ const data = await fetch__default.default("https://api.github.com/graphql", { | ||
if (!data.data) throw new Error("An error occurred when fetching data from GitHub\n" + JSON.stringify(data)); | ||
let cleanedData = {}, dataKeys = Object.keys(data.data); | ||
let cleanedData = {}; | ||
return Object.keys(repos).forEach(((repo, index) => { | ||
cleanedData[repo] = {}; | ||
for (let nearlyCommit in data.data[dataKeys[index]]) cleanedData[repo][nearlyCommit.substring(1)] = data.data[dataKeys[index]][nearlyCommit]; | ||
})), requests.map((({repo: repo, commit: commit}) => cleanedData[repo][commit])); | ||
let output = { | ||
commit: {}, | ||
pull: {} | ||
}; | ||
cleanedData[repo] = output, Object.entries(data.data["a" + index]).forEach((([field, value]) => { | ||
"a" === field[0] ? output.commit[field.substring(1)] = value : output.pull[field.replace("pr__", "")] = value; | ||
})); | ||
})), requests.map((_ref2 => { | ||
let {repo: repo} = _ref2, data = _objectWithoutProperties(_ref2, [ "repo" ]); | ||
return cleanedData[repo][data.kind]["pull" === data.kind ? data.pull : data.commit]; | ||
})); | ||
})); | ||
@@ -53,3 +111,5 @@ | ||
if (!validRepoNameRegex.test(request.repo)) throw new Error(`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`); | ||
const data = await GHDataLoader.load(request); | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "commit" | ||
}, request)); | ||
let user = null; | ||
@@ -69,2 +129,21 @@ data.author && data.author.user && (user = data.author.user); | ||
exports.getInfo = getInfo; | ||
async function getInfoFromPullRequest(request) { | ||
if (void 0 === request.pull) throw new Error("Please pass a pull request number"); | ||
if (!request.repo) throw new Error("Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo"); | ||
if (!validRepoNameRegex.test(request.repo)) throw new Error(`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`); | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "pull" | ||
}, request)); | ||
let user = null == data ? void 0 : data.author, commit = null == data ? void 0 : data.mergeCommit; | ||
return { | ||
user: user ? user.login : null, | ||
commit: commit ? commit.abbreviatedOid : null, | ||
links: { | ||
commit: commit ? `[\`${commit.abbreviatedOid}\`](${commit.commitUrl})` : null, | ||
pull: `[#${request.pull}](https://github.com/${request.repo}/pull/${request.pull})`, | ||
user: user ? `[@${user.login}](${user.url})` : null | ||
} | ||
}; | ||
} | ||
exports.getInfo = getInfo, exports.getInfoFromPullRequest = getInfoFromPullRequest; |
import fetch from 'node-fetch'; | ||
import DataLoader from 'dataloader'; | ||
// @ts-ignore | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
const validRepoNameRegex = /^[\w.-]+\/[\w.-]+$/; | ||
@@ -14,3 +98,3 @@ | ||
) { | ||
${repos[repo].map(commit => `a${commit}: object(expression: ${JSON.stringify(commit)}) { | ||
${repos[repo].map(data => data.kind === "commit" ? `a${data.commit}: object(expression: ${JSON.stringify(data.commit)}) { | ||
... on Commit { | ||
@@ -35,3 +119,13 @@ commitUrl | ||
} | ||
}}`).join("\n")} | ||
}}` : `pr__${data.pull}: pullRequest(number: ${data.pull}) { | ||
url | ||
author { | ||
login | ||
url | ||
} | ||
mergeCommit { | ||
commitUrl | ||
abbreviatedOid | ||
} | ||
}`).join("\n")} | ||
}`).join("\n")} | ||
@@ -57,6 +151,8 @@ } | ||
let repos = {}; | ||
requests.forEach(({ | ||
commit, | ||
repo | ||
}) => { | ||
requests.forEach((_ref) => { | ||
let { | ||
repo | ||
} = _ref, | ||
data = _objectWithoutProperties(_ref, ["repo"]); | ||
if (repos[repo] === undefined) { | ||
@@ -66,3 +162,3 @@ repos[repo] = []; | ||
repos[repo].push(commit); | ||
repos[repo].push(data); | ||
}); | ||
@@ -84,14 +180,26 @@ const data = await fetch("https://api.github.com/graphql", { | ||
let cleanedData = {}; | ||
let dataKeys = Object.keys(data.data); | ||
Object.keys(repos).forEach((repo, index) => { | ||
cleanedData[repo] = {}; | ||
let output = { | ||
commit: {}, | ||
pull: {} | ||
}; | ||
cleanedData[repo] = output; | ||
Object.entries(data.data[`a${index}`]).forEach(([field, value]) => { | ||
// this is "a" because that's how it was when it was first written, "a" means it's a commit not a pr | ||
// we could change it to commit__ but then we have to get new GraphQL results from the GH API to put in the tests | ||
if (field[0] === "a") { | ||
output.commit[field.substring(1)] = value; | ||
} else { | ||
output.pull[field.replace("pr__", "")] = value; | ||
} | ||
}); | ||
}); | ||
return requests.map((_ref2) => { | ||
let { | ||
repo | ||
} = _ref2, | ||
data = _objectWithoutProperties(_ref2, ["repo"]); | ||
for (let nearlyCommit in data.data[dataKeys[index]]) { | ||
cleanedData[repo][nearlyCommit.substring(1)] = data.data[dataKeys[index]][nearlyCommit]; | ||
} | ||
return cleanedData[repo][data.kind][data.kind === "pull" ? data.pull : data.commit]; | ||
}); | ||
return requests.map(({ | ||
repo, | ||
commit | ||
}) => cleanedData[repo][commit]); | ||
}); | ||
@@ -111,3 +219,5 @@ async function getInfo(request) { | ||
const data = await GHDataLoader.load(request); | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "commit" | ||
}, request)); | ||
let user = null; | ||
@@ -151,3 +261,31 @@ | ||
} | ||
async function getInfoFromPullRequest(request) { | ||
if (request.pull === undefined) { | ||
throw new Error("Please pass a pull request number"); | ||
} | ||
export { getInfo }; | ||
if (!request.repo) { | ||
throw new Error("Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo"); | ||
} | ||
if (!validRepoNameRegex.test(request.repo)) { | ||
throw new Error(`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)`); | ||
} | ||
const data = await GHDataLoader.load(_objectSpread2({ | ||
kind: "pull" | ||
}, request)); | ||
let user = data === null || data === void 0 ? void 0 : data.author; | ||
let commit = data === null || data === void 0 ? void 0 : data.mergeCommit; | ||
return { | ||
user: user ? user.login : null, | ||
commit: commit ? commit.abbreviatedOid : null, | ||
links: { | ||
commit: commit ? `[\`${commit.abbreviatedOid}\`](${commit.commitUrl})` : null, | ||
pull: `[#${request.pull}](https://github.com/${request.repo}/pull/${request.pull})`, | ||
user: user ? `[@${user.login}](${user.url})` : null | ||
} | ||
}; | ||
} | ||
export { getInfo, getInfoFromPullRequest }; |
{ | ||
"name": "@changesets/get-github-info", | ||
"version": "0.4.5", | ||
"version": "0.5.0", | ||
"description": "Get the GitHub username and PR number from a commit. Intended for use with changesets.", | ||
@@ -5,0 +5,0 @@ "main": "dist/get-github-info.cjs.js", |
@@ -1,2 +0,2 @@ | ||
import { getInfo } from "."; | ||
import { getInfo, getInfoFromPullRequest } from "."; | ||
import nock from "nock"; | ||
@@ -334,1 +334,67 @@ import prettier from "prettier"; | ||
}); | ||
test("associated with multiple PRs with only one merged", async () => { | ||
nock("https://api.github.com", { | ||
reqheaders: { | ||
Authorization: `Token ${process.env.GITHUB_TOKEN}` | ||
} | ||
}) | ||
.post(apiPath, ({ query }) => { | ||
expect(prettier.format(query, { parser: "graphql" })) | ||
.toMatchInlineSnapshot(` | ||
"query { | ||
a0: repository(owner: \\"emotion-js\\", name: \\"emotion\\") { | ||
pr__1613: pullRequest(number: 1613) { | ||
url | ||
author { | ||
login | ||
url | ||
} | ||
mergeCommit { | ||
commitUrl | ||
abbreviatedOid | ||
} | ||
} | ||
} | ||
} | ||
" | ||
`); | ||
return true; | ||
}) | ||
.reply( | ||
200, | ||
JSON.stringify({ | ||
data: { | ||
a0: { | ||
pr__1613: { | ||
url: "https://github.com/emotion-js/emotion/pull/1613", | ||
author: { | ||
login: "Andarist", | ||
url: "https://github.com/Andarist" | ||
}, | ||
mergeCommit: { | ||
commitUrl: | ||
"https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85", | ||
abbreviatedOid: "a085003" | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
); | ||
let result = await getInfoFromPullRequest({ | ||
pull: 1613, | ||
repo: "emotion-js/emotion" | ||
}); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"commit": "a085003", | ||
"links": Object { | ||
"commit": "[\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85)", | ||
"pull": "[#1613](https://github.com/emotion-js/emotion/pull/1613)", | ||
"user": "[@Andarist](https://github.com/Andarist)", | ||
}, | ||
"user": "Andarist", | ||
} | ||
`); | ||
}); |
129
src/index.ts
@@ -7,8 +7,12 @@ // @ts-ignore | ||
type RequestData = { | ||
commit: string; | ||
repo: string; | ||
}; | ||
type RequestData = | ||
| { kind: "commit"; repo: string; commit: string } | ||
| { kind: "pull"; repo: string; pull: number }; | ||
function makeQuery(repos: any) { | ||
type ReposWithCommitsAndPRsToFetch = Record< | ||
string, | ||
({ kind: "commit"; commit: string } | { kind: "pull"; pull: number })[] | ||
>; | ||
function makeQuery(repos: ReposWithCommitsAndPRsToFetch) { | ||
return ` | ||
@@ -24,8 +28,7 @@ query { | ||
${repos[repo] | ||
.map( | ||
( | ||
commit: string | ||
) => `a${commit}: object(expression: ${JSON.stringify( | ||
commit | ||
)}) { | ||
.map(data => | ||
data.kind === "commit" | ||
? `a${data.commit}: object(expression: ${JSON.stringify( | ||
data.commit | ||
)}) { | ||
... on Commit { | ||
@@ -51,2 +54,13 @@ commitUrl | ||
}}` | ||
: `pr__${data.pull}: pullRequest(number: ${data.pull}) { | ||
url | ||
author { | ||
login | ||
url | ||
} | ||
mergeCommit { | ||
commitUrl | ||
abbreviatedOid | ||
} | ||
}` | ||
) | ||
@@ -76,8 +90,8 @@ .join("\n")} | ||
} | ||
let repos: Record<RequestData["repo"], Array<RequestData["commit"]>> = {}; | ||
requests.forEach(({ commit, repo }) => { | ||
let repos: ReposWithCommitsAndPRsToFetch = {}; | ||
requests.forEach(({ repo, ...data }) => { | ||
if (repos[repo] === undefined) { | ||
repos[repo] = []; | ||
} | ||
repos[repo].push(commit); | ||
repos[repo].push(data); | ||
}); | ||
@@ -102,18 +116,35 @@ | ||
let cleanedData: Record<any, any> = {}; | ||
let dataKeys = Object.keys(data.data); | ||
let cleanedData: Record< | ||
string, | ||
{ commit: Record<string, any>; pull: Record<string, any> } | ||
> = {}; | ||
Object.keys(repos).forEach((repo, index) => { | ||
cleanedData[repo] = {}; | ||
for (let nearlyCommit in data.data[dataKeys[index]]) { | ||
cleanedData[repo][nearlyCommit.substring(1)] = | ||
data.data[dataKeys[index]][nearlyCommit]; | ||
} | ||
let output: { commit: Record<string, any>; pull: Record<string, any> } = { | ||
commit: {}, | ||
pull: {} | ||
}; | ||
cleanedData[repo] = output; | ||
Object.entries(data.data[`a${index}`]).forEach(([field, value]) => { | ||
// this is "a" because that's how it was when it was first written, "a" means it's a commit not a pr | ||
// we could change it to commit__ but then we have to get new GraphQL results from the GH API to put in the tests | ||
if (field[0] === "a") { | ||
output.commit[field.substring(1)] = value; | ||
} else { | ||
output.pull[field.replace("pr__", "")] = value; | ||
} | ||
}); | ||
}); | ||
return requests.map(({ repo, commit }) => cleanedData[repo][commit]); | ||
return requests.map( | ||
({ repo, ...data }) => | ||
cleanedData[repo][data.kind][ | ||
data.kind === "pull" ? data.pull : data.commit | ||
] | ||
); | ||
}); | ||
export async function getInfo( | ||
request: RequestData | ||
): Promise<{ | ||
export async function getInfo(request: { | ||
commit: string; | ||
repo: string; | ||
}): Promise<{ | ||
user: string | null; | ||
@@ -143,3 +174,3 @@ pull: number | null; | ||
const data = await GHDataLoader.load(request); | ||
const data = await GHDataLoader.load({ kind: "commit", ...request }); | ||
let user = null; | ||
@@ -184,1 +215,47 @@ if (data.author && data.author.user) { | ||
} | ||
export async function getInfoFromPullRequest(request: { | ||
pull: number; | ||
repo: string; | ||
}): Promise<{ | ||
user: string | null; | ||
commit: string | null; | ||
links: { | ||
commit: string | null; | ||
pull: string; | ||
user: string | null; | ||
}; | ||
}> { | ||
if (request.pull === undefined) { | ||
throw new Error("Please pass a pull request number"); | ||
} | ||
if (!request.repo) { | ||
throw new Error( | ||
"Please pass a GitHub repository in the form of userOrOrg/repoName to getInfo" | ||
); | ||
} | ||
if (!validRepoNameRegex.test(request.repo)) { | ||
throw new Error( | ||
`Please pass a valid GitHub repository in the form of userOrOrg/repoName to getInfo (it has to match the "${validRepoNameRegex.source}" pattern)` | ||
); | ||
} | ||
const data = await GHDataLoader.load({ kind: "pull", ...request }); | ||
let user = data?.author; | ||
let commit = data?.mergeCommit; | ||
return { | ||
user: user ? user.login : null, | ||
commit: commit ? commit.abbreviatedOid : null, | ||
links: { | ||
commit: commit | ||
? `[\`${commit.abbreviatedOid}\`](${commit.commitUrl})` | ||
: null, | ||
pull: `[#${request.pull}](https://github.com/${request.repo}/pull/${request.pull})`, | ||
user: user ? `[@${user.login}](${user.url})` : null | ||
} | ||
}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53027
1278