Skip to content

Commit af17fb2

Browse files
authored
Attempt to re-structure workflows to be more generic & reusable (#2364)
* Attempt to re-structure workflows to be more generic & reusable * Iterate for reusable workflows can't call each other * don't pass pullrequest params if no prnumber * Comments * Fix reusable workflow call * Pass pr_id properly * Fix run condition for prdetails job * Fix needs dependency * Stash work so far * Fix copypasta * Update * Define outputs from pr_details.yml * Fix output reporting * Fix something or other
1 parent 7201334 commit af17fb2

File tree

7 files changed

+190
-83
lines changed

7 files changed

+190
-83
lines changed

.github/workflows/notify-downstream.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: Notify Downstream Projects
22
on:
33
push:
44
branches: [ develop ]
5+
concurrency: ${{ github.workflow }}-${{ github.ref }}
56
jobs:
67
notify-matrix-react-sdk:
78
runs-on: ubuntu-latest

.github/workflows/pr_details.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Find details about the PR associated with this ref
2+
# Outputs:
3+
# prnumber: the ID number of the associated PR
4+
# headref: the name of the head branch of the PR
5+
# baseref: the name of the base branch of the PR
6+
name: PR Details
7+
on:
8+
workflow_call:
9+
inputs:
10+
owner:
11+
type: string
12+
required: true
13+
description: The github username of the owner of the head branch
14+
branch:
15+
type: string
16+
required: true
17+
description: The name of the head branch
18+
outputs:
19+
pr_id:
20+
description: The ID of the PR found
21+
value: ${{ jobs.prdetails.outputs.pr_id }}
22+
head_branch:
23+
description: The head branch of the PR found
24+
value: ${{ jobs.prdetails.outputs.head_branch }}
25+
base_branch:
26+
description: The base branch of the PR found
27+
value: ${{ jobs.prdetails.outputs.base_branch }}
28+
29+
jobs:
30+
prdetails:
31+
name: Find PR Details
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: "🔍 Read PR details"
35+
id: details
36+
# We need to find the PR number that corresponds to the branch, which we do by searching the GH API
37+
# The workflow_run event includes a list of pull requests, but it doesn't get populated for
38+
# forked PRs: https://docs.github.com/en/rest/reference/checks#create-a-check-run
39+
run: |
40+
head_branch='${{ inputs.owner }}:${{ inputs.branch }}'
41+
echo "Head branch: $head_branch"
42+
pulls_uri="https://api.github.com/repos/${{ github.repository }}/pulls?head=$(jq -Rr '@uri' <<<$head_branch)"
43+
pr_data=$(curl -s -H 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' "$pulls_uri")
44+
45+
pr_number=$(jq -r '.[] | .number' <<< "$pr_data")
46+
echo "PR number: $pr_number"
47+
echo "::set-output name=prnumber::$pr_number"
48+
49+
head_ref=$(jq -r '.[] | .head.ref' <<< "$pr_data")
50+
echo "Head ref: $head_ref"
51+
echo "::set-output name=headref::$head_ref"
52+
53+
base_ref=$(jq -r '.[] | .base.ref' <<< "$pr_data")
54+
echo "Base ref: $base_ref"
55+
echo "::set-output name=baseref::$base_ref"
56+
outputs:
57+
pr_id: ${{ steps.details.outputs.prnumber }}
58+
head_branch: ${{ steps.details.outputs.headref }}
59+
base_branch: ${{ steps.details.outputs.baseref }}

.github/workflows/pull_request.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: Pull Request
22
on:
33
pull_request_target:
44
types: [ opened, edited, labeled, unlabeled, synchronize ]
5+
concurrency:
6+
group: ${{ github.workflow }}-${{ github.ref }}
7+
cancel-in-progress: true
58
jobs:
69
changelog:
710
name: Preview Changelog

.github/workflows/sonarcloud.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: SonarCloud
2+
on:
3+
workflow_call:
4+
inputs:
5+
repo:
6+
type: string
7+
required: true
8+
description: The full name of the repo in org/repo format
9+
head_branch:
10+
type: string
11+
required: true
12+
description: The name of the head branch
13+
# We cannot use ${{ github.sha }} here as for pull requests it'll be a simulated merge commit instead
14+
revision:
15+
type: string
16+
required: true
17+
description: The git revision with which this sonar run should be associated
18+
19+
# Coverage specific parameters, assumes coverage reports live in a /coverage/ directory
20+
coverage_workflow_name:
21+
type: string
22+
required: false
23+
description: The name of the workflow which uploaded the `coverage` artifact, if any
24+
coverage_run_id:
25+
type: string
26+
required: false
27+
description: The run_id of the workflow which upload the coverage relevant to this run
28+
29+
# PR specific parameters
30+
pr_id:
31+
type: string
32+
required: false
33+
description: The ID number of the PR if this workflow is being triggered due to one
34+
base_branch:
35+
type: string
36+
required: false
37+
description: The base branch of the PR if this workflow is being triggered due to one
38+
39+
# Org specific parameters
40+
main_branch:
41+
type: string
42+
required: false
43+
description: The default branch of the repository
44+
default: "develop"
45+
secrets:
46+
SONAR_TOKEN:
47+
required: true
48+
jobs:
49+
analysis:
50+
name: Analysis
51+
runs-on: ubuntu-latest
52+
steps:
53+
- name: "🧮 Checkout code"
54+
uses: actions/checkout@v3
55+
with:
56+
repository: ${{ inputs.repo }}
57+
ref: ${{ inputs.head_branch }} # checkout commit that triggered this workflow
58+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
59+
60+
# Fetch develop so that Sonar can identify new issues in PR builds
61+
- name: "📕 Fetch ${{ inputs.main_branch }}"
62+
if: inputs.head_branch != inputs.main_branch
63+
run: git rev-parse HEAD && git fetch origin ${{ inputs.main_branch }}:${{ inputs.main_branch }} && git status && git rev-parse HEAD
64+
65+
# There's a 'download artifact' action, but it hasn't been updated for the workflow_run action
66+
# (https://github.com/actions/download-artifact/issues/60) so instead we get this mess:
67+
- name: "📥 Download Coverage Report"
68+
uses: dawidd6/action-download-artifact@v2
69+
if: inputs.coverage_workflow_name
70+
with:
71+
workflow: ${{ inputs.coverage_workflow_name }}
72+
run_id: ${{ inputs.coverage_run_id }}
73+
name: coverage
74+
path: coverage
75+
76+
- name: "🔍 Read package.json version"
77+
id: version
78+
uses: martinbeentjes/npm-get-version-action@main
79+
80+
- name: "🩻 SonarCloud Scan"
81+
uses: SonarSource/sonarcloud-github-action@master
82+
with:
83+
args: >
84+
-Dsonar.projectVersion=${{ steps.version.outputs.current-version }}
85+
-Dsonar.scm.revision=${{ inputs.revision }}
86+
-Dsonar.pullrequest.key=${{ inputs.pr_id }}
87+
-Dsonar.pullrequest.branch=${{ inputs.pr_id && inputs.head_branch }}
88+
-Dsonar.pullrequest.base=${{ inputs.pr_id && inputs.base_branch }}
89+
env:
90+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
91+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/sonarqube.yml

Lines changed: 29 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,34 @@ on:
44
workflows: [ "Tests" ]
55
types:
66
- completed
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.ref }}
9+
cancel-in-progress: true
710
jobs:
8-
sonarqube:
9-
name: SonarQube
10-
runs-on: ubuntu-latest
11-
if: github.event.workflow_run.conclusion == 'success'
12-
steps:
13-
- name: "🧮 Checkout code"
14-
uses: actions/checkout@v3
15-
with:
16-
repository: ${{ github.event.workflow_run.head_repository.full_name }}
17-
ref: ${{ github.event.workflow_run.head_branch }} # checkout commit that triggered this workflow
18-
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
19-
20-
# fetch develop so that Sonar can identify new issues in PR builds
21-
- name: "📕 Fetch develop"
22-
if: "github.event.workflow_run.head_branch != 'develop'"
23-
run: git rev-parse HEAD && git fetch origin develop:develop && git status && git rev-parse HEAD
24-
25-
# There's a 'download artifact' action, but it hasn't been updated for the workflow_run action
26-
# (https://github.com/actions/download-artifact/issues/60) so instead we get this mess:
27-
- name: "📥 Download Coverage Report"
28-
uses: actions/[email protected]
29-
with:
30-
script: |
31-
const artifacts = await github.actions.listWorkflowRunArtifacts({
32-
owner: context.repo.owner,
33-
repo: context.repo.repo,
34-
run_id: ${{ github.event.workflow_run.id }},
35-
});
36-
const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
37-
return artifact.name == "coverage"
38-
})[0];
39-
const download = await github.actions.downloadArtifact({
40-
owner: context.repo.owner,
41-
repo: context.repo.repo,
42-
artifact_id: matchArtifact.id,
43-
archive_format: 'zip',
44-
});
45-
const fs = require('fs');
46-
fs.writeFileSync('${{github.workspace}}/coverage.zip', Buffer.from(download.data));
47-
48-
- name: "🗃️ Extract Coverage Report"
49-
run: unzip -d coverage coverage.zip && rm coverage.zip
50-
51-
- name: "🔍 Read latest tag"
52-
id: version
53-
uses: WyriHaximus/github-action-get-previous-tag@v1
11+
prdetails:
12+
name: ℹ️ PR Details
13+
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'
14+
uses: matrix-org/matrix-js-sdk/.github/workflows/pr_details.yml@develop
15+
with:
16+
owner: ${{ github.event.workflow_run.head_repository.owner.login }}
17+
branch: ${{ github.event.workflow_run.head_branch }}
5418

