-
Notifications
You must be signed in to change notification settings - Fork 0
/
issueRecording.js
71 lines (60 loc) · 1.92 KB
/
issueRecording.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const fs = require("fs");
function scanRecordingId(body) {
const match = body.match(/https:\/\/app.replay\.io\/recording\/([a-zA-Z0-9\-]+)/);
if (!match) {
return null;
}
const titleAndId = match[1];
const match2 = titleAndId.match(/^.*?--([a-zA-Z0-9\-]+)$/);
if (match2) {
return match2[1];
}
return titleAndId;
}
async function getIssueRecording(options) {
const { github, eventPath, repositoryName } = options;
const eventPayload = JSON.parse(
fs.readFileSync(eventPath, "utf8")
);
const [owner, repo] = repositoryName.split("/");
const prNumber = eventPayload.pull_request.number;
const prRevision = eventPayload.pull_request.head.sha;
console.log("PullRequestData", owner, repo, prNumber, prRevision);
// Get linked issues using GraphQL
const query = `
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 10) {
nodes {
number
}
}
}
}
}
`;
const linkedIssuesResult = await github.graphql(query, {
owner,
repo,
prNumber
});
const linkedIssues = linkedIssuesResult.repository.pullRequest.closingIssuesReferences.nodes.map(node => node.number);
console.log("LinkedIssueNumbers", JSON.stringify(linkedIssues));
if (!linkedIssues.length) {
throw new Error("No linked issues found");
}
if (linkedIssues.length > 1) {
throw new Error("More than one linked issue found");
}
const issueNumber = linkedIssues[0];
const issue = await github.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
});
console.log("LinkedIssueContents", issueNumber, issue.data.title, issue.data.body);
const recordingId = scanRecordingId(issue.data.body);
return { owner, repo, prNumber, prRevision, issueNumber, recordingId };
}
module.exports = getIssueRecording;