Label API: Get the date when a label was added to an issue.

I am looking for a way to get the date/time a label was added to an issue in order to determine how long something has been labeled “needs review” (for example). It would be great if this endpoint returned the date of the labels added. 

GET /repos/:owner/:repo/issues/:number/labels

Or is there any other way to do this through the API? 

5 Likes

Hi @dereksmart,

Thanks for reaching out! There is a way in v4 using createdAt like in the query below, maybe someone else has found another way but I hope this helps!

query {
  resource(url:"https://github.com/atom/atom/pull/10999") {
    ... on PullRequest {
      labels(first:10) {
        edges {
          label: node {
            name
            createdAt
          }
        }
      }
    }
  }
} 
1 Like

@andreagriffiths11 Thanks for the reply, but createdAt seems to give the date in which the label was created. I am looking for the date the label was added to the PR/issue. 

Hey @dereksmart!

I believe this is possible by using the Timeline APIs (v3, v4 from the timeline connection). 

Using the REST API, I’m able to query:

curl -H "Accept: application/vnd.github.mockingbird-preview" https://api.github.com/repos/atom/atom/issues/10999/timeline

This gives me a list of events, including “labeled” events that have a “created_at” property that should indicate when the label was added.

Let me know if this helps!

Hi @nickvanw - thanks for the reply. 

Yes, Timeline seems to be the best way to go here. 

Without having to send multiple requests (one per issue/PR), this is the best I’ve come up with so far for getting multiple issue timelines in a single request: 

query{
  repository(owner: "ownerName", name: "repoName") {
    pullRequests(last: 100, states: OPEN, labels:"[Status] Needs Review" ) {
      totalCount
      edges {
        node {
          title
          url
          number
          timeline(last: 100) {
            totalCount
            edges {
              node {
                __typename
                ... on LabeledEvent {
                  createdAt
                  label {
                    name
                  }
                }
                ... on UnlabeledEvent {
                  createdAt
                  label {
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

And then whatever is consuming the response will need to do some maual filtering and calculations to see if the label is still applied, and get the date from the last applied instance. 

Suggestions welcome on how I could simplify this! 

Hey @dereksmart!

That looks good to me! If I have one suggestion, it might be to use the pullRequest.timelineItems connection that’s currently in preview: https://developer.github.com/v4/previews/#issues-preview

This connection allows filtering the timeline down to a specific list of items, so you could do something like this:

query{
  repository(owner: "owner", name: "name") {
    pullRequests(last: 10, states: OPEN) {
      totalCount
      edges {
        node {
          title
          url
          number
          timelineItems(last: 100, itemTypes: [LABELED_EVENT, UNLABELED_EVENT]) {
            totalCount
            edges {
              node {
                __typename
                ... on LabeledEvent {
                  createdAt
                  label {
                    name
                  }
                }
                ... on UnlabeledEvent {
                  createdAt
                  label {
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Some manual filtering will still be required, but this means that you’ll get less noise in your query. 

2 Likes

timelineItems is a beautiful suggestion. Thank you!