::nx::Object ::nx::Object __default_accessor __default_method_call_protection __object_configureparameter __resolve_method_path contains copy delete object method delete object property delete object variable destroy_on_cleanup info consts info info info lookup parameters info lookup slots info lookup syntax info lookup variables info object method args info object method body info object method callprotection info object method debug info object method definition info object method definitionhandle info object method deprecated info object method exists info object method handle info object method origin info object method parameters info object method registrationhandle info object method returns info object method submethods info object method syntax info object method type info object slots info object variables info variable definition info variable name info variable parameter move object alias object forward object method object property object variable private protected public qn require namespace require object method require private object method require protected object method require public object method serialize ::github::ActivityMonitor ::github::ActivityMonitor backfill_locked backfill_repo_history branch_enabled charge_request check_rate_limit discover discover_full discover_recent discovery_available fetch_new_events get get_state next_backfill_event_id observe_branches queue_backfill recent_since record_rate_limit set_state status summarize_push_event sync_from_github sync_locked sync_work ::github::ActivityMonitor->::nx::Object

Class ::github::ActivityMonitor

::github::ActivityMonitor[i] create ... \
           [ -allowed_branches (default "main oacs-5-10") ] \
           [ -api_base (default "https://api.github.com") ] \
           [ -api_token api_token ] \
           [ -backfill_pages_per_run (default "2") ] \
           [ -background_requests_per_run (default "2") ] \
           [ -discovery_requests_per_run (default "20") ] \
           [ -fast_repository_checks_per_run (default "8") ] \
           [ -full_reconcile_interval (default "86400") ] \
           [ -hot_repository_days (default "2") ] \
           [ -max_requests_per_run (default "200") ] \
           [ -max_seconds_per_run (default "120") ] \
           [ -organization (default "OpenACS") ] \
           [ -recent_days (default "180") ] \
           [ -reconcile_interval (default "3600") ] \
           [ -repository_inventory_interval (default "86400") ] \
           [ -repository_metadata_pages (default "4") ]

Defined in packages/xowiki/tcl/github-activity-monitor-procs.tcl

Class Relations

  • class: ::nx::Class[i]
  • superclass: ::nx::Object[i]
::nx::Class create ::github::ActivityMonitor \
     -superclass ::nx::Object

