Skip to content

Commit

Permalink
chore: refactor contributions.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
imskr committed Mar 10, 2024
1 parent 393db58 commit 5a074fd
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions contributions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,61 @@

class Contributions
def run
organization = ENV['INPUT_ORGANIZATION']
project = ENV['INPUT_PROJECT']
username = ENV['INPUT_USERNAME']
readme_path = ENV['INPUT_README_PATH']
git_username = ENV['INPUT_COMMIT_USER']
git_email = ENV['INPUT_COMMIT_EMAIL']
organization = ENV['INPUT_ORGANIZATION']
project = ENV['INPUT_PROJECT']
username = ENV['INPUT_USERNAME']
readme_path = ENV['INPUT_README_PATH']
git_username = ENV['INPUT_COMMIT_USER']
git_email = ENV['INPUT_COMMIT_EMAIL']
commit_message = ENV['INPUT_COMMIT_MESSAGE'] || 'Update README.md'

gitlab_url = "https://gitlab.com/api/v4/projects/#{organization}%2F#{project}/merge_requests?scope=all&state=merged&author_username=#{username}&per_page=1000"

uri = URI(gitlab_url)
fetch_and_update_readme(project, gitlab_url, readme_path, commit_message, git_username, git_email)
rescue StandardError => e
puts "Error: #{e.message}"
exit 1
end

private

def fetch_and_update_readme(project, url, readme_path, commit_message, git_username, git_email)
uri = URI(url)
req = Net::HTTP::Get.new(uri)

res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(req)
end

if res.code == '200'
merged_merge_requests = JSON.parse(res.body)
if response.is_a?(Net::HTTPSuccess)
merged_merge_requests = JSON.parse(response.body)
merged_count = merged_merge_requests.count

# Update README.md with the count
readme_content = File.read(readme_path)
start_marker = '<!-- MERGED_PULL_REQUESTS_START -->'
end_marker = '<!-- MERGED_PULL_REQUESTS_END -->'
updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}\nPull requests merged in #{project}: #{merged_count}\n#{end_marker}")
File.write(readme_path, updated_readme_content)

# git flow
`git config --global --add safe.directory /github/workspace`
`git config user.name #{git_username}`
`git config user.email #{git_email}`
status = `git status`
if !status.include?("nothing to commit")
`git add #{readme_path}`
`git commit -m "#{commit_message}"`
`git push`
end
update_readme_content(project, readme_path, merged_count)
update_git_repo(readme_path, commit_message, git_username, git_email)
else
puts "Failed to retrieve merged pull requests: #{res.code} - #{res.message}"
puts "Failed to retrieve merged pull requests: #{response.code} - #{response.message}"
end
end

def update_readme_content(project, readme_path, merged_count)
readme_content = File.read(readme_path)
start_marker = ''
end_marker = ''
updated_readme_content = readme_content.gsub(/#{start_marker}.*#{end_marker}/m, "#{start_marker}\nPull requests merged in #{project}: #{merged_count}\n#{end_marker}")
File.write(readme_path, updated_readme_content)
end

def update_git_repo(readme_path, commit_message, git_username, git_email)
`git config --global --add safe.directory /github/workspace`
`git config user.name #{git_username}`
`git config user.email #{git_email}`

status = `git status`
unless status.include?("nothing to commit")
`git add #{readme_path}`
`git commit -m "#{commit_message}"`
`git push`
end
end
end
Expand Down

0 comments on commit 5a074fd

Please sign in to comment.