Skip to content

Commit a9cd374

Browse files
authored
Merge pull request #6 from navikt/error_handling
Error handling
2 parents c6db68c + 3166d1f commit a9cd374

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ WORKDIR /action
33
RUN gem install jwt && \
44
apk add jq && \
55
apk add curl
6-
COPY generate-jwt.rb get-installation-access-token.sh ./
7-
ENTRYPOINT ["/action/get-installation-access-token.sh"]
6+
COPY generate_jwt.rb get-installation-access-token.sh ./
7+
ENTRYPOINT ["/action/get-installation-access-token.sh"]

generate-jwt.rb generate_jwt.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
# frozen_string_literal: true
2+
13
require 'openssl'
24
require 'jwt'
35

46
private_key = ENV.fetch('PRIVATE_KEY')
57
app_id = ENV.fetch('APP_ID')
68

7-
puts JWT.encode({
9+
payload = {
810
iat: Time.now.to_i,
911
exp: Time.now.to_i + (10 * 60),
1012
iss: app_id
11-
}, OpenSSL::PKey::RSA.new(private_key), 'RS256')
13+
}
14+
15+
puts JWT.encode(payload, OpenSSL::PKey::RSA.new(private_key), 'RS256')

get-installation-access-token.sh

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@ export PRIVATE_KEY=${1:?Usage: ${0} <private-key> <app-id>}
33
export APP_ID=${2:?Usage: ${0} <private-key> <app-id>}
44
repo=${GITHUB_REPOSITORY:?Missing required GITHUB_REPOSITORY environment variable}
55

6-
[[ ! -z "$INPUT_REPO" ]] && repo=$INPUT_REPO
6+
[ -n "$INPUT_REPO" ] && repo="$INPUT_REPO"
77

8-
jwt=$(ruby $(dirname $0)/generate-jwt.rb)
9-
installation_id=$(curl -s \
10-
-H "Authorization: Bearer ${jwt}" \
11-
-H "Accept: application/vnd.github.v3+json" \
12-
https://api.github.com/repos/${repo}/installation | jq -r .id)
8+
jwt=$(ruby "$(dirname "$0")"/generate_jwt.rb)
9+
response=$(curl -s -H "Authorization: Bearer ${jwt}" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/${repo}/installation")
10+
installation_id=$(echo "$response" | jq -r .id)
1311

1412
if [ "$installation_id" = "null" ]; then
15-
echo "Unable to get installation ID. Is the GitHub App installed on ${repo}?"
16-
exit 1
13+
echo "Unable to get installation ID. Is the GitHub App installed on ${repo}?"
14+
echo "$response" | jq -r .message
15+
exit 1
1716
fi
1817

1918
token=$(curl -s -X POST \
20-
-H "Authorization: Bearer ${jwt}" \
21-
-H "Accept: application/vnd.github.v3+json" \
22-
https://api.github.com/app/installations/${installation_id}/access_tokens | jq -r .token)
19+
-H "Authorization: Bearer ${jwt}" \
20+
-H "Accept: application/vnd.github.v3+json" \
21+
https://api.github.com/app/installations/"${installation_id}"/access_tokens | jq -r .token)
2322

2423
if [ "$token" = "null" ]; then
25-
echo "Unable to generate installation access token"
26-
exit 1
24+
echo "Unable to generate installation access token"
25+
exit 1
2726
fi
2827

29-
echo ::set-output name=token::${token}
28+
echo "::set-output name=token::${token}"

0 commit comments

Comments
 (0)