-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop GitHub Activity Metrics Script #9
Comments
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type SearchResult struct {
TotalCount int `json:"total_count"`
}
func getUserActivity(username string, token string) (int, int, int, []string, []string, []string, error) {
client := &http.Client{}
today := time.Now().UTC()
weekAgo := today.AddDate(0, 0, -7)
// URLs to be used for GitHub API
commitURL := fmt.Sprintf("https://api.github.com/search/commits?q=author:%s+committer-date:%s..%s", username, weekAgo.Format("2006-01-02"), today.Format("2006-01-02"))
issueURL := fmt.Sprintf("https://api.github.com/search/issues?q=author:%s+created:%s..%s", username, weekAgo.Format("2006-01-02"), today.Format("2006-01-02"))
pullRequestURL := fmt.Sprintf("https://api.github.com/search/issues?q=type:pr+author:%s+created:%s..%s", username, weekAgo.Format("2006-01-02"), today.Format("2006-01-02"))
// Dates when the information is retrieved
startDate := weekAgo.Format("02-01-2006")
endDate := today.Format("02-01-2006")
fmt.Printf("Information retrieved for the period: %s - %s\n", startDate, endDate)
// Making requests to GitHub API
req, _ := http.NewRequest("GET", commitURL, nil)
req.Header.Set("Authorization", "token "+token)
commitResp, err := client.Do(req)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer commitResp.Body.Close()
req, _ = http.NewRequest("GET", issueURL, nil)
req.Header.Set("Authorization", "token "+token)
issueResp, err := client.Do(req)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer issueResp.Body.Close()
req, _ = http.NewRequest("GET", pullRequestURL, nil)
req.Header.Set("Authorization", "token "+token)
pullRequestResp, err := client.Do(req)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer pullRequestResp.Body.Close()
// Parsing JSON responses and getting total counts
var commitResult SearchResult
json.NewDecoder(commitResp.Body).Decode(&commitResult)
var issueResult SearchResult
json.NewDecoder(issueResp.Body).Decode(&issueResult)
var pullRequestResult SearchResult
json.NewDecoder(pullRequestResp.Body).Decode(&pullRequestResult)
// Creating slices to store commit, issue, and pull request dates
var commitDates []string
var issueDates []string
var pullRequestDates []string
// Get commit dates
commitDatesResp, err := http.Get(commitURL)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer commitDatesResp.Body.Close()
var commitDatesResult map[string]interface{}
json.NewDecoder(commitDatesResp.Body).Decode(&commitDatesResult)
commitItems := commitDatesResult["items"].([]interface{})
for _, item := range commitItems {
commitDate := item.(map[string]interface{})["commit"].(map[string]interface{})["author"].(map[string]interface{})["date"].(string)
commitDates = append(commitDates, commitDate)
}
// Get issue dates
issueDatesResp, err := http.Get(issueURL)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer issueDatesResp.Body.Close()
var issueDatesResult map[string]interface{}
json.NewDecoder(issueDatesResp.Body).Decode(&issueDatesResult)
issueItems := issueDatesResult["items"].([]interface{})
for _, item := range issueItems {
issueDate := item.(map[string]interface{})["created_at"].(string)
issueDates = append(issueDates, issueDate)
}
// Get pull request dates
pullRequestDatesResp, err := http.Get(pullRequestURL)
if err != nil {
return 0, 0, 0, nil, nil, nil, err
}
defer pullRequestDatesResp.Body.Close()
var pullRequestDatesResult map[string]interface{}
json.NewDecoder(pullRequestDatesResp.Body).Decode(&pullRequestDatesResult)
pullRequestItems := pullRequestDatesResult["items"].([]interface{})
for _, item := range pullRequestItems {
pullRequestDate := item.(map[string]interface{})["created_at"].(string)
pullRequestDates = append(pullRequestDates, pullRequestDate)
}
return commitResult.TotalCount, issueResult.TotalCount, pullRequestResult.TotalCount, commitDates, issueDates, pullRequestDates, nil
}
func main() {
// Username and token
username := "username"
token := "github_token"
// Getting user activity
commits, issues, pullRequests, commitDates, issueDates, pullRequestDates, err := getUserActivity(username, token)
if err != nil {
fmt.Println("Error:", err)
return
}
// Printing results
fmt.Println("Weekly Activities:")
fmt.Println("Commits:", commits)
fmt.Println("Issues:", issues)
fmt.Println("Pull Requests:", pullRequests)
// Printing commit, issue, and pull request dates
fmt.Println("Commit Dates:")
for _, date := range commitDates {
fmt.Println(date)
}
fmt.Println("Issue Dates:")
for _, date := range issueDates {
fmt.Println(date)
}
fmt.Println("Pull Request Dates:")
for _, date := range pullRequestDates {
fmt.Println(date)
}
} go-bot> go run main.go
Information retrieved for the period: 12-05-2024 - 19-05-2024
Weekly Activities:
Commits: 2
Issues: 19
Pull Requests: 3
Commit Dates:
2024-05-16T13:14:56.000+03:00
2024-05-15T22:05:00.000+03:00
Issue Dates:
2024-05-19T10:37:43Z
2024-05-18T12:05:58Z
2024-05-18T09:22:53Z
2024-05-18T09:11:07Z
2024-05-18T09:10:15Z
2024-05-18T08:55:02Z
2024-05-18T08:46:33Z
2024-05-18T07:58:57Z
2024-05-18T07:57:09Z
2024-05-18T07:48:04Z
2024-05-18T07:46:26Z
2024-05-18T07:32:10Z
2024-05-18T07:25:49Z
2024-05-18T07:23:09Z
2024-05-18T07:21:38Z
2024-05-18T07:18:04Z
2024-05-17T20:39:24Z
2024-05-17T19:46:14Z
2024-05-15T19:41:23Z
Pull Request Dates:
2024-05-19T10:37:43Z
2024-05-17T20:39:24Z
2024-05-15T19:41:23Z This code is a Go program that retrieves the weekly activity of a specific user using GitHub's REST API. Firstly, the getUserActivity function is called with a given username and GitHub token. This function fetches the number of actions (commits, issues, and pull requests) performed by the user in the last week, along with their respective dates. GitHub API is utilized to obtain this information through three different URLs: commitURL: Retrieves the commits made by the user. GET requests are made to each URL to connect to the GitHub API and obtain responses. The responses are returned in JSON format, and these JSON data are processed to determine the counts of commits, issues, and pull requests. Additionally, date information for each activity is also obtained. Separate GET requests are made for this purpose, and the returned JSON data are processed to retrieve the date of each activity. Finally, the obtained data is printed in the main function. The total counts of commits, issues, and pull requests, along with their respective dates, are printed to the console. The purpose of this code is to report the weekly activities and their dates for a specific GitHub user. |
Description:
Task a team member with developing a script that simulates accessing GitHub data as if it were their own account (using their own GitHub credentials for testing purposes). This script should retrieve daily and weekly metrics such as commits, pull requests, reviews, open issues, and closed issues, along with the estimated total time spent on these activities.
Objective:
Create a script capable of fetching GitHub activity metrics for a specified user, simulating access as if it were the user's own account.
Details:
Tasks:
Deliverables:
Feel free to further elaborate or provide additional requirements for this issue.
The text was updated successfully, but these errors were encountered: