Skip to content

Commit 294983c

Browse files
authored
Merge pull request #73 from newrelic-csec/refactor-check-env-fossa-cli
Refactor check env and fossa cli
2 parents 1023198 + a950541 commit 294983c

File tree

8 files changed

+141
-420
lines changed

8 files changed

+141
-420
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: 'FOSSA Composite Action'
2+
description: 'Shared action for running FOSSA workflows'
3+
runs:
4+
using: 'composite'
5+
steps:
6+
- id: fossa-list-targets
7+
name: Run fossa list-targets
8+
shell: bash
9+
run: |
10+
curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install-latest.sh | bash
11+
export LIST_TARGETS_OUT_FILE=${{ runner.temp }}/list-targets_out.txt
12+
export LIST_TARGETS_ERR_FILE=${{ runner.temp }}/list-targets_err.txt
13+
14+
fossa list-targets --format text 1>$LIST_TARGETS_OUT_FILE 2>$LIST_TARGETS_ERR_FILE || true
15+
16+
if grep "\[ERROR\]" $LIST_TARGETS_ERR_FILE >/dev/null 2>&1
17+
then
18+
echo "::error::fossa list-targets ran with errors."
19+
cat $LIST_TARGETS_ERR_FILE
20+
echo "HAS_FOSSA_TARGETS=Error" >> "$GITHUB_ENV"
21+
elif [[ $(cat $LIST_TARGETS_OUT_FILE | wc -l) -gt 0 ]]
22+
then
23+
echo "::notice::Fossa found analysis targets."
24+
cat $LIST_TARGETS_OUT_FILE
25+
echo "HAS_FOSSA_TARGETS=True" >> "$GITHUB_ENV"
26+
else
27+
echo "::warning::Fossa did not find any analysis targets."
28+
echo "HAS_FOSSA_TARGETS=False" >> "$GITHUB_ENV"
29+
echo "FOSSA_ANALYZE_RESULT=N/A" >> "$GITHUB_ENV"
30+
fi
31+
32+
- id: fossa-analyze
33+
name: Run fossa analyze
34+
shell: bash
35+
if: ${{ env.HAS_FOSSA_TARGETS == 'True'}}
36+
run: |
37+
export ANALYZE_OUT_FILE=${{ runner.temp }}/analyze_out.txt
38+
export ANALYZE_ERR_FILE=${{ runner.temp }}/analyze_err.txt
39+
fossa analyze --team='Service Accounts' --policy='New Relic Public Github' 1>$ANALYZE_OUT_FILE 2>$ANALYZE_ERR_FILE || true
40+
if grep "\[ERROR\]" $ANALYZE_ERR_FILE >/dev/null 2>&1
41+
then
42+
echo "::error::fossa analyze ran with errors."
43+
cat $ANALYZE_ERR_FILE
44+
echo "FOSSA_ANALYZE_RESULT=Error" >> "$GITHUB_ENV"
45+
else
46+
cat $ANALYZE_OUT_FILE
47+
echo "FOSSA_ANALYZE_RESULT=Success" >> "$GITHUB_ENV"
48+
fi
49+
50+
- name: Set custom properties
51+
shell: bash
52+
run: |
53+
response=$(curl --write-out '%{http_code}' --silent --output /dev/null \
54+
-L \
55+
-X PATCH \
56+
-H "Accept: application/vnd.github+json" \
57+
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
58+
-H "X-GitHub-Api-Version: 2022-11-28" \
59+
https://api.github.com/orgs/$ORG/properties/values \
60+
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaHasTargets","value": "'"$HAS_FOSSA_TARGETS"'"}, {"property_name":"fossaAnalyzeResult","value": "'"$FOSSA_ANALYZE_RESULT"'"}]}' \
61+
)
62+
if [[ $response != 204 ]]
63+
then
64+
echo "::warning::Writing custom properties failed."
65+
fi
66+
- name: Exit
67+
shell: bash
68+
if: ${{ env.HAS_FOSSA_TARGETS == 'Error' || env.FOSSA_ANALYZE_RESULT == 'Error' }}
69+
run: |
70+
exit 1

