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

Restrict draft PR CI runs to label #172315

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 84 additions & 25 deletions .buildkite/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .buildkite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"tslib": "*"
},
"devDependencies": {
"@octokit/plugin-rest-endpoint-methods": "^10.2.0",
"@types/chai": "^4.3.3",
"@types/js-yaml": "^4.0.5",
"@types/minimatch": "^3.0.5",
Expand Down
23 changes: 22 additions & 1 deletion .buildkite/pipeline-utils/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods';

const github = new Octokit({
auth: process.env.GITHUB_TOKEN,
Expand Down Expand Up @@ -91,3 +92,23 @@ export const doAnyChangesMatch = async (

return anyFilesMatchRequired;
};

export const isDraftPR = async (
owner = process.env.GITHUB_PR_BASE_OWNER,
repo = process.env.GITHUB_PR_BASE_REPO,
prNumber: undefined | string | number = process.env.GITHUB_PR_NUMBER
) => {
if (!owner || !repo || !prNumber) {
throw Error(
"Couldn't retrieve Github PR info from environment variables in order to retrieve PR changes"
);
}

return await github.pulls
.get({
owner,
repo,
pull_number: typeof prNumber === 'number' ? prNumber : parseInt(prNumber, 10),
})
.then((r) => r.data.draft);
};
15 changes: 14 additions & 1 deletion .buildkite/scripts/pipelines/pull_request/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { execSync } from 'child_process';
import fs from 'fs';
import prConfigs from '../../../pull_requests.json';
import { areChangesSkippable, doAnyChangesMatch } from '#pipeline-utils';
import { areChangesSkippable, doAnyChangesMatch, isDraftPR } from '#pipeline-utils';

const prConfig = prConfigs.jobs.find((job) => job.pipelineSlug === 'kibana-pull-request');

Expand Down Expand Up @@ -39,6 +39,19 @@ const uploadPipeline = (pipelineContent: string | object) => {

(async () => {
try {
// Ideally could use process.env.BUILDKITE_PULL_REQUEST_DRAFT, but its not being set properly in CI
if (!GITHUB_PR_LABELS.includes('ci:run_on_draft') && (await isDraftPR())) {
console.log(
'Skipping CI for draft PR. If you need to run CI for this PR add the label ci:run_on_draft and retrigger CI.'
);

// Since we skip everything, including post-build, we need to at least make sure the commit status gets set
execSync('BUILD_SUCCESSFUL=true .buildkite/scripts/lifecycle/commit_status_complete.sh', {
stdio: 'inherit',
});
process.exit(0);
}

const skippable = await areChangesSkippable(SKIPPABLE_PR_MATCHERS, REQUIRED_PATHS);

if (skippable) {
Expand Down