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 ability to attach/detach issue label w/o side effects to other labels #456

Closed
basejumpa opened this issue Sep 23, 2018 · 0 comments
Closed

Comments

@basejumpa
Copy link

basejumpa commented Sep 23, 2018

Please improve usability for handling of issue labels. Current version 1.94.

Current situation

Currently it is very tedious to attach or detach a single label and leave the other labels of the issue untouched.

Currention: Currently it is even not possible.

It is because setLabels affects the complete list of labels of the issue.

The procedure to attach a single label without side effects to possible other labels of the issue is

  • Use getLabels to retrieve collection of GHLabel objects
  • Create list of Strings from the collection
  • Add name of label to be attached to the list
  • Now setLabels needed to be used to apply the list to the issue. But the method offers variadic argument list only, cast not possible, data can not be conveyed.

Class GHIssue offers three methods for labels

  • void setLabels(String... labels)
  • Collection<GHLabel> | getLabels()

Usecases

  • Attach a single label or a list of labels to an issue
  • Detach a single label or a list of labels to an issue

(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/456 
static void attachLabel (String token, GHIssue issue, String label_name) {
   def jsonString = "[ \"$label_name\" ]"
   def con = "https://api.github.com${issue.getIssuesApiRoute()}/labels".toURL().openConnection()
   con.setDoOutput(true)
   con.setDoInput(true)
   con.setRequestMethod("POST")
   con.setRequestProperty("Authorization", "token $token")
   con.setRequestProperty("Content-Type", "application/json")
   con.outputStream.withWriter { writer ->
     writer << jsonString
   }
   String response = con.inputStream.withReader { Reader reader -> reader.text }
}

// Works around https://github.com/kohsuke/github-api/issues/456 
static void detachLabel (String token, GHIssue issue, String label_name) {
   def con = "https://api.github.com${issue.getIssuesApiRoute()}/labels/$label_name".toURL().openConnection()
   con.setDoOutput(true)
   con.setDoInput(true)
   con.setRequestMethod("DELETE")
   con.setRequestProperty("Authorization", "token $token")
   con.setRequestProperty("Content-Type", "application/json")
   con.connect()
   String response = con.inputStream.withReader { Reader reader -> reader.text }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant