Skip to content

Commit

Permalink
Merge pull request #103 from Automattic/feature-support-comments-and-…
Browse files Browse the repository at this point in the history
…drafts

By default do not post generic support comments on draft PRs
  • Loading branch information
gudmdharalds authored Jun 11, 2020
2 parents b6383b4 + 51a3387 commit 8ca2ceb
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 36 deletions.
26 changes: 26 additions & 0 deletions github-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,9 @@ function vipgoci_github_pr_generic_support_comment(
'post-generic-pr-support-comments' =>
$options['post-generic-pr-support-comments'],

'post-generic-pr-support-comments-on-drafts' =>
$options['post-generic-pr-support-comments-on-drafts'],

'post-generic-pr-support-comments-string' =>
$options['post-generic-pr-support-comments-string'],

Expand Down Expand Up @@ -1941,6 +1944,29 @@ function vipgoci_github_pr_generic_support_comment(
continue;
}

/*
* Do not post support comments on drafts when
* not configured to do so.
*/
if (
( false === $options['post-generic-pr-support-comments-on-drafts'] ) &&
( true === $pr_item->draft )
) {
vipgoci_log(
'Not posting support-comment to PR, is draft',
array(
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'pr_number' => $pr_item->number,
'pr_base_ref' => $pr_item->base->ref,
'post-generic-pr-support-comments-on-drafts' =>
$options['post-generic-pr-support-comments-on-drafts'],
)
);

continue;
}

/*
* Check if the comment we are set to
* post already exists, and if so, do
Expand Down
282 changes: 246 additions & 36 deletions tests/GitHubPrGenericSupportCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ protected function setUp() {

$this->options['post-generic-pr-support-comments'] = true;

$this->options['post-generic-pr-support-comments-on-drafts'] = false;

$this->options['post-generic-pr-support-comments-string'] =
'This is a generic support message from `vip-go-ci`. We hope this is useful.';

Expand Down Expand Up @@ -341,16 +343,31 @@ public function testPostingWorksAnyBranch() {
$pr_item->number
);

$this->assertTrue(
count( $pr_comments ) > 0
);
if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}

/*
Expand All @@ -373,16 +390,31 @@ public function testPostingWorksAnyBranch() {
$pr_item->number
);

$this->assertTrue(
count( $pr_comments ) > 0
);
if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}
}

Expand Down Expand Up @@ -424,16 +456,31 @@ public function testPostingWorksSpecificBranch() {
$pr_item->number
);

$this->assertTrue(
count( $pr_comments ) > 0
);
if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}

/*
Expand All @@ -456,16 +503,31 @@ public function testPostingWorksSpecificBranch() {
$pr_item->number
);

$this->assertTrue(
count( $pr_comments ) > 0
);
if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}
}

Expand Down Expand Up @@ -543,4 +605,152 @@ public function testPostingSkippedInvalidBranch() {
);
}
}

/**
* @covers ::vipgoci_github_pr_generic_support_comment
*/
public function testPostingWorksWithDraftPRs() {
$options_test = vipgoci_unittests_options_test(
$this->options,
array(),
$this
);

if ( -1 === $options_test ) {
return;
}

// Configure branches we can post against
$this->options['post-generic-pr-support-comments-branches'] =
array( 'any' );

// Get Pull-Requests
$prs_implicated = $this->_getPrsImplicated();

// Check we have at least one PR
$this->assertTrue(
count( $prs_implicated ) > 0
);

// Try to submit support comment
vipgoci_github_pr_generic_support_comment(
$this->options,
$prs_implicated
);

// Check if commenting succeeded
foreach( $prs_implicated as $pr_item ) {
$pr_comments = $this->_getPrGenericComments(
$pr_item->number
);

if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}

/*
* Clear cache -- see explanation above
*/

vipgoci_cache(
VIPGOCI_CACHE_CLEAR
);

// Try re-posting
vipgoci_github_pr_generic_support_comment(
$this->options,
$prs_implicated
);

// And make sure it did not succeed
foreach( $prs_implicated as $pr_item ) {
$pr_comments = $this->_getPrGenericComments(
$pr_item->number
);

if ( $pr_item->draft === true ) {
$this->assertTrue(
count( $pr_comments ) === 0
);

$this->assertEquals(
0,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}

else {
$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}

/*
* Re-configure to post on drafts too and re-run
*/

vipgoci_cache(
VIPGOCI_CACHE_CLEAR
);

// Post on draft PRs
$this->options['post-generic-pr-support-comments-on-drafts'] = true;

// Try re-posting
vipgoci_github_pr_generic_support_comment(
$this->options,
$prs_implicated
);

// And make sure it did succeed
foreach( $prs_implicated as $pr_item ) {
$pr_comments = $this->_getPrGenericComments(
$pr_item->number
);

$this->assertTrue(
count( $pr_comments ) > 0
);

$this->assertEquals(
1,
$this->_countSupportCommentsFromUs(
$pr_comments
)
);
}
}
}
3 changes: 3 additions & 0 deletions vip-go-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function vipgoci_run() {
'dry-run:',
'informational-url:',
'post-generic-pr-support-comments:',
'post-generic-pr-support-comments-on-drafts:',
'post-generic-pr-support-comments-string:',
'post-generic-pr-support-comments-branches:',
'post-generic-pr-support-comments-repo-meta-match:',
Expand Down Expand Up @@ -770,6 +771,8 @@ function vipgoci_run() {

vipgoci_option_bool_handle( $options, 'post-generic-pr-support-comments', 'false' );

vipgoci_option_bool_handle( $options, 'post-generic-pr-support-comments-on-drafts', 'false' );

if ( ! empty( $options['post-generic-pr-support-comments-string'] ) ) {
$options['post-generic-pr-support-comments-string'] =
trim(
Expand Down

0 comments on commit 8ca2ceb

Please sign in to comment.