55-
- name: "🔍 Read PR details"
56-
id: prdetails
57-
if: github.event.workflow_run.event == 'pull_request'
58-
# We need to find the PR number that corresponds to the branch, which we do by searching the GH API
59-
# The workflow_run event includes a list of pull requests, but it doesn't get populated for
60-
# forked PRs: https://docs.github.com/en/rest/reference/checks#create-a-check-run
61-
run: |
62-
head_branch='${{github.event.workflow_run.head_repository.owner.login}}:${{github.event.workflow_run.head_branch}}'
63-
echo "Head branch: $head_branch"
64-
pulls_uri="https://api.github.com/repos/${{ github.repository }}/pulls?head=$(jq -Rr '@uri' <<<$head_branch)"
65-
pr_data=$(curl -s -H 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' "$pulls_uri")
66-
67-
pr_number=$(jq -r '.[] | .number' <<< "$pr_data")
68-
echo "PR number: $pr_number"
69-
echo "::set-output name=prnumber::$pr_number"
70-
71-
head_ref=$(jq -r '.[] | .head.ref' <<< "$pr_data")
72-
echo "Head ref: $head_ref"
73-
echo "::set-output name=headref::$head_ref"
74-
75-
base_ref=$(jq -r '.[] | .base.ref' <<< "$pr_data")
76-
echo "Base ref: $base_ref"
77-
echo "::set-output name=baseref::$base_ref"
78-
79-
- name: "🩻 SonarCloud Scan"
80-
uses: SonarSource/sonarcloud-github-action@master
81-
with:
82-
args: >
83-
-Dsonar.projectVersion=${{ steps.version.outputs.tag }}
84-
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
85-
-Dsonar.pullrequest.key=${{ steps.prdetails.outputs.prnumber }}
86-
-Dsonar.pullrequest.branch=${{ steps.prdetails.outputs.headref }}
87-
-Dsonar.pullrequest.base=${{ steps.prdetails.outputs.baseref }}
88-
env:
89-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
90-
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
19+
sonarqube:
20+
name: 🩻 SonarQube
21+
needs: prdetails
22+
# Only wait for prdetails if it isn't skipped
23+
if: |
24+
always() &&
25+
(needs.prdetails.result == 'success' || needs.prdetails.result == 'skipped') &&
26+
github.event.workflow_run.conclusion == 'success'
27+
uses: matrix-org/matrix-js-sdk/.github/workflows/sonarcloud.yml@develop
28+
with:
29+
repo: ${{ github.event.workflow_run.head_repository.full_name }}
30+
pr_id: ${{ needs.prdetails.outputs.pr_id }}
31+
head_branch: ${{ needs.prdetails.outputs.head_branch || github.event.workflow_run.head_branch }}
32+
base_branch: ${{ needs.prdetails.outputs.base_branch }}
33+
revision: ${{ github.event.workflow_run.head_sha }}
34+
coverage_workflow_name: tests.yml
35+
coverage_run_id: ${{ github.event.workflow_run.id }}
36+
secrets:
37+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

.github/workflows/static_analysis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ on:
33
pull_request: { }
44
push:
55
branches: [ develop, master ]
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.ref }}
8+
cancel-in-progress: true
69
jobs:
710
ts_lint:
811
name: "Typescript Syntax Check"

.github/workflows/tests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ name: Tests
22
on:
33
pull_request: { }
44
push:
5-
branches: [ develop, main, master ]
5+
branches: [ develop, master ]
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.ref }}
8+
cancel-in-progress: true
69
jobs:
710
jest:
811
name: Jest

0 commit comments

Comments
 (0)