From 3e821a558ea3ca6782e1f300a1c67e449cd93b7b Mon Sep 17 00:00:00 2001 From: Brend Smits Date: Mon, 12 Sep 2022 13:34:54 +0200 Subject: [PATCH] add audit_log endpoint with tests (#1481) * add audit_log endpoint with tests Signed-off-by: Brend Smits * Update link to correct GitHub docs Signed-off-by: Brend Smits Co-authored-by: Tim Rogers Signed-off-by: Brend Smits Co-authored-by: Tim Rogers --- lib/octokit/client/organizations.rb | 20 +++++++++++++++++++ ...returns_audit_log_for_an_organization.json | 1 + spec/octokit/client/organizations_spec.rb | 8 ++++++++ 3 files changed, 29 insertions(+) create mode 100644 spec/cassettes/Octokit_Client_Organizations/_organization_audit_log/returns_audit_log_for_an_organization.json diff --git a/lib/octokit/client/organizations.rb b/lib/octokit/client/organizations.rb index 69b81d993..5f218b673 100644 --- a/lib/octokit/client/organizations.rb +++ b/lib/octokit/client/organizations.rb @@ -823,6 +823,26 @@ def unlock_repository(org, id, repo, options = {}) def billing_actions(org) get "#{Organization.path(org)}/settings/billing/actions" end + + # Get organization audit log. + # + # Gets the audit log for an organization. + # + # @param org [String, Integer] Organization GitHub login or id for which + # to retrieve the audit log. + # @option options [String] :include ('all') Filter by event type. + # `all`, `git` or `web`. + # @option options [String] :phrase A search phrase. + # @option options [String] :order ('desc') The order of audit log events. To list newest events first, specify desc. + # To list oldest events first, specify asc. + # + # @return [Array] List of events + # @see https://docs.github.com/en/enterprise-cloud@latest/rest/orgs/orgs#get-the-audit-log-for-an-organization + # @example + # Octokit.organization_audit_log('github', {include: 'all', phrase: 'action:org.add_member created:>2022-08-29 user:octocat'}) + def organization_audit_log(org, options = {}) + paginate "#{Organization.path org}/audit-log", options + end end end end diff --git a/spec/cassettes/Octokit_Client_Organizations/_organization_audit_log/returns_audit_log_for_an_organization.json b/spec/cassettes/Octokit_Client_Organizations/_organization_audit_log/returns_audit_log_for_an_organization.json new file mode 100644 index 000000000..502fa721f --- /dev/null +++ b/spec/cassettes/Octokit_Client_Organizations/_organization_audit_log/returns_audit_log_for_an_organization.json @@ -0,0 +1 @@ +{"http_interactions":[{"request":{"method":"get","uri":"https://api.github.com/orgs/api-playground/audit-log?include=all&phrase=action:org.add_member%20created:%3E2022-08-29","body":{"encoding":"US-ASCII","base64_string":""},"headers":{"Accept":["application/vnd.github.v3+json"],"User-Agent":["Octokit Ruby Gem 5.4.0"],"Content-Type":["application/json"],"Authorization":["token <>"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":200,"message":"OK"},"headers":{"Server":["GitHub.com"],"Date":["Thu, 08 Sep 2022 09:46:38 GMT"],"Content-Type":["application/json; charset=utf-8"],"Transfer-Encoding":["chunked"],"Cache-Control":["private, max-age=60, s-maxage=60"],"Vary":["Accept, Authorization, Cookie, X-GitHub-OTP","Accept-Encoding, Accept, X-Requested-With"],"Etag":["W/\"b3452f345082e67e0b56ebea9a8eb8a079d51bebf854b03ad5087fcdc011f276\""],"X-Oauth-Scopes":["admin:org, repo:org, write:org"],"X-Accepted-Oauth-Scopes":["admin:org, read:org, write:org"],"Github-Authentication-Token-Expiration":["2022-09-08 22:00:00 UTC"],"X-Github-Media-Type":["github.v3; format=json"],"X-Ratelimit-Limit":["5000"],"X-Ratelimit-Remaining":["4991"],"X-Ratelimit-Reset":["1662630565"],"X-Ratelimit-Used":["9"],"X-Ratelimit-Resource":["core"],"Access-Control-Expose-Headers":["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset"],"Access-Control-Allow-Origin":["*"],"Strict-Transport-Security":["max-age=31536000; includeSubdomains; preload"],"X-Frame-Options":["deny"],"X-Content-Type-Options":["nosniff"],"X-Xss-Protection":["0"],"Referrer-Policy":["origin-when-cross-origin, strict-origin-when-cross-origin"],"Content-Security-Policy":["default-src 'none'"],"X-Github-Request-Id":["4133:295B:16ADE1C:17110CD:6319B9FE"]},"body":{"encoding":"ASCII-8BIT","base64_string":"WwogICAgewogICAgICAgICJhY3RvciI6ICJvY3Rva2l0IiwKICAgICAgICAiY3JlYXRlZF9hdCI6IDE2NjE4MzY2MTA2NjIsCiAgICAgICAgIm9yZyI6ICJjb2RlZm9yYW1lcmljYSIsCiAgICAgICAgInBlcm1pc3Npb24iOiAicmVhZCIsCiAgICAgICAgIkB0aW1lc3RhbXAiOiAxNjYxODM2NjEwNjYyLAogICAgICAgICJidXNpbmVzcyI6ICJjb2RlZm9yYW1lcmljYSIsCiAgICAgICAgInVzZXIiOiAibmV3LW9jdG9raXQiLAogICAgICAgICJhY3Rpb24iOiAib3JnLmFkZF9tZW1iZXIiLAogICAgICAgICJfZG9jdW1lbnRfaWQiOiAiREtqVWR2aVRBcTViU0cxVEdtOGxjQSIsCiAgICAgICAgIm9wZXJhdGlvbl90eXBlIjogImNyZWF0ZSIKICAgIH0KXQ=="}},"recorded_at":"Thu, 08 Sep 2022 09:46:38 GMT"}],"recorded_with":"VCR 6.1.0"} \ No newline at end of file diff --git a/spec/octokit/client/organizations_spec.rb b/spec/octokit/client/organizations_spec.rb index da09494ac..d692c62fd 100644 --- a/spec/octokit/client/organizations_spec.rb +++ b/spec/octokit/client/organizations_spec.rb @@ -411,4 +411,12 @@ assert_requested :get, github_url("/orgs/#{test_github_org}/settings/billing/actions") end end + + describe '.organization_audit_log', :vcr do + it 'returns audit log for an organization' do + audit_log = @client.organization_audit_log(test_github_org, { include: 'all', phrase: 'action:org.add_member created:>2022-08-29' }) + expect(audit_log).to be_kind_of Array + assert_requested :get, github_url("/orgs/#{test_github_org}/audit-log?include=all&phrase=action:org.add_member%20created:%3E2022-08-29") + end + end end