Last active
December 22, 2020 12:46
-
-
Save kangguru/95c4692b691a0105e3a0 to your computer and use it in GitHub Desktop.
Clean Collaborators from Heroku
This file contains 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
package main | |
import "os/exec" | |
import "fmt" | |
import "net/http" | |
import "io/ioutil" | |
import "bufio" | |
import "os" | |
import "encoding/json" | |
func doRequest(method string, url string, bearer []byte) ([]byte, error) { | |
client := &http.Client{} | |
req, err := http.NewRequest(method, url, nil) | |
req.Header.Add("Accept", `application/vnd.heroku+json; version=3`) | |
req.Header.Add("Accept-Encoding", ``) | |
req.Header.Add("Content-Type", `application/json`) | |
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearer)) | |
resp, err := client.Do(req) | |
body, err := ioutil.ReadAll(resp.Body) | |
return body, err | |
} | |
func main() { | |
app := "heroku" | |
arg0 := "auth:token" | |
cmd := exec.Command(app, arg0) | |
bearer, err := cmd.Output() | |
if err != nil { | |
println(err.Error()) | |
return | |
} | |
type User struct { | |
Id string | |
Email string | |
} | |
type Collaborator struct { | |
Id string | |
User User | |
} | |
type App struct { | |
Id string | |
Name string | |
} | |
var apps []App | |
body, err := doRequest("GET", "https://api.heroku.com/apps", bearer) | |
json.Unmarshal(body, &apps) | |
for _,element := range apps { | |
var collaborators []Collaborator | |
body, err := doRequest("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/collaborators", element.Id), bearer) | |
if err != nil { | |
println(err.Error()) | |
return | |
} | |
json.Unmarshal(body, &collaborators) | |
for _,collaborator := range collaborators { | |
if collaborator.User.Email == os.Args[1] { | |
reader := bufio.NewReader(os.Stdin) | |
fmt.Printf("Remove from (%s): ", element.Name) | |
text, _ := reader.ReadString('\n') | |
if text == "y\n" { | |
_, err := doRequest("DELETE", fmt.Sprintf("https://api.heroku.com/apps/%s/collaborators/%s", element.Id, collaborator.Id), bearer) | |
if err != nil { | |
println(err.Error()) | |
return | |
} | |
fmt.Println("Removed!") | |
} | |
} | |
} | |
} | |
} |
This file contains 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
# Author: Lars Brillert | |
# | |
# Copyright 2015, Railslove GmbH | |
# | |
# Licensed under the MIT License (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://mit-license.org/ | |
# | |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# | |
# | |
# Use this to remove a collaborator from a couple of applications at once | |
# it will be a huge timesaver if there are many apps and you also not | |
# sure where a particular user has been assigned to. | |
# | |
# Usage: ruby heroku-clean.rb [email protected] | |
require "faraday" | |
require "json" | |
require "colorize" | |
unless ARGV[0] | |
puts "needs <email> as argument!".red | |
puts "ruby #{__FILE__} [email protected]" | |
exit(1) | |
end | |
if bearer = `heroku auth:token` | |
puts "obtained token from heroku: #{bearer}" | |
puts "going to delete: #{ARGV[0]}" | |
heroku = Faraday.new('https://api.heroku.com') do |request| | |
request.headers['Accept'] = 'application/vnd.heroku+json; version=3' | |
request.headers['Accept-Encoding'] = '' # don't give us gziped data | |
request.headers['Content-Type'] = 'application/json' | |
request.headers['Authorization'] = "Bearer #{bearer}" | |
request.adapter :net_http | |
end | |
JSON.parse(heroku.get('/apps').body).each do |app| | |
if app['id'] | |
JSON.parse(heroku.get("/apps/#{app['id']}/collaborators").body).each do |colab| | |
if colab["user"] | |
if colab["user"]["email"] == ARGV[0].to_s | |
print "delete from '#{app["name"]}'? (y/N) " | |
if $stdin.gets == "y\n" | |
heroku.delete("/apps/#{app['id']}/collaborators/#{colab["id"]}") | |
puts "gone!".green | |
else | |
puts "skipped!".yellow | |
end | |
end | |
end | |
end | |
end | |
end | |
else | |
puts "someting has gone wrong!".red | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment