Skip to content

Commit

Permalink
Merge pull request #1 from imskr/initialize-project
Browse files Browse the repository at this point in the history
Testing project
  • Loading branch information
imskr authored Feb 24, 2024
2 parents 7ac050e + 4810b23 commit 3b55c08
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Build
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"

jobs:
build:
name: Build Job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.2
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM ruby:3.2

COPY . /app
WORKDIR /app

ENTRYPOINT ["ruby", "/app/contributions.rb"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# contributions-badge
Showcase your number of merged pull requests in profile's README dynamically

This project has MERGED_PULL_REQUESTS_COUNT merged pull requests.
50 changes: 50 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: "Contributions Badge"
description: "Showcase your number of merged pull requests in profile's README dynamically"
author: "Shubham Kumar @imskr"
inputs:
organization:
description: "Organization name"
default: "gitlab-org"
required: true
project:
description: "Project name"
default: "gitlab"
required: true
username:
description: "Username"
default: "imskr"
required: true
readme_path:
description: "Path of readme file"
default: "./README.md"
required: false
commit_user:
description: "Git username"
default: "contributions-badge-bot"
required: false
commit_email:
description: "Git user email"
default: "[email protected]"
required: false
commit_message:
description: "Git commit message"
default: "Update readme with latest contributions count"
required: false
outputs:
merged_count:
description: "Updated contributions count"

runs:
using: "docker"
image: "Dockerfile"
args:
- "-organization=${{ inputs.organization }}"
- "-project=${{ inputs.project }}"
- "-username=${{ inputs.username }}"
- "-commit_user=${{ inputs.commit_user }}"
- "-commit_email=${{ inputs.commit_email }}"
- "-commit_message=${{ inputs.commit_message }}"

branding:
icon: 'file-text'
color: 'red'
42 changes: 42 additions & 0 deletions contributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'net/http'
require 'json'

class Contributions
def run
organization = ENV['INPUT_ORGANIZATION']
project = ENV['INPUT_PROJECT']
username = ENV['INPUT_USERNAME']
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)
req = Net::HTTP::Get.new(uri)

res = 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)
merged_count = merged_merge_requests.count

# Update README.md with the count
# readme_path = File.join(__dir__, 'README.md')
readme_path = ENV['INPUT_README_PATH']
readme_content = File.read(readme_path)
updated_readme_content = readme_content.gsub(/MERGED_PULL_REQUESTS_COUNT/, merged_count.to_s)
File.write(readme_path, updated_readme_content)

# git flow
commit_message = ENV['INPUT_COMMIT_MESSAGE'] || 'Update README.md'
`git add #{readme_path}`
`git commit -m "#{commit_message}"`
`git push origin HEAD`
else
puts "Failed to retrieve merged pull requests: #{res.code} - #{res.message}"
end
end
end

Contributions.new.run

0 comments on commit 3b55c08

Please sign in to comment.