You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

943 lines
24 KiB

10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "crypto/hmac"
  8. "crypto/sha256"
  9. "crypto/tls"
  10. "encoding/hex"
  11. "encoding/json"
  12. "fmt"
  13. "io/ioutil"
  14. "net"
  15. "net/http"
  16. "net/url"
  17. "strings"
  18. "time"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/setting"
  21. api "code.gitea.io/gitea/modules/structs"
  22. "code.gitea.io/gitea/modules/sync"
  23. "code.gitea.io/gitea/modules/util"
  24. "github.com/Unknwon/com"
  25. gouuid "github.com/satori/go.uuid"
  26. )
  27. // HookQueue is a global queue of web hooks
  28. var HookQueue = sync.NewUniqueQueue(setting.Webhook.QueueLength)
  29. // HookContentType is the content type of a web hook
  30. type HookContentType int
  31. const (
  32. // ContentTypeJSON is a JSON payload for web hooks
  33. ContentTypeJSON HookContentType = iota + 1
  34. // ContentTypeForm is an url-encoded form payload for web hook
  35. ContentTypeForm
  36. )
  37. var hookContentTypes = map[string]HookContentType{
  38. "json": ContentTypeJSON,
  39. "form": ContentTypeForm,
  40. }
  41. // ToHookContentType returns HookContentType by given name.
  42. func ToHookContentType(name string) HookContentType {
  43. return hookContentTypes[name]
  44. }
  45. // Name returns the name of a given web hook's content type
  46. func (t HookContentType) Name() string {
  47. switch t {
  48. case ContentTypeJSON:
  49. return "json"
  50. case ContentTypeForm:
  51. return "form"
  52. }
  53. return ""
  54. }
  55. // IsValidHookContentType returns true if given name is a valid hook content type.
  56. func IsValidHookContentType(name string) bool {
  57. _, ok := hookContentTypes[name]
  58. return ok
  59. }
  60. // HookEvents is a set of web hook events
  61. type HookEvents struct {
  62. Create bool `json:"create"`
  63. Delete bool `json:"delete"`
  64. Fork bool `json:"fork"`
  65. Issues bool `json:"issues"`
  66. IssueComment bool `json:"issue_comment"`
  67. Push bool `json:"push"`
  68. PullRequest bool `json:"pull_request"`
  69. Repository bool `json:"repository"`
  70. Release bool `json:"release"`
  71. }
  72. // HookEvent represents events that will delivery hook.
  73. type HookEvent struct {
  74. PushOnly bool `json:"push_only"`
  75. SendEverything bool `json:"send_everything"`
  76. ChooseEvents bool `json:"choose_events"`
  77. HookEvents `json:"events"`
  78. }
  79. // HookStatus is the status of a web hook
  80. type HookStatus int
  81. // Possible statuses of a web hook
  82. const (
  83. HookStatusNone = iota
  84. HookStatusSucceed
  85. HookStatusFail
  86. )
  87. // Webhook represents a web hook object.
  88. type Webhook struct {
  89. ID int64 `xorm:"pk autoincr"`
  90. RepoID int64 `xorm:"INDEX"`
  91. OrgID int64 `xorm:"INDEX"`
  92. URL string `xorm:"url TEXT"`
  93. Signature string `xorm:"TEXT"`
  94. HTTPMethod string `xorm:"http_method"`
  95. ContentType HookContentType
  96. Secret string `xorm:"TEXT"`
  97. Events string `xorm:"TEXT"`
  98. *HookEvent `xorm:"-"`
  99. IsSSL bool `xorm:"is_ssl"`
  100. IsActive bool `xorm:"INDEX"`
  101. HookTaskType HookTaskType
  102. Meta string `xorm:"TEXT"` // store hook-specific attributes
  103. LastStatus HookStatus // Last delivery status
  104. CreatedUnix util.TimeStamp `xorm:"INDEX created"`
  105. UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
  106. }
  107. // AfterLoad updates the webhook object upon setting a column
  108. func (w *Webhook) AfterLoad() {
  109. w.HookEvent = &HookEvent{}
  110. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  111. log.Error("Unmarshal[%d]: %v", w.ID, err)
  112. }
  113. }
  114. // GetSlackHook returns slack metadata
  115. func (w *Webhook) GetSlackHook() *SlackMeta {
  116. s := &SlackMeta{}
  117. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  118. log.Error("webhook.GetSlackHook(%d): %v", w.ID, err)
  119. }
  120. return s
  121. }
  122. // GetDiscordHook returns discord metadata
  123. func (w *Webhook) GetDiscordHook() *DiscordMeta {
  124. s := &DiscordMeta{}
  125. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  126. log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
  127. }
  128. return s
  129. }
  130. // GetTelegramHook returns telegram metadata
  131. func (w *Webhook) GetTelegramHook() *TelegramMeta {
  132. s := &TelegramMeta{}
  133. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  134. log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
  135. }
  136. return s
  137. }
  138. // History returns history of webhook by given conditions.
  139. func (w *Webhook) History(page int) ([]*HookTask, error) {
  140. return HookTasks(w.ID, page)
  141. }
  142. // UpdateEvent handles conversion from HookEvent to Events.
  143. func (w *Webhook) UpdateEvent() error {
  144. data, err := json.Marshal(w.HookEvent)
  145. w.Events = string(data)
  146. return err
  147. }
  148. // HasCreateEvent returns true if hook enabled create event.
  149. func (w *Webhook) HasCreateEvent() bool {
  150. return w.SendEverything ||
  151. (w.ChooseEvents && w.HookEvents.Create)
  152. }
  153. // HasDeleteEvent returns true if hook enabled delete event.
  154. func (w *Webhook) HasDeleteEvent() bool {
  155. return w.SendEverything ||
  156. (w.ChooseEvents && w.HookEvents.Delete)
  157. }
  158. // HasForkEvent returns true if hook enabled fork event.
  159. func (w *Webhook) HasForkEvent() bool {
  160. return w.SendEverything ||
  161. (w.ChooseEvents && w.HookEvents.Fork)
  162. }
  163. // HasIssuesEvent returns true if hook enabled issues event.
  164. func (w *Webhook) HasIssuesEvent() bool {
  165. return w.SendEverything ||
  166. (w.ChooseEvents && w.HookEvents.Issues)
  167. }
  168. // HasIssueCommentEvent returns true if hook enabled issue_comment event.
  169. func (w *Webhook) HasIssueCommentEvent() bool {
  170. return w.SendEverything ||
  171. (w.ChooseEvents && w.HookEvents.IssueComment)
  172. }
  173. // HasPushEvent returns true if hook enabled push event.
  174. func (w *Webhook) HasPushEvent() bool {
  175. return w.PushOnly || w.SendEverything ||
  176. (w.ChooseEvents && w.HookEvents.Push)
  177. }
  178. // HasPullRequestEvent returns true if hook enabled pull request event.
  179. func (w *Webhook) HasPullRequestEvent() bool {
  180. return w.SendEverything ||
  181. (w.ChooseEvents && w.HookEvents.PullRequest)
  182. }
  183. // HasReleaseEvent returns if hook enabled release event.
  184. func (w *Webhook) HasReleaseEvent() bool {
  185. return w.SendEverything ||
  186. (w.ChooseEvents && w.HookEvents.Release)
  187. }
  188. // HasRepositoryEvent returns if hook enabled repository event.
  189. func (w *Webhook) HasRepositoryEvent() bool {
  190. return w.SendEverything ||
  191. (w.ChooseEvents && w.HookEvents.Repository)
  192. }
  193. func (w *Webhook) eventCheckers() []struct {
  194. has func() bool
  195. typ HookEventType
  196. } {
  197. return []struct {
  198. has func() bool
  199. typ HookEventType
  200. }{
  201. {w.HasCreateEvent, HookEventCreate},
  202. {w.HasDeleteEvent, HookEventDelete},
  203. {w.HasForkEvent, HookEventFork},
  204. {w.HasPushEvent, HookEventPush},
  205. {w.HasIssuesEvent, HookEventIssues},
  206. {w.HasIssueCommentEvent, HookEventIssueComment},
  207. {w.HasPullRequestEvent, HookEventPullRequest},
  208. {w.HasRepositoryEvent, HookEventRepository},
  209. {w.HasReleaseEvent, HookEventRelease},
  210. }
  211. }
  212. // EventsArray returns an array of hook events
  213. func (w *Webhook) EventsArray() []string {
  214. events := make([]string, 0, 7)
  215. for _, c := range w.eventCheckers() {
  216. if c.has() {
  217. events = append(events, string(c.typ))
  218. }
  219. }
  220. return events
  221. }
  222. // CreateWebhook creates a new web hook.
  223. func CreateWebhook(w *Webhook) error {
  224. return createWebhook(x, w)
  225. }
  226. func createWebhook(e Engine, w *Webhook) error {
  227. _, err := e.Insert(w)
  228. return err
  229. }
  230. // getWebhook uses argument bean as query condition,
  231. // ID must be specified and do not assign unnecessary fields.
  232. func getWebhook(bean *Webhook) (*Webhook, error) {
  233. has, err := x.Get(bean)
  234. if err != nil {
  235. return nil, err
  236. } else if !has {
  237. return nil, ErrWebhookNotExist{bean.ID}
  238. }
  239. return bean, nil
  240. }
  241. // GetWebhookByID returns webhook of repository by given ID.
  242. func GetWebhookByID(id int64) (*Webhook, error) {
  243. return getWebhook(&Webhook{
  244. ID: id,
  245. })
  246. }
  247. // GetWebhookByRepoID returns webhook of repository by given ID.
  248. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  249. return getWebhook(&Webhook{
  250. ID: id,
  251. RepoID: repoID,
  252. })
  253. }
  254. // GetWebhookByOrgID returns webhook of organization by given ID.
  255. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  256. return getWebhook(&Webhook{
  257. ID: id,
  258. OrgID: orgID,
  259. })
  260. }
  261. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  262. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  263. return getActiveWebhooksByRepoID(x, repoID)
  264. }
  265. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  266. webhooks := make([]*Webhook, 0, 5)
  267. return webhooks, e.Where("is_active=?", true).
  268. Find(&webhooks, &Webhook{RepoID: repoID})
  269. }
  270. // GetWebhooksByRepoID returns all webhooks of a repository.
  271. func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  272. webhooks := make([]*Webhook, 0, 5)
  273. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  274. }
  275. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  276. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  277. return getActiveWebhooksByOrgID(x, orgID)
  278. }
  279. func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
  280. err = e.
  281. Where("org_id=?", orgID).
  282. And("is_active=?", true).
  283. Find(&ws)
  284. return ws, err
  285. }
  286. // GetWebhooksByOrgID returns all webhooks for an organization.
  287. func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  288. err = x.Find(&ws, &Webhook{OrgID: orgID})
  289. return ws, err
  290. }
  291. // GetDefaultWebhook returns admin-default webhook by given ID.
  292. func GetDefaultWebhook(id int64) (*Webhook, error) {
  293. webhook := &Webhook{ID: id}
  294. has, err := x.
  295. Where("repo_id=? AND org_id=?", 0, 0).
  296. Get(webhook)
  297. if err != nil {
  298. return nil, err
  299. } else if !has {
  300. return nil, ErrWebhookNotExist{id}
  301. }
  302. return webhook, nil
  303. }
  304. // GetDefaultWebhooks returns all admin-default webhooks.
  305. func GetDefaultWebhooks() ([]*Webhook, error) {
  306. return getDefaultWebhooks(x)
  307. }
  308. func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
  309. webhooks := make([]*Webhook, 0, 5)
  310. return webhooks, e.
  311. Where("repo_id=? AND org_id=?", 0, 0).
  312. Find(&webhooks)
  313. }
  314. // UpdateWebhook updates information of webhook.
  315. func UpdateWebhook(w *Webhook) error {
  316. _, err := x.ID(w.ID).AllCols().Update(w)
  317. return err
  318. }
  319. // UpdateWebhookLastStatus updates last status of webhook.
  320. func UpdateWebhookLastStatus(w *Webhook) error {
  321. _, err := x.ID(w.ID).Cols("last_status").Update(w)
  322. return err
  323. }
  324. // deleteWebhook uses argument bean as query condition,
  325. // ID must be specified and do not assign unnecessary fields.
  326. func deleteWebhook(bean *Webhook) (err error) {
  327. sess := x.NewSession()
  328. defer sess.Close()
  329. if err = sess.Begin(); err != nil {
  330. return err
  331. }
  332. if count, err := sess.Delete(bean); err != nil {
  333. return err
  334. } else if count == 0 {
  335. return ErrWebhookNotExist{ID: bean.ID}
  336. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  337. return err
  338. }
  339. return sess.Commit()
  340. }
  341. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  342. func DeleteWebhookByRepoID(repoID, id int64) error {
  343. return deleteWebhook(&Webhook{
  344. ID: id,
  345. RepoID: repoID,
  346. })
  347. }
  348. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  349. func DeleteWebhookByOrgID(orgID, id int64) error {
  350. return deleteWebhook(&Webhook{
  351. ID: id,
  352. OrgID: orgID,
  353. })
  354. }
  355. // DeleteDefaultWebhook deletes an admin-default webhook by given ID.
  356. func DeleteDefaultWebhook(id int64) error {
  357. sess := x.NewSession()
  358. defer sess.Close()
  359. if err := sess.Begin(); err != nil {
  360. return err
  361. }
  362. count, err := sess.
  363. Where("repo_id=? AND org_id=?", 0, 0).
  364. Delete(&Webhook{ID: id})
  365. if err != nil {
  366. return err
  367. } else if count == 0 {
  368. return ErrWebhookNotExist{ID: id}
  369. }
  370. if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
  371. return err
  372. }
  373. return sess.Commit()
  374. }
  375. // copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  376. func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
  377. ws, err := getDefaultWebhooks(e)
  378. if err != nil {
  379. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  380. }
  381. for _, w := range ws {
  382. w.ID = 0
  383. w.RepoID = repoID
  384. if err := createWebhook(e, w); err != nil {
  385. return fmt.Errorf("CreateWebhook: %v", err)
  386. }
  387. }
  388. return nil
  389. }
  390. // ___ ___ __ ___________ __
  391. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  392. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  393. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  394. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  395. // \/ \/ \/ \/ \/
  396. // HookTaskType is the type of an hook task
  397. type HookTaskType int
  398. // Types of hook tasks
  399. const (
  400. GOGS HookTaskType = iota + 1
  401. SLACK
  402. GITEA
  403. DISCORD
  404. DINGTALK
  405. TELEGRAM
  406. MSTEAMS
  407. )
  408. var hookTaskTypes = map[string]HookTaskType{
  409. "gitea": GITEA,
  410. "gogs": GOGS,
  411. "slack": SLACK,
  412. "discord": DISCORD,
  413. "dingtalk": DINGTALK,
  414. "telegram": TELEGRAM,
  415. "msteams": MSTEAMS,
  416. }
  417. // ToHookTaskType returns HookTaskType by given name.
  418. func ToHookTaskType(name string) HookTaskType {
  419. return hookTaskTypes[name]
  420. }
  421. // Name returns the name of an hook task type
  422. func (t HookTaskType) Name() string {
  423. switch t {
  424. case GITEA:
  425. return "gitea"
  426. case GOGS:
  427. return "gogs"
  428. case SLACK:
  429. return "slack"
  430. case DISCORD:
  431. return "discord"
  432. case DINGTALK:
  433. return "dingtalk"
  434. case TELEGRAM:
  435. return "telegram"
  436. case MSTEAMS:
  437. return "msteams"
  438. }
  439. return ""
  440. }
  441. // IsValidHookTaskType returns true if given name is a valid hook task type.
  442. func IsValidHookTaskType(name string) bool {
  443. _, ok := hookTaskTypes[name]
  444. return ok
  445. }
  446. // HookEventType is the type of an hook event
  447. type HookEventType string
  448. // Types of hook events
  449. const (
  450. HookEventCreate HookEventType = "create"
  451. HookEventDelete HookEventType = "delete"
  452. HookEventFork HookEventType = "fork"
  453. HookEventPush HookEventType = "push"
  454. HookEventIssues HookEventType = "issues"
  455. HookEventIssueComment HookEventType = "issue_comment"
  456. HookEventPullRequest HookEventType = "pull_request"
  457. HookEventRepository HookEventType = "repository"
  458. HookEventRelease HookEventType = "release"
  459. HookEventPullRequestApproved HookEventType = "pull_request_approved"
  460. HookEventPullRequestRejected HookEventType = "pull_request_rejected"
  461. HookEventPullRequestComment HookEventType = "pull_request_comment"
  462. )
  463. // HookRequest represents hook task request information.
  464. type HookRequest struct {
  465. Headers map[string]string `json:"headers"`
  466. }
  467. // HookResponse represents hook task response information.
  468. type HookResponse struct {
  469. Status int `json:"status"`
  470. Headers map[string]string `json:"headers"`
  471. Body string `json:"body"`
  472. }
  473. // HookTask represents a hook task.
  474. type HookTask struct {
  475. ID int64 `xorm:"pk autoincr"`
  476. RepoID int64 `xorm:"INDEX"`
  477. HookID int64
  478. UUID string
  479. Type HookTaskType
  480. URL string `xorm:"TEXT"`
  481. Signature string `xorm:"TEXT"`
  482. api.Payloader `xorm:"-"`
  483. PayloadContent string `xorm:"TEXT"`
  484. HTTPMethod string `xorm:"http_method"`
  485. ContentType HookContentType
  486. EventType HookEventType
  487. IsSSL bool
  488. IsDelivered bool
  489. Delivered int64
  490. DeliveredString string `xorm:"-"`
  491. // History info.
  492. IsSucceed bool
  493. RequestContent string `xorm:"TEXT"`
  494. RequestInfo *HookRequest `xorm:"-"`
  495. ResponseContent string `xorm:"TEXT"`
  496. ResponseInfo *HookResponse `xorm:"-"`
  497. }
  498. // BeforeUpdate will be invoked by XORM before updating a record
  499. // representing this object
  500. func (t *HookTask) BeforeUpdate() {
  501. if t.RequestInfo != nil {
  502. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  503. }
  504. if t.ResponseInfo != nil {
  505. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  506. }
  507. }
  508. // AfterLoad updates the webhook object upon setting a column
  509. func (t *HookTask) AfterLoad() {
  510. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  511. if len(t.RequestContent) == 0 {
  512. return
  513. }
  514. t.RequestInfo = &HookRequest{}
  515. if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  516. log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err)
  517. }
  518. if len(t.ResponseContent) > 0 {
  519. t.ResponseInfo = &HookResponse{}
  520. if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  521. log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err)
  522. }
  523. }
  524. }
  525. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  526. p, err := json.Marshal(v)
  527. if err != nil {
  528. log.Error("Marshal [%d]: %v", t.ID, err)
  529. }
  530. return string(p)
  531. }
  532. // HookTasks returns a list of hook tasks by given conditions.
  533. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  534. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  535. return tasks, x.
  536. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  537. Where("hook_id=?", hookID).
  538. Desc("id").
  539. Find(&tasks)
  540. }
  541. // CreateHookTask creates a new hook task,
  542. // it handles conversion from Payload to PayloadContent.
  543. func CreateHookTask(t *HookTask) error {
  544. return createHookTask(x, t)
  545. }
  546. func createHookTask(e Engine, t *HookTask) error {
  547. data, err := t.Payloader.JSONPayload()
  548. if err != nil {
  549. return err
  550. }
  551. t.UUID = gouuid.NewV4().String()
  552. t.PayloadContent = string(data)
  553. _, err = e.Insert(t)
  554. return err
  555. }
  556. // UpdateHookTask updates information of hook task.
  557. func UpdateHookTask(t *HookTask) error {
  558. _, err := x.ID(t.ID).AllCols().Update(t)
  559. return err
  560. }
  561. // PrepareWebhook adds special webhook to task queue for given payload.
  562. func PrepareWebhook(w *Webhook, repo *Repository, event HookEventType, p api.Payloader) error {
  563. return prepareWebhook(x, w, repo, event, p)
  564. }
  565. func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType, p api.Payloader) error {
  566. for _, e := range w.eventCheckers() {
  567. if event == e.typ {
  568. if !e.has() {
  569. return nil
  570. }
  571. }
  572. }
  573. var payloader api.Payloader
  574. var err error
  575. // Use separate objects so modifications won't be made on payload on non-Gogs/Gitea type hooks.
  576. switch w.HookTaskType {
  577. case SLACK:
  578. payloader, err = GetSlackPayload(p, event, w.Meta)
  579. if err != nil {
  580. return fmt.Errorf("GetSlackPayload: %v", err)
  581. }
  582. case DISCORD:
  583. payloader, err = GetDiscordPayload(p, event, w.Meta)
  584. if err != nil {
  585. return fmt.Errorf("GetDiscordPayload: %v", err)
  586. }
  587. case DINGTALK:
  588. payloader, err = GetDingtalkPayload(p, event, w.Meta)
  589. if err != nil {
  590. return fmt.Errorf("GetDingtalkPayload: %v", err)
  591. }
  592. case TELEGRAM:
  593. payloader, err = GetTelegramPayload(p, event, w.Meta)
  594. if err != nil {
  595. return fmt.Errorf("GetTelegramPayload: %v", err)
  596. }
  597. case MSTEAMS:
  598. payloader, err = GetMSTeamsPayload(p, event, w.Meta)
  599. if err != nil {
  600. return fmt.Errorf("GetMSTeamsPayload: %v", err)
  601. }
  602. default:
  603. p.SetSecret(w.Secret)
  604. payloader = p
  605. }
  606. var signature string
  607. if len(w.Secret) > 0 {
  608. data, err := payloader.JSONPayload()
  609. if err != nil {
  610. log.Error("prepareWebhooks.JSONPayload: %v", err)
  611. }
  612. sig := hmac.New(sha256.New, []byte(w.Secret))
  613. _, err = sig.Write(data)
  614. if err != nil {
  615. log.Error("prepareWebhooks.sigWrite: %v", err)
  616. }
  617. signature = hex.EncodeToString(sig.Sum(nil))
  618. }
  619. if err = createHookTask(e, &HookTask{
  620. RepoID: repo.ID,
  621. HookID: w.ID,
  622. Type: w.HookTaskType,
  623. URL: w.URL,
  624. Signature: signature,
  625. Payloader: payloader,
  626. HTTPMethod: w.HTTPMethod,
  627. ContentType: w.ContentType,
  628. EventType: event,
  629. IsSSL: w.IsSSL,
  630. }); err != nil {
  631. return fmt.Errorf("CreateHookTask: %v", err)
  632. }
  633. return nil
  634. }
  635. // PrepareWebhooks adds new webhooks to task queue for given payload.
  636. func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
  637. return prepareWebhooks(x, repo, event, p)
  638. }
  639. func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payloader) error {
  640. ws, err := getActiveWebhooksByRepoID(e, repo.ID)
  641. if err != nil {
  642. return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
  643. }
  644. // check if repo belongs to org and append additional webhooks
  645. if repo.mustOwner(e).IsOrganization() {
  646. // get hooks for org
  647. orgHooks, err := getActiveWebhooksByOrgID(e, repo.OwnerID)
  648. if err != nil {
  649. return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
  650. }
  651. ws = append(ws, orgHooks...)
  652. }
  653. if len(ws) == 0 {
  654. return nil
  655. }
  656. for _, w := range ws {
  657. if err = prepareWebhook(e, w, repo, event, p); err != nil {
  658. return err
  659. }
  660. }
  661. return nil
  662. }
  663. func (t *HookTask) deliver() error {
  664. t.IsDelivered = true
  665. var req *http.Request
  666. var err error
  667. switch t.HTTPMethod {
  668. case "":
  669. log.Info("HTTP Method for webhook %d empty, setting to POST as default", t.ID)
  670. fallthrough
  671. case http.MethodPost:
  672. switch t.ContentType {
  673. case ContentTypeJSON:
  674. req, err = http.NewRequest("POST", t.URL, strings.NewReader(t.PayloadContent))
  675. if err != nil {
  676. return err
  677. }
  678. req.Header.Set("Content-Type", "application/json")
  679. case ContentTypeForm:
  680. var forms = url.Values{
  681. "payload": []string{t.PayloadContent},
  682. }
  683. req, err = http.NewRequest("POST", t.URL, strings.NewReader(forms.Encode()))
  684. if err != nil {
  685. return err
  686. }
  687. }
  688. case http.MethodGet:
  689. u, err := url.Parse(t.URL)
  690. if err != nil {
  691. return err
  692. }
  693. vals := u.Query()
  694. vals["payload"] = []string{t.PayloadContent}
  695. u.RawQuery = vals.Encode()
  696. req, err = http.NewRequest("GET", u.String(), nil)
  697. if err != nil {
  698. return err
  699. }
  700. default:
  701. return fmt.Errorf("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod)
  702. }
  703. req.Header.Add("X-Gitea-Delivery", t.UUID)
  704. req.Header.Add("X-Gitea-Event", string(t.EventType))
  705. req.Header.Add("X-Gitea-Signature", t.Signature)
  706. req.Header.Add("X-Gogs-Delivery", t.UUID)
  707. req.Header.Add("X-Gogs-Event", string(t.EventType))
  708. req.Header.Add("X-Gogs-Signature", t.Signature)
  709. req.Header["X-GitHub-Delivery"] = []string{t.UUID}
  710. req.Header["X-GitHub-Event"] = []string{string(t.EventType)}
  711. // Record delivery information.
  712. t.RequestInfo = &HookRequest{
  713. Headers: map[string]string{},
  714. }
  715. for k, vals := range req.Header {
  716. t.RequestInfo.Headers[k] = strings.Join(vals, ",")
  717. }
  718. t.ResponseInfo = &HookResponse{
  719. Headers: map[string]string{},
  720. }
  721. defer func() {
  722. t.Delivered = time.Now().UnixNano()
  723. if t.IsSucceed {
  724. log.Trace("Hook delivered: %s", t.UUID)
  725. } else {
  726. log.Trace("Hook delivery failed: %s", t.UUID)
  727. }
  728. if err := UpdateHookTask(t); err != nil {
  729. log.Error("UpdateHookTask [%d]: %v", t.ID, err)
  730. }
  731. // Update webhook last delivery status.
  732. w, err := GetWebhookByID(t.HookID)
  733. if err != nil {
  734. log.Error("GetWebhookByID: %v", err)
  735. return
  736. }
  737. if t.IsSucceed {
  738. w.LastStatus = HookStatusSucceed
  739. } else {
  740. w.LastStatus = HookStatusFail
  741. }
  742. if err = UpdateWebhookLastStatus(w); err != nil {
  743. log.Error("UpdateWebhookLastStatus: %v", err)
  744. return
  745. }
  746. }()
  747. resp, err := webhookHTTPClient.Do(req)
  748. if err != nil {
  749. t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
  750. return err
  751. }
  752. defer resp.Body.Close()
  753. // Status code is 20x can be seen as succeed.
  754. t.IsSucceed = resp.StatusCode/100 == 2
  755. t.ResponseInfo.Status = resp.StatusCode
  756. for k, vals := range resp.Header {
  757. t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
  758. }
  759. p, err := ioutil.ReadAll(resp.Body)
  760. if err != nil {
  761. t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
  762. return err
  763. }
  764. t.ResponseInfo.Body = string(p)
  765. return nil
  766. }
  767. // DeliverHooks checks and delivers undelivered hooks.
  768. // TODO: shoot more hooks at same time.
  769. func DeliverHooks() {
  770. tasks := make([]*HookTask, 0, 10)
  771. err := x.Where("is_delivered=?", false).Find(&tasks)
  772. if err != nil {
  773. log.Error("DeliverHooks: %v", err)
  774. return
  775. }
  776. // Update hook task status.
  777. for _, t := range tasks {
  778. if err = t.deliver(); err != nil {
  779. log.Error("deliver: %v", err)
  780. continue
  781. }
  782. }
  783. // Start listening on new hook requests.
  784. for repoIDStr := range HookQueue.Queue() {
  785. log.Trace("DeliverHooks [repo_id: %v]", repoIDStr)
  786. HookQueue.Remove(repoIDStr)
  787. repoID, err := com.StrTo(repoIDStr).Int64()
  788. if err != nil {
  789. log.Error("Invalid repo ID: %s", repoIDStr)
  790. continue
  791. }
  792. tasks = make([]*HookTask, 0, 5)
  793. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  794. log.Error("Get repository [%d] hook tasks: %v", repoID, err)
  795. continue
  796. }
  797. for _, t := range tasks {
  798. if err = t.deliver(); err != nil {
  799. log.Error("deliver: %v", err)
  800. }
  801. }
  802. }
  803. }
  804. var webhookHTTPClient *http.Client
  805. // InitDeliverHooks starts the hooks delivery thread
  806. func InitDeliverHooks() {
  807. timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
  808. webhookHTTPClient = &http.Client{
  809. Transport: &http.Transport{
  810. TLSClientConfig: &tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify},
  811. Dial: func(netw, addr string) (net.Conn, error) {
  812. conn, err := net.DialTimeout(netw, addr, timeout)
  813. if err != nil {
  814. return nil, err
  815. }
  816. return conn, conn.SetDeadline(time.Now().Add(timeout))
  817. },
  818. },
  819. }
  820. go DeliverHooks()
  821. }