--[[ $Id: $ $DateTime: $ $Author: $ $Change: $ ]]-- --[[ The purpose of this Lua extension is to shows an example of how the RunCommand() could be used. This command will shows the jobs owned by the user running the command and the status of these jobs along with the changelists of the pending fixes. This information can already be obtained using the p4 client interface by running first p4 jobs -e '' and then p4 fixes -j for each of the jobs listed. This therefore require multiple interactive commands between the client and server. With the use of RunCommand(), the interaction can be localized to the server and only providing the relevant final information to the end user. This extension does not interact with any triggers, it only make use of the RunCommand() but nothing would prevent an extension writer to create an extension that uses both. ]]-- function GlobalConfigFields() return { "" } end function InstanceConfigFields() return { } end function InstanceConfigEvents() return { ["extension-run"] = "unset" } end function isempty( s ) return s == nil or s == '' end local usage = "Usage: ExampleInc::JobInfoExt joblist\n" function RunCommand( args ) local cmdname = table.remove( args, 1 ) if isempty( cmdname ) or not isempty( args[1] ) then Helix.Core.Server.ClientOutputText( usage ) return false end if cmdname ~= "joblist" then Helix.Core.Server.ClientOutputText( usage ) return false end local username = Helix.Core.Server.GetVar( "user" ) local p4 = P4.P4:new() p4:autoconnect() if not p4:connect() then Helix.Core.Server.ClientOutputText( "Error connecting to server\n" ) return false end local listjob = {} local myarg = "User=" .. username local jobinfo = p4:run( "jobs", "-e", myarg ) if ( type(jobinfo) ~= "table" ) then Helix.Core.Server.ClientOutputText( "Error accessing server for p4 jobs\n" ) return false end for mk,mval in pairs( jobinfo ) do local j local s for k,v in pairs( mval ) do if k == "Job" then j = v end if k == "Status" then s = v end end if j ~= nil then listjob[j] = s end end -- Lua iteration order is unspecified, let's sort by keys local jobkeys = {} for j in pairs(listjob) do table.insert(jobkeys, j) end table.sort(jobkeys) -- Let's go through the list and find changes of any local jobcount = 0 for _, jobid in ipairs( jobkeys ) do local jobstatus = listjob[jobid] local fixinfo = p4:run( "fixes", "-j", jobid ) if ( type(fixinfo) == "table" ) then local linetoprint local changenum = "" for mk,mval in pairs( fixinfo ) do for k,v in pairs( mval ) do if k == "Change" then changenum = v end end end if changenum ~= "" then linetoprint = "Job " .. jobid .. ", Status: " .. jobstatus .. ", associated with change " .. changenum .. "\n" else linetoprint = "Job " .. jobid .. ", Status: " .. jobstatus .. ", not associated to a change number\n" end jobcount = jobcount + 1 Helix.Core.Server.ClientOutputText( linetoprint ) end end if jobcount == 0 then Helix.Core.Server.ClientOutputText( "No jobs with user " .. username .. "\n" ) end return true end