Skip to content

Commit 2851438

Browse files
authored
Add ability to use GH CLI Authentication (#66)
1 parent c38e325 commit 2851438

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

docs/index.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,36 @@ You can choose to *remove* some types of PRs from your changelog by passing the
8080
left-most column above.
8181
```
8282

83-
### Using a GitHub API token
83+
## Use a GitHub API token
8484

8585
`github-activity` uses the GitHub API to pull information about a repository's activity.
86-
You will quickly hit your API limit unless you use a personal access token. Here are
87-
instructions to generate and use a GitHub access token for use with `github-activity`.
86+
You will quickly hit your API limit unless you use a personal access token.
87+
There are two ways that you can generate your own access token for use with `github-activity`, each is described below:
88+
89+
### Create a token using the GitHub CLI
90+
91+
You can use [the GitHub command line interface](https://cli.github.com/manual/) to authenticate your account and store an access token in your local environment.
92+
To do so, download the GitHub CLI, and run the following command:
93+
94+
```bash
95+
# Authenticate with GitHub via the web interface
96+
gh auth login --web
97+
```
98+
99+
This will open a web interface for you to authenticate.
100+
When finished, it will store an access token locally, which you can print with:
101+
102+
```bash
103+
# Print an access token if it is stored
104+
gh auth status -t
105+
```
106+
107+
This token will automatically be used by `github-activity` if it exists.
108+
109+
### Manually create your own API token
110+
111+
Alternativelly, you can create your own GitHub access token and store it yourself.
112+
To do so, follow these steps:
88113

89114
- Create your own access token. Go to the [new GitHub access token page](https://github.com/settings/tokens/new)
90115
and follow the instructions. Note that while working with a public repository,
@@ -101,7 +126,7 @@ instructions to generate and use a GitHub access token for use with `github-acti
101126
for an environment variable called `GITHUB_ACCESS_TOKEN`. If it finds this variable,
102127
it will use this in the API calls to GitHub.
103128

104-
## How does this tool define contributions in the reports?
129+
## How we define contributors in the reports
105130

106131
GitHub Activity tries to automatically determine the unique list of contributors within
107132
a given window of time. There are many ways to define this, and there isn't necessarily a

github_activity/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
default=None,
5454
help=(
5555
"An authentication token for GitHub. If None, then the environment "
56-
"variable `GITHUB_ACCESS_TOKEN` will be tried."
56+
"variable `GITHUB_ACCESS_TOKEN` will be tried. If it does not exist "
57+
"then attempt to infer the token from `gh auth status -t`."
5758
),
5859
)
5960
parser.add_argument(

github_activity/github_activity.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def get_activity(
8585
Return only issues or PRs. If None, both will be returned.
8686
auth : string | None
8787
An authentication token for GitHub. If None, then the environment
88-
variable `GITHUB_ACCESS_TOKEN` will be tried.
88+
variable `GITHUB_ACCESS_TOKEN` will be tried. If it does not exist,
89+
then attempt to infer a token from `gh auth status -t`.
8990
cache : bool | str | None
9091
Whether to cache the returned results. If None, no caching is
9192
performed. If True, the cache is located at
@@ -108,16 +109,26 @@ def get_activity(
108109
# We have just org
109110
search_query = f"user:{org}"
110111

111-
auth = auth or os.environ.get("GITHUB_ACCESS_TOKEN")
112112
if not auth:
113-
raise ValueError(
114-
"Either the environment variable GITHUB_ACCESS_TOKEN or the "
115-
"--auth flag or must be used to pass a Personal Access Token "
116-
"needed by the GitHub API. You can generate a token at "
117-
"https://github.com/settings/tokens/new. Note that while "
118-
"working with a public repository, you don’t need to set any "
119-
"scopes on the token you create."
120-
)
113+
if "GITHUB_ACCESS_TOKEN" in os.environ:
114+
print("Using GH access token stored in `GITHUB_ACCESS_TOKEN`.")
115+
auth = os.environ.get("GITHUB_ACCESS_TOKEN")
116+
else:
117+
out = run(shlex.split("gh auth status -t"), capture_output=True)
118+
lines = [ii for ii in out.stderr.decode().split("\n") if "Token:" in ii]
119+
if lines:
120+
print("Using GH access token stored via GH CLI.")
121+
auth = lines[0].split(": ")[-1].strip()
122+
else:
123+
raise ValueError(
124+
"Either the environment variable GITHUB_ACCESS_TOKEN or the "
125+
"--auth flag or must be used to pass a Personal Access Token "
126+
"needed by the GitHub API. You can generate a token at "
127+
"https://github.com/settings/tokens/new. Note that while "
128+
"working with a public repository, you don’t need to set any "
129+
"scopes on the token you create. Alternatively, you may log-in "
130+
"via the GitHub CLI (`gh auth login`)."
131+
)
121132

122133
# Figure out dates for our query
123134
since_dt, since_is_git_ref = _get_datetime_and_type(org, repo, since, auth)

0 commit comments

Comments
 (0)