@@ -44,11 +44,11 @@ func (ga *GithubActions) GetLanguage() *Language {
44
44
}
45
45
46
46
func (ga * GithubActions ) AddWorkflow (workflow * Workflow ) error {
47
- exists , err := ga .fileExists (workflow .workflowFileName )
47
+ sha , err := ga .getFileSHA (workflow .workflowFileName )
48
48
if err != nil {
49
49
return err
50
50
}
51
- if exists {
51
+ if sha != "" {
52
52
log .Printf ("github actions Workflow %s already exists\n " , workflow .workflowFileName )
53
53
return nil
54
54
}
@@ -77,11 +77,11 @@ func (ga *GithubActions) AddWorkflow(workflow *Workflow) error {
77
77
}
78
78
79
79
func (ga * GithubActions ) DeleteWorkflow (workflow * Workflow ) error {
80
- exists , err := ga .fileExists (workflow .workflowFileName )
80
+ sha , err := ga .getFileSHA (workflow .workflowFileName )
81
81
if err != nil {
82
82
return err
83
83
}
84
- if ! exists {
84
+ if sha == "" {
85
85
log .Printf ("github actions Workflow %s already removed\n " , workflow .workflowFileName )
86
86
return nil
87
87
}
@@ -109,27 +109,31 @@ func (ga *GithubActions) DeleteWorkflow(workflow *Workflow) error {
109
109
return nil
110
110
}
111
111
112
- func (ga * GithubActions ) fileExists (filename string ) (bool , error ) {
113
- _ , _ , resp , err := ga .client .Repositories .GetContents (
112
+ // getFileSHA:
113
+ // 1. if file exists without error: return (string(SHA), nil)
114
+ // 2. if some errors occurred: return ("", err)
115
+ // 3. if file not found without error: return ("", nil)
116
+ func (ga * GithubActions ) getFileSHA (filename string ) (string , error ) {
117
+ content , _ , resp , err := ga .client .Repositories .GetContents (
114
118
ga .ctx ,
115
119
ga .options .Owner ,
116
120
ga .options .Repo ,
117
121
generateGitHubWorkflowFileByName (filename ),
118
122
& github.RepositoryContentGetOptions {},
119
123
)
120
124
121
- if err != nil {
122
- return false , err
125
+ if resp . StatusCode == http . StatusNotFound {
126
+ return "" , nil
123
127
}
124
128
125
- if resp . StatusCode == http . StatusNotFound {
126
- return false , nil
129
+ if err != nil {
130
+ return "" , err
127
131
}
128
132
129
133
if resp .StatusCode == http .StatusOK {
130
- return true , nil
134
+ return * content . SHA , nil
131
135
}
132
- return false , fmt .Errorf ("got some error is not expected" )
136
+ return "" , fmt .Errorf ("got some error is not expected" )
133
137
}
134
138
135
139
func generateGitHubWorkflowFileByName (f string ) string {
0 commit comments