Methods (to be applied on instances)

  • backfill_repo_history (scripted, public)

     <instance of github::ActivityMonitor[i]> backfill_repo_history args \
        [ args... ]
    Parameters:
    args (required)

    Testcases:
    No testcase defined.
    ::xo::dc transaction {
        set locked [::xo::dc get_value github_backfill_lock {
            select case when pg_try_advisory_xact_lock(714230, 1)
                        then 1 else 0 end
        }]
        if {!$locked} {
            error "GitHub activity writer already running; retry later"
        }
        # Catch inside the transaction so a cooldown learned from an
        # HTTP failure commits even when this explicit call fails.
        set code [catch {
            :check_rate_limit
            :backfill_locked {*}$args
        } result options]
    }
    if {$code} {return -options $options $result}
    return $result
  • get (scripted, public)

     <instance of github::ActivityMonitor[i]> get path [ query_args ]

    Issue an API call on GitHub for the configured organization.

    Parameters:
    path (required)
    query_args (optional)

    Testcases:
    No testcase defined.
    :check_rate_limit
    :charge_request
    set url "${:api_base}${path}"
    
    # default query arg
    dict set query per_page 100
    foreach {k v} $query_args {
        dict set query $k $v
    }
    set urlDict [ns_parseurl $url]
    set encoded {}
    foreach {k v} $query {
        lappend encoded "[ns_urlencode -part query $k]=[ns_urlencode -part query $v]"
    }
    dict set urlDict query [join $encoded &]
    
    set headers [ns_set create headers  Authorization "Bearer ${:api_token}"  Accept        "application/vnd.github+json"  User-Agent    "openacs-activity-dashboard/1.0"  ]
    
    # Do not log the Authorization header.
    ns_log notice "GitHub GET [ns_joinurl $urlDict]"
    set timeout 15
    if {[info exists :deadline]} {
        set timeout [expr {min(15, max(1, ${:deadline} - [clock seconds]))}]
    }
    set response_headers ""
    try {
        set result [ns_http run -timeout $timeout -method GET -headers $headers [ns_joinurl $urlDict]]
        set status [dict get $result status]
        set body   [dict get $result body]
        set rate_headers {}
        if {[dict exists $result headers]} {
            set response_headers [dict get $result headers]
            foreach name {x-ratelimit-remaining x-ratelimit-reset retry-after} {
                dict set rate_headers $name [ns_set iget $response_headers $name]
            }
        }
        :record_rate_limit $status $rate_headers $body
        
        if {$status < 200 || $status >= 300} {
            ns_log Error "GitHub GET $url failed: status $status, body: $body"
            return -code error -errorcode [list GITHUB HTTP $status] "GitHub API error $status"
        }
        return $body
    } finally {
        ns_set free $headers
        if {$response_headers ne ""} {ns_set free $response_headers}
    }
  • status (scripted, public)

     <instance of github::ActivityMonitor[i]> status

    Testcases:
    No testcase defined.
    set result [dict create pending_branches [dict size [:get_state pending_backfills {}]]  pending_repository_checks [dict size [:get_state recent_repository_checks {}]]  inventory_page [:get_state repository_metadata_page 1]  last_inventory [:get_state last_repository_inventory 0]  last_full_reconcile [:get_state last_repository_reconcile 0]  github_not_before [:get_state github_not_before 0]  unexpected_master_heads [:get_state unexpected_master_heads {}]  attention_required [:get_state attention_required {}]  discovery_error [:get_state discovery_error ""]  repository_errors [:get_state repository_check_errors {}]  backfill_errors [:get_state backfill_errors {}]  last_sync [:get_state last_sync_status {}]]
    if {[dict get $result github_not_before] > [clock seconds]} {
        dict set result state paused
    } elseif {[dict get $result discovery_error] ne ""
              || [dict size [dict get $result unexpected_master_heads]]
              || [dict size [dict get $result attention_required]]
              || [dict size [dict get $result repository_errors]]
              || [dict size [dict get $result backfill_errors]]} {
        dict set result state needs-attention
    } elseif {[dict get $result pending_branches] || [dict get $result pending_repository_checks]
              || [dict get $result inventory_page] != 1 || [:get_state repository_discovery {}] ne ""} {
        dict set result state catching-up
    } elseif {![dict get $result last_inventory]} {
        dict set result state not-yet-checked
    } else {
        dict set result state idle
    }
    return $result
  • summarize_push_event (scripted, public)

     <instance of github::ActivityMonitor[i]> summarize_push_event ev

    Fetch commit details and build a summary for PushEvents.

    Parameters:
    ev (required)
    Returns:
    dict with fields of push event

    Testcases:
    No testcase defined.
    set type [dict get $ev type]
    if {$type ne "PushEvent"} {
        return ""
    }
    
    set repo_full   [dict get $ev repo name]          ;# e.g. "openacs/openacs-core"
    set ref         [dict get $ev payload ref]        ;# "refs/heads/main"
    if {![string match "refs/heads/*" $ref]} {
        return ""
    }
    set branch      [string range $ref [string length "refs/heads/"] end]
    set head_sha    [dict get $ev payload head]
    set created_at  [dict get $ev created_at]
    set actor_login [dict get $ev actor login]
    
    if {$repo_full eq "" || $head_sha eq ""} {
        return ""
    }
    
    # Fetch commit details
    set commit_body [:get "/repos/$repo_full/commits/$head_sha"]
    set c          [::util::json2dict $commit_body]
    set commit     [dict get $c commit]
    
    set author_dict [dict get $commit author]
    set author_name [dict get $author_dict name]
    set commit_date [dict get $author_dict date]    ;# ISO8601 string
    
    set message_full [dict get $commit message]
    set title        [lindex [split $message_full "\n"] 0]
    
    set stats        [dict get $c stats]
    set additions    [dict get $stats additions]
    set deletions    [dict get $stats deletions]
    set files        [dict get $c files]
    set files_changed [llength $files]
    
    return [list  event_id      [dict get $ev id]  repo          $repo_full  branch        $branch  sha           [dict get $c sha]  author_name   $author_name  author_login  $actor_login  commit_date   $commit_date  created_at    $created_at  title         $title  url           [dict get $c html_url]  files_changed $files_changed  additions     $additions  deletions     $deletions  raw_payload   $commit_body  ]
  • sync_from_github (scripted, public)

     <instance of github::ActivityMonitor[i]> sync_from_github

    An xact-scoped lock stays on the transaction's checked-out DB connection, is shared across threads/processes, and is released automatically on commit, rollback or connection loss.

    Testcases:
    No testcase defined.
    ::xo::dc transaction {
        set locked [::xo::dc get_value github_sync_lock {
            select case when pg_try_advisory_xact_lock(714230, 1)
                        then 1 else 0 end
        }]
        if {$locked} {
            :sync_locked
        } else {
            ns_log Notice "GitHub: sync skipped; another activity writer is running"
        }
    }