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 support for handling team's privacy #455

Closed
basejumpa opened this issue Sep 23, 2018 · 2 comments · Fixed by #683
Closed

Add support for handling team's privacy #455

basejumpa opened this issue Sep 23, 2018 · 2 comments · Fixed by #683

Comments

@basejumpa
Copy link

basejumpa commented Sep 23, 2018

Please add ability to get or set a team's property privacy (see https://developer.github.com/v3/teams/#parameters). Current version 1.94.

Usecases

  • Set visibility when creating a team or teams
  • Set visibility of existing team or teams
  • Get visibility of a team or teams

Affected API elements (list may not exchaustive):

(Dirty) workaround

This is my workaround using in my groovy scripts currently:

// File kohsuke_github_workarounds.groovy
// Use it with import kohsuke_github_workarounds

@Grab(group='org.kohsuke', module='github-api', version='1.94')
import org.kohsuke.github.*

// Works around https://github.com/kohsuke/github-api/issues/455
static void setPrivacy (String token, GHTeam team, String privacy) {
   allowMethods('PATCH')
   def jsonString = "{ \"name\": \"${team.getName()}\", \"privacy\": \"$privacy\" }"
   def con = "https://api.github.com/teams/${team.getId()}".toURL().openConnection()
   con.setDoOutput(true)
   con.setDoInput(true)
   con.setRequestMethod("PATCH");
   con.setRequestProperty("Authorization", "token $token")
   con.setRequestProperty("Content-Type", "application/json")
   con.connect()
   con.outputStream.withWriter { writer ->
     writer << jsonString
   }
   String response = con.inputStream.withReader { Reader reader -> reader.text }
}

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

private static void allowMethods(String... methods) {
    try {
        Field methodsField = HttpURLConnection.class.getDeclaredField("methods");

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);

        methodsField.setAccessible(true);

        String[] oldMethods = (String[]) methodsField.get(null);
        Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
        methodsSet.addAll(Arrays.asList(methods));
        String[] newMethods = methodsSet.toArray(new String[0]);

        methodsField.set(null/*static field*/, newMethods);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}
@basejumpa basejumpa changed the title Add support for team's privacy Add support for handling team's privacy Sep 23, 2018
@apratina
Copy link

apratina commented Aug 9, 2019

It would be good to have support for this :)

@timja
Copy link
Collaborator

timja commented Jan 26, 2020

This + more is covered in: #683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants