Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 7

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

Solved! Go to Solution.

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? 

6 Replies
Community Manager
Message 2 of 7

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

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

 


Best,
AndreaG

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Copilot Lvl 3
Message 3 of 7

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

@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. 

GitHub Staff
Message 4 of 7

Re: Label API: Get the date when a label was added to an 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!

Copilot Lvl 3
Message 5 of 7

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

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! 

Solution
GitHub Staff
Message 6 of 7

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

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. 

Copilot Lvl 3
Message 7 of 7

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

timelineItems is a beautiful suggestion. Thank you!