.github/workflows/fossa-caos.yml

Lines changed: 8 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
name: FOSSA CLI Analysis
1+
name: FOSSA CLI Analysis - CAOS
22
on:
33
pull_request:
44
branches: [ $default-branch ]
55

66
jobs:
77
check_env:
8-
runs-on: ubuntu-latest
9-
env:
10-
HAS_FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY != '' }}
11-
steps:
12-
- id: check-fossa-api-key
13-
run: echo "check=$HAS_FOSSA_API_KEY" >> "$GITHUB_OUTPUT"
14-
outputs:
15-
HAS_FOSSA_API_KEY: ${{ steps.check-fossa-api-key.outputs.check }}
8+
uses: newrelic/.github/.github/workflows/fossa-check-env.yml@main
9+
secrets:
10+
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
1611

1712
fossa:
1813
needs: check_env
@@ -23,71 +18,14 @@ jobs:
2318
ORG: ${{ github.repository_owner }}
2419
REPO: ${{ github.repository }}
2520
CUSTOM_PROPS_PAT: ${{ secrets.FOSSA_PAT }}
21+
HAS_FOSSA_TARGETS: ""
22+
FOSSA_ANALYZE_RESULT: ""
2623

2724
steps:
2825
- uses: actions/checkout@v3
2926
- name: Give GitHub Actions access to private crates
3027
uses: webfactory/[email protected]
3128
with:
3229
ssh-private-key: ${{ secrets.CAOS_RUST_CRATE_FOSSA }}
33-
- id: fossa-list-targets
34-
name: Run fossa list-targets
35-
run: |
36-
curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install-latest.sh | bash
37-
export LIST_TARGETS_OUT_FILE=${{ runner.temp }}/list-targets_out.txt
38-
export LIST_TARGETS_ERR_FILE=${{ runner.temp }}/list-targets_err.txt
39-
40-
fossa list-targets --format text 1>$LIST_TARGETS_OUT_FILE 2>$LIST_TARGETS_ERR_FILE || true
41-
42-
if [[ $(grep -i "error" $LIST_TARGETS_ERR_FILE | wc -l) -gt 0 ]]
43-
then
44-
echo "::error::fossa list-targets ran with errors."
45-
cat $LIST_TARGETS_ERR_FILE
46-
echo "HAS_FOSSA_TARGETS=Error" >> "$GITHUB_OUTPUT"
47-
elif [[ $(cat $LIST_TARGETS_OUT_FILE | wc -l) -gt 0 ]]
48-
then
49-
echo "::notice::Fossa found analysis targets."
50-
cat $LIST_TARGETS_OUT_FILE
51-
echo "HAS_FOSSA_TARGETS=True" >> "$GITHUB_OUTPUT"
52-
else
53-
echo "::warning::Fossa did not find any analysis targets."
54-
echo "HAS_FOSSA_TARGETS=False" >> "$GITHUB_OUTPUT"
55-
fi
56-
- name: Set fossaHasTargets custom property
57-
run: |
58-
curl -L \
59-
-X PATCH \
60-
-H "Accept: application/vnd.github+json" \
61-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
62-
-H "X-GitHub-Api-Version: 2022-11-28" \
63-
https://api.github.com/orgs/$ORG/properties/values \
64-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaHasTargets","value":"'"${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS }}"'"}]}'
65-
- id: fossa-analyze
66-
name: Run fossa analyze
67-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'True'}}
68-
run: |
69-
export ANALYZE_OUT_FILE=${{ runner.temp }}/analyze_out.txt
70-
export ANALYZE_ERR_FILE=${{ runner.temp }}/analyze_err.txt
71-
fossa analyze --team='Service Accounts' --policy='New Relic Public Github' 1>$ANALYZE_OUT_FILE 2>$ANALYZE_ERR_FILE || true
72-
if [[ $(grep "ERROR" $ANALYZE_ERR_FILE | wc -l) -gt 0 ]]
73-
then
74-
echo "::error::fossa analyze ran with errors."
75-
cat $ANALYZE_ERR_FILE
76-
echo "FOSSA_ANALYZE_RESULT=Error" >> "$GITHUB_OUTPUT"
77-
else
78-
cat $ANALYZE_OUT_FILE
79-
echo "FOSSA_ANALYZE_RESULT=Success" >> "$GITHUB_OUTPUT"
80-
fi
81-
- name: Set fossaAnalyzeResult custom property
82-
run: |
83-
curl -L \
84-
-X PATCH \
85-
-H "Accept: application/vnd.github+json" \
86-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
87-
-H "X-GitHub-Api-Version: 2022-11-28" \
88-
https://api.github.com/orgs/$ORG/properties/values \
89-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaAnalyzeResult","value":"'"${{ steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT }}"'"}]}'
90-
- name: Exit
91-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'Error' || steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT == 'Error' }}
92-
run: exit 1
93-
30+
- id: fossa-cli
31+
uses: newrelic/.github/.github/composite/fossa-composite@main
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: FOSSA Check Env
2+
on:
3+
workflow_call:
4+
secrets:
5+
FOSSA_API_KEY:
6+
required: false
7+
outputs:
8+
HAS_FOSSA_API_KEY:
9+
description: "True if FOSSA API key is available."
10+
value: ${{ jobs.check_env.outputs.check }}
11+
12+
jobs:
13+
check_env:
14+
runs-on: ubuntu-latest
15+
env:
16+
HAS_FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY != '' }}
17+
steps:
18+
- id: check-fossa-api-key
19+
run: echo "check=$HAS_FOSSA_API_KEY" >> "$GITHUB_OUTPUT"
20+
outputs:
21+
check: ${{ steps.check-fossa-api-key.outputs.check }}
Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
name: FOSSA CLI Analysis
1+
name: FOSSA CLI Analysis - Default
22
on:
33
pull_request:
44
branches: [ $default-branch ]
55

