Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add commit fields to access authors and PR number #1717

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
402 changes: 266 additions & 136 deletions __snapshots__/github.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface Commit {
message: string;
files?: string[];
pullRequest?: PullRequest;
pullRequestNumbers?: number[];
authors?: string[];
}

export interface ConventionalCommit extends Commit {
Expand Down Expand Up @@ -384,6 +386,8 @@ export function parseConventionalCommits(
message: parsedCommit.header,
files: commit.files,
pullRequest: commit.pullRequest,
pullRequestNumbers: commit.pullRequestNumbers,
authors: commit.authors,
type: parsedCommit.type,
scope: parsedCommit.scope,
bareMessage: parsedCommit.subject,
Expand Down
59 changes: 58 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,20 @@ interface GitHubCreateOptions {

type CommitFilter = (commit: Commit) => boolean;

interface GraphQLAuthor {
name: string;
email: string;
}

interface GraphQLCommit {
sha: string;
message: string;
associatedPullRequests: {
nodes: GraphQLPullRequest[];
};
authors: {
nodes: GraphQLAuthor[];
};
}

interface GraphQLPullRequest {
Expand All @@ -108,6 +116,11 @@ interface GraphQLPullRequest {
mergeCommit?: {
oid: string;
};
commits: {
nodes: {
commit: GraphQLCommit;
}[];
};
files: {
nodes: {
path: string;
Expand Down Expand Up @@ -428,6 +441,24 @@ export class GitHub {
hasNextPage
}
}
commits(first: 100) {
nodes {
commit {
authors(first: 10) {
nodes {
email
name
}
}
}
}
}
}
}
authors(first: 10) {
nodes {
email
name
}
}
sha: oid
Expand Down Expand Up @@ -479,7 +510,33 @@ export class GitHub {
sha: graphCommit.sha,
message: graphCommit.message,
};
const pullRequest = graphCommit.associatedPullRequests.nodes.find(pr => {

const authors = new Set<string>();
const associatedPRs = graphCommit.associatedPullRequests.nodes;

if (associatedPRs.length > 0) {
// Add authors from commits of associated PRs
associatedPRs.forEach(pr => {
pr.commits.nodes.forEach(node => {
node.commit.authors.nodes
.map(author => author.name)
.forEach(authors.add, authors);
});
});
Comment on lines +519 to +525
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will grab authors that are not necessarily intended. Associated pull requests include any PRs that are mentioned in the commit message, not necessarily the PR that created it.

So if we mention another PR by another author, this will grab that PR's author who should not necessarily be attached to the current PR.

This weird behavior is why we have that logic to find the associated pull request whose merge commit SHA matches the commit SHA (skip the mentioned PRs)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little puzzled, and I'm a little curious about what scenario this will happen in. Can you tell me how to trigger this problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you reference another PR in the PR body like See #1234, then graphql will return that as an associatedPullRequest.

} else {
graphCommit.authors.nodes
.map(author => author.name)
.forEach(authors.add, authors);
}

commit.authors = Array.from(authors);

if (associatedPRs.length > 0) {
commit.pullRequestNumbers =
graphCommit.associatedPullRequests.nodes.map(pr => pr.number);
}

const pullRequest = associatedPRs.find(pr => {
return pr.mergeCommit && pr.mergeCommit.oid === graphCommit.sha;
});
if (pullRequest) {
Expand Down
72 changes: 72 additions & 0 deletions test/fixtures/commits-since-many-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,46 @@
"pageInfo": {
"hasNextPage": true
}
},
"commits": {
"nodes": [
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
},
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
}
]
}
}
]
},
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
},
"sha": "e6daec403626c9987c7af0d97b34f324cd84320a",
"message": "Merge pull request #7 from chingor13/feature-branch-plain-merge\n\nfeat: feature that will be plain merged"
},
Expand All @@ -48,10 +84,46 @@
},
"files": {
"nodes": []
},
"commits": {
"nodes": [
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
},
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
}
]
}
}
]
},
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
},
"sha": "b29149f890e6f76ee31ed128585744d4c598924c",
"message": "feat: feature-branch-plain-merge commit 2"
}
Expand Down
74 changes: 73 additions & 1 deletion test/fixtures/commits-since-no-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,43 @@
"mergeCommit": {
"oid": "e6daec403626c9987c7af0d97b34f324cd84320a"
},
"files": null
"files": null,
"commits": {
"nodes": [
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
},
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
}
]
}
}
]
},
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
},
Expand All @@ -43,10 +79,46 @@
},
"files": {
"nodes": []
},
"commits": {
"nodes": [
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
},
{
"commit": {
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
}
}
}
]
}
}
]
},
"authors": {
"nodes": [
{
"email": "chingor@google.com",
"name": "Jeff Ching"
}
]
},
"sha": "b29149f890e6f76ee31ed128585744d4c598924c",
"message": "feat: feature-branch-plain-merge commit 2"
}
Expand Down