Skip to content

Commit 6e4ad7d

Browse files
authored
feat: add support for Python poetry.lock files (#9)
* feat: add support for Python poetry.lock files * style: format Python files with black and isort * style: adhere to yamllint and format action.yml * style: adhere to markdownlint and format README * docs: update README for 1.4.1 release * refactor: update action.yml dependencies and format * build: Ensure each dependent action is pinned to the latest major version tag * style: correct for basic Python linting issues * style: format and cleanup analyze.py * style: provide descriptive step names in action.yml
1 parent a9b3399 commit 6e4ad7d

File tree

5 files changed

+331
-271
lines changed

5 files changed

+331
-271
lines changed

README.md

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
# phylum-analyze-pr-action
22
A GitHub Action to automatically analyze Pull Requests for changes to package manager lockfiles using Phylum.
33

4-
Phylum provides a complete risk analyis of "open-source packages" (read: untrusted software from random Internet strangers). Phylum evolved forward from legacy SCA tools to defend from supply-chain malware, malicious open-source authors, and engineering risk, in addtion to software vulnerabilities and license risks. To learn more, please see [our website](https://phylum.io)
4+
Phylum provides a complete risk analyis of "open-source packages" (read: untrusted software from random Internet
5+
strangers). Phylum evolved forward from legacy SCA tools to defend from supply-chain malware, malicious open-source
6+
authors, and engineering risk, in addtion to software vulnerabilities and license risks. To learn more, please see
7+
[our website](https://phylum.io)
58

6-
This action enables users to configure thresholds for each of Phylum's five risk domain scores. If a package risk domain score is below the threshold, the action will fail the check on the pull request. When packages fail the risk analysis, a comment is created on the PR to summarize the issues.
9+
This action enables users to configure thresholds for each of Phylum's five risk domain scores. If a package risk
10+
domain score is below the threshold, the action will fail the check on the pull request. When packages fail the risk
11+
analysis, a comment is created on the PR to summarize the issues.
712

813
## Features
914
- configurable risk domain thresholds
10-
- uses [peter-evans/create-or-update-comment](https://github.com/marketplace/actions/create-or-update-comment) to add comments to PRs
15+
- uses [peter-evans/create-or-update-comment](https://github.com/marketplace/actions/create-or-update-comment)
16+
to add comments to PRs
1117

1218
## Getting Started
13-
1. Create a workflow in a repository that uses the workflow definition listed below as an example.
14-
2. Be sure to include the base branches you use for development, the defaults are set to `master` and `main`.
15-
3. Define risk domain thresholds using `vul_threshold`, `mal_threshold`, etc to define a score requirement. A Phylum project score requirement of 60 is defined as `0.6`, for example.
19+
1. Create a workflow in a repository that uses the workflow definition listed below as an example
20+
2. Be sure to include the base/default branches used for development, where the defaults are set to `master` and `main`
21+
3. Define risk domain thresholds using `vul_threshold`, `mal_threshold`, etc. to define a score requirement
22+
1. For example, a Phylum project score requirement of 60 is defined as `0.6`
23+
4. Additional inputs can be used - see [action.yml](action.yml) for full list
1624

1725
```yaml
1826
on:
@@ -26,9 +34,9 @@ jobs:
2634
runs-on: ubuntu-latest
2735
name: A job to analyze PR with phylum
2836
steps:
29-
- uses: actions/checkout@v2
37+
- uses: actions/checkout@v3
3038
- id: analyze-pr-test
31-
uses: phylum-dev/phylum-analyze-pr-action@v1.4
39+
uses: phylum-dev/phylum-analyze-pr-action@v1
3240
with:
3341
vul_threshold: 0.6
3442
mal_threshold: 0.6
@@ -39,34 +47,39 @@ jobs:
3947
```
4048
4149
### Supported lockfiles
42-
- requirements.txt (Python PyPI)
43-
- package-lock.json (JavaScript/TypeScript NPM)
44-
- yarn.lock (JavaScript/TypeScript NPM)
45-
- Gemfile.lock (Ruby Rubygems/Bundler)
50+
- `requirements.txt` (Python PyPI)
51+
- `poetry.lock` (Python PyPI)
52+
- `package-lock.json` (JavaScript/TypeScript NPM)
53+
- `yarn.lock` (JavaScript/TypeScript NPM)
54+
- `Gemfile.lock` (Ruby Rubygems/Bundler)
4655

4756
### Requirements
4857
- active Phylum account ([Register here](https://app.phylum.io/auth/registration))
49-
- GitHub repository secret defined: PHYLUM_TOKEN (extracted from Phylum CLI configuration file "offline_access")
50-
1. Ensure you've updated the Phylum CLI on a local installation to a version >= `1.2.0`
51-
2. Successfully authenticate using Phylum CLI. This will ensure the token is populated in the phylum config file `~/.phylum/settings.yaml` in stanza `offline_access`
52-
3. Copy the token value in the `offline_access` stanza
53-
4. Create a new GitHub secret in the desired repository. This can be done through the GitHub web UI or using the gh command line tool: `gh secret set PHYLUM_TOKEN -b <token_value>`
54-
5. Optionally, you can remove the vestigial `PHYLUM_USER` and `PHYLUM_PASS` GitHub secrets as they are no longer used.
55-
- concrete package versions (only applicable for requirements.txt)
58+
- GitHub repository secret defined: `PHYLUM_TOKEN`
59+
1. Ensure you've updated the Phylum CLI on a local installation to a version >= `2.0.1`
60+
2. Successfully authenticate using Phylum CLI to ensure the token is populated and correct
61+
3. Copy the token value from the output of the `phylum auth token` command
62+
4. Create a new GitHub secret named `PHYLUM_TOKEN` in the desired repository, through the GitHub web UI or using the gh command line tool: `gh secret set PHYLUM_TOKEN -b <token_value>`
63+
- concrete package versions (only applicable for `requirements.txt`)
5664
- existing Phylum project for repository (`.phylum_project` must be present)
5765

5866
### Known Issues
59-
~~1. Incomplete packages: if Phylum hasn't yet analyzed a package requested by this action, the action will fail with an exit code of 5. This is momentarily preferable than waiting.~~
67+
- [Issue tracker](https://github.com/phylum-dev/phylum-analyze-pr-action/issues)
68+
- [Open bugs](https://github.com/phylum-dev/phylum-analyze-pr-action/labels/%F0%9F%95%B7%EF%B8%8F%20bug)
6069

6170
### Incomplete Packages
62-
Sometimes, users will request risk analysis information for open-source packages Phylum has not yet processed. When this occurs, Phylum cannot reasonably provide risk scoring information until those packages have been processed.
71+
Sometimes, users will request risk analysis information for open-source packages Phylum has not yet processed.
72+
When this occurs, Phylum cannot reasonably provide risk scoring information until those packages have been processed.
6373

64-
New in `v1.4`, `phylum-analyze-pr-action` will:
74+
Starting with `v1.4.0`, `phylum-analyze-pr-action` will:
6575
1. Detect the case of incomplete packages
66-
2. Return an exit code of 0 (a "passing" mark in GitHub Action parlance). This is to avoid failing a check in the PR with incomplete information.
67-
3. Add a comment to the PR indicating that there were incomplete packages. The comment will advise users to wait 30m and re-run the check on the Pull Request. This will give Phylum sufficient time to download, process and analyze the incomplete packages.
68-
4. When the check is run a second time, another comment will be added to the Pull Request noting the result of the risk analysis operation.
76+
2. Return an exit code of 0 (a "passing" mark in GitHub Action parlance)
77+
1. This is to avoid failing a check in the PR with incomplete information
78+
3. Add a comment to the PR indicating that there were incomplete packages
79+
1. The comment will advise users to wait 30m and re-run the check on the Pull Request
80+
2. This will give Phylum sufficient time to download, process and analyze the incomplete packages
81+
4. When the check is run a second time, another comment will be added to the Pull Request noting the result of the
82+
risk analysis operation.
6983

7084
### Example comment
7185
![image](https://user-images.githubusercontent.com/132468/140830714-24acc278-0102-4613-b006-6032a62b6896.png)
72-

action.yml

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
# _
2-
# __ _ _ __ __ _| |_ _ _______
3-
# / _` | '_ \ / _` | | | | |_ / _ \
4-
#| (_| | | | | (_| | | |_| |/ / __/
5-
# \__,_|_| |_|\__,_|_|\__, /___\___|
6-
# |___/
1+
# _
2+
# __ _ _ __ __ _| |_ _ _______
3+
# / _` | '_ \ / _` | | | | |_ / _ \
4+
# | (_| | | | | (_| | | |_| |/ / __/
5+
# \__,_|_| |_|\__,_|_|\__, /___\___|
6+
# |___/
7+
---
78
name: 'Analyze PR'
89
description: 'Analyze Pull request'
910
inputs:
@@ -42,27 +43,27 @@ inputs:
4243
runs:
4344
using: "composite"
4445
steps:
45-
- id: phylum-test
46-
uses: phylum-dev/install-phylum-latest-action@v1.3
46+
- name: Install phylum CLI
47+
uses: phylum-dev/install-phylum-latest-action@v1
4748
with:
4849
phylum_token: ${{ inputs.phylum_token }}
4950
phylum_version: ${{ inputs.phylum_version }}
5051

51-
- name: Check for previous comment
52+
- name: Check for INCOMPLETE comment
5253
uses: peter-evans/find-comment@v1
5354
id: fc
5455
with:
5556
issue-number: ${{ github.event.pull_request.number }}
5657
body-includes: INCOMPLETE
5758

58-
- name: Store result of id=fc in environment
59+
- name: Record presence of Phylum INCOMPLETE comment
5960
shell: bash
6061
if: "contains(steps.fc.outputs.comment-body, 'Phylum')"
6162
run: |
6263
echo "storing PREVIOUS_INCOMPLETE"
6364
echo PREVIOUS_INCOMPLETE=1 >> $GITHUB_ENV
6465
65-
- name: Check for existing project
66+
- name: Check for existing .phylum_project
6667
shell: bash
6768
run: |
6869
result=$(find . -maxdepth 1 -iname ".phylum_project")
@@ -73,29 +74,31 @@ runs:
7374
exit 11
7475
fi
7576
76-
- name: Generate Phylum label
77+
- name: Generate PHYLUM_LABEL
7778
shell: bash
78-
run: |
79-
echo PHYLUM_LABEL="GHA-PR${{ github.event.number }}-${GITHUB_HEAD_REF}" >> $GITHUB_ENV
79+
run: echo PHYLUM_LABEL="GHA-PR${{ github.event.number }}-${GITHUB_HEAD_REF}" >> $GITHUB_ENV
8080

81-
- uses: actions/setup-python@v2
81+
- name: Setup Python
82+
uses: actions/setup-python@v3
8283
with:
8384
python-version: '3.x'
8485

85-
- name: install python dependencies
86+
- name: Install python dependencies
8687
shell: bash
8788
run: |
88-
pip install unidiff
89+
python -m pip install -U pip setuptools
90+
python -m pip install unidiff packaging
8991
90-
- name: run analyze.py pr_type
92+
- name: Determine the PR type
9193
shell: bash
9294
run: python $GITHUB_ACTION_PATH/analyze.py "pr_type" $GITHUB_REPOSITORY ${{ github.event.number }}
9395

94-
- name: cat prtype
96+
- name: Display the PR type
9597
shell: bash
9698
run: cat ~/prtype.txt
9799

98-
- id: get-prtype
100+
- name: Make PR type available for future steps
101+
id: get-prtype
99102
shell: bash
100103
run: |
101104
ret="$(cat ~/prtype.txt)"
@@ -104,42 +107,45 @@ runs:
104107
ret="${ret//$'\r'/'%0A'}"
105108
echo "::set-output name=prtype::$ret"
106109
107-
- id: should-proceed
110+
- name: Bail when no updates to analyze
111+
id: should-proceed
108112
shell: bash
109113
if: "contains(steps.get-prtype.outputs.prtype, 'NA')"
110114
run: |
111115
echo 'exiting with 0 since package dependency files were not modified'
112116
echo '0' > $HOME/returncode.txt
113117
114-
- name: Analyze project lockfile
118+
- name: Analyze project lockfile with phylum CLI
115119
shell: bash
116120
if: "!contains(steps.get-prtype.outputs.prtype, 'NA')"
117121
run: |
118122
export PATH="$HOME/.phylum:$PATH"
119123
pushd $GITHUB_WORKSPACE || exit 11
120-
phylum analyze -l $PHYLUM_LABEL ${{ steps.get-prtype.outputs.prtype }} --verbose --json > ~/phylum_analysis.json
121-
echo "[*] Analyzed ${{ steps.get-prtype.outputs.prtype }} under label ${PHYLUM_LABEL} and wrote results to ~/phylum_analysis.json"
124+
phylum analyze -l $PHYLUM_LABEL ${{ steps.get-prtype.outputs.prtype }} --verbose --json > ~/phylum_analysis.json
125+
echo "[*] Analyzed ${{ steps.get-prtype.outputs.prtype }} under label ${PHYLUM_LABEL} and wrote results to ~/phylum_analysis.json"
122126
popd
123127
124-
125-
# - name: tmate
126-
# uses: mxschmitt/action-tmate@v3
127-
128-
- name: invoke test matrix
128+
- name: Invoke test matrix
129129
shell: bash
130130
if: "contains(inputs.invoke_test_matrix, 'true')"
131-
run: |
132-
python $GITHUB_ACTION_PATH/test_matrix.py
131+
run: python $GITHUB_ACTION_PATH/test_matrix.py
133132

134-
- name: python script analyze.py
133+
- name: Compare added dependencies in PR to analysis results
135134
shell: bash
136135
if: "!contains(steps.get-prtype.outputs.prtype, 'NA')"
137-
run: python $GITHUB_ACTION_PATH/analyze.py "analyze" $GITHUB_REPOSITORY ${{ github.event.number }} ${{ inputs.vul_threshold }} ${{ inputs.mal_threshold }} ${{ inputs.eng_threshold }} ${{ inputs.lic_threshold }} ${{ inputs.aut_threshold }}
138-
139-
# - name: tmate
140-
# uses: mxschmitt/action-tmate@v3
141-
142-
- id: get-returncode
136+
run: >
137+
python $GITHUB_ACTION_PATH/analyze.py
138+
"analyze"
139+
$GITHUB_REPOSITORY
140+
${{ github.event.number }}
141+
${{ inputs.vul_threshold }}
142+
${{ inputs.mal_threshold }}
143+
${{ inputs.eng_threshold }}
144+
${{ inputs.lic_threshold }}
145+
${{ inputs.aut_threshold }}
146+
147+
- name: Get return code
148+
id: get-returncode
143149
shell: bash
144150
run: |
145151
ret="$(cat ~/returncode.txt)"
@@ -149,14 +155,15 @@ runs:
149155
echo "::set-output name=ret::$ret"
150156
151157
# This will catch SUCCESS cases
152-
- name: return 0 for success
158+
- name: Return 0 for success
153159
shell: bash
154160
if: "contains(steps.get-returncode.outputs.ret, '0')"
155161
run: |
156-
echo 'exiting with 0 for success'
162+
echo "exiting with 0 for success"
157163
exit 0
158164
159-
- id: get-comment-body
165+
- name: Get comment body
166+
id: get-comment-body
160167
# this will have to check for 1 or 5 AND if on the second run
161168
# if: "contains(steps.get-returncode.outputs.ret, '1')"
162169
if: "steps.get-returncode.outputs.ret > 0"
@@ -171,26 +178,25 @@ runs:
171178
- name: Set comment
172179
# This will have to check for 1 or 5
173180
# Could check for > 0 ?
174-
#if: "contains(steps.get-returncode.outputs.ret, '1')"
181+
# if: "contains(steps.get-returncode.outputs.ret, '1')"
175182
if: "steps.get-returncode.outputs.ret > 0"
176-
uses: peter-evans/create-or-update-comment@v1
183+
uses: peter-evans/create-or-update-comment@v2
177184
with:
178185
issue-number: ${{ github.event.pull_request.number }}
179186
body: ${{ steps.get-comment-body.outputs.body }}
180187

181188
# This will catch INCOMPLETE and COMPLETE_SUCCESS
182-
- name: handle ret values of 4 or 5
189+
- name: Handle return values of 4 or 5
183190
shell: bash
184191
if: "steps.get-returncode.outputs.ret >= 4"
185192
run: |
186193
echo 'exiting with 0 for success - ret = ${{ steps.get-returncode.outputs.ret }}'
187194
exit 0
188195
189196
# This will catch FAILURE and COMPLETE_FAILURE
190-
- name: return 1 for risk analysis failure
197+
- name: Handle risk analysis failures
191198
shell: bash
192199
if: "contains(steps.get-returncode.outputs.ret, '1')"
193200
run: |
194-
echo 'exiting with 1 for risk analysis failure'
201+
echo "exiting with 1 for risk analysis failure"
195202
exit 1
196-

0 commit comments

Comments
 (0)