66
jobs:
77
check_env:
8-
runs-on: ubuntu-latest
9-
env:
10-
HAS_FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY != '' }}
11-
steps:
12-
- id: check-fossa-api-key
13-
run: echo "check=$HAS_FOSSA_API_KEY" >> "$GITHUB_OUTPUT"
14-
outputs:
15-
HAS_FOSSA_API_KEY: ${{ steps.check-fossa-api-key.outputs.check }}
8+
uses: newrelic/.github/.github/workflows/fossa-check-env.yml@main
9+
secrets:
10+
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
1611

1712
fossa:
1813
needs: check_env
@@ -23,66 +18,10 @@ jobs:
2318
ORG: ${{ github.repository_owner }}
2419
REPO: ${{ github.repository }}
2520
CUSTOM_PROPS_PAT: ${{ secrets.FOSSA_PAT }}
21+
HAS_FOSSA_TARGETS: ""
22+
FOSSA_ANALYZE_RESULT: ""
2623

2724
steps:
2825
- uses: actions/checkout@v3
29-
- id: fossa-list-targets
30-
name: Run fossa list-targets
31-
run: |
32-
curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install-latest.sh | bash
33-
export LIST_TARGETS_OUT_FILE=${{ runner.temp }}/list-targets_out.txt
34-
export LIST_TARGETS_ERR_FILE=${{ runner.temp }}/list-targets_err.txt
35-
36-
fossa list-targets --format text 1>$LIST_TARGETS_OUT_FILE 2>$LIST_TARGETS_ERR_FILE || true
37-
38-
if [[ $(grep -i "error" $LIST_TARGETS_ERR_FILE | wc -l) -gt 0 ]]
39-
then
40-
echo "::error::fossa list-targets ran with errors."
41-
cat $LIST_TARGETS_ERR_FILE
42-
echo "HAS_FOSSA_TARGETS=Error" >> "$GITHUB_OUTPUT"
43-
elif [[ $(cat $LIST_TARGETS_OUT_FILE | wc -l) -gt 0 ]]
44-
then
45-
echo "::notice::Fossa found analysis targets."
46-
cat $LIST_TARGETS_OUT_FILE
47-
echo "HAS_FOSSA_TARGETS=True" >> "$GITHUB_OUTPUT"
48-
else
49-
echo "::warning::Fossa did not find any analysis targets."
50-
echo "HAS_FOSSA_TARGETS=False" >> "$GITHUB_OUTPUT"
51-
fi
52-
- name: Set fossaHasTargets custom property
53-
run: |
54-
curl -L \
55-
-X PATCH \
56-
-H "Accept: application/vnd.github+json" \
57-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
58-
-H "X-GitHub-Api-Version: 2022-11-28" \
59-
https://api.github.com/orgs/$ORG/properties/values \
60-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaHasTargets","value":"'"${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS }}"'"}]}'
61-
- id: fossa-analyze
62-
name: Run fossa analyze
63-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'True'}}
64-
run: |
65-
export ANALYZE_OUT_FILE=${{ runner.temp }}/analyze_out.txt
66-
export ANALYZE_ERR_FILE=${{ runner.temp }}/analyze_err.txt
67-
fossa analyze --team='Service Accounts' --policy='New Relic Public Github' 1>$ANALYZE_OUT_FILE 2>$ANALYZE_ERR_FILE || true
68-
if [[ $(grep "ERROR" $ANALYZE_ERR_FILE | wc -l) -gt 0 ]]
69-
then
70-
echo "::error::fossa analyze ran with errors."
71-
cat $ANALYZE_ERR_FILE
72-
echo "FOSSA_ANALYZE_RESULT=Error" >> "$GITHUB_OUTPUT"
73-
else
74-
cat $ANALYZE_OUT_FILE
75-
echo "FOSSA_ANALYZE_RESULT=Success" >> "$GITHUB_OUTPUT"
76-
fi
77-
- name: Set fossaAnalyzeResult custom property
78-
run: |
79-
curl -L \
80-
-X PATCH \
81-
-H "Accept: application/vnd.github+json" \
82-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
83-
-H "X-GitHub-Api-Version: 2022-11-28" \
84-
https://api.github.com/orgs/$ORG/properties/values \
85-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaAnalyzeResult","value":"'"${{ steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT }}"'"}]}'
86-
- name: Exit
87-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'Error' || steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT == 'Error' }}
88-
run: exit 1
26+
- id: fossa-cli
27+
uses: newrelic/.github/.github/composite/fossa-composite@main

