Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into 29-devops-add-supe…
Browse files Browse the repository at this point in the history
…rlinter
  • Loading branch information
fletort committed Nov 13, 2024
2 parents e2511d9 + f7d5435 commit 5fd7a2a
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 240 deletions.
24 changes: 18 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ jobs:
# See https://github.com/irongut/CodeCoverageSummary/issues/236
- name: Write Coverage to Job Summary
run: |
cat code-coverage-results.md > my-code-coverage-results.md
echo "\`\`\`" >> my-code-coverage-results.md
coverage report >> my-code-coverage-results.md
echo "\`\`\`" >> my-code-coverage-results.md
cat my-code-coverage-results.md >> $GITHUB_STEP_SUMMARY
{
cat code-coverage-results.md
echo "\`\`\`"
coverage report
echo "\`\`\`"
} >> "$GITHUB_STEP_SUMMARY"
- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
Expand All @@ -87,7 +88,7 @@ jobs:
REGEX="(https://[a-z0-9]+.testspace.com/spaces/[0-9]+/result_sets/[0-9]+)"
[[ $output =~ $REGEX ]]
result_set_link="${BASH_REMATCH[1]}"
echo "[TestSpace Test Result set]($result_set_link)" >> $GITHUB_STEP_SUMMARY
echo "[TestSpace Test Result set]($result_set_link)" >> "$GITHUB_STEP_SUMMARY"
- name: Publish Test Results in GitHub
uses: EnricoMi/publish-unit-test-result-action@v2
Expand Down Expand Up @@ -153,18 +154,29 @@ jobs:
echo "${{ env.FILE_KEY_1_VALUE }}" >> expected
echo "{{ exemple.FILE_KEY_2 }}" >> all_test.j2
echo "${{ env.FILE_KEY_2_VALUE }}" >> expected
echo "{{ url.URL_KEY_1 }}" >> all_test.j2
echo "from_url" >> expected
echo "{{ url.URL_KEY_2 }}" >> all_test.j2
echo "it_is_working" >> expected
echo "" >> all_test.j2
cat all_test.j2
cat expected
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_OUTPUT"
id: extract_branch

- name: Test Local Action
id: test-action
uses: ./
with:
#keep_template: true
data_file: test-data.yml
data_format: yaml
data_url: https://raw.githubusercontent.com/${{ github.repository }}/refs/heads/${{ steps.extract_branch.outputs.branch }}/test/url_data.yml
data_url_format: yaml
variables: |
TEST1=${{ env.VAR_TEST1_VALUE }}
TEST2=${{ env.VAR_TEST2_VALUE }}
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ with the jinja2 library.
The new filename is the same filename without the j2 extension.
For exemple, `README.md.j2` becomes `README.md`.

It this version, it can only be used with environment variable, see my [test template file](./test/template.j2).
It this version, it can only be used with environment variable,
see my [test template file](./test/template.j2).

Environement variable as used as with the jinja2 cli, with a _kind_ of `environ` method :
Environement variable as used as with the jinja2 cli,
with a _kind_ of `environ` method :

```file
{{ environ('TEST') }}
Expand Down Expand Up @@ -81,7 +83,7 @@ SMART=yoyo
MY_VAR=True
```

#### 3. yaml
#### 3. YAML

```file
---
Expand Down Expand Up @@ -117,7 +119,7 @@ For previous ENV example, jinja template will be:
{{ EXEMPLE_MY_VAR }}
```

### Using Workflow Github contextual Information
### Using Workflow GitHub contextual Information

