Skip to content

Commit

Permalink
github service
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenvanhuan243 committed Sep 21, 2024
1 parent 83ae5c0 commit 504884d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ git_source(:github) do |repo_name|
end

gem 'grape'
gem 'octokit'
gem 'grape-entity'
gem 'grape-rails-cache'
gem 'grape-swagger'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
Expand Down Expand Up @@ -90,6 +92,11 @@ GEM
railties (>= 3.0.0)
faker (2.22.0)
i18n (>= 1.8.11, < 2)
faraday (2.8.1)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
gelf (3.1.0)
json
globalid (1.1.0)
Expand Down Expand Up @@ -167,6 +174,9 @@ GEM
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
racc (~> 1.4)
octokit (4.25.1)
faraday (>= 1, < 3)
sawyer (~> 0.9)
oga (3.4)
ast
ruby-ll (~> 2.1)
Expand All @@ -180,6 +190,7 @@ GEM
pg (1.5.6)
pg (1.5.6-x64-mingw32)
pg (1.5.6-x86-mingw32)
public_suffix (5.1.1)
puma (3.12.6)
puma (3.12.6-java)
racc (1.7.3)
Expand Down Expand Up @@ -280,6 +291,9 @@ GEM
ast
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (>= 0.17.3, < 3)
sidekiq (5.2.9)
connection_pool (~> 2.2, >= 2.2.2)
rack (~> 2.0)
Expand Down Expand Up @@ -341,6 +355,7 @@ DEPENDENCIES
grape-swagger-rails
httparty
jwt
octokit
pg
puma (~> 3.0)
rack-cors
Expand Down
55 changes: 25 additions & 30 deletions app/services/github/follow_service.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
require 'octokit'

class Github::FollowService
def initialize; end
def initialize
@client = github_client
end

def follow_user(target_user)
headers = {
'Accept' => '*/*',
'Accept-Language' => 'en-US,en;q=0.9,vi;q=0.8',
'Cookie' => '',
'Origin' => 'https://github.com',
'Referer' => 'https://github.com/filiph?tab=followers',
'Sec-Ch-UA' => '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
'Sec-Ch-UA-Mobile' => '?0',
'Sec-Ch-UA-Platform' => '"macOS"',
'Sec-Fetch-Dest' => 'empty',
'Sec-Fetch-Mode' => 'cors',
'Sec-Fetch-Site' => 'same-origin',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
'X-Requested-With' => 'XMLHttpRequest'
}
def follow_user(target_username)
# Follow the user on GitHub
@client.follow(target_username)

# { success: true, message: "Successfully followed #{target_username}" }
puts '###############################################################################'
puts "############################# Done #{target_username} #########################"
puts '###############################################################################'
rescue Octokit::NotFound
puts "Fail #{target_username}"
rescue Octokit::Unauthorized
puts "Fail #{target_username}"
rescue StandardError => e
puts "Fail #{target_username}"
end

options = {
headers: headers,
body: {
commit: 'Follow',
authenticity_token: ENV["GITHUB_ACCESS_TOKEN"]
}
}
private

response = HTTParty.post("https://github.com/users/follow?target=#{target_user}", options)
def github_client
token = ENV['GITHUB_ACCESS_TOKEN']
raise StandardError, 'GitHub access token not found in environment variables' if token.nil? || token.empty?

if response.success?
puts "Successfully followed #{target_user}!"
else
puts "Error following #{target_user}: #{response.code} - #{response.message}"
end
Octokit::Client.new(access_token: token)
end
end

0 comments on commit 504884d

Please sign in to comment.