Skip to content
This repository was archived by the owner on Feb 5, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ When grading, use the `clone_repos` command to clone all the repositories in the

When running a GitHub workshop, it's nice to be able to merge a bunch of pull requests on a particular repository all at once. `merge_pull_requests` will handle this for you.

### Delete specified repositories

You can delete the specified repositories via `delete_repos`, it'll be useful once you pushed something wrong, or you may when to recycle the quota of private repo. Be careful with this command, the delete process can not be reverted, you can backup the repositories locally in case.

## Related projects

* https://education.github.com/guide
Expand Down
42 changes: 42 additions & 0 deletions lib/teachers_pet/actions/delete_repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module TeachersPet
module Actions
class DeleteRepos < Base
def read_info
@repository = self.options[:repository]
@organization = self.options[:organization]
end

def load_files
@students = self.read_students_file
end

def delete
# delete specified repo of each student
self.init_client
org_hash = self.client.organization(@organization)
abort('Organization could not be found') if org_hash.nil?
puts "Found organization at: #{org_hash[:login]}"
puts "\nDeleting specified repositories of students..."
@students.keys.sort.each do |student|
repo_name = "#{student}-#{@repository}"
if self.client.repository?(@organization, repo_name)
puts " --> Deleting '#{repo_name}'"
if self.client.delete_repository(@organization + '/' + repo_name)
puts "#{repo_name} deleted successfully"
else
puts "Failed, please make sure you have permission on this repo"
end
else
puts " --> Repository '#{student}-#{@repository}' not found"
end
end
end

def run
self.read_info
self.load_files
self.delete
end
end
end
end
14 changes: 14 additions & 0 deletions lib/teachers_pet/commands/delete_repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module TeachersPet
class Cli
option :organization, required: true
option :repository, required: true

students_option
common_options

desc 'delete_repos', "Delete specified repositories of students."
def delete_repos
TeachersPet::Actions::DeleteRepos.new(options).run
end
end
end
72 changes: 72 additions & 0 deletions spec/commands/delete_repos_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'spec_helper'

describe 'delete_repos' do
include CommandHelpers

def common_test(create_as_public)
request_stubs = []

request_stubs << stub_get_json('https://testteacher:abc123@api.github.com/orgs/testorg',
login: 'testorg',
url: 'https://api.github.com/orgs/testorg'
)
request_stubs << stub_get_json('https://testteacher:abc123@api.github.com/orgs/testorg/teams?per_page=100', student_teams)
student_usernames.each do |username|
# Check for the repos existing already
stub_request(:get, "https://testteacher:abc123@api.github.com/repos/testorg/#{username}-testrepo").
to_return(status: 404)
end

student_usernames.each do |username|
# create the repo for test
team_id = 0
student_teams.each do |st|
if st[:name].eql?(username)
team_id = st[:id]
end
end
stub_request(:post, "https://testteacher:abc123@api.github.com/orgs/testorg/repos").
with(body: {
description: "testrepo created for #{username}",
private: !create_as_public,
has_issues: true,
has_wiki: false,
has_downloads: false,
team_id: team_id,
name: "#{username}-testrepo"
}.to_json)
end

teachers_pet(:create_repos,
repository: 'testrepo',
organization: 'testorg',
public: create_as_public,

students: students_list_fixture_path,

username: 'testteacher',
password: 'abc123'
)
teachers_pet(:delete_repos,
repository: 'testrepo',
organization: 'testorg',

students: students_list_fixture_path,

username: 'testteacher',
password: 'abc123'
)

request_stubs.each do |request_stub|
expect(request_stub).to have_been_requested.at_least_once
end
end

it "create repos public" do
common_test(true)
end

it "create repos private" do
common_test(false)
end
end