Chore: change triggering of the detect-breaking-changes Github flow (#43188)

* chore: only run the breaking-changes flow on pull requests

* chore: run the detect-breaking-changes flow on opening a PR

* chore: use

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* Wip

* trying to get workflow split running.

* trying to trigger workflow.

* trying to trigger.

* Splits levitate job into two workflows.

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
This commit is contained in:
Levente Balogh
2022-01-03 12:48:12 +01:00
committed by GitHub
parent 1695468de1
commit 8ed5b95f42
6 changed files with 184 additions and 67 deletions

View File

@ -0,0 +1,49 @@
name: Levitate / Detect breaking changes
on: pull_request
jobs:
build:
name: Detect
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup environment
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Get link for the Github Action job
id: job
uses: actions/github-script@v5
with:
script: |
const script = require('./.github/workflows/scripts/pr-get-job-link.js')
await script({github, context, core})
- name: Install dependencies
run: yarn install --immutable
- name: Build packages
run: yarn packages:build
- name: Detect breaking changes
id: breaking-changes
run: ./scripts/check-breaking-changes.sh
env:
FORCE_COLOR: 3
GITHUB_JOB_LINK: ${{ steps.job.outputs.link }}
GITHUB_STEP_NUMBER: 7
- name: Persisting the check output
run: |
mkdir -p ./levitate
echo "{ \"exit_code\": ${{ steps.breaking-changes.outputs.is_breaking }}, \"message\": \"${{ steps.breaking-changes.outputs.mesage }}\", \"job_link\": \"${{ steps.job.outputs.link }}\" }" > ./levitate/result.json
- name: Upload check output as artifact
uses: actions/upload-artifact@v2
with:
name: levitate
path: levitate/

View File

@ -1,46 +1,53 @@
name: Levitate name: Levitate / Report breaking changes
on: push on:
workflow_run:
workflows: ["Levitate / Detect breaking changes"]
types: [completed]
jobs: jobs:
build: notify:
name: Detecting breaking changes name: Report
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Install Node.js - name: Setup environment
uses: actions/setup-node@v2 uses: actions/setup-node@v2
with: with:
node-version: '16' node-version: 16
- run: npm install adm-zip
- name: Install Yarn - name: Download artifact
run: npm install --global yarn
# We need this as we can only access the "Run ID" through the context and we need the "Job ID".
- name: Get link for the Github Action job
id: get-job-link
uses: actions/github-script@v5 uses: actions/github-script@v5
id: download-artifact
env:
RUN_ID: ${{github.event.workflow_run.id }}
with: with:
result-encoding: string result-encoding: string
script: | script: |
const { owner, repo } = context.repo; const runId = process.env.RUN_ID;
const url = `https://api.github.com/repos/${owner}/${repo}/actions/runs/${context.runId}/jobs` const artifactName = 'levitate';
const result = await github.request(url) const script = require('./.github/workflows/scripts/get-workflow-run-artifact.js');
console.log(result.data) return await script({ github, context, core, runId, artifactName });
return `https://github.com/grafana/grafana/runs/${result.data.jobs[0].id}?check_suite_focus=true`; - name: Parsing levitate result
uses: actions/github-script@v5
- name: Find current pull request ID id: levitate-run
uses: jwalton/gh-find-current-pr@v1 env:
id: finder ARTIFACT_FOLDER: ${{ steps.download-artifact.outputs.result }}
with:
script: |
const filePath = `${process.env.ARTIFACT_FOLDER}/result.json`;
const script = require('./.github/workflows/scripts/json-file-to-job-output.js');
await script({ core, filePath });
- name: Check if "breaking change" label exists - name: Check if "breaking change" label exists
id: does-label-exist id: does-label-exist
uses: actions/github-script@v5 uses: actions/github-script@v5
env: env:
PR_NUMBER: ${{ steps.finder.outputs.pr }} PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with: with:
script: | script: |
const { data } = await github.rest.issues.listLabelsOnIssue({ const { data } = await github.rest.issues.listLabelsOnIssue({
@ -53,52 +60,36 @@ jobs:
return doesExist ? 1 : 0; return doesExist ? 1 : 0;
- name: Debug
run: echo -e "Job link - ${{steps.get-job-link.outputs.result}} \nPull request - ${{steps.finder.outputs.pr}} \nLabel exists - ${{steps.does-label-exist.outputs.result}}"
- name: Install dependencies
run: yarn install --immutable
- name: Build packages
run: yarn packages:build
- name: Detect breaking changes
id: breaking-changes
run: ./scripts/check-breaking-changes.sh
env:
FORCE_COLOR: 3
GITHUB_JOB_LINK: ${{steps.get-job-link.outputs.result}}
- name: Comment on PR - name: Comment on PR
if: ${{ steps.breaking-changes.outputs.is_breaking == 1 }} if: ${{ steps.levitate-run.outputs.exit_code == 1 }}
uses: marocchino/sticky-pull-request-comment@v2 uses: marocchino/sticky-pull-request-comment@v2
with: with:
number: ${{ steps.finder.outputs.pr }} number: ${{ github.event.workflow_run.pull_requests[0].number }}
message: | message: |
⚠️ &nbsp;&nbsp;**Possible breaking changes** ⚠️ &nbsp;&nbsp;**Possible breaking changes**
_(Open the links below in a new tab to go to the correct steps)_ _(Open the links below in a new tab to go to the correct steps)_
${{ steps.breaking-changes.outputs.message }} ${{ steps.levitate-run.outputs.message }}
[Check console output](${{steps.get-job-link.outputs.result}}) [Check console output](${{ steps.levitate-run.outputs.job_link }})
- name: Remove comment on PR - name: Remove comment on PR
if: ${{ steps.breaking-changes.outputs.is_breaking == 0 }} if: ${{ steps.levitate-run.outputs.exit_code == 0 }}
uses: marocchino/sticky-pull-request-comment@v2 uses: marocchino/sticky-pull-request-comment@v2
with: with:
number: ${{ steps.finder.outputs.pr }} number: ${{ github.event.workflow_run.pull_requests[0].number }}
delete: true delete: true
- name: Add "breaking change" label - name: Add "breaking change" label
if: ${{ steps.breaking-changes.outputs.is_breaking == 1 && steps.does-label-exist.outputs.result == 0 }} if: ${{ steps.levitate-run.outputs.exit_code == 1 && steps.does-label-exist.outputs.result == 0 }}
uses: actions/github-script@v5 uses: actions/github-script@v5
env: env:
PR_NUMBER: ${{ steps.finder.outputs.pr }} PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with: with:
script: | script: |
github.rest.issues.addLabels({ await github.rest.issues.addLabels({
issue_number: process.env.PR_NUMBER, issue_number: process.env.PR_NUMBER,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@ -106,13 +97,13 @@ jobs:
}) })
- name: Remove "breaking change" label - name: Remove "breaking change" label
if: ${{ steps.breaking-changes.outputs.is_breaking == 0 && steps.does-label-exist.outputs.result == 1 }} if: ${{ steps.levitate-run.outputs.exit_code == 0 && steps.does-label-exist.outputs.result == 1 }}
uses: actions/github-script@v5 uses: actions/github-script@v5
env: env:
PR_NUMBER: ${{ steps.finder.outputs.pr }} PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with: with:
script: | script: |
github.rest.issues.removeLabel({ await github.rest.issues.removeLabel({
issue_number: process.env.PR_NUMBER, issue_number: process.env.PR_NUMBER,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
@ -122,40 +113,36 @@ jobs:
# This is very weird, the actual request goes through (comes back with a 201), but does not assign the team. # This is very weird, the actual request goes through (comes back with a 201), but does not assign the team.
# Related issue: https://github.com/renovatebot/renovate/issues/1908 # Related issue: https://github.com/renovatebot/renovate/issues/1908
- name: Add "grafana/plugins-platform-frontend" as a reviewer - name: Add "grafana/plugins-platform-frontend" as a reviewer
if: ${{ steps.breaking-changes.outputs.is_breaking == 1 }} if: ${{ steps.levitate-run.outputs.exit_code == 1 }}
uses: actions/github-script@v5 uses: actions/github-script@v5
env: env:
PR_NUMBER: ${{ steps.finder.outputs.pr }} PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with: with:
script: | script: |
const response = await github.rest.pulls.requestReviewers({ await github.rest.pulls.requestReviewers({
pull_number: process.env.PR_NUMBER, pull_number: process.env.PR_NUMBER,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
reviewers: [], reviewers: [],
team_reviewers: ['grafana/plugins-platform-frontend'] team_reviewers: ['grafana/plugins-platform-frontend']
}) });
console.log(response)
- name: Remove "grafana/plugins-platform-frontend" from the list of reviewers - name: Remove "grafana/plugins-platform-frontend" from the list of reviewers
if: ${{ steps.breaking-changes.outputs.is_breaking == 0 }} if: ${{ steps.levitate-run.outputs.exit_code == 0 }}
uses: actions/github-script@v5 uses: actions/github-script@v5
env: env:
PR_NUMBER: ${{ steps.finder.outputs.pr }} PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with: with:
script: | script: |
const response = await github.rest.pulls.removeRequestedReviewers({ await github.rest.pulls.removeRequestedReviewers({
pull_number: process.env.PR_NUMBER, pull_number: process.env.PR_NUMBER,
owner: context.repo.owner, owner: context.repo.owner,
repo: context.repo.repo, repo: context.repo.repo,
reviewers: [], reviewers: [],
team_reviewers: ['grafana/plugins-platform-frontend'] team_reviewers: ['grafana/plugins-platform-frontend']
}) });
console.log(response)
- name: Exit - name: Exit
run: exit ${{ steps.breaking-changes.outputs.is_breaking }} run: exit ${{ steps.levitate-run.outputs.exit_code }}
shell: bash shell: bash

View File

@ -0,0 +1,45 @@
module.exports = async ({ github, context, core, runId, artifactName }) => {
try {
const AdmZip = require('adm-zip');
const fs = require('fs');
const { owner, repo } = context.repo;
const { data } = await github.rest.actions.listWorkflowRunArtifacts({
owner,
repo,
run_id: runId,
});
const artifact = data.artifacts.find(a => a.name === artifactName);
if (!artifact) {
throw new Error(`Could not find artifact ${artifactName} in workflow (${runId})`);
}
const zip = await github.rest.actions.downloadArtifact({
owner,
repo,
artifact_id: artifact.id,
archive_format: "zip",
});
const dir = `./tmp/${artifactName}`;
await mkdirRecursive(fs, dir);
const admZip = new AdmZip(Buffer.from(zip.data));
admZip.extractAllTo(dir, true);
return dir;
} catch (error) {
core.restFailed(error.message);
}
}
async function mkdirRecursive(fs, path) {
return new Promise((resolve, reject) => {
fs.mkdir(path, { recursive: true }, (error) => {
if (error) return reject(error);
return resolve();
});
});
}

View File

@ -0,0 +1,27 @@
module.exports = async ({ core, filePath }) => {
try {
const fs = require('fs');
const content = await readFile(fs, filePath);
const result = JSON.parse(content);
core.startGroup('Parsing json file...');
for (const property in result) {
core.info(`${property} <- ${result[property]}`);
core.setOutput(property, result[property]);
}
core.endGroup();
} catch (error) {
core.restFailed(error.message);
}
}
async function readFile(fs, path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (error, data) => {
if (error) return reject(error);
return resolve(data);
});
});
}

View File

@ -0,0 +1,9 @@
module.exports = async ({ github, context, core }) => {
const { owner, repo } = context.repo;
const url = `https://api.github.com/repos/${owner}/${repo}/actions/runs/${context.runId}/jobs`
const result = await github.request(url)
const link = `https://github.com/grafana/grafana/runs/${result.data.jobs[0].id}?check_suite_focus=true`;
core.setOutput('link', link);
}

View File

@ -1,10 +1,11 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Find existing packages using Lerna
PACKAGES=$(lerna list -p -l) PACKAGES=$(lerna list -p -l)
EXIT_CODE=0 EXIT_CODE=0
GITHUB_MESSAGE="" GITHUB_MESSAGE=""
# Loop through packages # Loop through the packages
while IFS= read -r line; do while IFS= read -r line; do
# Read package info # Read package info
@ -16,7 +17,6 @@ while IFS= read -r line; do
PREV="$PACKAGE_NAME@canary" PREV="$PACKAGE_NAME@canary"
CURRENT="$PACKAGE_PATH/dist/" CURRENT="$PACKAGE_PATH/dist/"
# Temporarily skipping @grafana/toolkit, as it doesn't have any exposed static typing # Temporarily skipping @grafana/toolkit, as it doesn't have any exposed static typing
if [[ "$PACKAGE_NAME" == '@grafana/toolkit' ]]; then if [[ "$PACKAGE_NAME" == '@grafana/toolkit' ]]; then
continue continue
@ -39,7 +39,7 @@ while IFS= read -r line; do
if [ $STATUS -gt 0 ] if [ $STATUS -gt 0 ]
then then
EXIT_CODE=1 EXIT_CODE=1
GITHUB_MESSAGE="${GITHUB_MESSAGE}**\`${PACKAGE_NAME}\`** has possible breaking changes ([more info](${GITHUB_JOB_LINK}#step:11:1))<br />" GITHUB_MESSAGE="${GITHUB_MESSAGE}**\`${PACKAGE_NAME}\`** has possible breaking changes ([more info](${GITHUB_JOB_LINK}#step:${GITHUB_STEP_NUMBER}:1))<br />"
fi fi
done <<< "$PACKAGES" done <<< "$PACKAGES"