From 81f40b4925138e33755a44513d8a80b1df884ac9 Mon Sep 17 00:00:00 2001 From: Peter Dave Hello Date: Tue, 7 Apr 2015 01:06:45 +0800 Subject: [PATCH] add command delete_repos --- README.md | 4 ++ lib/teachers_pet/actions/delete_repos.rb | 42 +++++++++++++ lib/teachers_pet/commands/delete_repos.rb | 14 +++++ spec/commands/delete_repos_spec.rb | 72 +++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100755 lib/teachers_pet/actions/delete_repos.rb create mode 100644 lib/teachers_pet/commands/delete_repos.rb create mode 100644 spec/commands/delete_repos_spec.rb diff --git a/README.md b/README.md index 4c46e61..d8342b2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/teachers_pet/actions/delete_repos.rb b/lib/teachers_pet/actions/delete_repos.rb new file mode 100755 index 0000000..efb3f2c --- /dev/null +++ b/lib/teachers_pet/actions/delete_repos.rb @@ -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 diff --git a/lib/teachers_pet/commands/delete_repos.rb b/lib/teachers_pet/commands/delete_repos.rb new file mode 100644 index 0000000..c4733c2 --- /dev/null +++ b/lib/teachers_pet/commands/delete_repos.rb @@ -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 diff --git a/spec/commands/delete_repos_spec.rb b/spec/commands/delete_repos_spec.rb new file mode 100644 index 0000000..ecdbbbc --- /dev/null +++ b/spec/commands/delete_repos_spec.rb @@ -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