Skip to content

Commit bcab9c0

Browse files
authored
Support Dependency Review Action (#116)
1 parent 3db4a67 commit bcab9c0

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

.github/actions/setup-runtime-env/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ runs:
1818
- name: "Cache Deps / Build"
1919
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
2020
with:
21-
path: >-
21+
path: |-
2222
deps
2323
_build
2424
key: "${{ format('{0}-{1}-{2}', inputs.mix-env, hashFiles('.tool-versions'), hashFiles('mix.exs')) }}"

.github/workflows/part_report_deps.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ permissions:
88

99
jobs:
1010
binary:
11-
1211
permissions:
1312
contents: write
1413

.github/workflows/pr.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ jobs:
5656
dependency-review:
5757
name: "Dependency Review"
5858

59+
needs: ['report_deps']
60+
5961
runs-on: ubuntu-latest
6062

6163
steps:

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ jobs:
5454
- uses: erlef/mix-dependency-submission@v1
5555
```
5656
57+
### Example Using `actions/dependency-review-action`
58+
59+
```yaml
60+
name: "Mix Dependency Submission"
61+
62+
on:
63+
push:
64+
branches:
65+
- "main"
66+
pull_request: {}
67+
68+
# The API requires write permission on the repository to submit dependencies
69+
permissions:
70+
contents: write
71+
72+
jobs:
73+
report_mix_deps:
74+
name: "Report Mix Dependencies"
75+
runs-on: ubuntu-latest
76+
steps:
77+
- uses: actions/checkout@v4
78+
- uses: erlef/mix-dependency-submission@v1
79+
- uses: actions/dependency-review-action@v4
80+
if: "${{ github.event_name == 'pull_request' }}"
81+
```
82+
5783
## Inputs
5884

5985
| Name | Description | Default |

lib/mix_dependency_submission/cli.ex

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defmodule MixDependencySubmission.CLI do
8181
help: "GitHub Actions Workflow Name"
8282
),
8383
sha:
84-
optimus_options_with_env_default("GITHUB_SHA",
84+
sha_option(
8585
value_name: "SHA",
8686
long: "--sha",
8787
help: "Current Git SHA"
@@ -140,4 +140,41 @@ defmodule MixDependencySubmission.CLI do
140140
:error -> [required: true]
141141
end ++ details
142142
end
143+
144+
@spec sha_option(Keyword.t()) :: Keyword.t()
145+
defp sha_option(base_opts) do
146+
# If the GitHub event is a pull request, we need to use the head SHA of the PR
147+
# instead of the commit SHA of the workflow run.
148+
# This is because the workflow run is triggered by the base commit of the PR,
149+
# and we want to report the dependencies of the head commit.
150+
# See: https://github.com/github/dependency-submission-toolkit/blob/72f5e31325b5e1bcc91f1b12eb7abe68e75b2105/src/snapshot.ts#L36-L61
151+
case load_pr_head_sha() do
152+
{:ok, sha} ->
153+
Keyword.put(base_opts, :default, sha)
154+
155+
:error ->
156+
# If we can't load the PR head SHA, we fall back to the default behavior
157+
# of using the GITHUB_SHA environment variable.
158+
optimus_options_with_env_default("GITHUB_SHA", base_opts)
159+
end
160+
end
161+
162+
# Note that pull_request_target is omitted here.
163+
# That event runs in the context of the base commit of the PR,
164+
# so the snapshot should not be associated with the head commit.
165+
166+
@pr_events ~w[pull_request pull_request_comment pull_request_review pull_request_review_comment]
167+
168+
@spec load_pr_head_sha :: {:ok, <<_::320>>} | :error
169+
defp load_pr_head_sha do
170+
with {:ok, event} when event in @pr_events <- System.fetch_env("GITHUB_EVENT_NAME"),
171+
{:ok, event_path} <- System.fetch_env("GITHUB_EVENT_PATH") do
172+
event_details_json = File.read!(event_path)
173+
174+
%{"pull_request" => %{"head" => %{"sha" => <<_binary::320>> = sha}}} =
175+
JSON.decode!(event_details_json)
176+
177+
{:ok, sha}
178+
end
179+
end
143180
end

0 commit comments

Comments
 (0)