Beispiel #1
0
func getUsers(c *gin.Context) {
	u := UserList{}
	err := u.List()
	if err != nil {
		logging.WarnWithTags([]string{"api"}, "Users endpoint route failed.", err.Error())
		c.JSON(400, gin.H{"error": err})
	}

	c.JSON(200, u)
}
Beispiel #2
0
func userRoutine(u User, c chan<- []Activity) error {

	ts := tokenSource{
		&oauth2.Token{
			AccessToken: u.AccessToken,
		},
	}

	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := github.NewClient(tc)

	//List Options Page, PerPage
	opts := github.ListOptions{1, 50}

	for {
		events, resp, err := client.Activity.ListEventsPerformedByUser(u.Username, false, &opts)
		if err != nil {
			logging.WarnWithTags([]string{"github"}, "Problem retrieving events for user", u.Username, err.Error())
		}

		activityPayload, err := ProcessPayload(events, u)
		if err != nil {
			logging.WarnWithTags([]string{"payload"}, "Failed to process payload", err.Error())
			continue
		}

		activities := ActivityParser(activityPayload.SaveUnique())

		c <- activities

		// Wait as long as the X-Poll-Interval header says to
		interval, err := strconv.ParseInt(resp.Header["X-Poll-Interval"][0], 10, 8)
		if err != nil {
			// if strconv failed for whatever reason, use the default X-Poll-Interval value of 60
			time.Sleep(60 * time.Second)
		} else {
			time.Sleep(time.Duration(interval) * time.Second)
		}
	}

	panic("Shouldn't be here")
}
Beispiel #3
0
func ActivityParser(p ActivityPayloadList) []Activity {
	activities := ActivityList{}

	for _, payload := range p {
		isBreak := make(map[string]bool)

		// Sort templates by priority
		sort.Sort(ByPriority{Templates})

		for _, template := range Templates {
			activity := Activity{}

			//Set the event time on the activity object to the payloads event time
			activity.EventTime = payload.EventTime

			if isBreak[template.Event] {
				continue
			}

			if CheckEventType(template.Event, payload) && ValidateConditional(template.Condition, payload) {
				if template.Break {
					isBreak[template.Event] = true
				}

				logging.InfoWithTags([]string{"activity"}, "Adding activity for event ", activity.Event.EventType)

				//Create the activity for this template and append to list
				activity.TemplateId = template.Id
				activity.Event = payload
				activity.UpdateTime()

				err := activity.Create()
				if err != nil {
					logging.WarnWithTags([]string{"activity"}, "Failed to save activity", err.Error())
				}

				activities = append(activities, activity)

			}
		}
	}

	return activities
}
Beispiel #4
0
func getActivity(c *gin.Context) {
	limit, _ := strconv.Atoi(c.Request.URL.Query().Get("limit"))
	page, _ := strconv.Atoi(c.Request.URL.Query().Get("page"))

	if limit == 0 {
		limit = 50
	}

	if page == 0 {
		page = 1
	}

	a := ActivityList{}
	err := a.List(limit, page)
	if err != nil {
		logging.WarnWithTags([]string{"api"}, "Activity endpoint route failed.", err.Error())
		c.JSON(400, gin.H{"error": err})
	}

	c.JSON(200, a)
}