.github/workflows/fossa-elixir.yml

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
name: FOSSA CLI Analysis
1+
name: FOSSA CLI Analysis - Elixir
22
on:
33
pull_request:
44
branches: [ $default-branch ]
55

66
jobs:
77
check_env:
8-
runs-on: ubuntu-latest
9-
env:
10-
HAS_FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY != '' }}
11-
steps:
12-
- id: check-fossa-api-key
13-
run: echo "check=$HAS_FOSSA_API_KEY" >> "$GITHUB_OUTPUT"
14-
outputs:
15-
HAS_FOSSA_API_KEY: ${{ steps.check-fossa-api-key.outputs.check }}
8+
uses: newrelic/.github/.github/workflows/fossa-check-env.yml@main
9+
secrets:
10+
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
1611

1712
fossa:
1813
needs: check_env
@@ -23,71 +18,14 @@ jobs:
2318
ORG: ${{ github.repository_owner }}
2419
REPO: ${{ github.repository }}
2520
CUSTOM_PROPS_PAT: ${{ secrets.FOSSA_PAT }}
21+
HAS_FOSSA_TARGETS: ""
22+
FOSSA_ANALYZE_RESULT: ""
2623

2724
steps:
2825
- uses: actions/checkout@v3
2926
- uses: erlef/setup-beam@v1
3027
with:
3128
otp-version: '26'
32-
elixir: '1.15'
33-
- id: fossa-list-targets
34-
name: Run fossa list-targets
35-
run: |
36-
curl -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/fossas/fossa-cli/master/install-latest.sh | bash
37-
export LIST_TARGETS_OUT_FILE=${{ runner.temp }}/list-targets_out.txt
38-
export LIST_TARGETS_ERR_FILE=${{ runner.temp }}/list-targets_err.txt
39-
40-
fossa list-targets --format text 1>$LIST_TARGETS_OUT_FILE 2>$LIST_TARGETS_ERR_FILE || true
41-
42-
if [[ $(grep -i "error" $LIST_TARGETS_ERR_FILE | wc -l) -gt 0 ]]
43-
then
44-
echo "::error::fossa list-targets ran with errors."
45-
cat $LIST_TARGETS_ERR_FILE
46-
echo "HAS_FOSSA_TARGETS=Error" >> "$GITHUB_OUTPUT"
47-
elif [[ $(cat $LIST_TARGETS_OUT_FILE | wc -l) -gt 0 ]]
48-
then
49-
echo "::notice::Fossa found analysis targets."
50-
cat $LIST_TARGETS_OUT_FILE
51-
echo "HAS_FOSSA_TARGETS=True" >> "$GITHUB_OUTPUT"
52-
else
53-
echo "::warning::Fossa did not find any analysis targets."
54-
echo "HAS_FOSSA_TARGETS=False" >> "$GITHUB_OUTPUT"
55-
fi
56-
- name: Set fossaHasTargets custom property
57-
run: |
58-
curl -L \
59-
-X PATCH \
60-
-H "Accept: application/vnd.github+json" \
61-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
62-
-H "X-GitHub-Api-Version: 2022-11-28" \
63-
https://api.github.com/orgs/$ORG/properties/values \
64-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaHasTargets","value":"'"${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS }}"'"}]}'
65-
- id: fossa-analyze
66-
name: Run fossa analyze
67-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'True'}}
68-
run: |
69-
export ANALYZE_OUT_FILE=${{ runner.temp }}/analyze_out.txt
70-
export ANALZYE_ERR_FILE=${{ runner.temp }}/analyze_err.txt
71-
fossa analyze --team='Service Accounts' --policy='New Relic Public Github' 1>$ANALYZE_OUT_FILE 2>$ANALZYE_ERR_FILE || true
72-
if [[ $(grep -i "error" $ANALYZE_ERR_FILE | wc -l) -gt 0 ]]
73-
then
74-
echo "::error::fossa analyze ran with errors."
75-
cat $ANALYZE_ERR_FILE
76-
echo "FOSSA_ANALYZE_RESULT=Error" >> "$GITHUB_OUTPUT"
77-
else
78-
cat $ANALYZE_OUT_FILE
79-
echo "FOSSA_ANALYZE_RESULT=Success" >> "$GITHUB_OUTPUT"
80-
fi
81-
- name: Set fossaAnalyzeResult custom property
82-
run: |
83-
curl -L \
84-
-X PATCH \
85-
-H "Accept: application/vnd.github+json" \
86-
-H "Authorization: Bearer $CUSTOM_PROPS_PAT" \
87-
-H "X-GitHub-Api-Version: 2022-11-28" \
88-
https://api.github.com/orgs/$ORG/properties/values \
89-
-d '{"repository_names":["'"${REPO##*/}"'"],"properties":[{"property_name":"fossaAnalyzeResult","value":"'"${{ steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT }}"'"}]}'
90-
- name: Exit
91-
if: ${{ steps.fossa-list-targets.outputs.HAS_FOSSA_TARGETS == 'Error' || steps.fossa-analyze.outputs.FOSSA_ANALYZE_RESULT == 'Error' }}
92-
run: exit 1
93-
29+
elixir-version: '1.15'
30+
- id: fossa-cli
31+
uses: newrelic/.github/.github/composite/fossa-composite@main

0 commit comments

Comments
 (0)