Skip to content

Commit

Permalink
Publish port service experimental action
Browse files Browse the repository at this point in the history
  • Loading branch information
gwthm-in committed Nov 24, 2023
1 parent 7798482 commit 7e6ad76
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 9 deletions.
14 changes: 5 additions & 9 deletions port-packages/action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: port-packages
author: Gowtham Sai (gwthm.in@gmail.com)
description: Reports port packages to getport
description: Reports packages to getport
inputs:
clientId:
required: true
description: 'Client ID'
clientSecret:
required: true
description: 'Client secret'
outputs:
port_packages:
description: 'Port packages entity'
value: ${{ steps.extract-packages.outputs.packages }}
runs:
using: 'composite'
steps:
Expand All @@ -18,14 +22,6 @@ runs:
run: |
ruby ${{ github.action_path }}/packages.rb
cat packages.txt >> "$GITHUB_OUTPUT"
echo "Packages...."
cat packages.txt
- name: "Extract packages identifiers"
id: extract-packages-identifiers
shell: bash
run: |
echo "identifiers=$(echo '${{steps.extract-packages.outputs.packages}}' | jq --compact-output -r '[.[] | .identifier]')" >> "$GITHUB_OUTPUT"
- name: "Report packages to port"
uses: port-labs/port-github-action@v1
Expand Down
61 changes: 61 additions & 0 deletions port-service/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: port-service
author: Gowtham Sai (gwthm.in@gmail.com)
description: Reports service to getport
inputs:
clientId:
required: true
description: 'Client ID'
clientSecret:
required: true
description: 'Client secret'
testCoverage:
required: false
descripition: 'Service test coverage'
sonarStatus:
required: false
description: 'Service sonar status'
runs:
using: 'composite'
steps:
- name: "Extract packages"
id: extract-packages
shell: bash
run: |
ruby ${{ github.action_path }}/packages.rb
cat packages.txt >> "$GITHUB_OUTPUT"
- name: "Extract packages identifiers"
id: extract-packages-identifiers
shell: bash
run: |
echo "identifiers=$(echo '${{steps.extract-packages.outputs.packages}}' | jq --compact-output -r '[.[] | .identifier]')" >> "$GITHUB_OUTPUT"
# Extract packages from package manager version file based on the language.
- name: "Readme content"
id: readme
shell: bash
run: |
echo "readme=$(awk '{printf("%s\n", $0)}' README.md | tr '\n' '|' | sed 's/|/\\n/g')" >> "$GITHUB_OUTPUT"
- name: "Report service to port"
uses: port-labs/port-github-action@v1
with:
clientId: ${{ inputs.clientId }}
clientSecret: ${{ inputs.clientSecret }}
identifier: ${{ github.event.repository.name }}
title: ${{ github.event.repository.name }}
blueprint: service
properties: |
{
"name": "${{ github.event.repository.name }}",
"url": "${{ github.server_url }}/${{ github.repository }}",
"README": "${{ steps.readme.outputs.readme }}",
"about": "${{ github.event.repository.description }}",
"test_coverage": "${{ inputs.testCoverage }}",
"sonar_status": "${{ inputs.sonarStatus }}"
}
relations: |
{
"package": ${{ steps.extract-packages-identifiers.outputs.identifiers }}
}
94 changes: 94 additions & 0 deletions port-service/packages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env ruby

require 'json'

$packages = []

# Extract Python package information from requirements.txt
def extract_python_packages
return unless File.exist?('requirements.txt')

$packages += File.readlines('requirements.txt').map do |line|
name, version = line.strip.split('==')
{"properties" => {"name" => name, "version" => version, "language" => "Python"}, "blueprint" => "package", "identifier" => "#{name}@#{version}", "title" => "#{name}@#{version}"}
end
end

# Extract Node.js package information from package.json
def extract_node_packages
return unless File.exist?('package.json')

package_json = JSON.parse(File.read('package.json'))
$packages += (package_json['dependencies'] || {}).merge(package_json['devDependencies'] || {}).map do |key, value|
{"properties" => {"name" => key, "version" => value, "language" => "Node"}, "blueprint" => "package", "identifier" => "#{key.gsub(/[\/.^]/, '-')}@#{value}", "title" => "#{key}@#{value}"}
end
end

# Extract Go package information from go.mod
def extract_go_packages
return unless File.exist?('go.mod')

in_require_block = false

# Regex pattern to match and capture host
host_pattern = /^(https?:\/\/)?([^\/]+)\/(.+)$/

File.readlines('go.mod').each do |line|
if line =~ /^require\s+\(/
in_require_block = true
elsif line =~ /^\)\s*$/
in_require_block = false
elsif in_require_block && line =~ /^\s*([^\s]+)\s+([^\s]+)\s*$/
name_with_host, version = $1, $2.gsub(/\+incompatible$/, '')

# Use regex to match and capture host
match = name_with_host.match(host_pattern)

if match
host, name = match.captures[1..2] # Extract the second and third captured groups
else
host = nil
name = name_with_host
end

# $packages << {"properties" => {"name" => name, "version" => version, "language" => "Go"}, "blueprint" => "package", "identifier" => "#{name_with_host}@#{version}", "title" => "#{name}@#{version}"}
# We should consider identifier as package after trimming host.
$packages << {"properties" => {"name" => name, "version" => version, "language" => "Go"}, "blueprint" => "package", "identifier" => "#{name}@#{version}", "title" => "#{name}@#{version}"}
end
end
end

# Extract Ruby package information from Gemfile.lock
def extract_ruby_packages
return unless File.exist?('Gemfile.lock')

in_specification_block = false

File.readlines('Gemfile.lock').each do |line|
line.strip!

if line.start_with?('GEM')
in_specification_block = true
elsif in_specification_block && line =~ /^(\S+)\s+\(([^,]+),.*\)$/
name, version = $1, $2
$packages << {"properties" => {"name" => name, "version" => version, "language" => "Ruby"}, "blueprint" => "package", "identifier" => "#{name}@#{version}", "title" => "#{name}@#{version}"}
elsif line.empty? && in_specification_block
in_specification_block = false
end
end
end

# Helper method to write packages to the output file
def write_to_file(packages=$packages)
File.open('packages.txt', 'a') do |file|
file.puts 'packages=' + JSON.generate(packages)
end
end

# Main script
extract_python_packages
extract_node_packages
extract_go_packages
extract_ruby_packages
write_to_file

0 comments on commit 7e6ad76

Please sign in to comment.