Last active
March 23, 2022 03:00
-
-
Save matias-eduardo/1dd62870c4ede6ed18c6cceefa782fe6 to your computer and use it in GitHub Desktop.
A script that updates GitHub user status using the GraphQL API v4 (Usage: Run once per minute)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "dotenv/load" | |
require "graphql/client" | |
require "graphql/client/http" | |
module GitHub | |
# -1- Initialize client | |
HTTP = GraphQL::Client::HTTP.new("https://api.github.com/graphql") do | |
def headers(context) | |
if token = context[:access_token] || ENV["GITHUB_ACCESS_TOKEN"] | |
{ "Authorization" => "Bearer #{token}" } | |
else | |
fail "Missing GitHub access token" | |
end | |
end | |
end | |
Schema = GraphQL::Client.load_schema(HTTP) | |
Client = GraphQL::Client.new(schema: Schema, execute: HTTP) | |
def self.query(query, **params) | |
response = GitHub::Client.query(query, **params) | |
raise "GraphQLError: #{response.errors.inspect}" if response.errors.messages.any? | |
response | |
end | |
# -2- Build GraphQL query | |
UpdateUserStatus = GitHub::Client.parse <<~GRAPHQL | |
mutation($emoji: String!, $message: String!) { | |
changeUserStatus(input: { emoji: $emoji, message: $message }) | |
} | |
GRAPHQL | |
# -3- Run the query | |
def self.update_user_status | |
time = Time.now | |
GitHub.query(GitHub::UpdateUserStatus, variables: { | |
emoji: clock_emoji(time), | |
message: time.strftime("%l:%M%P in :puerto_rico:") | |
}) | |
end | |
# -3.1- Helper method used to build clock emoji | |
private_class_method def self.clock_emoji(time) | |
# Hour in 12-hour format | |
hour = time.strftime("%l").to_i | |
# Minute of the hour | |
minute = time.strftime("%M").to_i | |
# If minute is between 15 and 45, use the emoji clocks | |
# that point at minute 30. For example: 🕦 or 🕝. | |
is_halfway = minute > 15 && minute < 45 | |
thirty_or_blank = is_halfway ? "30" : "" | |
# Return the clock emoji that most resembles the current time | |
":clock#{hour}#{thirty_or_blank}:" | |
end | |
end | |
# -0- Run script | |
GitHub.update_user_status |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
ruby "2.6.5" | |
gem "dotenv" | |
gem "graphql-client" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GITHUB_ACCESS_TOKEN="[YOUR_ACCESS_TOKEN]" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github_status_update: ruby github_status_update.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment