Skip to content

Commit cff4b1d

Browse files
committed
init
0 parents  commit cff4b1d

File tree

7 files changed

+146
-0
lines changed

7 files changed

+146
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv/

Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.10-slim-buster
2+
3+
COPY . /
4+
5+
RUN pip install -r requirements.txt
6+
7+
# Code file to execute when the docker container starts up (`entrypoint.sh`)
8+
ENTRYPOINT ["/entrypoint.sh"]

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# set-codeql-languages
2+
3+
This action reads the languages API for your repository and sets the CodeQL supported languages as the job matrix for your Actions run.
4+
5+
## Why use this action?
6+
7+
The default Actions workflow for CodeQL auto-populates the job matrix with your repo's supported CodeQL languages. However, as new code is added to a repository, that language matrix is not updated. You need to manually add those languages to the matrix definition to have CodeQL scan them.
8+
9+
This action reads the repository languages API and adds all supported languages to the job matrix. No additional configuration is required.
10+
11+
## How to use this action
12+
13+
Call this action before defining the CodeQL analyze job strategy, then set the matrix to the output from the action: `${{ fromJSON(needs.create-matrix.outputs.matrix) }}`
14+
15+
**Example**
16+
```
17+
name: "CodeQL"
18+
19+
on: workflow_dispatch
20+
21+
jobs:
22+
create-matrix:
23+
name: Set CodeQL Languages
24+
runs-on: ubuntu-latest
25+
outputs:
26+
matrix: ${{ steps.set-matrix.outputs.languages }}
27+
steps:
28+
- name: Get languages from repo
29+
id: get-languages
30+
uses: leftrightleft/set-codeql-languages@main
31+
with:
32+
access-token: ${{ secrets.GITHUB_TOKEN }}
33+
endpoint: ${{ github.event.repository.languages_url }}
34+
35+
analyze:
36+
needs: create-matrix
37+
name: Analyze
38+
runs-on: ubuntu-latest
39+
permissions:
40+
actions: read
41+
contents: read
42+
security-events: write
43+
44+
strategy:
45+
fail-fast: false
46+
matrix:
47+
language: ${{ fromJSON(needs.create-matrix.outputs.matrix) }} # Set output from create-matrix job
48+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
49+
50+
steps:
51+
- name: Checkout repository
52+
uses: actions/checkout@v2
53+
54+
- name: Initialize CodeQL
55+
uses: github/codeql-action/init@v2
56+
with:
57+
languages: ${{ matrix.language }}
58+
59+
- name: Autobuild
60+
uses: github/codeql-action/autobuild@v1
61+
62+
- name: Perform CodeQL Analysis
63+
uses: github/codeql-action/analyze@v2

action.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# action.yml
2+
name: 'Set CodeQL Languages'
3+
description: 'Auto-populate the Actions matrix definition to include languages for CodeQL'
4+
inputs:
5+
access-token:
6+
description: 'github token'
7+
required: true
8+
endpoint:
9+
description: 'languages API endpoint'
10+
required: true
11+
outputs:
12+
languages:
13+
description: 'List of languages that will set the job matrix'
14+
runs:
15+
using: 'docker'
16+
image: 'Dockerfile'
17+
args:
18+
- ${{ inputs.access-token }}
19+
- ${{ inputs.endpoint }}
20+
- ${{ inputs.codeql-languages }}
21+

entrypoint.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh -l
2+
3+
# kick off the command
4+
python /main.py $1 $2 $3

main.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
import requests
3+
import json
4+
import sys
5+
6+
token = sys.argv[1]
7+
endpoint = sys.argv[2]
8+
codeql_languages = ["cpp", "csharp", "go", "java", "javascript", "python", "ruby"]
9+
10+
11+
# Connect to the languages API and return languages
12+
def get_languages():
13+
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/vnd.github.v3+json'}
14+
response = requests.get(endpoint, headers=headers)
15+
return response.json()
16+
17+
# Find the intersection of the languages returned by the API and the languages supported by CodeQL
18+
def build_languages_list(languages):
19+
languages = [language.lower() for language in languages.keys()]
20+
for i in range(len(languages)):
21+
if languages[i] == "c#":
22+
languages[i] = ("csharp")
23+
if languages[i] == "c++":
24+
languages[i] = ("cpp")
25+
26+
intersection = list(set(languages) & set(codeql_languages))
27+
return intersection
28+
29+
# Set the output of the action
30+
def set_action_output(output_name, value) :
31+
if "GITHUB_OUTPUT" in os.environ :
32+
with open(os.environ["GITHUB_OUTPUT"], "a") as f :
33+
print("{0}={1}".format(output_name, value), file=f)
34+
print("{0}={1}".format(output_name, value))
35+
36+
def main():
37+
languages = get_languages()
38+
output = build_languages_list(languages)
39+
set_action_output("languages", json.dumps(output))
40+
41+
if __name__ == '__main__':
42+
main()
43+
44+

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
certifi==2022.9.24
2+
charset-normalizer==2.1.1
3+
idna==3.4
4+
requests==2.28.1
5+
urllib3==1.26.12

0 commit comments

Comments
 (0)