iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📌

[GraphQL][GitHub] Investigating the Difference Between createdAt and publishedAt

に公開

Key Point of this Article

In this validation, there was no difference.
createdAt and publishedAt indicated the same date and time.

Introduction

As part of measuring the development team's productivity, I decided to measure the average difference between the time a GitHub PR was created and the time it was closed.
I planned to use the GitHub GraphQL API for this, but when I read the Objects - pullRequest section in the API documentation, there were many similar-looking date/time fields.

https://docs.github.com/en/graphql/reference/objects#pullrequest

  • mergedAt : The date and time the PR was merged.
  • closedAt : The date and time the PR was closed; I will use this.
  • createdAt : The date and time the PR was created.
  • publishedAt : The publication date and time of the PR? The time it went from Draft -> Open?

The API documentation for publishedAt states:

publishedAt (DateTime)
Identifies when the comment was published at.

It's a bit difficult to grasp the exact nuance of publishedAt in English.
What does it mean for a "comment" to be published? Is the PR's own body text treated as a comment, or is it triggered by a comment from someone else?
Therefore, I decided to verify publishedAt.

What I did

Development Environment

  • vscode
  • python3

Prerequisites

  • pip3 install gql
  • Obtain a private access token from GitHub

Verification Method

First, I created the following three types of PRs:

  • Create a PR as a Draft in a public repository (PR1)
  • Create a PR as a Draft in a public repository -> Change to Open (PR2)
  • Create a PR as a Draft in a public repository -> Change to Open, then add a comment myself (PR3)

I used the following script to call the GraphQL API and check the publishedAt date for each.

get_github_info_test_publishedAt.py
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import json

test_publishedAt_query="""
  query {
    repository(owner:"masakid", name:"github-sample") {
      pullRequests(last: 10) {
        nodes {
          title
          url
          createdAt
          publishedAt
          closedAt
        }
      }
    }
  }
"""


# token
with open('token_file.txt', 'r') as f:
    token = f.read().strip()

# Transport settings
transport = RequestsHTTPTransport(
    url='https://api.github.com/graphql',
    headers={"Authorization": f"bearer {token}"},
    use_json=True,
)

def post(query):
    # Create a client instance
    client = Client(transport=transport, fetch_schema_from_transport=True)

    # Execute the query and get the result
    result = client.execute(query)
    # Output the execution result
    with open("test_publishedAt_query.txt", 'w', encoding='utf-8') as f:
        f.write(json.dumps(result, indent=4, ensure_ascii=False))
    return result

def main():
    query = gql(test_publishedAt_query)
    post(query)

if __name__ == '__main__':
  main()

Results

For each PR, createdAt and publishedAt were the same date and time.

{
    "repository": {
        "pullRequests": {
            "nodes": [
                {
                    "title": "PR1 : add file",
                    "url": "https://github.com/masakid/github-sample/pull/1",
                    "createdAt": "2024-03-10T06:38:47Z",
                    "publishedAt": "2024-03-10T06:38:47Z",
                    "closedAt": null
                },
                {
                    "title": "PR2 : add file",
                    "url": "https://github.com/masakid/github-sample/pull/2",
                    "createdAt": "2024-03-10T13:09:55Z",
                    "publishedAt": "2024-03-10T13:09:55Z",
                    "closedAt": null
                },
                {
                    "title": "PR3 : add file",
                    "url": "https://github.com/masakid/github-sample/pull/3",
                    "createdAt": "2024-03-10T13:10:20Z",
                    "publishedAt": "2024-03-10T13:10:20Z",
                    "closedAt": null
                }
            ]
        }
    }
}

Summary

At least for PR Drafts and Opens, createdAt and publishedAt do not differ. I found that for my original goal of measuring the "difference between GitHub PR creation time and close time," either one could be used.

Closing

If any experts know in what situations createdAt and publishedAt would differ, I would appreciate it if you could let me know!

Discussion