Using Github API to query labels?

In my hope to use Github Labels as a go/no-go for running tests, I was wondering if anyone out there has an example of an orb or other code that queries the Github API?

It seems like the API for a PR should have access and I see there is CIRCLE_PULL_REQUEST with the PR number at the end. So maybe with the right JSON…

My problem is I’m pretty basic on my CircleCI skills and have never used the Github API, so I was hoping someone out there has something similar I could…“borrow” from :slight_smile:

I think it can be a little challenging because, as best I know / remember, Circle may not synchronize every time any action (in this case, a label being added) is set, whereas with, say, GitHub Actions, it’s possible to explicitly have something run again when labels are added / removed? Is it a given that the label will be set / present when a PR is opened and / or the branch is pushed to, or is your idea that the label would get added sometime later and then the job would need to be run. As long as you are Ok with needing to either have the label already present or to first push to the branch, it would probably be possible to do this…

CircleCI Developer Hub - circleci/github-cli does exist; while it doesn’t have any commands directly that do what you want, you might be able to use it to setup the CLI and then run shell commands with it vs. working with the GH API.

While the links mentioned seem to be dead now, this issue asks a similar question. May also want to read / vote on:

Would a “manual approval” step work for your use case?

To directly answer your question: GitHub’s API is not that fun to work with. If you use a language / library you’re comfortable with (say, Python with PyGitHub), that will probably streamline things somewhat.

Just as a rough idea of something you could do if you went this direction, here’s an example that you could write as a standalone step upstream of your tests; if one of the labels in WANTED_LABELS at the top of the program is present, the program will exit 0 (success), and if none are present, it will exit 1 (failure).

import os
import sys
import github

org_name = os.environ["CIRCLE_PROJECT_USERNAME"]
repo_name = os.environ["CIRCLE_PROJECT_REPONAME"]

WANTED_LABELS = ["foo", "bar"] 

def main():

    # Note: could add some more error handling for the case where this does not
    # exist or where the last chunk is not an integer
        pr_number = int(os.environ["CIRCLE_PULL_REQUEST"].split("/")[-1])
    except KeyError:
        sys.stderr.write("Make sure CIRCLE_PULL_REQUEST is set\n")

    gh = github.Github(auth=github.Auth.Token(os.environ["GITHUB_TOKEN"]))
    repo = gh.get_repo(f"{org_name}/{repo_name}")
    pr = repo.get_pull(pr_number)
    labels = [ for x in pr.labels]
    if not any(x in labels for x in WANTED_LABELS):

if  __name__ == "__main__":