Skip to content

Instantly share code, notes, and snippets.

@eumel8
Created February 8, 2025 05:21
Show Gist options
  • Save eumel8/ebccdc1a1dfc9088cca01edbc99b998a to your computer and use it in GitHub Desktop.
Save eumel8/ebccdc1a1dfc9088cca01edbc99b998a to your computer and use it in GitHub Desktop.
Gitlab Loc Leaderboard
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
)
type Contributor struct {
Name string `json:"name"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
}
const (
gitlabURL = "<your-gitlab-url>"
privateToken = "<your-private-token>"
projectID = "<your-project-id>"
)
func getContributors() ([]Contributor, error) {
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/repository/contributors", gitlabURL, projectID)
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("PRIVATE-TOKEN", privateToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var contributors []Contributor
if err := json.NewDecoder(resp.Body).Decode(&contributors); err != nil {
return nil, err
}
return contributors, nil
}
func main() {
contributors, err := getContributors()
if err != nil {
fmt.Println("Error fetching data:", err)
return
}
scoreList := make([]struct {
Name string
NetLOC int
}, len(contributors))
for i, c := range contributors {
scoreList[i] = struct {
Name string
NetLOC int
}{c.Name, c.Additions - c.Deletions}
}
sort.Slice(scoreList, func(i, j int) bool {
return scoreList[i].NetLOC > scoreList[j].NetLOC
})
fmt.Println("\nContributor Score List (Netto LOC):")
for i, s := range scoreList {
fmt.Printf("%d. %s: %d LOC\n", i+1, s.Name, s.NetLOC)
}
}
import requests
# GitLab API Configuration
GITLAB_URL = "<your-gitlab-url>" # e.g., "https://gitlab.com"
PRIVATE_TOKEN = "<your-private-token>"
PROJECT_ID = "<your-project-id>"
# GitLab API Endpoint for Contributors
API_URL = f"{GITLAB_URL}/api/v4/projects/{PROJECT_ID}/repository/contributors"
# Fetch Contributor Data
def get_contributors():
headers = {"PRIVATE-TOKEN": PRIVATE_TOKEN}
response = requests.get(API_URL, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Error: Unable to fetch data (Status Code: {response.status_code})")
return []
# Process Data and Sort
contributors = get_contributors()
score_list = []
for contributor in contributors:
name = contributor["name"]
additions = contributor.get("additions", 0)
deletions = contributor.get("deletions", 0)
net_loc = additions - deletions # Netto LOC
score_list.append((name, net_loc))
# Sort by Netto LOC (Descending)
score_list.sort(key=lambda x: x[1], reverse=True)
# Print Leaderboard
print("\nContributor Score List (Netto LOC):")
for rank, (name, net_loc) in enumerate(score_list, start=1):
print(f"{rank}. {name}: {net_loc} LOC")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment