Skip to content

Commit

Permalink
Project Management Automation: Check for single commit of author
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 27, 2020
1 parent 7a5f6ee commit 9bd7760
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ async function addFirstTimeContributorLabel( payload, octokit ) {
`add-first-time-contributor-label: Searching for commits in ${ owner }/${ repo } by @${ author }`
);

const {
data: { total_count: totalCount },
} = await octokit.search.commits( {
q: `repo:${ owner }/${ repo }+author:${ author }`,
const { data: commits } = await octokit.repos.listCommits( {
owner,
repo,
author,
} );

if ( totalCount !== 0 ) {
if ( commits.length > 1 ) {
debug(
`add-first-time-contributor-label: ${ totalCount } commits found. Aborting`
`add-first-time-contributor-label: Not the first commit for author. Aborting`
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe( 'addFirstTimeContributorLabel', () => {
ref: 'refs/heads/master',
commits: [
{
id: '4c535288a6a2b75ff23ee96c75f7d9877e919241',
message: 'Add a feature from pull request (#123)',
author: {
name: 'Ghost',
Expand All @@ -42,14 +43,14 @@ describe( 'addFirstTimeContributorLabel', () => {
};

const octokit = {
search: {
commits: jest.fn(),
repos: {
listCommits: jest.fn(),
},
};

await addFirstTimeContributorLabel( payloadForBranchPush, octokit );

expect( octokit.search.commits ).not.toHaveBeenCalled();
expect( octokit.repos.listCommits ).not.toHaveBeenCalled();
} );

it( 'does nothing if commit pull request undeterminable', async () => {
Expand All @@ -68,24 +69,25 @@ describe( 'addFirstTimeContributorLabel', () => {
};

const octokit = {
search: {
commits: jest.fn(),
repos: {
listCommits: jest.fn(),
},
};

await addFirstTimeContributorLabel( payloadDirectToMaster, octokit );

expect( octokit.search.commits ).not.toHaveBeenCalled();
expect( octokit.repos.listCommits ).not.toHaveBeenCalled();
} );

it( 'does nothing if the user has commits', async () => {
it( 'does nothing if the user has multiple commits', async () => {
const octokit = {
search: {
commits: jest.fn( () =>
repos: {
listCommits: jest.fn( () =>
Promise.resolve( {
data: {
total_count: 100,
},
data: [
{ sha: '4c535288a6a2b75ff23ee96c75f7d9877e919241' },
{ sha: '59b07cc57adff90630fc9d5cf2317269a0f4f158' },
],
} )
),
},
Expand All @@ -96,20 +98,22 @@ describe( 'addFirstTimeContributorLabel', () => {

await addFirstTimeContributorLabel( payload, octokit );

expect( octokit.search.commits ).toHaveBeenCalledWith( {
q: 'repo:WordPress/gutenberg+author:ghost',
expect( octokit.repos.listCommits ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
author: 'ghost',
} );
expect( octokit.issues.addLabels ).not.toHaveBeenCalled();
} );

it( 'adds the label if the user does not have commits', async () => {
it( 'adds the label if this was the first commit for the user', async () => {
const octokit = {
search: {
commits: jest.fn( () =>
repos: {
listCommits: jest.fn( () =>
Promise.resolve( {
data: {
total_count: 0,
},
data: [
{ sha: '4c535288a6a2b75ff23ee96c75f7d9877e919241' },
],
} )
),
},
Expand All @@ -123,8 +127,10 @@ describe( 'addFirstTimeContributorLabel', () => {

await addFirstTimeContributorLabel( payload, octokit );

expect( octokit.search.commits ).toHaveBeenCalledWith( {
q: 'repo:WordPress/gutenberg+author:ghost',
expect( octokit.repos.listCommits ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
author: 'ghost',
} );
expect( octokit.issues.addLabels ).toHaveBeenCalledWith( {
owner: 'WordPress',
Expand All @@ -137,12 +143,12 @@ describe( 'addFirstTimeContributorLabel', () => {

it( 'aborts if the request to retrieve WordPress.org user profile fails', async () => {
const octokit = {
search: {
commits: jest.fn( () =>
repos: {
listCommits: jest.fn( () =>
Promise.resolve( {
data: {
total_count: 0,
},
data: [
{ sha: '4c535288a6a2b75ff23ee96c75f7d9877e919241' },
],
} )
),
},
Expand All @@ -158,8 +164,10 @@ describe( 'addFirstTimeContributorLabel', () => {

await addFirstTimeContributorLabel( payload, octokit );

expect( octokit.search.commits ).toHaveBeenCalledWith( {
q: 'repo:WordPress/gutenberg+author:ghost',
expect( octokit.repos.listCommits ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
author: 'ghost',
} );
expect( octokit.issues.addLabels ).toHaveBeenCalledWith( {
owner: 'WordPress',
Expand All @@ -172,12 +180,12 @@ describe( 'addFirstTimeContributorLabel', () => {

it( 'prompts the user to link their GitHub account to their WordPress.org profile', async () => {
const octokit = {
search: {
commits: jest.fn( () =>
repos: {
listCommits: jest.fn( () =>
Promise.resolve( {
data: {
total_count: 0,
},
data: [
{ sha: '4c535288a6a2b75ff23ee96c75f7d9877e919241' },
],
} )
),
},
Expand All @@ -201,8 +209,10 @@ describe( 'addFirstTimeContributorLabel', () => {

await addFirstTimeContributorLabel( payload, octokit );

expect( octokit.search.commits ).toHaveBeenCalledWith( {
q: 'repo:WordPress/gutenberg+author:ghost',
expect( octokit.repos.listCommits ).toHaveBeenCalledWith( {
owner: 'WordPress',
repo: 'gutenberg',
author: 'ghost',
} );
expect( octokit.issues.addLabels ).toHaveBeenCalledWith( {
owner: 'WordPress',
Expand Down

0 comments on commit 9bd7760

Please sign in to comment.