|
1 |
| -<p align="center"> |
2 |
| - <a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a> |
3 |
| -</p> |
| 1 | +# Secret Scanning Alerts GitHub Action |
4 | 2 |
|
5 |
| -# Create a JavaScript Action using TypeScript |
| 3 | +This GitHub Action retrieves secret scanning alerts from GitHub and filters them based on a specified date range. It then saves the filtered alerts to files and outputs a summary of the new and resolved alerts. |
6 | 4 |
|
7 |
| -Use this template to bootstrap the creation of a TypeScript action.:rocket: |
| 5 | +You can use the ouput to create a GitHub issue or send a notification to Email, Slack, Teams, etc. |
8 | 6 |
|
9 |
| -This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance. |
10 | 7 |
|
11 |
| -If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action) |
| 8 | +## How does it work? |
12 | 9 |
|
13 |
| -## Create an action from this template |
| 10 | +The code is realitvely simple. The flow is defined in `src/main.ts` and is as follows: |
14 | 11 |
|
15 |
| -Click the `Use this Template` and provide the new repo details for your action |
| 12 | +1. Parse inputs using the `getInput()` function. |
| 13 | +2. Calculate the minimum date for the alert scan using the `calculateDateRange()` function. |
| 14 | +3. Retrieve secret scanning alerts for the specified scope by calling the GitHub Secret Scanning REST API. |
| 15 | +4. Filter the alerts based on the minimum date using the `filterAlerts()` function. |
| 16 | +5. Write the new alerts and resolved alerts to json files. |
| 17 | +6. Print the results as GitHub Actions summary. |
| 18 | +7. Set the `summary-markdown` output to make it available to other steps in the workflow. |
16 | 19 |
|
17 |
| -## Code in Main |
| 20 | +## Usage |
18 | 21 |
|
19 |
| -> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance. |
| 22 | +To use this action, add the following step to your workflow: |
20 | 23 |
|
21 |
| -Install the dependencies |
22 |
| -```bash |
23 |
| -$ npm install |
24 |
| -``` |
25 |
| - |
26 |
| -Build the typescript and package it for distribution |
27 |
| -```bash |
28 |
| -$ npm run build && npm run package |
29 |
| -``` |
30 |
| - |
31 |
| -Run the tests :heavy_check_mark: |
32 |
| -```bash |
33 |
| -$ npm test |
34 |
| - |
35 |
| - PASS ./index.test.js |
36 |
| - ✓ throws invalid number (3ms) |
37 |
| - ✓ wait 500 ms (504ms) |
38 |
| - ✓ test runs (95ms) |
39 |
| - |
40 |
| -... |
41 |
| -``` |
42 |
| - |
43 |
| -## Change action.yml |
44 |
| - |
45 |
| -The action.yml defines the inputs and output for your action. |
46 |
| - |
47 |
| -Update the action.yml with your name, description, inputs and outputs for your action. |
48 |
| - |
49 |
| -See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions) |
50 |
| - |
51 |
| -## Change the Code |
52 |
| - |
53 |
| -Most toolkit and CI/CD operations involve async operations so the action is run in an async function. |
54 |
| - |
55 |
| -```javascript |
56 |
| -import * as core from '@actions/core'; |
57 |
| -... |
58 |
| - |
59 |
| -async function run() { |
60 |
| - try { |
61 |
| - ... |
62 |
| - } |
63 |
| - catch (error) { |
64 |
| - core.setFailed(error.message); |
65 |
| - } |
66 |
| -} |
67 |
| - |
68 |
| -run() |
| 24 | +```yaml |
| 25 | +- name: Secret Scanning Alerts |
| 26 | + uses: advanced-security/secret-scanning-custom-notifications/@v1 |
| 27 | + with: |
| 28 | + frequency: 24 # hours |
| 29 | + scope: 'repository' |
| 30 | + repository: 'repo-name' |
| 31 | + new_alerts_filepath: 'new_alerts.json' |
| 32 | + closed_alerts_filepath: 'closed_alerts.json' |
69 | 33 | ```
|
70 | 34 |
|
71 |
| -See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages. |
| 35 | +## Inputs |
| 36 | +This action requires the following inputs: |
72 | 37 |
|
73 |
| -## Publish to a distribution branch |
| 38 | +- `frequency`: The frequency of the action. Valid values are daily, weekly, and monthly. Required. |
| 39 | +- `scope`: The scope of the action. Valid values are repo, org, and enterprise. Required. |
| 40 | +- `token`: The GitHub API token to use for the action. Required. |
| 41 | +- `new_alerts_filepath`: The path to the file where the new alerts should be stored for email attachment. Required. |
| 42 | +- `closed_alerts_filepath`: The path to the file where the closed alerts should be stored for email attachment. Required. |
| 43 | +- `api_url`: The GitHub API URL to use for the action. Needed only if you are using GitHub Enterprise Server. Optional. |
| 44 | +- `repo`: The repo to run the action on. Needed only if scope is repo and you intend to fetch alerts from other repos than the one where the action is running. Optional. |
| 45 | +- `org`: The org to run the action on. Needed only if scope is org and you intend to fetch alerts from other repos than the one where the action is running. Optional. |
| 46 | +- `enterprise`: The enterprise to run the action on. Required if you run it on the enterprise level. Optional. |
74 | 47 |
|
75 |
| -Actions are run from GitHub repos so we will checkin the packed dist folder. |
76 |
| - |
77 |
| -Then run [ncc](https://github.com/zeit/ncc) and push the results: |
78 |
| -```bash |
79 |
| -$ npm run package |
80 |
| -$ git add dist |
81 |
| -$ git commit -a -m "prod dependencies" |
82 |
| -$ git push origin releases/v1 |
83 |
| -``` |
84 | 48 |
|
85 |
| -Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project. |
| 49 | +## Outputs |
| 50 | +This action has the following outputs: |
86 | 51 |
|
87 |
| -Your action is now published! :rocket: |
| 52 | +- `summary-markdown`: A markdown formatted summary of the new and resolved alerts. |
88 | 53 |
|
89 |
| -See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) |
90 | 54 |
|
91 |
| -## Validate |
| 55 | +## Example Workflow |
92 | 56 |
|
93 |
| -You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml)) |
| 57 | +Run the action every 4 hours and upload the new and resolved alerts to an artifact if there are any new or resolved alerts. |
94 | 58 |
|
95 | 59 | ```yaml
|
96 |
| -uses: ./ |
97 |
| -with: |
98 |
| - milliseconds: 1000 |
| 60 | +name: Secret Scanning Alerts |
| 61 | +
|
| 62 | +on: |
| 63 | + schedule: |
| 64 | + - cron: '0 */6 * * 0' # Run every 4 hours |
| 65 | +jobs: |
| 66 | + secret-scanning-alerts: |
| 67 | + runs-on: ubuntu-latest |
| 68 | +
|
| 69 | + steps: |
| 70 | + - name: Secret Scanning Alerts |
| 71 | + uses: advanced-security/secret-scanning-custom-notifications/@v1 |
| 72 | + with: |
| 73 | + frequency: 3000 |
| 74 | + scope: 'repository' |
| 75 | + new_alerts_filepath: 'created_alerts.json' |
| 76 | + closed_alerts_filepath: 'closed_alerts.json' |
| 77 | + token: ${{ secrets.TOKEN }} |
| 78 | +
|
| 79 | + - name: Count the number of entries in the alert files |
| 80 | + id: count_alerts |
| 81 | + run: | |
| 82 | + created_alerts_count=$(jq '. | length' created_alerts.json) |
| 83 | + closed_alerts_count=$(jq '. | length' closed_alerts.json) |
| 84 | + echo "created_alerts_count=$created_alerts_count" >> $GITHUB_OUTPUT |
| 85 | + echo "closed_alerts_count=$closed_alerts_count" >> $GITHUB_OUTPUT |
| 86 | + |
| 87 | + - name: Upload artifact |
| 88 | + if: steps.count_alerts.outputs.created_alerts_count > 0 || steps.count_alerts.outputs.closed_alerts_count > 0 |
| 89 | + uses: actions/upload-artifact@v3 |
| 90 | + with: |
| 91 | + name: my-artifact |
| 92 | + path: | |
| 93 | + created_alerts.json |
| 94 | + closed_alerts.json |
99 | 95 | ```
|
100 | 96 |
|
101 |
| -See the [actions tab](https://github.com/actions/typescript-action/actions) for runs of this action! :rocket: |
102 | 97 |
|
103 |
| -## Usage: |
| 98 | + |
| 99 | +## License |
| 100 | +This project is distributed under the [MIT license](LICENSE.md). |
104 | 101 |
|
105 |
| -After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action |
| 102 | +## Contributing |
| 103 | +- Fork this repo |
| 104 | +- Work on your new feature |
| 105 | +- Create new Pull Request |
0 commit comments