Skip to content

Add ability to attach/detach issue label w/o side effects to other labels #456

@basejumpa

Description

@basejumpa

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 }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions