Skip to content

Commit

Permalink
Merge pull request #6 from navikt/error_handling
Browse files Browse the repository at this point in the history
Error handling
  • Loading branch information
Kyrremann authored Nov 12, 2021
2 parents c6db68c + 3166d1f commit a9cd374
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ WORKDIR /action
RUN gem install jwt && \
apk add jq && \
apk add curl
COPY generate-jwt.rb get-installation-access-token.sh ./
ENTRYPOINT ["/action/get-installation-access-token.sh"]
COPY generate_jwt.rb get-installation-access-token.sh ./
ENTRYPOINT ["/action/get-installation-access-token.sh"]
8 changes: 6 additions & 2 deletions generate-jwt.rb → generate_jwt.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# frozen_string_literal: true

require 'openssl'
require 'jwt'

private_key = ENV.fetch('PRIVATE_KEY')
app_id = ENV.fetch('APP_ID')

puts JWT.encode({
payload = {
iat: Time.now.to_i,
exp: Time.now.to_i + (10 * 60),
iss: app_id
}, OpenSSL::PKey::RSA.new(private_key), 'RS256')
}

puts JWT.encode(payload, OpenSSL::PKey::RSA.new(private_key), 'RS256')
27 changes: 13 additions & 14 deletions get-installation-access-token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ export PRIVATE_KEY=${1:?Usage: ${0} <private-key> <app-id>}
export APP_ID=${2:?Usage: ${0} <private-key> <app-id>}
repo=${GITHUB_REPOSITORY:?Missing required GITHUB_REPOSITORY environment variable}

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

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

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

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

if [ "$token" = "null" ]; then
echo "Unable to generate installation access token"
exit 1
echo "Unable to generate installation access token"
exit 1
fi

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

0 comments on commit a9cd374

Please sign in to comment.