-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package metrics | ||
|
||
import ( | ||
"encoding/json" | ||
"github-actions-exporter/config" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var workflows = map[string]map[int]workflow{} | ||
|
||
type workflowsReturn struct { | ||
TotalCount int `json:"total_count"` | ||
Workflows []workflow `json:"workflows"` | ||
} | ||
|
||
type workflow struct { | ||
ID int `json:"id"` | ||
NodeID string `json:"node_id"` | ||
Name string `json:"name"` | ||
Path string `json:"path"` | ||
State string `json:"state"` | ||
} | ||
|
||
func WorkflowsCache() { | ||
client := &http.Client{} | ||
|
||
for { | ||
for _, repo := range config.Github.Repositories { | ||
var p workflowsReturn | ||
req, _ := http.NewRequest("GET", "https://api.github.com/repos/"+repo+"/actions/workflows", nil) | ||
req.Header.Set("Authorization", "token "+config.Github.Token) | ||
resp, err := client.Do(req) | ||
defer resp.Body.Close() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if resp.StatusCode != 200 { | ||
log.Fatalf("the status code returned by the server is different from 200: %d", resp.StatusCode) | ||
} | ||
err = json.NewDecoder(resp.Body).Decode(&p) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
s := make(map[int]workflow) | ||
for _, w := range p.Workflows { | ||
s[w.ID] = w | ||
} | ||
|
||
workflows[repo] = s | ||
} | ||
time.Sleep(time.Duration(60) * time.Second) | ||
} | ||
} |