Skip to content

Commit

Permalink
Set github token from the environment variable. (#4)
Browse files Browse the repository at this point in the history
* Set github token from the environment variable.

Signed-off-by: Taewan Kim <[email protected]>

* Add LPVS_GITHUB_TOKEN env variable to README.md

Signed-off-by: Taewan Kim <[email protected]>
  • Loading branch information
tiokim authored Aug 1, 2022
1 parent eea152d commit 9061cd6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ LPVS will start scanning automatically, then provide comments about the licenses
Or alternatively build and run the Docker container with LPVS:
```bash
docker build -t lpvs .
docker run -p 7896:7896 --name lpvs lpvs:latest
docker run -p 7896:7896 --name lpvs -e LPVS_GITHUB_TOKEN=<`github.token`> lpvs:latest
```
For additional information about using Docker and tips, please check file [Docker_Usage](.github/Docker_Usage.md).
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/lpvs/service/GitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public String getPullRequestFiles (WebhookConfig webhookConfig) {
webhookConfig.getRepositoryName() + "/pulls/" + webhookConfig.getPullRequestId());
}
try {
if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv();
if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
else gitHub = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
GHRepository repository = gitHub.getRepository(webhookConfig.getRepositoryOrganization()+"/"
Expand Down Expand Up @@ -82,6 +83,7 @@ private GHPullRequest getPullRequest(WebhookConfig webhookConfig, GHRepository r

public void setPendingCheck(WebhookConfig webhookConfig) {
try {
if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv();
if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
else gitHub = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
GHRepository repository = gitHub.getRepository(webhookConfig.getRepositoryOrganization() + "/"
Expand All @@ -95,6 +97,7 @@ public void setPendingCheck(WebhookConfig webhookConfig) {

public void setErrorCheck(WebhookConfig webhookConfig) {
try {
if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv();
if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
else gitHub = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
GHRepository repository = gitHub.getRepository(webhookConfig.getRepositoryOrganization() + "/"
Expand Down Expand Up @@ -173,6 +176,7 @@ public String getRepositoryLicense(WebhookConfig webhookConfig) {
try {
String repositoryName = webhookConfig.getRepositoryName();
String repositoryOrganization = webhookConfig.getRepositoryOrganization();
if (GITHUB_AUTH_TOKEN.isEmpty()) setGithubTokenFromEnv();
if (GITHUB_API_URL.isEmpty()) gitHub = GitHub.connect(GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
else gitHub = GitHub.connectToEnterpriseWithOAuth(GITHUB_API_URL, GITHUB_LOGIN, GITHUB_AUTH_TOKEN);
GHRepository repository = gitHub.getRepository(repositoryOrganization + "/" + repositoryName);
Expand Down Expand Up @@ -203,4 +207,8 @@ public String getMatchedLinesAsLink(WebhookConfig webhookConfig, LPVSFile file)
return matchedLines;
}

public void setGithubTokenFromEnv() {
if (System.getenv("LPVS_GITHUB_TOKEN") != null) GITHUB_AUTH_TOKEN = System.getenv("LPVS_GITHUB_TOKEN");
}

}

0 comments on commit 9061cd6

Please sign in to comment.