Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Branch Scanning #258

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
17 changes: 13 additions & 4 deletions api/ruby/find-inactive-members/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
```
find_inactive_members.rb - Find and output inactive members in an organization
-c, --check Check connectivity and scope
-d, --date MANDATORY Date from which to start looking for activity
-d, --date MANDATORY Date from which to start looking for activity. The format is DD-MM-YYYY
-e, --email Fetch the user email (can make the script take longer
-o, --organization MANDATORY Organization to scan for inactive users
-v, --verbose More output to STDERR
-b, --branches Iterate through all branches instead of only checking the default branch
-h, --help Display this help
```

This utility finds users inactive since a configured date, writes those users to a file `inactive_users.csv`.

## Installation

### Generate a token

[Generate new GitHub token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) with scopes `repo` and `admin:org`

### Clone this repository

```shell
Expand All @@ -29,7 +34,7 @@ gem install octokit

### Configure Octokit

The `OCTOKIT_ACCESS_TOKEN` is required in order to see activities on private repositories. However the `OCTOKIT_API_ENDPOINT` isn't required if connecting to GitHub.com, but is required if connecting to a GitHub Enterprise instance.
The `OCTOKIT_ACCESS_TOKEN` is required in order to see activities on private repositories. However the `OCTOKIT_API_ENDPOINT` isn't required if connecting to GitHub.com, but is required if connecting to a GitHub Enterprise Server instance.

```shell
export OCTOKIT_ACCESS_TOKEN=00000000000000000000000 # Required if looking for activity in private repositories.
Expand All @@ -38,14 +43,18 @@ export OCTOKIT_API_ENDPOINT="https://<your_github_enterprise_instance>/api/v3" #

## Usage


```
ruby find_inactive_members.rb [-cehv] -o ORGANIZATION -d DATE
ruby find_inactive_members.rb [-bcehv] -o ORGANIZATION -d DATE
```




## How Inactivity is Defined

Members are defined as inactive if they haven't, since the specified **DATE**, in any repository in the specified **ORGANIZATION**:

* Have not merged or pushed commits into the default branch
* Have not merged or pushed commits into the default branch (all branches with '-b')
* Have not opened an Issue or Pull Request
* Have not commented on an Issue or Pull Request
44 changes: 33 additions & 11 deletions api/ruby/find-inactive-members/find_inactive_members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize(options={})
@date = options[:date]
@organization = options[:organization]
@email = options[:email]
@branches = options[:branches]
JennieOhyoung marked this conversation as resolved.
Show resolved Hide resolved
@unrecognized_authors = []

organization_members
Expand Down Expand Up @@ -82,7 +83,7 @@ def organization_members
# get all organization members and place into an array of hashes
info "Finding #{@organization} members "
@members = @client.organization_members(@organization).collect do |m|
email =
email =
{
login: m["login"],
email: member_email(m[:login]),
Expand Down Expand Up @@ -115,16 +116,33 @@ def commit_activity(repo)
# get all commits after specified date and iterate
info "...commits"
begin
@client.commits_since(repo, @date).each do |commit|
# if commmitter is a member of the org and not active, make active
if commit["author"].nil?
add_unrecognized_author(commit[:commit][:author])
next
end
if t = @members.find {|member| member[:login] == commit["author"]["login"] && member[:active] == false }
make_active(t[:login])
end
end
# Get all the branches so we can loop through them
if @branches
primetheus marked this conversation as resolved.
Show resolved Hide resolved
@client.branches(repo).each do |branch|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JennieOhyoung: looks as if indenting is broken here

info "... branch = " + branch["name"]
@client.commits_since(repo, @date, {:sha => branch["name"]}).each do |commit|
# if commmitter is a member of the org and not active, make active
if commit["author"].nil?
add_unrecognized_author(commit[:commit][:author])
next
end
if t = @members.find {|member| member[:login] == commit["author"]["login"] && member[:active] == false }
make_active(t[:login])
end
end
end
else
@client.commits_since(repo, @date).each do |commit|
# if commmitter is a member of the org and not active, make active
if commit["author"].nil?
add_unrecognized_author(commit[:commit][:author])
next
end
if t = @members.find {|member| member[:login] == commit["author"]["login"] && member[:active] == false }
make_active(t[:login])
end
end
end
rescue Octokit::Conflict
info "...no commits"
rescue Octokit::NotFound
Expand Down Expand Up @@ -245,6 +263,10 @@ def member_activity
options[:verbose] = v
end

opts.on('-b', '--branches', "Iterate through all branches instead of default") do |b|
options[:branches] = b
end

opts.on('-h', '--help', "Display this help") do |h|
puts opts
exit 0
Expand Down