taxonomy – Set post terms on post publish

I am trying to set post terms on the post, when it is puublished

I use following code but is doesn’t work

add_action('pending_to_publish', 'oj_publish_post');
function oj_publish_post($post_id) {
  $taxonomy = 'category';
  $term_id = array(8);
  $term_id = array_map('intval', $term_id);    
  wp_set_post_terms( $post_id ,$term_id, $taxonomy,true);
}

But, when I pass the id of the post manually
i.e instead of using post_id. I use 773 it works

   add_action('pending_to_publish', 'oj_publish_post');
    function oj_publish_post($post_id) {
      $taxonomy = 'category';
      $term_id = array(8);
      $term_id = array_map('intval', $term_id);    
      wp_set_post_terms( 773 ,$term_id, $taxonomy,true);
    }

What am I doing wrong, please help