Skip to content

Commit 42610d1

Browse files
SONARIAC-2096 Add "Update Analyzer" Github Action (#11)
* add 'Update Analyzer' Github Action * fix typo in readme * update sed command * Update update-analyzer/action.yml Co-authored-by: Jonas Wielage <[email protected]> * fix review issues --------- Co-authored-by: Jonas Wielage <[email protected]>
1 parent 234f272 commit 42610d1

File tree

3 files changed

+177
-1
lines changed

3 files changed

+177
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ A centralized collection of reusable GitHub Actions designed to streamline and a
1010
* [**Publish GitHub Release**](publish-github-release/README.md): Publishes a GitHub Release with notes fetched from Jira or provided directly.
1111
* [**Release Jira Version**](release-jira-version/README.md): Releases a Jira version and creates the next one.
1212
* [**Update integration tickets**](update-integration-tickets/README.md): Finds and optionally updates SQS and SC integration tickets.
13-
13+
* [**Update Analyzer**](update-analyzer/README.md): Updates an analyzer version in SonarQube or SonarCloud and creates a pull request.

update-analyzer/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Update Analyzer Action
2+
3+
This GitHub Action automates the process of updating an analyzer's version within SonarQube or SonarCloud. It checks out the respective product repository, modifies the `build.gradle` file with the new version, and opens a pull request with the changes.
4+
5+
The action determines whether to target SonarQube (`sonar-enterprise`) or SonarCloud (`sonarcloud-core`) based on the prefix of the provided `ticket` number (`SONAR-` or `SC-`).
6+
7+
## Prerequisites
8+
9+
The `github-token` provided to the action must have the following permissions for the target repository (e.g., `SonarSource/sonar-enterprise`):
10+
* `contents: write`
11+
* `pull-requests: write`
12+
13+
An example PR how to request a token with those permissions can be found [here](https://github.com/SonarSource/re-terraform-aws-vault/pull/6693).
14+
15+
## Inputs
16+
17+
| Input | Description | Required | Default |
18+
|-------------------|---------------------------------------------------------------------------------------------|----------|----------|
19+
| `version` | The new version to set for the analyzer (e.g., `1.12.0.12345`). | `true` | |
20+
| `ticket` | The Jira ticket number. Must start with `SONAR-` (for SonarQube) or `SC-` (for SonarCloud). | `true` | |
21+
| `plugin-language` | The language key of the plugin to update (e.g., `architecture`, `java`). | `true` | |
22+
| `github-token` | A GitHub token with permissions to create pull requests in the target repository. | `true` | |
23+
| `base_branch` | The base branch for the pull request. | `false` | `master` |
24+
| `draft` | A boolean value (`true`/`false`) to control if the pull request is created as a draft. | `false` | `false` |
25+
| `reviewers` | A comma-separated list of GitHub usernames to request a review from (e.g., `user1,user2`). | `false` | |
26+
| `body` | The body of the pull request. | `false` | |
27+
28+
29+
## Outputs
30+
31+
| Output | Description |
32+
|----------|--------------------------------------|
33+
| `pr-url` | The URL of the created pull request. |
34+
35+
## Example Usage
36+
37+
Here is an example of how to use this action in a workflow. This workflow can be triggered manually (`workflow_dispatch`) and uses a secret to provide the required token. The second job demonstrates how to use the `pr-url` output.
38+
39+
```yaml
40+
# .github/workflows/update-my-analyzer.yml
41+
name: Manually Update Analyzer
42+
43+
on:
44+
workflow_dispatch:
45+
inputs:
46+
version:
47+
description: 'The new analyzer version (e.g. 1.12.0.12345)'
48+
required: true
49+
type: string
50+
ticket:
51+
description: 'The Jira ticket number (e.g. SONAR-12345)'
52+
required: true
53+
type: string
54+
55+
jobs:
56+
update-analyzer:
57+
name: Update PHP Analyzer in SonarQube
58+
runs-on: ubuntu-latest
59+
outputs:
60+
pull_request_url: ${{ steps.update_step.outputs.pr-url }}
61+
steps:
62+
- name: get secrets
63+
id: secrets
64+
uses: SonarSource/vault-action-wrapper@d1c1ab4ca5ad07fd9cdfe1eff038a39673dfca64 # v2.4.2-1
65+
with:
66+
secrets: |
67+
development/github/token/SonarSource-sonar-php-release-automation token | GITHUB_TOKEN;
68+
69+
- name: Update analyzer and create PR
70+
id: update_step
71+
uses: SonarSource/release-github-actions/update-analyzer@master
72+
with:
73+
version: ${{ inputs.version }}
74+
ticket: ${{ inputs.ticket }}
75+
plugin-language: 'php'
76+
github-token: ${{ fromJSON(steps.secrets.outputs.vault).GITHUB_TOKEN }}
77+
draft: true
78+
79+
report-pr-url:
80+
name: Report PR URL
81+
runs-on: ubuntu-latest
82+
needs: update-analyzer
83+
if: needs.update-analyzer.outputs.pull_request_url != ''
84+
steps:
85+
- name: Echo the PR URL
86+
run: |
87+
echo "Pull request created at: ${{ needs.update-analyzer.outputs.pull_request_url }}"

update-analyzer/action.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: 'Update Analyzer'
2+
description: 'Updates the version of a specified analyzer in SonarQube or SonarCloud and creates a pull request.'
3+
author: 'SonarSource'
4+
5+
inputs:
6+
version:
7+
description: 'The new version to set for the analyzer (e.g., 1.12.0.12345).'
8+
required: true
9+
ticket:
10+
description: 'The Jira ticket number (e.g., SONAR-12345 or SC-12345). This determines the target product.'
11+
required: true
12+
plugin-language:
13+
description: 'The language key of the plugin to update (e.g., architecture, java, csharp).'
14+
required: true
15+
github-token:
16+
description: 'A GitHub token with permissions to create pull requests in the target repository.'
17+
required: true
18+
base_branch:
19+
description: 'The base branch for the product pull request.'
20+
required: false
21+
default: 'master'
22+
draft:
23+
description: 'A boolean value to control if the pull request is created as a draft.'
24+
required: false
25+
default: 'false'
26+
reviewers:
27+
description: 'A comma-separated list of GitHub usernames to request a review from.'
28+
required: false
29+
pr_body:
30+
description: 'The body of the pull request.'
31+
required: false
32+
33+
outputs:
34+
pr-url:
35+
description: 'The URL of the created pull request.'
36+
value: ${{ steps.create_pr.outputs.pull-request-url }}
37+
38+
runs:
39+
using: 'composite'
40+
steps:
41+
- name: Set up environment
42+
id: setup_env
43+
shell: bash
44+
run: |
45+
if [[ "${{ inputs.ticket }}" == SONAR-* ]]; then
46+
echo "PRODUCT_REPOSITORY=sonar-enterprise" >> $GITHUB_ENV
47+
echo "BUILD_GRADLE_FILE=build.gradle" >> $GITHUB_ENV
48+
elif [[ "${{ inputs.ticket }}" == SC-* ]]; then
49+
echo "PRODUCT_REPOSITORY=sonarcloud-core" >> $GITHUB_ENV
50+
echo "BUILD_GRADLE_FILE=private/edition-sonarcloud/build.gradle" >> $GITHUB_ENV
51+
else
52+
echo "::error::Invalid ticket format. Must start with SONAR- or SC-."
53+
exit 1
54+
fi
55+
56+
- name: Checkout target repository
57+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
58+
with:
59+
repository: SonarSource/${{ env.PRODUCT_REPOSITORY }}
60+
ref: ${{ inputs.base_branch }}
61+
sparse-checkout: ${{ env.BUILD_GRADLE_FILE }}
62+
sparse-checkout-cone-mode: false
63+
fetch-depth: 0
64+
token: ${{ inputs.github-token }}
65+
66+
- name: Update analyzer version in build file
67+
shell: bash
68+
run: |
69+
set -euo pipefail
70+
echo "Updating analyzer version in ${{ env.BUILD_GRADLE_FILE }} for plugin ${{ inputs.plugin-language }}"
71+
72+
sed -i "s/\(:sonar-${{ inputs.plugin-language }}.*-plugin:\)[0-9.]*/\1${{ inputs.version }}/g" ${{ env.BUILD_GRADLE_FILE }}
73+
74+
echo "Showing diff:"
75+
git --no-pager diff ${{ env.BUILD_GRADLE_FILE }}
76+
77+
- name: Create Pull Request
78+
id: create_pr
79+
uses: peter-evans/create-pull-request@c5a7806660adbe173f04e3e038b0ccdcd758773c # v6.1.0
80+
with:
81+
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
82+
commit-message: '${{ inputs.ticket }} Update `${{ inputs.plugin-language }}` plugins to version ${{ inputs.version }}'
83+
title: '${{ inputs.ticket }} Update `${{ inputs.plugin-language }}` to version ${{ inputs.version }}'
84+
body: ${{ inputs.pr_body }}
85+
base: ${{ inputs.base_branch }}
86+
branch: '${{ inputs.plugin-language }}/update-analyzer-${{ inputs.version }}'
87+
token: ${{ inputs.github-token }}
88+
draft: ${{ inputs.draft }}
89+
reviewers: ${{ inputs.reviewers }}

0 commit comments

Comments
 (0)