Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 32ce527

Browse files
authored
Add API token (#10)
* Specify API token and force repository * add default api token key * update definition of api keys * more debug * more check on entrypoint * checking the forcing branch mechanism * update force branch to force ref * update variables
1 parent 33e144c commit 32ce527

11 files changed

+54
-24
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,33 @@ The action lets you customize the number of metrics based on
1919

2020
## How to use it?
2121

22-
### Step 1: Get your Code Inspector API keys
22+
### Step 1: Get your Code Inspector API Token
2323

2424
Sign up on [Code Inspector](https://www.code-inspector.com).
2525

26-
In your profile, generate API keys.
26+
In the top right menu go to API token
2727

28-
![Generate API keys](images/ci-generate-api-keys.png)
28+
![Generate API Token](images/api-token-creation.gif)
2929

3030

31-
Once you click on the button, the following window will appear.
31+
Create a token and save it.
3232

33-
![Example API keys](images/ci-api-keys.png)
3433

34+
### Step 2: Configure your Code Inspector API Token in your GitHub repository
3535

36-
### Step 2: Configure your Code Inspector API keys in your GitHub repository
37-
38-
You need to add your Code Inspector API keys into GitHub.
36+
You need to add your Code Inspector API Token into GitHub.
3937

4038
On GitHub, go in your repository settings, click on the secret *Secrets* (on the right) and create a new secret.
4139

42-
Create a secret called `CODE_INSPECTOR_ACCESS_KEY` and set it to the value of the access key generated at the previous step.
43-
44-
![Example API keys](images/github-add-access-key.png)
40+
Create a secret called `CODE_INSPECTOR_API_TOKEN` and set it to the value of the API token generated at the previous step.
4541

42+
![Create API token on GitHub](images/github-add-api-token.png)
4643

47-
Create another secret called `CODE_INSPECTOR_SECRET_KEY` and set it to the value of the secret key generated at the previous step.
4844

49-
![Example API keys](images/github-add-secret-key.png)
45+
Once the secret is created, the secrets page will look as follow.
5046

51-
Once all secrets have been created, we should have the following secrets generated.
5247

53-
![Example API keys](images/github-keys-generated.png)
48+
![Example API token on GitHub](images/github-action-secrets.png)
5449

5550

5651
### Step 3: Configure the GitHub action
@@ -71,8 +66,8 @@ jobs:
7166
uses: codeinspectorio/github-action@master
7267
with:
7368
repo_token: ${{ secrets.GITHUB_TOKEN }}
74-
code_inspector_access_key: ${{ secrets.CODE_INSPECTOR_ACCESS_KEY }}
75-
code_inspector_secret_key: ${{ secrets.CODE_INSPECTOR_SECRET_KEY }}
69+
code_inspector_api_token: ${{ secrets.CODE_INSPECTOR_API_TOKEN }}
70+
force_ref: 'none'
7671
min_quality_grade: 'WARNING'
7772
min_quality_score: '50'
7873
max_defects_rate: '0.0001'
@@ -85,9 +80,10 @@ jobs:
8580
The following parameters should *NOT* be changed:
8681
8782
* **repo_token**: this is how Code Inspector can access your repository
88-
* **code_inspector_access_key** and **code_inspector_access_key**: this is how the action can communicate with the Code Inspector analysis engine.
83+
* **code_inspector_api_token**: this is how the action can communicate with the Code Inspector analysis engine.
8984
9085
The following parameters can be changed:
86+
* **force_branch** is used to force the branch being checked. Use 'none' if you want to analyze the current branch.
9187
* **min_quality_grade**: the minimum grade your project should have. Valid values are: `EXCELLENT`, `GOOD`, `NEUTRAL`, `WARNING`, `CRITICAL`
9288
* **min_quality_score**: the minimum code quality score your project should have. This is a value between `0` and `100`.
9389
* **max_defects_rate**: the number of defects per line of code. For example, the value `0.001` means 1 defect per 1000 lines of codes.

action.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ inputs:
55
description: 'Token to checkout the repository.'
66
required: true
77
code_inspector_access_key:
8-
description: 'Access key on Code Inspector'
8+
description: 'Access key on Code Inspector. Set to none if you use the API Token.'
99
required: true
10+
default: ''
1011
code_inspector_secret_key:
11-
description: 'Secret key on Code Inspector'
12+
description: 'Secret key on Code Inspector. Set to none if you use the API Token.'
13+
required: true
14+
default: ''
15+
code_inspector_api_token:
16+
description: 'API Token to access Code Inspector. Set to none if you use access and secret keys'
1217
required: true
18+
default: ''
1319
min_quality_grade:
1420
description: 'Minimum grade. Valid options are EXCELLENT, GOOD, NEUTRAL, WARNING, CRITICAL. Leave blank to ignore.'
1521
required: true
@@ -38,6 +44,10 @@ inputs:
3844
description: 'Timeout before giving up (in case analysis takes too long). Default is 10 minutes'
3945
required: true
4046
default: '600'
47+
force_ref:
48+
description: 'Force the GitHub action to analyze a given ref. Leave to none if you want to analyze branch being pushed (default)'
49+
required: true
50+
default: 'none'
4151
branding:
4252
icon: 'search'
4353
color: 'blue'
@@ -52,3 +62,4 @@ runs:
5262
- ${{ inputs.max_long_functions_rate }}
5363
- ${{ inputs.project_name }}
5464
- ${{ inputs.max_timeout_sec }}
65+
- ${{ inputs.force_ref }}

entrypoint.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ MAX_COMPLEX_FUNCTIONS_RATE=$4
99
MAX_LONG_FUNCTIONS_RATE=$5
1010
PROJECT_NAME=$6
1111
MAX_TIMEOUT_SEC=$7
12+
FORCE_REF=$8
1213

1314

1415
echo "Code Inspector GitHub action"
@@ -21,16 +22,38 @@ echo "MAX_COMPLEX_FUNCTIONS_RATE: ${MAX_COMPLEX_FUNCTIONS_RATE}"
2122
echo "MAX_LONG_FUNCTIONS_RATE: ${MAX_LONG_FUNCTIONS_RATE}"
2223
echo "PROJECT_NAME: ${PROJECT_NAME}"
2324
echo "MAX_TIMEOUT_SEC: ${MAX_TIMEOUT_SEC}"
25+
echo "FORCE_REF: ${FORCE_REF}"
2426

25-
export CODE_INSPECTOR_ACCESS_KEY=${INPUT_CODE_INSPECTOR_ACCESS_KEY}
26-
export CODE_INSPECTOR_SECRET_KEY=${INPUT_CODE_INSPECTOR_SECRET_KEY}
27+
28+
if [ "$INPUT_CODE_INSPECTOR_API_TOKEN" != "" ]; then
29+
echo "Authentication using API token"
30+
export CODE_INSPECTOR_API_TOKEN=${INPUT_CODE_INSPECTOR_API_TOKEN}
31+
fi
32+
33+
if [ "$INPUT_CODE_INSPECTOR_ACCESS_KEY" != "" ] && [ "$INPUT_CODE_INSPECTOR_SECRET_KEY" != "" ]; then
34+
echo "Authentication using access/secret keys"
35+
export CODE_INSPECTOR_ACCESS_KEY=${INPUT_CODE_INSPECTOR_ACCESS_KEY}
36+
export CODE_INSPECTOR_SECRET_KEY=${INPUT_CODE_INSPECTOR_SECRET_KEY}
37+
fi
38+
39+
40+
# By default, use the GitHub ref
41+
REF_TO_CHECK=${GITHUB_REF}
42+
SHA_TO_CHECK=${GITHUB_SHA}
43+
44+
# If the branch is forced, we do not specify a SHA and force the branch
45+
if [ "$FORCE_REF" != "" ] && [ "$FORCE_REF" != "none" ]; then
46+
echo "Forcing ref to ${FORCE_REF}"
47+
REF_TO_CHECK=${FORCE_REF}
48+
SHA_TO_CHECK="none"
49+
fi
2750

2851
${CODE_INSPECTOR_BIN} \
2952
--token "${INPUT_REPO_TOKEN}" \
3053
--actor "${GITHUB_ACTOR}" \
3154
--repository "${GITHUB_REPOSITORY}" \
32-
--sha "${GITHUB_SHA}" \
33-
--ref "${GITHUB_REF}" \
55+
--sha "${SHA_TO_CHECK}" \
56+
--ref "${REF_TO_CHECK}" \
3457
--project "${PROJECT_NAME}" \
3558
--min-quality-score "${MIN_QUALITY_SCORE}" \
3659
--min-quality-grade "${MIN_QUALITY_GRADE}" \

images/api-token-creation.gif

1.53 MB
Loading

images/ci-api-keys.png

-28.7 KB
Binary file not shown.

images/ci-generate-api-keys.png

-26.7 KB
Binary file not shown.

images/github-action-secrets.png

43.8 KB
Loading

images/github-add-access-key.png

-84.3 KB
Binary file not shown.

images/github-add-api-token.png

106 KB
Loading

images/github-add-secret-key.png

-82.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)