name: devops operator
author: Bolt
description: 'Devops operator'
inputs:
  pat:
    description: ''
    required: true
    default: ''
  environment:
    description: 'ex) dev or qa or staging or prod'
    required: false
    default: ''
  dora:
    description: 'ex) enabled or disabled'
    required: false
    default: ''
  project_name:
    description: 'ex) go-klip-backend or blossomops'
    required: false
    default: ''
  category:
    description: 'ex) klip or devops or kas'
    required: false
    default: ''
  type:
    description: 'ex) ci or cd'
    required: false
    default: ''
  image_tag:
    description: ''
    required: false
    default: ''  
  version:
    description: ''
    required: false
    default: ''       
  

runs:
  using: "composite"
  steps:
  - name: gh auth login
    shell: bash
    run: |
      echo ${{ inputs.pat }} | gh auth login --with-token
  - name: set outputs
    id: set-outputs
    shell: bash
    run: |
      echo "pat=${{ inputs.pat }}" >> $GITHUB_OUTPUT
      echo "environment=${{ inputs.environment }}" >> $GITHUB_OUTPUT
      echo "dora=${{ inputs.dora }}" >> $GITHUB_OUTPUT
      echo "project_name=${{ inputs.project_name }}" >> $GITHUB_OUTPUT
      echo "category=${{ inputs.category }}" >> $GITHUB_OUTPUT
      echo "type=${{ inputs.type }}" >> $GITHUB_OUTPUT
  - name: dora enabled + type(ci) 
    shell: bash
    if:
      steps.set-outputs.outputs.dora == 'enabled' && steps.set-outputs.outputs.type == 'ci'
    run: |
      cd ./actions/scripts
      chmod +x job_detail.sh

      worklow_id=${{ github.run_id }}
      echo "workflow_id : $worklow_id"
      
      gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs > jobs.json
      gh api repos/${{ github.repository }}/actions/runs/${{ github.run_id }} > workflow.json
      cat jobs.json | jq > jobs-jq.json   
      cat workflow.json | jq > workflow-jq.json
    

      ./job_detail.sh ${{ github.sha }} ${{ inputs.environment }} ${{ github.repository }} $worklow_id ${{ github.event_name }} ${{ inputs.project_name }} ${{ inputs.category }}
  - name: dora enabled + type(ci) + environment(staging)
    shell: bash
    if:
      steps.set-outputs.outputs.dora == 'enabled' && steps.set-outputs.outputs.type == 'ci' && 
      ( steps.set-outputs.outputs.environment == 'staging' || steps.set-outputs.outputs.environment == 'devops-sandbox' )
    run: |
      cd ./actions/scripts
      chmod +x ci_lead_time_for_change.sh

      project_name=${{ inputs.project_name }}
      merge_commit=$(gh pr view ${{ github.event.number }} --repo ${{ github.repository }} --json mergeCommit --jq '.[].oid')
      category=${{ inputs.category }}
      
      first_commit_timestamp=$(gh pr view ${{ github.event.number }} --repo ${{ github.repository }} --json commits --jq '.commits[0].committedDate')
      start_timestamp=$(date -d "$first_commit_timestamp" "+%Y-%m-%d %H:%M:%S")

      echo "first_commit_timestamp: $first_commit_timestamp"
      echo "start_timestamp: $start_timestamp"
      version=${{ github.head_ref }}

      additions=$(gh pr view ${{ github.event.number }} --repo ${{ github.repository }} --json additions --jq '.additions')
      deletions=$(gh pr view ${{ github.event.number }} --repo ${{ github.repository }} --json deletions --jq '.deletions')
      url=$(gh pr view ${{ github.event.number }} --repo ${{ github.repository }} --json url --jq '.url')
  

      ./ci_lead_time_for_change.sh $project_name $merge_commit $category "$start_timestamp" $version $additions $deletions $url
  
  - name: dora enabled + type(cd) + environment(prod)
    shell: bash
    if:
      steps.set-outputs.outputs.dora == 'enabled' && steps.set-outputs.outputs.type == 'cd' && 
      ( steps.set-outputs.outputs.environment == 'prod' || steps.set-outputs.outputs.environment == 'devops-sandbox' )
    run: |
      cd ./actions/scripts
      chmod +x cd_lead_time_for_change.sh

      project_name=${{ inputs.project_name }}
      image_tag=${{ inputs.image_tag }}
      category=${{ inputs.category }}
      end_timestamp=$(date "+%Y-%m-%d %H:%M:%S")
      version=${{ inputs.version }}

      ./cd_lead_time_for_change.sh $project_name $image_tag $category "$end_timestamp" $version

      
      
