Skip to content

Commit 1f13869

Browse files
committed
separate server & cli apps & update readme
1 parent 129d709 commit 1f13869

7 files changed

Lines changed: 149 additions & 105 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ jobs:
2222
- name: Check go.mod
2323
run: go mod tidy && git diff --no-patch --exit-code
2424

25-
- name: Build
26-
run: go build ./cmd/github-service
25+
- name: Build Server
26+
run: go build ./cmd/server
27+
28+
- name: Build CLI
29+
run: go build ./cmd/cli
2730

2831
- name: Run tests
2932
run: go test ./...

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: bin/github-service
1+
web: bin/server

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
## Deploying to Heroku
2-
The Heroku app is updated whenever a new commit is pushed to the `master` branch.
1+
# github-service
2+
A simple service to query GitHub issues & pull requests.
3+
* See [Server Mode](#server-mode) for using it as an [entrello](https://github.com/utkuufuk/entrello) service
4+
* See [CLI Mode](#cli-mode) for using it as a CLI tool
5+
6+
## Configuration
7+
Put your environment variables in a file called `.env`. See `.env.example` for reference.
8+
9+
## Server Mode
10+
Start the server:
11+
```sh
12+
go run ./cmd/server
13+
```
14+
15+
### List of Endpoints
16+
#### `GET <SERVER_URL>/entrello`
17+
Fetch self-assigned issues from personal repositories.
18+
19+
#### `GET <SERVER_URL>/entrello/prlo`
20+
Fetch pull requests that meet all of the following conditions:
21+
- belongs to the configured organization
22+
- belongs to one of the configured subscribed repositories
23+
- neither created by nor assigned to the configured user
24+
- not draft
25+
26+
#### `GET <SERVER_URL>/entrello/prlmy`
27+
Fetch pull requests that meet all of the following conditions:
28+
- belongs to the configured organization
29+
- created by the configured user
30+
31+
#### `GET <SERVER_URL>/entrello/prlme`
32+
Fetch pull requests that meet all of the following conditions:
33+
- belongs to the configured organization
34+
- assigned to the configured user
35+
- not created by configured user
36+
37+
## CLI Mode
38+
The result set of each endpoint listed above can be alternatively retrieved using the corresponding CLI command.
39+
40+
### List of Commands
41+
```sh
42+
go run ./cmd/cli
43+
go run ./cmd/cli prlo
44+
go run ./cmd/cli prlmy
45+
go run ./cmd/cli prlme
46+
```

cmd/cli/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/utkuufuk/entrello/pkg/trello"
10+
"github.com/utkuufuk/github-service/internal/github"
11+
)
12+
13+
func main() {
14+
client := github.GetClient()
15+
16+
if len(os.Args) == 1 {
17+
displayCards(client.FetchAssignedIssues)
18+
return
19+
}
20+
21+
switch os.Args[1] {
22+
case "prlo":
23+
displayCards(client.FetchOtherPullRequests)
24+
case "prlmy":
25+
displayCards(client.FetchMyPullRequests)
26+
case "prlme":
27+
displayCards(client.FetchOtherPullRequestsAssignedToMe)
28+
default:
29+
log.Fatalf("Uncrecognized command: %s", os.Args[2])
30+
}
31+
}
32+
33+
func displayCards(query func(ctx context.Context) ([]trello.Card, error)) {
34+
cards, err := query(context.Background())
35+
if err != nil {
36+
log.Fatalf(err.Error())
37+
}
38+
39+
for _, c := range cards {
40+
fmt.Printf("%s - %s\n", c.Name, c.Desc)
41+
}
42+
}

cmd/github-service/main.go

Lines changed: 0 additions & 85 deletions
This file was deleted.

cmd/server/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
9+
"github.com/utkuufuk/entrello/pkg/trello"
10+
"github.com/utkuufuk/github-service/internal/config"
11+
"github.com/utkuufuk/github-service/internal/github"
12+
)
13+
14+
func main() {
15+
client := github.GetClient()
16+
http.HandleFunc("/entrello", handleGetRequest(client.FetchAssignedIssues))
17+
http.HandleFunc("/entrello/prlo", handleGetRequest(client.FetchOtherPullRequests))
18+
http.HandleFunc("/entrello/prlme", handleGetRequest(client.FetchMyPullRequests))
19+
http.HandleFunc("/entrello/prlmy", handleGetRequest(client.FetchOtherPullRequestsAssignedToMe))
20+
http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
21+
}
22+
23+
func handleGetRequest(
24+
fetchCards func(ctx context.Context) ([]trello.Card, error),
25+
) func(w http.ResponseWriter, r *http.Request) {
26+
return func(w http.ResponseWriter, r *http.Request) {
27+
if r.Method != http.MethodGet {
28+
w.WriteHeader(http.StatusMethodNotAllowed)
29+
return
30+
}
31+
32+
cards, err := fetchCards(r.Context())
33+
if err != nil {
34+
w.WriteHeader(http.StatusInternalServerError)
35+
fmt.Fprintf(w, "could not fetch github cards for entrello: %v", err)
36+
return
37+
}
38+
39+
w.Header().Set("Content-Type", "application/json")
40+
json.NewEncoder(w).Encode(cards)
41+
}
42+
}

internal/github/github.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,23 @@ func (c Client) FetchAssignedIssues(ctx context.Context) ([]trello.Card, error)
3737
return entrello.CreateCardsFromIssues(nonPullRequestIssues)
3838
}
3939

40-
func (c Client) FetchOtherPullRequests() func(ctx context.Context) ([]trello.Card, error) {
41-
return func(ctx context.Context) ([]trello.Card, error) {
42-
pullRequests := make([]*github.PullRequest, 0)
43-
for _, repo := range config.SubscribedRepos {
44-
prs, _, err := c.client.PullRequests.List(ctx, config.OrgName, repo, nil)
45-
if err != nil {
46-
return nil, fmt.Errorf("could not retrieve pull requests from %s/%s: %w", config.OrgName, repo, err)
47-
}
48-
pullRequests = append(pullRequests, prs...)
40+
func (c Client) FetchOtherPullRequests(ctx context.Context) ([]trello.Card, error) {
41+
pullRequests := make([]*github.PullRequest, 0)
42+
for _, repo := range config.SubscribedRepos {
43+
prs, _, err := c.client.PullRequests.List(ctx, config.OrgName, repo, nil)
44+
if err != nil {
45+
return nil, fmt.Errorf("could not retrieve pull requests from %s/%s: %w", config.OrgName, repo, err)
4946
}
47+
pullRequests = append(pullRequests, prs...)
48+
}
5049

51-
otherPullRequests := make([]*github.PullRequest, 0)
52-
for _, i := range pullRequests {
53-
if !*i.Draft && *i.User.Login != config.UserName && (i.Assignee == nil || *i.Assignee.Login != config.UserName) {
54-
otherPullRequests = append(otherPullRequests, i)
55-
}
50+
otherPullRequests := make([]*github.PullRequest, 0)
51+
for _, i := range pullRequests {
52+
if !*i.Draft && *i.User.Login != config.UserName && (i.Assignee == nil || *i.Assignee.Login != config.UserName) {
53+
otherPullRequests = append(otherPullRequests, i)
5654
}
57-
return entrello.CreateCardsFromPullRequests(otherPullRequests)
5855
}
56+
return entrello.CreateCardsFromPullRequests(otherPullRequests)
5957
}
6058

6159
func (c Client) FetchOtherPullRequestsAssignedToMe(ctx context.Context) ([]trello.Card, error) {

0 commit comments

Comments
 (0)