Some of the [contextual information about workflow runs](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs)
are available inside your jinja Template:
Expand All @@ -140,7 +142,9 @@ Property dereference syntax: `github.sha`
{{ matrix.your_matrix_variable_name }}
```

Note: All strategy information key contains dashes that must me marked as underscore in jinja expression: `${{ strategy.job-index }}` becomes `{{ strategy.job_index }}`.
Note: All strategy information key contains dashes that must me marked as
underscore in jinja expression: `${{ strategy.job-index }}` becomes
`{{ strategy.job_index }}`.

### Actions inputs

Expand All @@ -150,7 +154,9 @@ Note: All strategy information key contains dashes that must me marked as unders
| `variables` | Variable to substitute in the jinja templates. Must be Key, value pairs in .env file format (key=value). | "" |
| `keep_template` | Put to `true` to keep original template file. | `false` |
| `data_file` | Source file contening inputs variable for the jinja template. | "" |
| `data_format` | Format of the `data_file`. Can be `env`, `ini`, `yaml`, `json` or `automatic` (for automatic detection). The automatic detction is based on the extension then on the content. | `automatic` |
| `data_format` | Format of the `data_file`. Can be `env`, `ini`, `yaml`, `json` or `automatic` (for automatic detection). The automatic detection is based on the extension then on the content. | `automatic` |
| `data_url` | URL Link contening inputs variable for the jinja template. | "" |
| `data_url_format` | Format of the `data_url`. Can be `env`, `ini`, `yaml`, `json` or `automatic` (for automatic detection). The automatic detection is based on the http header content-type then on the content itself. | `automatic` |
<!-- prettier-ignore-end -->

## Code Quality
Expand Down
20 changes: 19 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ inputs:
description:
'Format of the `data_file`. Can be `env`, `ini`, `yaml`, `json` or `automatic` (for automatic detection). The automatic detction is based on the extension then on the content.'
default: automatic
data_url:
description:
'Link to a file contening inputs variable for the jinja template.'
default: ''
data_url_format:
description:
'Format of the `url_file`. Can be `env`, `ini`, `yaml`, `json` or `automatic` (for automatic detection). The automatic detction is based on the content-type http header then on the content.'
default: automatic
runs:
using: 'composite'
steps:
Expand Down Expand Up @@ -53,7 +61,17 @@ runs:
data_format="--data_format=${{inputs.data_format}}"
fi
fi
python3 ${{github.action_path}}/entrypoint.py ${keep_template} ${data_file} ${data_format} \
data_url=""
data_url_format=""
if [[ ! -z "${{inputs.data_url}}" ]];then
data_url="--data_url=${{inputs.data_url}}"
if [[ "${{inputs.data_url_format}}" != "automatic" ]]; then
data_url_format="--data_url_format=${{inputs.data_url_format}}"
fi
fi
python3 ${{github.action_path}}/entrypoint.py ${keep_template} \
${data_file} ${data_format} \
${data_url} ${data_url_format} \
--var_file ${{ runner.temp }}/build_logs/inputs_variables.env \
--context ${{ runner.temp }}/build_logs/github.json \
--context ${{ runner.temp }}/build_logs/job.json \
Expand Down
45 changes: 25 additions & 20 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,44 @@
import os
import json
from jinja2 import Environment, FileSystemLoader
from .parser import Parser
from .parser import FileParser, UrlParser

class Main:
"""Main class of the jinja2-template-action"""

def __init__(self, extensions=('.j2'), basepath='./', keep_template=False):
def __init__(self, extensions=(".j2"), basepath="./", keep_template=False):
self.ext = extensions
self.basepath = basepath
self.keep_template = keep_template
self.env = Environment(
loader=FileSystemLoader(self.basepath)
)
self.env = Environment(loader=FileSystemLoader(self.basepath))
self.data = {}
# Keep the environ method in template as whe have in the
#jinja2 cli in the first version of this action
# jinja2 cli in the first version of this action
self.env.globals["environ"] = os.environ.get
# Also add env variable in a classic way env.VAR_NAME
self.data["env"] = dict(os.environ)

def add_variables(self, variables):
'''
"""
Add Variables in the jinja2 context
Parameters:
variables (str): Envrionement Variable list, one declaration by line
with a = between the key and the value
'''
"""
for variable in variables.split("\n"):
clean_variable = bytes(variable.strip(), "utf-8").decode("unicode_escape")
if clean_variable != "":
name, value = clean_variable.split("=", 1)
self.data.update({name: value})

def add_json_section(self, section_name, json_content):
'''
"""
Add Json Data in a given section to the Data available to the template engine
Parameters:
sectionName (str): Name of the added section. The jsconContent will be encapsuled
in this key.
jsonContent (str/dict): Json content added in the key defined by sectionName
'''
"""
if isinstance(json_content, str):
data = json.loads(json_content)
elif isinstance(json_content, dict):
Expand All @@ -52,35 +50,42 @@ def add_json_section(self, section_name, json_content):

# protect again key contening dashes (it is the case in the keys of strategy
# context for example)
problematic_keys = [key for key in data.keys() if '-' in key]
problematic_keys = [key for key in data.keys() if "-" in key]
for problematic_key in problematic_keys:
new_key = problematic_key.replace("-", "_")
data[new_key] = data.pop(problematic_key)

self.data[section_name] = data

def add_data_file(self, file_path, file_format=None):
'''
"""
Add Variable from a file to jinja2 context.
'''
parser = Parser(file_path, file_format)
"""
parser = FileParser(file_path, file_format)
content = parser.parse()
self.data.update(content)

def add_data_url(self, url, data_format=None):
"""
Add Variable from a url to jinja2 context.
"""
parser = UrlParser(url, data_format)
content = parser.parse()
self.data.update(content)

def render_file(self, file_path):
'''
"""
Render One File with saved jinja2 context.
'''
with open(f"{file_path}".rsplit(".", 1)[0], 'w', encoding="utf-8") as out:
"""
with open(f"{file_path}".rsplit(".", 1)[0], "w", encoding="utf-8") as out:
out.write(self.env.get_template( f"{file_path}").render(self.data))
out.flush()
if not self.keep_template:
os.remove(f"{file_path}")

def render_all(self):
'''
"""
Render All File with saved jinja2 context.
'''
"""
for path, _, files in os.walk(self.basepath):
for name in files:
if name.endswith(self.ext):
Expand Down
Loading

0 comments on commit 5fd7a2a

Please sign in to comment.